Views
No views yet
compressed-tensors, num_bits=4, type=int, symmetric=true, group_size=128group_size=128, actorder=static), stored as pack-quantizedblock_sparse_moe.gate) and lm_headgate_proj / up_proj / down_proj matrices are quantized. Only the tiny routing layer stays in BF16, which is why the on-disk reduction (~74%) is larger than for dense models: the experts dominate the parameter count.1numactl --physcpubind=0-95 python llm_compressor_quantize_and_run.py \
2 --model_id mistralai/Mixtral-8x7B-Instruct-v0.1 \
3 --save_dir ./Mixtral-8x7B-Instruct-v0.1-w4a16-llmcompressor-v0.12.0 \
4 --recipe gptq \
5 --scheme W4A16 \
6 --run1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3from datasets import load_dataset
4
5from llmcompressor import oneshot
6from llmcompressor.modifiers.quantization.gptq import GPTQModifier
7
8model_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
9output_dir = "./Mixtral-8x7B-Instruct-v0.1-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 recipe. The preset implies group_size=128, which is
35# valid here because Mixtral's 4096 and 14336 column counts are both divisible
36# by 128. The router is skipped: it is tiny and wrong routing wrecks accuracy.
37recipe = GPTQModifier(
38 targets="Linear",
39 scheme="W4A16",
40 ignore=[
41 "lm_head",
42 r"re:.*\.router$",
43 r"re:.*\.router\..*",
44 r"re:.*\.gate$",
45 r"re:.*\.mlp\.gate$",
46 ],
47)
48
49# Step 4: One-shot quantize with calibration and save in compressed-tensors format
50oneshot(
51 model=model,
52 dataset=ds,
53 recipe=recipe,
54 max_seq_length=MAX_SEQUENCE_LENGTH,
55 processor=tokenizer,
56)
57model.save_pretrained(output_dir, save_compressed=True)
58tokenizer.save_pretrained(output_dir)
59
60# Smoke test
61inputs = tokenizer("What are we having for dinner?", return_tensors="pt")
62output = model.generate(**inputs, max_new_tokens=30)
63print(tokenizer.decode(output[0], skip_special_tokens=True))1from vllm import LLM, SamplingParams
2
3model = LLM(
4 model="amd/Mixtral-8x7B-Instruct-v0.1-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.6406 | 0.6331 | 98.83% |
1lm_eval \
2 --model vllm \
3 --model_args pretrained=amd/Mixtral-8x7B-Instruct-v0.1-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 .