Views
No views yet
1import torch
2from datasets import load_dataset
3from transformers import AutoProcessor, Qwen3_5ForConditionalGeneration
4
5from llmcompressor import oneshot
6from llmcompressor.modifiers.gptq import GPTQModifier
7from llmcompressor.modifiers.transform.awq import AWQModifier
8from llmcompressor.utils import load_context
9
10MODEL_ID = "Qwen/Qwen3.8-27B"
11
12# Load model.
13with load_context(Qwen3_5ForConditionalGeneration):
14 model = Qwen3_5ForConditionalGeneration.from_pretrained(MODEL_ID)
15processor = AutoProcessor.from_pretrained(MODEL_ID)
16
17
18recipe = [
19 AWQModifier(duo_scaling="both"),
20 GPTQModifier(
21 targets="Linear",
22 scheme="W4A16",
23 ignore=[
24 "re:visual.*",
25 "re:model.visual.*",
26 r"re:.*lm_head",
27 "re:.*embed_tokens$",
28 r"re:.*linear_attn\.in_proj_a$",
29 r"re:.*linear_attn\.in_proj_b$",
30 ],
31 kv_cache_scheme={
32 "num_bits": 8,
33 "type": "float",
34 "symmetric": True,
35 "strategy": "tensor",
36 "dynamic": False,
37 "observer": "static_minmax",
38 },
39 ),
40]
41
42NUM_CALIBRATION_SAMPLES = 512
43MAX_SEQUENCE_LENGTH = 4096
44
45ds = load_dataset(
46 "mlabonne/open-perfectblend",
47 split=f"train[:{NUM_CALIBRATION_SAMPLES}]",
48)
49ds = ds.shuffle(seed=42)
50
51ROLE_MAP = {"human": "user", "gpt": "assistant"}
52
53
54def preprocess_function(example):
55 messages = [
56 {
57 "role": ROLE_MAP.get(msg["from"], msg["from"]),
58 "content": [{"type": "text", "text": msg["value"]}],
59 }
60 for msg in example["conversations"]
61 ]
62 return processor.apply_chat_template(
63 messages,
64 tokenize=True,
65 return_dict=True,
66 add_generation_prompt=False,
67 processor_kwargs={
68 "return_tensors": "pt",
69 "padding": False,
70 "truncation": True,
71 "max_length": MAX_SEQUENCE_LENGTH,
72 "add_special_tokens": False,
73 },
74 )
75
76
77ds = ds.map(preprocess_function, batched=False, remove_columns=ds.column_names)
78
79
80def data_collator(batch):
81 assert len(batch) == 1
82 return {key: torch.tensor(value) for key, value in batch[0].items()}
83
84
85# Apply quantization.
86oneshot(
87 model=model,
88 recipe=recipe,
89 dataset=ds,
90 max_seq_length=MAX_SEQUENCE_LENGTH,
91 num_calibration_samples=NUM_CALIBRATION_SAMPLES,
92 moe_calibrate_all_experts=True,
93 data_collator=data_collator,
94)
95
96# Save to disk in compressed-tensors format.
97SAVE_DIR = MODEL_ID.rstrip("/").split("/")[-1] + "-INT4"
98model.save_pretrained(SAVE_DIR)
99processor.save_pretrained(SAVE_DIR)
1001vllm serve RedHatAI/Qwen3.8-27B-INT4
2 --tensor-parallel-size 1
3 --enable-auto-tool-choice
4 --tool-call-parser qwen3_coder
5 --reasoning-parser qwen3
6 --mm-encoder-tp-mode data
7 --max-model-len 69632
8 --gpu-memory-utilization 0.9| Eval | Qwen/Qwen3.8-27B (BF16) | RedHatAI/Qwen3.8-27B-INT4 | Recovery (INT4/BF16) |
|---|---|---|---|
| gsm8k_platinum (strict-match) | 95.75% | 96.77% | 101.07% |
| ifeval (prompt-level strict) | 92.24% | 91.93% | 99.67% |
| mmlu_pro_chat (exact-match) | 84.46% | 83.45% | 98.81% |
| math_500 (pass@1) | 83.73% | 83.33% | 99.52% |
| gpqa_diamond (pass@1) | 89.23% | 87.88% | 98.49% |
| aime25 (avg@1, 8 seeds) | 95.42% | 94.17% | 98.69% |
lm_eval \
--model local-chat-completions \
--tasks gsm8k_platinum_cot_llama \
--model_args model=Qwen/Qwen3.8-27B,max_length=69632,base_url=http://127.0.0.1:8002/v1/chat/completions,num_concurrent=32,max_retries=3,tokenized
_requests=False,tokenizer_backend=None,timeout=3600 \
--num_fewshot 0 \
--seed <SEED> \
--gen_kwargs do_sample=True,temperature=1.0,top_p=0.95,top_k=20,seed=<SEED>,max_gen_toks=32000 \
--apply_chat_templatelm_eval \
--model local-chat-completions \
--tasks ifeval \
--model_args model=Qwen/Qwen3.8-27B,max_length=69632,base_url=http://127.0.0.1:8006/v1/chat/completions,num_concurrent=16,max_retrie
s=3,tokenized_requests=False,tokenizer_backend=None,timeout=3600 \
--num_fewshot 0 \
--seed <SEED> \
--gen_kwargs do_sample=True,temperature=1.0,top_p=0.95,top_k=20,seed=1234,max_gen_toks=32000 \
--apply_chat_templatelighteval endpoint litellm \
runs/20260818_110422_qwen3.8-27b-int4_full_ifeval-math-aime-gpqa/configs/litellm_aime25_seed1234.yaml \
'aime25|0' \
--save-detailslighteval endpoint litellm \
runs/20260818_110422_qwen3.8-27b-int4_full_ifeval-math-aime-gpqa/configs/litellm_math_500_seed1234.yaml \
'math_500|0' \
--save-detailslighteval endpoint litellm \
runs/20260818_110422_qwen3.8-27b-int4_full_ifeval-math-aime-gpqa/configs/litellm_gpqa_diamond_seed1234.yaml \
'gpqa:diamond|0' \
--save-details