Skip to content

Commit 31e585c

Browse files
yiliu30dsikka
andauthored
[Autoround] Add MXFP8 example for Qwen3-8B (#2534)
## Summary - Adds an experimental example script demonstrating MXFP8 quantization of Qwen3-8B using `AutoRoundModifier` - Includes sample generation verification and saving in compressed format ```bash ========== SAMPLE GENERATION ============== Hello my name is Mandy and I am 25 years old. I live in a small village called Tynedale in the north of England. I have a small family, my parents and my younger brother. I work as a teacher in a local school. I love my job because I enjoy working with children. I have a dog called Lucy. She is a golden retriever and she is very friendly and smart. I also have a cat called Charlie. He is a black cat and he is very ========================================== ``` cc @hshen14 @thuang6 --------- Signed-off-by: yiliu30 <yi4.liu@intel.com> Co-authored-by: Dipika Sikka <dipikasikka1@gmail.com>
1 parent db18422 commit 31e585c

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from pathlib import Path
2+
3+
from auto_round.calib_dataset import get_dataset
4+
from transformers import AutoModelForCausalLM, AutoTokenizer
5+
6+
from llmcompressor import oneshot
7+
from llmcompressor.modifiers.autoround import AutoRoundModifier
8+
from compressed_tensors.offload import dispatch_model
9+
10+
# Select model and load it.
11+
model_id = "Qwen/Qwen3-8B"
12+
model = AutoModelForCausalLM.from_pretrained(model_id, dtype="auto")
13+
tokenizer = AutoTokenizer.from_pretrained(model_id)
14+
15+
# Select calibration dataset.
16+
NUM_CALIBRATION_SAMPLES = 128
17+
MAX_SEQUENCE_LENGTH = 2048
18+
# Get aligned calibration dataset.
19+
20+
ds = get_dataset(
21+
tokenizer=tokenizer,
22+
seqlen=MAX_SEQUENCE_LENGTH,
23+
nsamples=NUM_CALIBRATION_SAMPLES,
24+
)
25+
26+
# Configure the quantization algorithm to run.
27+
# * quantize the model to W8A8-MXFP8 with AutoRound
28+
recipe = AutoRoundModifier(
29+
targets="Linear",
30+
scheme="MXFP8",
31+
ignore=["lm_head"],
32+
iters=200,
33+
)
34+
35+
# Apply algorithms.
36+
oneshot(
37+
model=model,
38+
dataset=ds,
39+
recipe=recipe,
40+
max_seq_length=MAX_SEQUENCE_LENGTH,
41+
num_calibration_samples=NUM_CALIBRATION_SAMPLES,
42+
# disable shuffling to get slightly better mmlu score
43+
shuffle_calibration_samples=False,
44+
)
45+
46+
# Confirm generations of the quantized model look sane.
47+
print("\n\n")
48+
print("========== SAMPLE GENERATION ==============")
49+
dispatch_model(model)
50+
sample = tokenizer("Hello my name is", return_tensors="pt")
51+
sample = {key: value.to(model.device) for key, value in sample.items()}
52+
output = model.generate(**sample, max_new_tokens=100)
53+
print(tokenizer.decode(output[0]))
54+
print("==========================================\n\n")
55+
56+
# Save to disk compressed.
57+
SAVE_DIR = Path(model_id).name + "-W8A8-MXFP8-AutoRound"
58+
model.save_pretrained(SAVE_DIR, save_compressed=True)
59+
tokenizer.save_pretrained(SAVE_DIR)

src/llmcompressor/modifiers/autoround/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,8 @@ def _postprocess_qparams(self, model, llmc_registered_qparams):
440440
ar_value = getattr(module, ar_param_name)
441441
if ar_value is None:
442442
continue
443-
if self.scheme == "MXFP4" and ar_param_name == "scale":
444-
# Convert log2 scale back to normal scale for MXFP4
443+
if self.scheme in ("MXFP4", "MXFP8") and ar_param_name == "scale":
444+
# Convert log2 scale back to normal scale for MXFP4 and MXFP8
445445
ar_value = torch.exp2(ar_value.float())
446446
if not isinstance(ar_value, torch.Tensor):
447447
ar_value = torch.tensor(ar_value)

0 commit comments

Comments
 (0)