Views
No views yet
diffusers library, ComfyUI, or any other model), although models that use architectures which are unfamiliar to me might be more difficult.transformers1import torch
2from transformers import Qwen3VLForConditionalGeneration, AutoProcessor
3from dfloat11 import DFloat11Model
4
5# default: Load the model on the available device(s)
6model = Qwen3VLForConditionalGeneration.from_pretrained(
7 "Qwen/Qwen3-VL-8B-Instruct", dtype=torch.bfloat16, device_map="cpu"
8)
9
10# We recommend enabling flash_attention_2 for better acceleration and memory saving, especially in multi-image and video scenarios.
11# model = Qwen3VLForConditionalGeneration.from_pretrained(
12# "Qwen/Qwen3-VL-8B-Instruct",
13# dtype=torch.bfloat16,
14# attn_implementation="flash_attention_2",
15# device_map="auto",
16# )
17
18DFloat11Model.from_pretrained("mingyi456/Qwen3-VL-8B-Instruct-DF11", device = "cpu", bfloat16_model = model)
19model.to("cuda")
20
21processor = AutoProcessor.from_pretrained("Qwen/Qwen3-VL-8B-Instruct")
22
23messages = [
24 {
25 "role": "user",
26 "content": [
27 {
28 "type": "image",
29 "image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg",
30 },
31 {"type": "text", "text": "Describe this image."},
32 ],
33 }
34]
35
36# Preparation for inference
37inputs = processor.apply_chat_template(
38 messages,
39 tokenize=True,
40 add_generation_prompt=True,
41 return_dict=True,
42 return_tensors="pt"
43)
44inputs = inputs.to(model.device)
45
46# Inference: Generation of the output
47generated_ids = model.generate(**inputs, max_new_tokens=512)
48generated_ids_trimmed = [
49 out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
50]
51output_text = processor.batch_decode(
52 generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
53)
54print(output_text)pattern_dict for compression:1pattern_dict={
2 r"model\.visual\.blocks\.\d+": [
3 "attn.qkv",
4 "attn.proj",
5 "mlp.linear_fc1",
6 "mlp.linear_fc2"
7 ],
8 r"model\.visual\.merger": [
9 "linear_fc1",
10 "linear_fc2",
11 ],
12 r"model\.visual\.deepstack_merger_list\.\d+": [
13 "linear_fc1",
14 "linear_fc2",
15 ],
16
17 r"model\.language_model\.embed_tokens": [],
18 r"model\.language_model\.layers\.\d+":[
19 "self_attn.q_proj",
20 "self_attn.k_proj",
21 "self_attn.v_proj",
22 "self_attn.o_proj",
23 "mlp.gate_proj",
24 "mlp.up_proj",
25 "mlp.down_proj"
26 ],
27
28 r"lm_head": []
29}