File size: 6,419 Bytes
93dfa0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
---
base_model:
- Qwen/Qwen3-VL-235B-A22B-Instruct
---

## Model Details

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).
Please follow the license of the original model.

## How To Use

### INT4 Inference
**Please import `Qwen3VLMoeForConditionalGeneration` from `modeling_qwen3_vl_moe.py` in the model folder.**
```python
# need use the modeling_qwen3_vl_moe.py provided to load the model, modeling_qwen3_vl_moe.py is in the model weight folder
from modeling_qwen3_vl_moe import Qwen3VLMoeForConditionalGeneration 
from transformers import AutoProcessor
# default: Load the model on the available device(s)
model_name = "Intel/Qwen3-VL-235B-A22B-Instruct-int4-AutoRound"
model = Qwen3VLMoeForConditionalGeneration.from_pretrained(
    model_name, dtype="auto", device_map="auto"
)

processor = AutoProcessor.from_pretrained(model_name)

messages = [
    {
        "role": "user",
        "content": [
            {
                "type": "image",
                "image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg",
            },
            {"type": "text", "text": "Describe this image."},
        ],
    }
]

# Preparation for inference
inputs = processor.apply_chat_template(
    messages,
    tokenize=True,
    add_generation_prompt=True,
    return_dict=True,
    return_tensors="pt"
)

# Inference: Generation of the output
generated_ids = model.generate(**inputs, max_new_tokens=128)
generated_ids_trimmed = [
    out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
]
output_text = processor.batch_decode(
    generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
)
print(output_text)
"""
['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']
"""
```

### Generate the model
Step 1: use the script to convert model
```python
from tqdm import tqdm
from accelerate import init_empty_weights
from transformers import Qwen3VLMoeForConditionalGeneration, AutoProcessor, AutoConfig

model_name = "Qwen/Qwen3-VL-235B-A22B-Instruct"
model = Qwen3VLMoeForConditionalGeneration.from_pretrained(
    model_name, dtype="auto", device_map="auto"
)
config = AutoConfig.from_pretrained(model_name)
orig_state_dict = model.state_dict()
model = model.to_empty(device="cpu")
keys = list(orig_state_dict.keys())
num_experts = config.text_config.num_experts
for key in tqdm(keys, desc="convert"):
    if "gate_up_proj" in key and "expert" in key:
        for i in range(num_experts):
            new_key = key.replace("gate_up_proj","gate_up_projs")
            new_key+="."+str(i)+".weight"
            value = orig_state_dict[key][i,...].transpose(0, 1).contiguous()
            orig_state_dict[new_key] = value
        orig_state_dict[key] = None
        orig_state_dict.pop(key)
    elif "down_proj" in key and "expert" in key:
        for i in range(num_experts):
            new_key = key.replace("down_proj", "down_projs")
            new_key += "." + str(i) + ".weight"
            value = orig_state_dict[key][i, ...].transpose(0, 1).contiguous()
            orig_state_dict[new_key] = value
        orig_state_dict[key] = None
        orig_state_dict.pop(key)


with init_empty_weights():
    from modeling_qwen3_vl_moe import Qwen3VLMoeForConditionalGeneration
    modify_model = Qwen3VLMoeForConditionalGeneration._from_config(config)

modelfied_state_dict = modify_model.state_dict()
modify_model.load_state_dict(orig_state_dict,strict=True,assign=True)
modify_model.to("cpu")
output_dir="Qwen3-VL-235B-A22B-Instruct-convert-linear"
modify_model.save_pretrained(output_dir)

```

Step 2: generate quantized model
```python
import torch
import transformers
from auto_round import AutoRound
from transformers import AutoProcessor, AutoTokenizer
from modeling_qwen3_vl_moe import Qwen3VLMoeForConditionalGeneration


model_name = "Qwen3-VL-235B-A22B-Instruct-convert-linear"
model = Qwen3VLMoeForConditionalGeneration.from_pretrained(model_name, dtype="auto",device_map="cpu")
processor = AutoProcessor.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)

autoround = AutoRound(model, tokenizer=tokenizer, processor=processor, scheme="W4A16", device="auto", low_gpu_mem_usage=True, fp_layers="mlp.gate", seqlen=1024)
autoround.quantize_and_save(format="auto_round", output_dir="Qwen3-VL-235B-A22B-Instruct-int4-AutoRound")

```

## Ethical Considerations and Limitations

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.

Therefore, before deploying any applications of the model, developers should perform safety testing.

## Caveats and Recommendations

Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model.

Here are a couple of useful links to learn more about Intel's AI software:

- Intel Neural Compressor [link](https://github.com/intel/neural-compressor)

## Disclaimer

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.

## Cite

@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} }

[arxiv](https://arxiv.org/abs/2309.05516) [github](https://github.com/intel/auto-round)