Views
No views yet
1from vllm import LLM, SamplingParams
2from transformers import AutoTokenizer
3
4model_id = "RedHatAI/Qwen3-4B-Instruct-2507.w8a8"
5number_gpus = 1
6sampling_params = SamplingParams(temperature=0.6, top_p=0.95, top_k=20, min_p=0, max_tokens=256)
7
8messages = [
9 {"role": "user", "content": prompt}
10]
11
12tokenizer = AutoTokenizer.from_pretrained(model_id)
13
14messages = [{"role": "user", "content": "Give me a short introduction to large language model."}]
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 llmcompressor.modifiers.quantization import GPTQModifier
2from llmcompressor.modifiers.smoothquant import SmoothQuantModifier
3from llmcompressor.transformers import oneshot
4from transformers import AutoModelForCausalLM, AutoTokenizer
5
6# Load model
7model_stub = "Qwen/Qwen3-4B-Instruct"
8model_name = model_stub.split("/")[-1]
9
10num_samples = 1024
11max_seq_len = 8192
12
13model = AutoModelForCausalLM.from_pretrained(model_stub)
14
15tokenizer = AutoTokenizer.from_pretrained(model_stub)
16
17def preprocess_fn(example):
18 return {"text": tokenizer.apply_chat_template(example["messages"], add_generation_prompt=False, tokenize=False)}
19
20ds = load_dataset("neuralmagic/LLM_compression_calibration", split="train")
21ds = ds.map(preprocess_fn)
22
23# Configure the quantization algorithm and scheme
24recipe = [
25 SmoothQuantModifier(
26 smoothing_strength=0.9,
27 mappings=[
28 [["re:.*q_proj", "re:.*k_proj", "re:.*v_proj"], "re:.*input_layernorm"],
29 ],
30 ),
31 GPTQModifier(
32 ignore: ["lm_head"]
33 config_groups={"group_0": {"targets": ["Linear"], "weights": { "num_bits": 4, "type": int, "strategy": "group", "group_size": 128, "symmetric": true, "dynamic": false, "observer": "mse" } } },
34 dampening_frac=0.1,
35 )
36]
37
38
39# Apply quantization
40oneshot(
41 model=model,
42 dataset=ds,
43 recipe=recipe,
44 max_seq_length=max_seq_len,
45 num_calibration_samples=num_samples,
46)
47
48# Save to disk in compressed-tensors format
49save_path = model_name + "-quantized.w8a8"
50model.save_pretrained(save_path)
51tokenizer.save_pretrained(save_path)
52print(f"Model and tokenizer saved to: {save_path}")vllm serve RedHatAI/Qwen3-4B-Instruct-2507.w8a8 --max-model-len 262144lm_eval --model local-chat-completions \
--tasks mmlu_pro_chat \
--model_args "model=RedHatAI/Qwen3-4B-Instruct-2507.w8a8,max_length=262144,base_url=http://0.0.0.0:8000/v1/chat/completions,num_concurrent=64,max_retries=3,tokenized_requests=False,tokenizer_backend=None,timeout=1200" \
--num_fewshot 0 \
--apply_chat_template \
--gen_kwargs "do_sample=True,temperature=0.6,top_p=0.95,top_k=20,min_p=0.0,max_gen_toks=64000lm_eval --model local-chat-completions \
--tasks ifeval \
--model_args "model=RedHatAI/Qwen3-4B-Instruct-2507.w8a8,max_length=262144,base_url=http://0.0.0.0:8000/v1/chat/completions,num_concurrent=64,max_retries=3,tokenized_requests=False,tokenizer_backend=None,timeout=1200" \
--num_fewshot 0 \
--apply_chat_template \
--gen_kwargs "do_sample=True,temperature=0.6,top_p=0.95,top_k=20,min_p=0.0,max_gen_toks=64000lm_eval --model local-chat-completions \
--tasks mmlu_cot_llama \
--model_args "model=RedHatAI/Qwen3-4B-Instruct-2507.w8a8,max_length=262144,base_url=http://0.0.0.0:8000/v1/chat/completions,num_concurrent=64,max_retries=3,tokenized_requests=False,tokenizer_backend=None,timeout=1200" \
--num_fewshot 0 \
--apply_chat_template \
--gen_kwargs "do_sample=True,temperature=0.6,top_p=0.95,top_k=20,min_p=0.0,max_gen_toks=64000lm_eval --model local-chat-completions \
--tasks gsm8k_platinum_cot_llama \
--model_args "model=RedHatAI/Qwen3-4B-Instruct-2507.w8a8,max_length=262144,base_url=http://0.0.0.0:8000/v1/chat/completions,num_concurrent=64,max_retries=3,tokenized_requests=False,tokenizer_backend=None,timeout=1200" \
--num_fewshot 0 \
--apply_chat_template \
--gen_kwargs "do_sample=True,temperature=0.6,top_p=0.95,top_k=20,min_p=0.0,max_gen_toks=640001model_parameters:
2 model_name: RedHatAI/Qwen3-4B-Instruct-2507.w8a8
3 dtype: auto
4 gpu_memory_utilization: 0.9
5 max_model_length: 40960
6 generation_parameters:
7 temperature: 0.6
8 top_k: 20
9 min_p: 0.0
10 top_p: 0.95
11 max_new_tokens: 32000lighteval endpoint litellm lighteval_model_arguments.yaml \
"aime25|0,math_500|0,gpqa:diamond|0"| Benchmark | Qwen3-4B Instruct | Qwen3-4B Instruct.w8a8 (this model) | Recovery (%) |
|---|---|---|---|
| GSM8k Platinum (5-shot) | 95.62 | 95.73 | 100.12 |
| MMLU-CoT (5-shot) | 77.55 | 77.04 | 99.34 |
| MMLU-Pro (5-shot) | 70.13 | 69.96 | 99.75 |
| IfEval | 89.01 | 89.05 | 100.04 |
| AIME 2025 | 47.62 | 46.67 | 98.00 |
| GPQA diamond | 45.20 | 45.20 | 100.00 |
| Math 500 | 84.33 | 83.63 | 99.17 |