Views
No views yet
compressed-tensors, num_bits=4, type=int, symmetric=true, group_size=128group_size=128, actorder=static), stored as pack-quantizedlm_head1import torch
2from datasets import load_dataset
3from transformers import AutoModelForCausalLM, AutoTokenizer
4
5from llmcompressor import oneshot
6from llmcompressor.modifiers.gptq import GPTQModifier
7
8model_id = "microsoft/Phi-4-reasoning-plus"
9output_dir = "./Phi-4-reasoning-plus-w4a16-llmcompressor-v0.12.0"
10
11NUM_CALIBRATION_SAMPLES = 128
12MAX_SEQUENCE_LENGTH = 2048
13
14# Step 1: Load the BF16 model and tokenizer
15model = AutoModelForCausalLM.from_pretrained(
16 model_id,
17 dtype=torch.bfloat16,
18 device_map="cpu",
19 trust_remote_code=True,
20)
21tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
22
23# Step 2: Load calibration data. GPTQ is data-driven: it needs real activations
24# to build the per-layer Hessians used to compensate the rounding error.
25ds = load_dataset(
26 "HuggingFaceH4/ultrachat_200k",
27 split=f"train_sft[:{NUM_CALIBRATION_SAMPLES}]",
28)
29ds = ds.map(
30 lambda example: {"text": "\n".join(m["content"] for m in example["messages"])},
31 remove_columns=ds.column_names,
32)
33
34# Step 3: Define the W4A16 GPTQ recipe
35recipe = GPTQModifier(scheme="W4A16", targets="Linear", ignore=["lm_head"])
36
37# Step 4: One-shot quantize with calibration and save in compressed-tensors format
38oneshot(
39 model=model,
40 dataset=ds,
41 recipe=recipe,
42 max_seq_length=MAX_SEQUENCE_LENGTH,
43 tokenizer=tokenizer,
44 output_dir=output_dir,
45 trust_remote_code_model=True,
46)
47
48# Smoke test
49inputs = tokenizer("What are we having for dinner?", return_tensors="pt")
50output = model.generate(**inputs, max_new_tokens=30)
51print(tokenizer.decode(output[0], skip_special_tokens=True))1from vllm import LLM, SamplingParams
2
3model = LLM(
4 model="amd/Phi-4-reasoning-plus-w4a16-llmcompressor-v0.12.0",
5 dtype="bfloat16",
6)
7
8sampling_params = SamplingParams(temperature=0.7, max_tokens=256)
9outputs = model.generate(["Hello, how are you?"], sampling_params)
10print(outputs[0].outputs[0].text)torch==2.11.0
zentorch==2.11.0.3
vllm==0.26.0
llmcompressor==0.12.0LD_PRELOAD with libomp.so (LLVM OpenMP) or libiomp5.so (Intel OpenMP):1# Using LLVM OpenMP (llvmopenmp)
2export LD_PRELOAD=$(find /path/to/env -name "libomp.so" | head -1)
3
4# Or using Intel OpenMP (libiomp)
5export LD_PRELOAD=$(find /path/to/env -name "libiomp5.so" | head -1)Note: SetLD_PRELOADbefore launching vLLM or any inference script.
| Benchmark | BF16 Baseline | W4A16 (this model) | Recovery |
|---|---|---|---|
| GSM8K (5-shot) | 0.8704 | 0.8688 | 99.82% |
1lm_eval \
2 --model vllm \
3 --model_args pretrained=amd/Phi-4-reasoning-plus-w4a16-llmcompressor-v0.12.0,dtype=bfloat16 \
4 --tasks gsm8k \
5 --batch_size auto \
6 --trust_remote_code \
7 --num_fewshot 5 \
8 --apply_chat_template \
9 --log_samples \
10 --gen_kwargs "max_gen_toks=2048" \
11 --output_path .