Create upload_model.py
Browse files- upload_model.py +95 -0
upload_model.py
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
from huggingface_hub import create_repo, upload_folder, snapshot_download
|
3 |
+
import torch
|
4 |
+
import transformers
|
5 |
+
from transformers import AutoModelForCausalLM
|
6 |
+
import os
|
7 |
+
from pathlib import Path
|
8 |
+
|
9 |
+
model_id = 'Qwen/Qwen-VL-Chat'
|
10 |
+
new_repo_id = 'yujiepan/qwen-vl-tiny-random'
|
11 |
+
|
12 |
+
|
13 |
+
def replace_in_file(file_path, old: str, new: str):
|
14 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
15 |
+
visual_code = f.read()
|
16 |
+
visual_code = visual_code.replace(old, new)
|
17 |
+
with open(file_path, 'w', encoding='utf-8') as f:
|
18 |
+
f.write(visual_code)
|
19 |
+
|
20 |
+
|
21 |
+
def download_modeling_codes():
|
22 |
+
snapshot_download(repo_id=model_id, allow_patterns='*.py',
|
23 |
+
local_dir='./qwen_vl_tiny_random', local_dir_use_symlinks=False)
|
24 |
+
# The hard coded "128" is changed for smaller model size.
|
25 |
+
replace_in_file('./qwen_vl_tiny_random/visual.py',
|
26 |
+
'num_heads=output_dim // 128,', 'num_heads=output_dim // 4,')
|
27 |
+
|
28 |
+
|
29 |
+
def create_config():
|
30 |
+
from qwen_vl_tiny_random.configuration_qwen import QWenConfig
|
31 |
+
config = QWenConfig()
|
32 |
+
config.fp16 = True
|
33 |
+
config.hidden_size = 8
|
34 |
+
config.intermediate_size = 16
|
35 |
+
config.kv_channels = 4
|
36 |
+
config.num_attention_heads = 2
|
37 |
+
config.num_hidden_layers = 2
|
38 |
+
config.seq_length = 2048
|
39 |
+
|
40 |
+
config.visual = {
|
41 |
+
"heads": 2,
|
42 |
+
"image_size": 448,
|
43 |
+
"image_start_id": 151857,
|
44 |
+
"layers": 2,
|
45 |
+
"mlp_ratio": 1.0,
|
46 |
+
"output_dim": 8,
|
47 |
+
"patch_size": 14,
|
48 |
+
"width": 8,
|
49 |
+
}
|
50 |
+
print(config)
|
51 |
+
return config
|
52 |
+
|
53 |
+
|
54 |
+
def create_model(config):
|
55 |
+
from qwen_vl_tiny_random.modeling_qwen import QWenLMHeadModel, QWenModel
|
56 |
+
from qwen_vl_tiny_random.configuration_qwen import QWenConfig
|
57 |
+
from transformers import AutoModelForCausalLM, AutoConfig, AutoModel
|
58 |
+
AutoConfig.register("qwen", QWenConfig)
|
59 |
+
AutoModel.register(QWenConfig, QWenModel)
|
60 |
+
AutoModelForCausalLM.register(QWenConfig, QWenLMHeadModel)
|
61 |
+
model = AutoModelForCausalLM.from_config(config, trust_remote_code=True)
|
62 |
+
model.generation_config = transformers.GenerationConfig.from_pretrained(
|
63 |
+
model_id, trust_remote_code=True)
|
64 |
+
return model
|
65 |
+
|
66 |
+
|
67 |
+
def try_inference(model, tokenizer):
|
68 |
+
model = model.cuda()
|
69 |
+
query = tokenizer.from_list_format([
|
70 |
+
{'image': 'https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg'},
|
71 |
+
{'text': '这是什么'},
|
72 |
+
])
|
73 |
+
response, history = model.chat(tokenizer, query=query, history=None)
|
74 |
+
print(response)
|
75 |
+
|
76 |
+
|
77 |
+
download_modeling_codes()
|
78 |
+
config = create_config()
|
79 |
+
model = create_model(config)
|
80 |
+
tokenizer = transformers.AutoTokenizer.from_pretrained(
|
81 |
+
model_id, trust_remote_code=True)
|
82 |
+
try_inference(model, tokenizer)
|
83 |
+
|
84 |
+
model.save_pretrained('./qwen_vl_tiny_random/')
|
85 |
+
tokenizer.save_pretrained('./qwen_vl_tiny_random/')
|
86 |
+
|
87 |
+
create_repo(new_repo_id, exist_ok=True)
|
88 |
+
upload_folder(repo_id=new_repo_id, folder_path='./qwen_vl_tiny_random/',
|
89 |
+
ignore_patterns='__pycache__')
|
90 |
+
|
91 |
+
model = transformers.AutoModelForCausalLM.from_pretrained(
|
92 |
+
new_repo_id, trust_remote_code=True).cuda()
|
93 |
+
tokenizer = transformers.AutoTokenizer.from_pretrained(
|
94 |
+
new_repo_id, trust_remote_code=True)
|
95 |
+
try_inference(model, tokenizer)
|