Views
No views yet
1from vllm import LLM, SamplingParams
2from transformers import AutoTokenizer
3
4model_id = "RedHatAI/Qwen3-235B-A22B-Instruct-2507-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-Instruct-2507"
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-Instruct-2507 | RedHatAI/Qwen3-235B-A22B-Instruct-2507-NVFP4 (this model) | Recovery |
|---|---|---|---|---|
| OpenLLM V1 | arc_challenge | 72.78 | 72.27 | 99.29 |
| gsm8k | 89.92 | 90.37 | 100.50 | |
| hellaswag | 87.77 | 87.54 | 99.74 | |
| mmlu | 87.48 | 87.08 | 99.54 | |
| truthfulqa_mc2 | 62.10 | 60.89 | 98.06 | |
| winogrande | 82.95 | 81.69 | 98.47 | |
| Average | 80.50 | 79.97 | 101.43 | |
| OpenLLM V2 | BBH | 68.3 | 69.05 | 101.01% |
| MMLU-Pro | 63.51 | 63.33 | 99.72 | |
| MuSR | 45.11 | 45.77 | 101.46 | |
| IFEval | 90.17 | 91.01 | 100.93 | |
| GPQA | 34.56 | 35.49 | 102.7 | |
| Average | 60.33 | 60.93 | 101.00 | |
| Reasoning | GPQA (Diamond, 0-shot) | 69.19 | 64.65 | 93.44 |
| Math-500 (0-shot) | 94.20 | 89.20 | 94.69 | |
| Average | 81.70 | 76.93 | 94.16 | |
| Coding | HumanEval_64 pass@2 | 96.67 | 96.46 | 99.78 |
lm_eval \
--model vllm \
--model_args pretrained="RedHatAI/Qwen3-235B-A22B-Instruct-2507-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-Instruct-2507-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-Instruct-2507-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