Views
No views yet
1from vllm import LLM, SamplingParams
2from transformers import AutoTokenizer
3
4model_id = "RedHatAI/Qwen3-32B-NVFP4A16"
5number_gpus = 2
6
7sampling_params = SamplingParams(temperature=6, top_p=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-32B"
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
51recipe = QuantizationModifier(targets="Linear", scheme="NVFP4A16", ignore=["lm_head"])
52
53# Save to disk in compressed-tensors format.
54SAVE_DIR = MODEL_ID.rstrip("/").split("/")[-1] + "-NVFP4A16"
55
56# Apply quantization.
57oneshot(
58 model=model,
59 dataset=ds,
60 recipe=recipe,
61 max_seq_length=MAX_SEQUENCE_LENGTH,
62 num_calibration_samples=NUM_CALIBRATION_SAMPLES,
63 output_dir=SAVE_DIR,
64)
65
66print("\n\n")
67print("========== SAMPLE GENERATION ==============")
68dispatch_for_generation(model)
69input_ids = tokenizer("Hello my name is", return_tensors="pt").input_ids.to("cuda")
70output = model.generate(input_ids, max_new_tokens=100)
71print(tokenizer.decode(output[0]))
72print("==========================================\n\n")
73
74model.save_pretrained(SAVE_DIR, save_compressed=True)
75tokenizer.save_pretrained(SAVE_DIR)
76| Category | Metric | Qwen/Qwen3-32B | RedHatAI/Qwen3-32B-NVFP4A16 (this model) | Recovery (%) |
|---|---|---|---|---|
| OpenLLM V1 | MMLU | 80.94 | 80.57 | 99.55% |
| ARC Challenge (0-shot) | 68.34 | 68.43 | 100.12% | |
| GSM8K (8-shot, strict-match) | 87.34 | 87.72 | 100.43% | |
| Hellaswag (10-shot) | 71.16 | 70.48 | 99.05% | |
| Winogrande (5-shot) | 69.93 | 70.09 | 100.23% | |
| TruthfulQA (0-shot, mc2) | 58.63 | 58.96 | 100.56% | |
| Average | 72.72 | 72.71 | 99.98% | |
| OpenLLM V2 | MMLU-Pro (5-shot) | 54.48 | 51.61 | 94.73% |
| IFEval (0-shot) | 88.85 | 88.49 | 99.59% | |
| BBH (3-shot) | 62.61 | 62.14 | 99.25% | |
| Math-|v|-5 (4-shot) | 56.87 | 56.27 | 98.94% | |
| GPQA (0-shot) | 30.45 | 30.29 | 99.47% | |
| MuSR (0-shot) | 39.15 | 40.48 | 103.40% | |
| Average | 55.40 | 54.88 | 99.06% | |
| Coding | HumanEval Instruct pass@1 | 88.41 | 87.20 | 98.63% |
| HumanEval 64 Instruct pass@2 | 90.27 | 89.66 | 99.32% | |
| HumanEval 64 Instruct pass@8 | 92.20 | 92.13 | 99.92% | |
| HumanEval 64 Instruct pass@16 | 92.96 | 93.27 | 100.33% | |
| HumanEval 64 Instruct pass@32 | 93.58 | 94.47 | 100.95% | |
| HumanEval 64 Instruct pass@64 | 93.90 | 95.73 | 101.95% |
lm_eval \
--model vllm \
--model_args pretrained="RedHatAI/Qwen3-32B-NVFP4A16",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-32B-NVFP4A16",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-32B-NVFP4A16",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_instruct \
--batch_size auto
lm_eval \
--model vllm \
--model_args pretrained="RedHatAI/Qwen3-32B-NVFP4A16",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