n1ck-guo commited on
Commit
93dfa0a
·
verified ·
1 Parent(s): fb00ec6

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +151 -0
README.md ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ base_model:
3
+ - Qwen/Qwen3-VL-235B-A22B-Instruct
4
+ ---
5
+
6
+ ## Model Details
7
+
8
+ This model is an int4 model with group_size 128 and symmetric quantization of [Qwen/Qwen3-VL-235B-A22B-Instruct](https://huggingface.co/Qwen/Qwen3-VL-235B-A22B-Instruct) generated by [intel/auto-round](https://github.com/intel/auto-round).
9
+ Please follow the license of the original model.
10
+
11
+ ## How To Use
12
+
13
+ ### INT4 Inference
14
+ **Please import `Qwen3VLMoeForConditionalGeneration` from `modeling_qwen3_vl_moe.py` in the model folder.**
15
+ ```python
16
+ # need use the modeling_qwen3_vl_moe.py provided to load the model, modeling_qwen3_vl_moe.py is in the model weight folder
17
+ from modeling_qwen3_vl_moe import Qwen3VLMoeForConditionalGeneration
18
+ from transformers import AutoProcessor
19
+ # default: Load the model on the available device(s)
20
+ model_name = "Intel/Qwen3-VL-235B-A22B-Instruct-int4-AutoRound"
21
+ model = Qwen3VLMoeForConditionalGeneration.from_pretrained(
22
+ model_name, dtype="auto", device_map="auto"
23
+ )
24
+
25
+ processor = AutoProcessor.from_pretrained(model_name)
26
+
27
+ messages = [
28
+ {
29
+ "role": "user",
30
+ "content": [
31
+ {
32
+ "type": "image",
33
+ "image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg",
34
+ },
35
+ {"type": "text", "text": "Describe this image."},
36
+ ],
37
+ }
38
+ ]
39
+
40
+ # Preparation for inference
41
+ inputs = processor.apply_chat_template(
42
+ messages,
43
+ tokenize=True,
44
+ add_generation_prompt=True,
45
+ return_dict=True,
46
+ return_tensors="pt"
47
+ )
48
+
49
+ # Inference: Generation of the output
50
+ generated_ids = model.generate(**inputs, max_new_tokens=128)
51
+ generated_ids_trimmed = [
52
+ out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
53
+ ]
54
+ output_text = processor.batch_decode(
55
+ generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
56
+ )
57
+ print(output_text)
58
+ """
59
+ ['This is a warm, serene, and heartwarming photograph capturing a tender moment between a young woman and her dog on a sandy beach at sunset.\n\n**Main Subjects:**\n- A young woman with long, dark hair is sitting cross-legged on the sand, smiling joyfully as she looks at her dog. She’s wearing a black-and-white plaid shirt, dark pants, and a white watch on her left wrist.\n- A golden Labrador Retriever sits beside her, facing her and offering its right paw to her in a “shake” gesture. The dog is wearing a colorful harness with a floral pattern and a red leash lies']
60
+ """
61
+ ```
62
+
63
+ ### Generate the model
64
+ Step 1: use the script to convert model
65
+ ```python
66
+ from tqdm import tqdm
67
+ from accelerate import init_empty_weights
68
+ from transformers import Qwen3VLMoeForConditionalGeneration, AutoProcessor, AutoConfig
69
+
70
+ model_name = "Qwen/Qwen3-VL-235B-A22B-Instruct"
71
+ model = Qwen3VLMoeForConditionalGeneration.from_pretrained(
72
+ model_name, dtype="auto", device_map="auto"
73
+ )
74
+ config = AutoConfig.from_pretrained(model_name)
75
+ orig_state_dict = model.state_dict()
76
+ model = model.to_empty(device="cpu")
77
+ keys = list(orig_state_dict.keys())
78
+ num_experts = config.text_config.num_experts
79
+ for key in tqdm(keys, desc="convert"):
80
+ if "gate_up_proj" in key and "expert" in key:
81
+ for i in range(num_experts):
82
+ new_key = key.replace("gate_up_proj","gate_up_projs")
83
+ new_key+="."+str(i)+".weight"
84
+ value = orig_state_dict[key][i,...].transpose(0, 1).contiguous()
85
+ orig_state_dict[new_key] = value
86
+ orig_state_dict[key] = None
87
+ orig_state_dict.pop(key)
88
+ elif "down_proj" in key and "expert" in key:
89
+ for i in range(num_experts):
90
+ new_key = key.replace("down_proj", "down_projs")
91
+ new_key += "." + str(i) + ".weight"
92
+ value = orig_state_dict[key][i, ...].transpose(0, 1).contiguous()
93
+ orig_state_dict[new_key] = value
94
+ orig_state_dict[key] = None
95
+ orig_state_dict.pop(key)
96
+
97
+
98
+ with init_empty_weights():
99
+ from modeling_qwen3_vl_moe import Qwen3VLMoeForConditionalGeneration
100
+ modify_model = Qwen3VLMoeForConditionalGeneration._from_config(config)
101
+
102
+ modelfied_state_dict = modify_model.state_dict()
103
+ modify_model.load_state_dict(orig_state_dict,strict=True,assign=True)
104
+ modify_model.to("cpu")
105
+ output_dir="Qwen3-VL-235B-A22B-Instruct-convert-linear"
106
+ modify_model.save_pretrained(output_dir)
107
+
108
+ ```
109
+
110
+ Step 2: generate quantized model
111
+ ```python
112
+ import torch
113
+ import transformers
114
+ from auto_round import AutoRound
115
+ from transformers import AutoProcessor, AutoTokenizer
116
+ from modeling_qwen3_vl_moe import Qwen3VLMoeForConditionalGeneration
117
+
118
+
119
+ model_name = "Qwen3-VL-235B-A22B-Instruct-convert-linear"
120
+ model = Qwen3VLMoeForConditionalGeneration.from_pretrained(model_name, dtype="auto",device_map="cpu")
121
+ processor = AutoProcessor.from_pretrained(model_name)
122
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
123
+
124
+ autoround = AutoRound(model, tokenizer=tokenizer, processor=processor, scheme="W4A16", device="auto", low_gpu_mem_usage=True, fp_layers="mlp.gate", seqlen=1024)
125
+ autoround.quantize_and_save(format="auto_round", output_dir="Qwen3-VL-235B-A22B-Instruct-int4-AutoRound")
126
+
127
+ ```
128
+
129
+ ## Ethical Considerations and Limitations
130
+
131
+ The model can produce factually incorrect output, and should not be relied on to produce factually accurate information. Because of the limitations of the pretrained model and the finetuning datasets, it is possible that this model could generate lewd, biased or otherwise offensive outputs.
132
+
133
+ Therefore, before deploying any applications of the model, developers should perform safety testing.
134
+
135
+ ## Caveats and Recommendations
136
+
137
+ Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model.
138
+
139
+ Here are a couple of useful links to learn more about Intel's AI software:
140
+
141
+ - Intel Neural Compressor [link](https://github.com/intel/neural-compressor)
142
+
143
+ ## Disclaimer
144
+
145
+ The license on this model does not constitute legal advice. We are not responsible for the actions of third parties who use this model. Please consult an attorney before using this model for commercial purposes.
146
+
147
+ ## Cite
148
+
149
+ @article{cheng2023optimize, title={Optimize weight rounding via signed gradient descent for the quantization of llms}, author={Cheng, Wenhua and Zhang, Weiwei and Shen, Haihao and Cai, Yiyang and He, Xin and Lv, Kaokao and Liu, Yi}, journal={arXiv preprint arXiv:2309.05516}, year={2023} }
150
+
151
+ [arxiv](https://arxiv.org/abs/2309.05516) [github](https://github.com/intel/auto-round)