Create merge_qwen.py
Browse files- merge_qwen.py +93 -0
merge_qwen.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import Qwen2Model, Qwen2ForCausalLM, Qwen2_5_VLPreTrainedModel, Qwen2_5_VLForConditionalGeneration, AutoProcessor, AutoTokenizer, AddedToken
|
| 2 |
+
import torch
|
| 3 |
+
from qwen_vl_utils import process_vision_info
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
qwen25_model = Qwen2_5_VLForConditionalGeneration.from_pretrained("Qwen/Qwen2.5-VL-7B-Instruct", device_map="auto", torch_dtype=torch.bfloat16)
|
| 7 |
+
llm_device = qwen25_model.model.device
|
| 8 |
+
deepseek_model = Qwen2ForCausalLM.from_pretrained("deepseek-ai/DeepSeek-R1-Distill-Qwen-7B").to(torch.bfloat16).to(llm_device)
|
| 9 |
+
qwen25_model.model.load_state_dict(deepseek_model.model.state_dict())
|
| 10 |
+
qwen25_model.lm_head.load_state_dict(deepseek_model.lm_head.state_dict())
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
qwen25_model = qwen25_model.to(torch.bfloat16)
|
| 14 |
+
min_pixels = 256*28*28
|
| 15 |
+
max_pixels = 1280*28*28
|
| 16 |
+
processor = AutoProcessor.from_pretrained("Qwen/Qwen2.5-VL-7B-Instruct", min_pixels=min_pixels, max_pixels=max_pixels, use_fast=False)
|
| 17 |
+
ID_TO_NEW_TOKEN = {
|
| 18 |
+
151643: "<|end▁of▁sentence|>",
|
| 19 |
+
151644: "<|User|>",
|
| 20 |
+
151645: "<|Assistant|>",
|
| 21 |
+
151646: "<|begin▁of▁sentence|>",
|
| 22 |
+
151648: "<think>",
|
| 23 |
+
151649: "</think>",
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
# The reverse mapping: new text -> old ID
|
| 27 |
+
NEW_TOKEN_TO_ID = {v: k for k, v in ID_TO_NEW_TOKEN.items()}
|
| 28 |
+
|
| 29 |
+
for old_id, text in ID_TO_NEW_TOKEN.items():
|
| 30 |
+
# Create an AddedToken that won't get split
|
| 31 |
+
# 'special=True' ensures it is recognized as one piece
|
| 32 |
+
# 'normalized=False' means "do not lowercase or strip it"
|
| 33 |
+
# so it is preserved exactly.
|
| 34 |
+
tok = AddedToken(
|
| 35 |
+
text,
|
| 36 |
+
special=True,
|
| 37 |
+
normalized=False,
|
| 38 |
+
lstrip=False,
|
| 39 |
+
rstrip=False,
|
| 40 |
+
single_word=False
|
| 41 |
+
)
|
| 42 |
+
# Register in the slow tokenizer's internal data structures:
|
| 43 |
+
# _added_tokens_decoder: maps ID -> AddedToken object
|
| 44 |
+
# _added_tokens_encoder: maps text -> ID
|
| 45 |
+
# Then update the trie so that it can match them in raw text.
|
| 46 |
+
processor.tokenizer._added_tokens_decoder[old_id] = tok
|
| 47 |
+
processor.tokenizer._added_tokens_encoder[text] = old_id
|
| 48 |
+
|
| 49 |
+
processor.tokenizer._update_trie()
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
print("Model loaded and move to GPU")
|
| 53 |
+
repo_name = "ahmedheakl/vlm-r1-base2"
|
| 54 |
+
qwen25_model.push_to_hub(repo_name)
|
| 55 |
+
processor.push_to_hub(repo_name)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
# messages = [
|
| 60 |
+
# {
|
| 61 |
+
# "role": "user",
|
| 62 |
+
# "content": [
|
| 63 |
+
# # {
|
| 64 |
+
# # "type": "image",
|
| 65 |
+
# # "image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg",
|
| 66 |
+
# # },
|
| 67 |
+
# {"type": "text", "text": "What is the integration of cos^2(x)"},
|
| 68 |
+
# ],
|
| 69 |
+
# }
|
| 70 |
+
# ]
|
| 71 |
+
|
| 72 |
+
# text = processor.apply_chat_template(
|
| 73 |
+
# messages, tokenize=False, add_generation_prompt=True
|
| 74 |
+
# )
|
| 75 |
+
# image_inputs, video_inputs = process_vision_info(messages)
|
| 76 |
+
# inputs = processor(
|
| 77 |
+
# text=[text],
|
| 78 |
+
# images=image_inputs,
|
| 79 |
+
# videos=video_inputs,
|
| 80 |
+
# padding=True,
|
| 81 |
+
# return_tensors="pt",
|
| 82 |
+
# )
|
| 83 |
+
# inputs = inputs.to("cuda")
|
| 84 |
+
|
| 85 |
+
# # Inference: Generation of the output
|
| 86 |
+
# generated_ids = qwen25_model.generate(**inputs, max_new_tokens=1000)
|
| 87 |
+
# generated_ids_trimmed = [
|
| 88 |
+
# out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
| 89 |
+
# ]
|
| 90 |
+
# output_text = processor.batch_decode(
|
| 91 |
+
# generated_ids_trimmed, skip_special_tokens=False, clean_up_tokenization_spaces=False
|
| 92 |
+
# )
|
| 93 |
+
# print(output_text[0])
|