Views
No views yet
1from vllm import LLM, SamplingParams
2from transformers import AutoTokenizer
3
4model_id = "RedHatAI/Qwen3-14B-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
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 = "Qwen/Qwen3-14B"
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 = 512
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
26def preprocess(example):
27 return {
28 "text": tokenizer.apply_chat_template(
29 example["messages"],
30 tokenize=False,
31 )
32 }
33
34ds = ds.map(preprocess)
35
36# Tokenize inputs.
37def tokenize(sample):
38 return tokenizer(
39 sample["text"],
40 padding=False,
41 max_length=MAX_SEQUENCE_LENGTH,
42 truncation=True,
43 add_special_tokens=False,
44 )
45
46ds = ds.map(tokenize, remove_columns=ds.column_names)
47
48# Configure the quantization algorithm and scheme.
49# In this case, we:
50# * quantize the weights to fp4 with per group 16 via ptq
51# * calibrate a global_scale for activations, which will be used to
52# quantize activations to fp4 on the fly
53recipe = [
54 QuantizationModifier(
55 ignore=["re:.*lm_head.*"],
56 config_groups={
57 "group_0": {
58 "targets": ["Linear"],
59 "weights": {
60 "num_bits": 4,
61 "type": "float",
62 "strategy": "tensor_group",
63 "group_size": 16,
64 "symmetric": True,
65 "observer": "mse",
66 },
67 "input_activations": {
68 "num_bits": 4,
69 "type": "float",
70 "strategy": "tensor_group",
71 "group_size": 16,
72 "symmetric": True,
73 "dynamic": "local",
74 "observer": "minmax",
75 },
76 }
77 },
78 )
79]
80
81# Save to disk in compressed-tensors format.
82SAVE_DIR = MODEL_ID.rstrip("/").split("/")[-1] + "-NVFP4"
83
84# Apply quantization.
85oneshot(
86 model=model,
87 dataset=ds,
88 recipe=recipe,
89 max_seq_length=MAX_SEQUENCE_LENGTH,
90 num_calibration_samples=NUM_CALIBRATION_SAMPLES,
91 output_dir=SAVE_DIR,
92)
93
94print("\n\n")
95print("========== SAMPLE GENERATION ==============")
96dispatch_for_generation(model)
97input_ids = tokenizer("Hello my name is", return_tensors="pt").input_ids.to("cuda")
98output = model.generate(input_ids, max_new_tokens=100)
99print(tokenizer.decode(output[0]))
100print("==========================================\n\n")
101
102model.save_pretrained(SAVE_DIR, save_compressed=True)
103tokenizer.save_pretrained(SAVE_DIR)
104| Category | Metric | Qwen3-14B | Qwen3-14B-NVFP4 (this model) | Recovery |
|---|---|---|---|---|
| OpenLLM V1 | arc_challenge | 67.32 | 67.06 | 99.61 |
| gsm8k | 88.70 | 88.25 | 99.49 | |
| hellaswag | 79.62 | 78.24 | 98.27 | |
| mmlu | 78.86 | 77.23 | 97.93 | |
| truthfulqa_mc2 | 58.59 | 58.49 | 99.83 | |
| winogrande | 73.72 | 73.80 | 100.11 | |
| Average | 74.47 | 73.85 | 99.16 | |
| OpenLLM V2 | BBH (3-shot) | 59.45 | 56.78 | 95.51 |
| MMLU-Pro (5-shot) | 44.39 | 41.15 | 92.70 | |
| MuSR (0-shot) | 38.62 | 37.83 | 97.95 | |
| IFEval (0-shot) | 89.45 | 90.41 | 101.07 | |
| GPQA (0-shot) | 27.43 | 26.59 | 96.94 | |
| Math-|v|-5 (4-shot) | 57.33 | 53.40 | 93.14 | |
| Average | 52.78 | 51.03 | 96.68 | |
| Coding | HumanEval_64 pass@2 | 90.74 | 89.87 | 99.04 |
| Reasoning | AIME24 (0-shot) | 75.86 | 65.52 | 86.34 |
| AIME25 (0-shot) | 68.97 | 65.52 | 95.00 | |
| GPQA (Diamond, 0-shot) | 64.97 | 60.40 | 93.00 | |
| Average | 69.93 | 63.81 | 91.45 |
lm_eval \
--model vllm \
--model_args pretrained="RedHatAI/Qwen3-14B-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-14B-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-14B-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# --- model_args.yaml ---
cat > model_args.yaml <<'YAML'
model_parameters:
model_name: "RedHatAI/Qwen3-14B-NVFP4"
dtype: auto
gpu_memory_utilization: 0.9
tensor_parallel_size: 2
max_model_length: 40960
generation_parameters:
seed: 42
temperature: 0.6
top_k: 20
top_p: 0.95
min_p: 0.0
max_new_tokens: 32768
YAML
lighteval vllm model_args.yaml \
"lighteval|aime24|0,lighteval|aime25|0,lighteval|gpqa:diamond|0" \
--max-samples -1 \
--output-dir out_dir