Views
No views yet
1from vllm import LLM, SamplingParams
2from transformers import AutoTokenizer
3
4model_id = "RedHatAI/GLM-4.6-quantized.w4a16"
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 GPTQModifier
6from llmcompressor.utils import dispatch_for_generation
7
8MODEL_ID = "zai-org/GLM-4.6"
9
10# Load model.
11model = AutoModelForCausalLM.from_pretrained(
12 MODEL_ID, torch_dtype="auto"
13)
14tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
15
16# Select calibration dataset.
17DATASET_ID = "HuggingFaceH4/ultrachat_200k"
18DATASET_SPLIT = "train_sft"
19
20# Select number of samples.
21# Increasing the number of samples can improve accuracy.
22NUM_CALIBRATION_SAMPLES = 512
23MAX_SEQUENCE_LENGTH = 2048
24
25# Load dataset and preprocess.
26ds = load_dataset(DATASET_ID, split=f"{DATASET_SPLIT}[:{NUM_CALIBRATION_SAMPLES}]")
27ds = ds.shuffle(seed=42)
28
29def preprocess(example):
30 return {
31 "text": tokenizer.apply_chat_template(
32 example["messages"],
33 tokenize=False,
34 )
35 }
36
37ds = ds.map(preprocess)
38
39# Tokenize inputs.
40def tokenize(sample):
41 return tokenizer(
42 sample["text"],
43 padding=False,
44 max_length=MAX_SEQUENCE_LENGTH,
45 truncation=True,
46 add_special_tokens=False,
47 )
48
49ds = ds.map(tokenize, remove_columns=ds.column_names)
50
51# Configure the quantization algorithm and scheme with explicit parameters.
52recipe = GPTQModifier(
53 targets="Linear",
54 scheme="W4A16",
55 ignore=[
56 "lm_head",
57 "re:.*mlp.gate$"
58 ],
59)
60
61# Apply quantization.
62oneshot(
63 model=model,
64 dataset=ds,
65 recipe=recipe,
66 max_seq_length=MAX_SEQUENCE_LENGTH,
67 num_calibration_samples=NUM_CALIBRATION_SAMPLES,
68 pipeline="sequential",
69 sequential_targets=["Glm4MoeDecoderLayer"],
70 trust_remote_code_model=True,
71)
72
73SAVE_DIR = "./" + MODEL_ID.rstrip("/").split("/")[-1] + "-quantized.w4a16"
74model.save_pretrained(SAVE_DIR, save_compressed=True)
75tokenizer.save_pretrained(SAVE_DIR)
76| Category | Metric | zai-org/GLM-4.6-FP8 | RedHatAI/GLM-4.6-quantized.w4a16 (this model) | Recovery |
|---|---|---|---|---|
| Leaderboard | MMLU Pro | 50.65% | 53.22% | 105.07% |
| IFEVAL | 91.97% | 92.21% | 100.26% | |
| Reasoning | AIME25 | 96.67% | 90.00% | 93.10% |
| 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-quantized.w4a16,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-quantized.w4a16,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-W4A16"
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