Views
No views yet
1from vllm import LLM, SamplingParams
2from transformers import AutoTokenizer
3
4model_id = "RedHatAI/Qwen3-235B-A22B-NVFP4"
5number_gpus = 1
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
2
3from llmcompressor import oneshot
4from llmcompressor.modifiers.quantization import QuantizationModifier
5from llmcompressor.modifiers.quantization import GPTQModifier
6from llmcompressor.modifiers.smoothquant import SmoothQuantModifier
7
8from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer
9from llmcompressor.modeling.prepare import replace_modules_for_calibration
10
11MODEL_ID = "Qwen/Qwen3-235B-A22B"
12
13 #Load model.
14model = AutoModelForCausalLM.from_pretrained(
15 MODEL_ID, device_map=None, torch_dtype="auto"
16)
17tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
18print(model)
19
20
21tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
22
23DATASET_ID = "HuggingFaceH4/ultrachat_200k"
24DATASET_SPLIT = "train_sft"
25
26NUM_CALIBRATION_SAMPLES = 256
27MAX_SEQUENCE_LENGTH = 1024
28
29# --- Replace MoE modules for calibration ---
30model = replace_modules_for_calibration(model, calibrate_all_experts=False)
31
32# Load dataset and preprocess.
33ds = load_dataset(DATASET_ID, split=f"{DATASET_SPLIT}[:{NUM_CALIBRATION_SAMPLES}]")
34ds = ds.shuffle(seed=42)
35
36def preprocess(example):
37 return {
38 "text": tokenizer.apply_chat_template(
39 example["messages"],
40 tokenize=False,
41 )
42 }
43
44
45ds = ds.map(preprocess)
46
47# Tokenize inputs.
48def tokenize(sample):
49 return tokenizer(
50 sample["text"],
51 padding=False,
52 max_length=MAX_SEQUENCE_LENGTH,
53 truncation=True,
54 add_special_tokens=False,
55 )
56
57
58ds = ds.map(tokenize, remove_columns=ds.column_names)
59
60recipe = QuantizationModifier(
61 targets="Linear",
62 scheme="NVFP4",
63 ignore=["re:.*lm_head.*", "re:.*mlp.gate$", "re:.*self_attn",
64],
65)
66
67# Save to disk in compressed-tensors format.
68SAVE_DIR = MODEL_ID.rstrip("/").split("/")[-1] + "-NVFP4"
69
70# Apply quantization.
71oneshot(
72 model=model,
73 dataset=ds,
74 recipe=recipe,
75 max_seq_length=MAX_SEQUENCE_LENGTH,
76 num_calibration_samples=NUM_CALIBRATION_SAMPLES,
77 output_dir=SAVE_DIR,
78 pipeline="sequential",
79 sequential_targets=["Qwen3MoeDecoderLayer"],
80 calibrate_moe_context=True,
81
82)
83# Save to disk in compressed-tensors format.
84model.save_pretrained(SAVE_DIR, save_compressed=True)
85tokenizer.save_pretrained(SAVE_DIR)
86| Category | Metric | Qwen/Qwen3-235B-A22B | RedHatAI/Qwen3-235B-A22B-NVFP4 (this model) | Recovery |
|---|---|---|---|---|
| OpenLLM V1 | arc_challenge | 73.38 | 72.61 | 98.95 |
| gsm8k | 85.14 | 86.43 | 101.52 | |
| hellaswag | 86.86 | 86.67 | 99.78 | |
| mmlu | 86.36 | 85.76 | 99.30 | |
| truthfulqa_mc2 | 60.58 | 60.09 | 99.19 | |
| winogrande | 80.74 | 80.90 | 100.20 | |
| Average | 78.84 | 78.74 | 99.87 | |
| OpenLLM V2 | BBH | 63.67 | 63.81 | 100.22 |
| MMLU-Pro | 58.23 | 57.99 | 99.59 | |
| MuSR | 43.25 | 42.99 | 99.40 | |
| IFEval | 88.25 | 88.25 | 100.00 | |
| GPQA | 29.28 | 28.94 | 98.84 | |
| Average | 56.54 | 56.40 | 99.75 | |
| Reasoning | GPQA (Diamond, 0-shot) | 72.22 | 69.19 | 95.80 |
| Math-500 (0-shot) | 95.00 | 94.20 | 99.16 | |
| Average | 83.61 | 81.70 | 97.72 | |
| Coding | HumanEval_64 pass@2 | 92.56 | 94.73 | 102.34 |
lm_eval \
--model vllm \
--model_args pretrained="RedHatAI/Qwen3-235B-A22B-NVFP4",dtype=auto,max_model_len=4096,tensor_parallel_size=2,enable_chunked_prefill=True,enforce_eager=True\
--apply_chat_template \
--fewshot_as_multiturn \
--tasks openllm \
--batch_size autolm_eval \
--model vllm \
--model_args pretrained="RedHatAI/Qwen3-235B-A22B-NVFP4",dtype=auto,max_model_len=4096,tensor_parallel_size=2,enable_chunked_prefill=True,enforce_eager=True\
--apply_chat_template \
--fewshot_as_multiturn \
--tasks leaderboard \
--batch_size autolm_eval \
--model vllm \
--model_args pretrained="RedHatAI/Qwen3-235B-A22B-NVFP4",dtype=auto,max_model_len=4096,tensor_parallel_size=2,enable_chunked_prefill=True,enforce_eager=True\
--apply_chat_template \
--fewshot_as_multiturn \
--tasks humaneval_64_instruct \
--batch_size auto