Views
No views yet
1from vllm import LLM, SamplingParams
2from transformers import AutoTokenizer
3
4model_id = "RedHatAI/GLM-4.6-NVFP4"
5number_gpus = 4
6
7sampling_params = SamplingParams(temperature=0.6, top_p=0.9, max_tokens=256)
8
9tokenizer = AutoTokenizer.from_pretrained(model_id)
10
11messages = [
12 {"role": "system", "content": "You are a pirate chatbot who always responds in pirate speak!"},
13 {"role": "user", "content": "Who are you?"},
14]
15
16prompts = tokenizer.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
17
18llm = LLM(model=model_id, tensor_parallel_size=number_gpus)
19
20outputs = llm.generate(prompts, sampling_params)
21
22generated_text = outputs[0].outputs[0].text
23print(generated_text)1from datasets import load_dataset
2from transformers import AutoModelForCausalLM, AutoTokenizer
3
4from llmcompressor import oneshot
5from llmcompressor.modifiers.quantization import QuantizationModifier
6from llmcompressor.utils import dispatch_for_generation
7
8MODEL_ID = "zai-org/GLM-4.6"
9
10# Load model.
11model = AutoModelForCausalLM.from_pretrained( MODEL_ID, torch_dtype="auto")
12tokenizer = AutoTokenizer.from_pretrained( MODEL_ID)
13
14DATASET_ID = "HuggingFaceH4/ultrachat_200k"
15DATASET_SPLIT = "train_sft"
16
17# Select number of samples. 512 samples is a good place to start.
18# Increasing the number of samples can improve accuracy.
19NUM_CALIBRATION_SAMPLES = 256
20MAX_SEQUENCE_LENGTH = 2048
21
22# Load dataset and preprocess.
23ds = load_dataset(DATASET_ID, split=f"{DATASET_SPLIT}[:{NUM_CALIBRATION_SAMPLES}]")
24ds = ds.shuffle(seed=42)
25
26
27def preprocess(example):
28 return {
29 "text": tokenizer.apply_chat_template(
30 example["messages"],
31 tokenize=False,
32 )
33 }
34
35ds = ds.map(preprocess)
36
37# Tokenize inputs.
38def tokenize(sample):
39 return tokenizer(
40 sample["text"],
41 padding=False,
42 max_length=MAX_SEQUENCE_LENGTH,
43 truncation=True,
44 add_special_tokens=False,
45 )
46
47ds = ds.map(tokenize, remove_columns=ds.column_names)
48
49# Configure the quantization algorithm and scheme.
50recipe = QuantizationModifier(
51 targets="Linear",
52 scheme="NVFP4",
53 ignore=[
54 "lm_head",
55 "re:.*mlp.gate$"
56 ],
57)
58
59# Apply quantization.
60oneshot(
61 model=model,
62 dataset=ds,
63 recipe=recipe,
64 max_seq_length=MAX_SEQUENCE_LENGTH,
65 num_calibration_samples=NUM_CALIBRATION_SAMPLES,
66 pipeline="sequential",
67 sequential_targets=["Glm4MoeDecoderLayer"],
68 trust_remote_code_model=True,
69)
70
71
72SAVE_DIR = "./" + MODEL_ID.rstrip("/").split("/")[-1] + "-NVFP4"
73model.save_pretrained(SAVE_DIR, save_compressed=True)
74tokenizer.save_pretrained(SAVE_DIR)
75| Category | Metric | zai-org/GLM-4.6-FP8 | RedHatAI/GLM-4.6-NVFP4 (this model) | Recovery |
|---|---|---|---|---|
| Leaderboard | MMLU Pro | 50.65 | 55.09 | 108.77% |
| IFEVAL | 91.97 | 92.69 | 100.78% | |
| Reasoning | AIME25 | 96.67% | 93.33% | 96.54% |
| Math-500 (0-shot) | 88.80% | 88.00% | 99.10% | |
| GPQA (Diamond, 0-shot) | 81.82% | 80.30% | 98.14% |
lm_eval --model local-chat-completions \
--tasks mmlu_pro \
--model_args "model=RedHatAI/GLM-4.6-NVFP4,max_length=90000,base_url=http://0.0.0.0:3758/v1/chat/completions,num_concurrent=128,max_retries=3,tokenized_requests=False,tokenizer_backend=None,timeout=1200" \
--num_fewshot 5 \
--apply_chat_template \
--fewshot_as_multiturn \
--output_path ./ \
--seed 42 \
--gen_kwargs "do_sample=True,temperature=1.0,top_p=0.95,max_gen_toks=64000"
lm_eval --model local-chat-completions \
--tasks leaderboard_ifeval \
--model_args "model=RedHatAI/GLM-4.6-NVFP4,max_length=90000,base_url=http://0.0.0.0:3758/v1/chat/completions,num_concurrent=128,max_retries=3,tokenized_requests=False,tokenizer_backend=None,timeout=1200" \
--num_fewshot 5 \
--apply_chat_template \
--fewshot_as_multiturn \
--output_path ./ \
--seed 42 \
--gen_kwargs "do_sample=True,temperature=1.0,top_p=0.95,max_gen_toks=64000"litellm_config.yaml:
model_parameters:
provider: "hosted_vllm"
model_name: "hosted_vllm/redhatai-glm-4.6-nvfp4"
base_url: "http://0.0.0.0:3759/v1"
api_key: ""
timeout: 3600
concurrent_requests: 128
generation_parameters:
temperature: 1.0
max_new_tokens: 131072
top_p: 0.95
seed: 0
lighteval endpoint litellm litellm_config.yaml \
"aime25|0,math_500|0,gpqa:diamond|0" \
--output-dir ./ \
--save-details