Views
No views yet
1vllm serve RedHatAI/GLM-5.2-NVFP4-FP8 \
2 --tensor-parallel-size 4 \
3 --reasoning-parser glm45 \
4 --tool-call-parser glm47 \
5 --enable-auto-tool-choice \
6 --kv-cache-dtype fp8HuggingFaceH4/ultrachat_200k), as presented in the code snippet below.1import torch
2from compressed_tensors.offload import init_dist
3from compressed_tensors.quantization.quant_scheme import (
4 FP8_BLOCK,
5 NVFP4,
6 QuantizationScheme,
7)
8from datasets import load_dataset
9from transformers import AutoModelForCausalLM, AutoTokenizer
10
11from llmcompressor import oneshot
12from llmcompressor.datasets.utils import get_rank_partition
13from llmcompressor.modifiers.quantization import QuantizationModifier
14from llmcompressor.utils import load_context
15
16# Load the model
17init_dist()
18model_id = "zai-org/GLM-5.2"
19with load_context():
20 model = AutoModelForCausalLM.from_pretrained(
21 model_id,
22 device_map="auto_offload",
23 max_memory={},
24 offload_folder="./offload",
25 )
26tokenizer = AutoTokenizer.from_pretrained(model_id)
27
28# Select calibration dataset.
29DATASET_ID = "HuggingFaceH4/ultrachat_200k"
30DATASET_SPLIT = "train_sft"
31
32# Select number of samples. 512 samples is a good place to start.
33# Increasing the number of samples can improve accuracy.
34NUM_CALIBRATION_SAMPLES = 512
35MAX_SEQUENCE_LENGTH = 2048
36
37# Load dataset and preprocess.
38ds = load_dataset(
39 DATASET_ID, split=get_rank_partition(DATASET_SPLIT, NUM_CALIBRATION_SAMPLES)
40)
41ds = ds.shuffle(seed=42)
42
43
44def preprocess(example):
45 return {
46 "text": tokenizer.apply_chat_template(
47 example["messages"],
48 tokenize=False,
49 )
50 }
51
52
53ds = ds.map(preprocess)
54
55
56# Tokenize inputs.
57def tokenize(sample):
58 return tokenizer(
59 sample["text"],
60 padding=False,
61 max_length=MAX_SEQUENCE_LENGTH,
62 truncation=True,
63 add_special_tokens=False,
64 )
65
66
67ds = ds.map(tokenize, remove_columns=ds.column_names)
68
69
70# Configure the quantization algorithm to run.
71recipe = QuantizationModifier(
72 config_groups={
73 "attention_shared_experts": QuantizationScheme(
74 targets=[r"re:.*self_attn\..*"],
75 **FP8_BLOCK,
76 ),
77 "mlp": QuantizationScheme(
78 targets=[r"re:.*mlp\..*"],
79 **NVFP4,
80 ),
81 },
82 ignore=[
83 r"re:^model\.layers\.[0-2]\..*"
84 r"re:.*mlp\.gate.*", # not technically necessary
85 r"re:.*indexer\.weights_proj$", # sensitive to quantization
86 r"lm_head",
87 ],
88)
89
90# Apply algorithms.
91oneshot(
92 model=model,
93 dataset=ds,
94 batch_size=4,
95 recipe=recipe,
96 shuffle_calibration_samples=False,
97)
98
99# Save to disk compressed.
100# Note: base checkpoint generation_config needs fixing for newer transformers versions
101model.generation_config.top_p = None
102SAVE_DIR = model_id.rstrip("/").split("/")[-1] + "-NVFP4-FP8"
103model.save_pretrained(SAVE_DIR, save_compressed=True)
104tokenizer.save_pretrained(SAVE_DIR)
105
106torch.distributed.destroy_process_group()| Category | Benchmark | zai-org/GLM-5.2 | RedHatAI/GLM-5.2-NVFP4-FP8 | Recovery |
|---|---|---|---|---|
| Reasoning | GPQA Diamond (0-shot, pass@1) | 91.2 | 89.1 | 97.7% |