Views
No views yet
1VLLM_USE_V2_MODEL_RUNNER=1
2vllm serve RedHatAI/diffusiongemma-26B-A4B-it-NVFP4 \
3 --trust-remote-code \
4 --max-num-seqs 4 \
5 --hf-overrides '{"diffusion_sampler": "entropy_bound", "diffusion_entropy_bound": 0.1}' \
6 --default-chat-template-kwargs '{"enable_thinking": true}'1"""
2Quantize DiffusionGemma to NVFP4 using LLM Compressor v0.11.0
3
4Model: google/diffusiongemma-26B-A4B-it
5- Total parameters: ~25.8B
6- Expert parameters: 22.8B (88.4%)
7- Non-expert parameters: 3.0B (11.6%)
8
9Note: This will require a local update to transformers to support the model definition.
10"""
11
12import torch
13from compressed_tensors.offload import dispatch_model
14from datasets import load_dataset
15from transformers import AutoProcessor
16from transformers.models.diffusion_gemma import DiffusionGemmaForBlockDiffusion
17from compressed_tensors.offload import dispatch_model
18
19from llmcompressor import oneshot
20from llmcompressor.modeling.diffusion_gemma4 import ( # noqa: F401
21 CalibrationDiffusionGemmaTextExperts,
22)
23from llmcompressor.modifiers.quantization import QuantizationModifier
24
25# Load model
26MODEL_ID = "google/diffusiongemma-26B-A4B-it"
27
28model = DiffusionGemmaForBlockDiffusion.from_pretrained(
29 MODEL_ID, dtype="auto", trust_remote_code=True
30)
31processor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True)
32
33# CalibrationDiffusionGemmaTextExperts replaces the original
34# DiffusionGemmaTextExperts class during calibration to:
35# 1. Linearize the 3D expert tensors into individual nn.Linear modules
36# 2. Ensure all experts are properly calibrated, even those not activated
37# for certain tokens during calibration
38
39# Configure the quantization scheme
40# NVFP4 (4-bit weights, 4-bit activations) for Linear layers
41recipe = QuantizationModifier(
42 targets="Linear",
43 scheme="NVFP4",
44 ignore=[
45 "lm_head",
46 "re:.*embed.*",
47 "re:.*self_attn",
48 "re:.*router",
49 "re:.*vision_tower.*",
50 "re:.*self_conditioning.*",
51 ],
52)
53
54DATASET_ID = "neuralmagic/calibration"
55NUM_CALIBRATION_SAMPLES = 256
56MAX_SEQUENCE_LENGTH = 4096
57
58
59ds = load_dataset(DATASET_ID, name="LLM", split=f"train[:{NUM_CALIBRATION_SAMPLES}]")
60
61
62def preprocess_function(example):
63 messgages = []
64 for message in example["messages"]:
65 messgages.append(
66 {
67 "role": message["role"],
68 "content": [{"type": "text", "text": message["content"]}],
69 }
70 )
71
72 return processor.apply_chat_template(
73 messgages,
74 return_tensors="pt",
75 padding=False,
76 truncation=True,
77 max_length=MAX_SEQUENCE_LENGTH,
78 tokenize=True,
79 add_special_tokens=False,
80 return_dict=True,
81 add_generation_prompt=False,
82 )
83
84
85ds = ds.map(preprocess_function, batched=False, remove_columns=ds.column_names)
86
87
88def data_collator(batch):
89 assert len(batch) == 1
90 return {
91 key: (
92 torch.tensor(value)
93 if key != "pixel_values"
94 else torch.tensor(value, dtype=torch.bfloat16).squeeze(0)
95 )
96 for key, value in batch[0].items()
97 }
98
99
100# Apply quantization with calibration data
101oneshot(
102 model=model,
103 recipe=recipe,
104 dataset=ds,
105 max_seq_length=MAX_SEQUENCE_LENGTH,
106 num_calibration_samples=NUM_CALIBRATION_SAMPLES,
107 data_collator=data_collator,
108 sequential_targets=[
109 "DiffusionGemmaDecoderTextLayer",
110 "DiffusionGemmaEncoderTextLayer",
111 ],
112)
113
114# Test sample generation
115print("========== SAMPLE GENERATION ==============")
116dispatch_model(model)
117
118# "The reason the sky is blue is because" + chat template
119input_ids = torch.tensor(
120 [[
121 2, 105, 2364, 107, 818, 3282, 506, 7217, 563, 3730, 563,
122 1547, 106, 107, 105, 4368, 107
123 ]]
124).to(model.device)
125
126output = model.generate(
127 input_ids,
128 max_new_tokens=100,
129 max_denoising_steps=48,
130)
131print(processor.tokenizer.decode(output[0]))
132print("==========================================\n\n")
133
134# Save to disk in compressed-tensors format
135SAVE_DIR = MODEL_ID.rstrip("/").split("/")[-1] + "-NVFP4"
136model.save_pretrained(SAVE_DIR)
137processor.save_pretrained(SAVE_DIR)| Benchmark | google/diffusiongemma-26B-A4B-it | RedHatAI/diffusiongemma-26B-A4B-it-NVFP4 | Recovery (%) |
|---|---|---|---|
| AIME 2025 | 0.437 | 0.427 | 97.7% |
| GPQA Diamond | 0.641 | 0.644 | 100.5% |
| IFEval | 0.879 | 0.866 | 98.5% |
| GSM8K | 0.943 | 0.943 | 100.0% |
| MMLU 0-Shot | 0.539 | 0.616 | 114.3% |
| Thinking | |||
| AIME 2025 | 0.650 | 0.637 | 98.0% |
| GPQA Diamond | 0.698 | 0.677 | 97.0% |
| GSM8K | 0.951 | 0.952 | 100.1% |