-
Notifications
You must be signed in to change notification settings - Fork 652
Expand file tree
/
Copy pathglm5_example.py
More file actions
67 lines (61 loc) · 1.95 KB
/
Copy pathglm5_example.py
File metadata and controls
67 lines (61 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# NOTE: to use a custom dataset, see examples/custom_dataset_example.py
import torch
from compressed_tensors.offload import init_dist
from compressed_tensors.quantization.quant_scheme import (
FP8_BLOCK,
NVFP4,
QuantizationScheme,
)
from transformers import AutoModelForCausalLM, AutoTokenizer
from llmcompressor import oneshot
from llmcompressor.modifiers.quantization import QuantizationModifier
from llmcompressor.utils import load_context
# Load the model
# NOTE: `transformers==5.14` breaks saving for disk-offloaded models.
# Please install `transformers>=5.15` or install from source
init_dist()
model_id = "zai-org/GLM-5.2"
with load_context():
model = AutoModelForCausalLM.from_pretrained(
model_id,
device_map="auto_offload",
max_memory={"cpu": "500GiB"},
offload_folder="offload_folder",
)
tokenizer = AutoTokenizer.from_pretrained(model_id)
# Configure the quantization algorithm to run.
recipe = QuantizationModifier(
config_groups={
"attention_shared_experts": QuantizationScheme(
targets=[r"re:.*self_attn\..*"],
**FP8_BLOCK,
),
"mlp": QuantizationScheme(
targets=[r"re:.*mlp\..*"],
**NVFP4,
),
},
ignore=[
r"re:^model\.layers\.[0-2]\..*",
r"re:.*mlp\.gate.*",
r"re:.*indexer\.weights_proj$", # sensitive to quantization
r"lm_head",
],
)
# Apply algorithms.
oneshot(
model=model,
dataset="perfectblend",
splits="train[:512]",
batch_size=4,
recipe=recipe,
num_calibration_samples=512,
shuffle_calibration_samples=False,
)
# Save to disk compressed.
# Note: base checkpoint generation_config needs fixing for newer transformers versions
model.generation_config.top_p = None
SAVE_DIR = model_id.rstrip("/").split("/")[-1] + "-NVFP4-FP8"
model.save_pretrained(SAVE_DIR, save_compressed=True)
tokenizer.save_pretrained(SAVE_DIR)
torch.distributed.destroy_process_group()