Skip to content

Commit ba72530

Browse files
Zhao-Dongyurahul-tulidsikka
authored
[Examples] Create qwen_2_5_vl_example.py (#1752)
SUMMARY: Add Qwen2.5-VL support for W8A8 FP8 quantization example This PR adds support for Qwen2.5-VL model in the W8A8 FP8 quantization examples. While Qwen2-VL is still available, Qwen2.5-VL has gained more popularity among users. This addition provides users with an example for FP8 quantization of Qwen's newer vision-language model alongside the existing Qwen2-VL support. TEST PLAN: This is a low-risk change that simply adapts the existing Qwen2-VL example for Qwen2.5-VL: Simple Adaptation: Only changed the model ID from Qwen2-VL to Qwen2.5-VL - no structural changes to the quantization logic Proven Feasibility: The code has been tested and verified to work correctly with Qwen2.5-VL Consistency: Follows the exact same pattern as other Qwen2.5-VL examples already in the repository No Risk: This is a straightforward model ID update that maintains all existing functionality Co-authored-by: Rahul Tuli <[email protected]> Co-authored-by: Dipika Sikka <[email protected]>
1 parent 679a704 commit ba72530

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from transformers import AutoProcessor, Qwen2_5_VLForConditionalGeneration
2+
3+
from llmcompressor import oneshot
4+
from llmcompressor.modifiers.quantization import QuantizationModifier
5+
from llmcompressor.utils import dispatch_for_generation
6+
7+
MODEL_ID = "Qwen/Qwen2.5-VL-7B-Instruct"
8+
9+
# Load model.
10+
model = Qwen2_5_VLForConditionalGeneration.from_pretrained(MODEL_ID, torch_dtype="auto")
11+
processor = AutoProcessor.from_pretrained(MODEL_ID)
12+
13+
# Configure the quantization algorithm and scheme.
14+
# In this case, we:
15+
# * quantize the weights to fp8 with per channel via ptq
16+
# * quantize the activations to fp8 with dynamic per token
17+
recipe = QuantizationModifier(
18+
targets="Linear",
19+
scheme="FP8_DYNAMIC",
20+
ignore=["re:.*lm_head", "re:visual.*"],
21+
)
22+
23+
# Apply quantization and save to disk in compressed-tensors format.
24+
oneshot(model=model, recipe=recipe)
25+
26+
# Confirm generations of the quantized model look sane.
27+
print("========== SAMPLE GENERATION ==============")
28+
dispatch_for_generation(model)
29+
input_ids = processor(text="Hello my name is", return_tensors="pt").input_ids.to("cuda")
30+
output = model.generate(input_ids, max_new_tokens=20)
31+
print(processor.decode(output[0]))
32+
print("==========================================")
33+
34+
# Save to disk in compressed-tensors format.
35+
SAVE_DIR = MODEL_ID.rstrip("/").split("/")[-1] + "-FP8-Dynamic"
36+
model.save_pretrained(SAVE_DIR, save_compressed=True)
37+
processor.save_pretrained(SAVE_DIR)

0 commit comments

Comments
 (0)