Views
No views yet
vllm serve RedHatAI/Qwen3-Next-80B-A3B-Thinking-NVFP4 --tensor_parallel_size 21from openai import OpenAI
2
3# Modify OpenAI's API key and API base to use vLLM's API server.
4openai_api_key = "EMPTY"
5openai_api_base = "http://<your-server-host>:8000/v1"
6
7client = OpenAI(
8 api_key=openai_api_key,
9 base_url=openai_api_base,
10)
11
12model = "RedHatAI/Qwen3-Next-80B-A3B-Thinking-NVFP4"
13
14messages = [
15 {"role": "user", "content": "Explain quantum mechanics clearly and concisely."},
16]
17
18
19outputs = client.chat.completions.create(
20 model=model,
21 messages=messages,
22)
23
24generated_text = outputs.choices[0].message.content
25print(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
8
9# NOTE: Requires a minimum of transformers 4.57.0
10
11MODEL_ID = "Qwen/Qwen3-Next-80B-A3B-Thinking"
12
13# Load model.
14model = AutoModelForCausalLM.from_pretrained(MODEL_ID, torch_dtype="auto")
15tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
16
17
18DATASET_ID = "HuggingFaceH4/ultrachat_200k"
19DATASET_SPLIT = "train_sft"
20
21# Select number of samples
22NUM_CALIBRATION_SAMPLES = 20
23MAX_SEQUENCE_LENGTH = 2048
24
25# Load dataset and preprocess.
26ds = load_dataset(DATASET_ID, split=f"{DATASET_SPLIT}[:{NUM_CALIBRATION_SAMPLES}]")
27ds = ds.shuffle(seed=42)
28
29
30def preprocess(example):
31 return {
32 "text": tokenizer.apply_chat_template(
33 example["messages"],
34 tokenize=False,
35 )
36 }
37
38
39ds = ds.map(preprocess)
40
41
42# Tokenize inputs.
43def tokenize(sample):
44 return tokenizer(
45 sample["text"],
46 padding=False,
47 max_length=MAX_SEQUENCE_LENGTH,
48 truncation=True,
49 add_special_tokens=False,
50 )
51
52
53ds = ds.map(tokenize, remove_columns=ds.column_names)
54
55# Configure the quantization algorithm and scheme.
56# In this case, we:
57# * quantize the weights to fp4 with per group 16 via ptq
58# * calibrate a global_scale for activations, which will be used to
59# quantize activations to fp4 on the fly
60recipe = QuantizationModifier(
61 targets="Linear",
62 scheme="NVFP4",
63 ignore=[
64 "lm_head",
65 "re:.*mlp.gate$",
66 "re:.*mlp.shared_expert_gate$",
67 "re:.*linear_attn.*",
68 ],
69)
70
71# Apply quantization.
72# MoE calibration is now handled automatically by the pipeline.
73# We set `moe_calibrate_all_experts` to True to ensure all experts receive
74# calibration data. This temporarily updates the model definition to use
75# `CalibrationQwen3NextSparseMoeBlock` (from `llmcompressor.modeling.qwen3_next_moe`)
76# which replaces the original `Qwen3NextSparseMoeBlock` class.
77# This updates how the forward pass is handled in the MoE block during calibration.
78# Feel free to update the definition under
79# llm-compressor/src/llmcompressor/modeling/qwen3_next_moe.py to play around with
80# this behavior and evaluate its impact on quantization performance.
81oneshot(
82 model=model,
83 dataset=ds,
84 recipe=recipe,
85 max_seq_length=MAX_SEQUENCE_LENGTH,
86 num_calibration_samples=NUM_CALIBRATION_SAMPLES,
87 moe_calibrate_all_experts=True,
88)
89
90
91print("\n\n")
92print("========== SAMPLE GENERATION ==============")
93dispatch_for_generation(model)
94input_ids = tokenizer("Hello my name is", return_tensors="pt").input_ids.to(
95 model.device
96)
97output = model.generate(input_ids, max_new_tokens=100)
98print(tokenizer.decode(output[0]))
99print("==========================================\n\n")
100
101
102# Save to disk in compressed-tensors format.
103SAVE_DIR = MODEL_ID.rstrip("/").split("/")[-1] + "-NVFP4"
104model.save_pretrained(SAVE_DIR, save_compressed=True)
105tokenizer.save_pretrained(SAVE_DIR)lm_eval \
--model vllm \
--model_args pretrained="RedHatAI/Qwen3-Next-80B-A3B-Thinking-NVFP4",dtype=auto,add_bos_token=True,max_model_len=16384,tensor_parallel_size=2,gpu_memory_utilization=0.9,enable_chunked_prefill=True,trust_remote_code=True \
--tasks openllm \
--write_out \
--batch_size auto \
--show_configlm_eval \
--model vllm \
--model_args pretrained="RedHatAI/Qwen3-Next-80B-A3B-Thinking-NVFP4",dtype=auto,add_bos_token=False,max_model_len=16384,tensor_parallel_size=2,gpu_memory_utilization=0.7,disable_log_stats=True,enable_chunked_prefill=True,trust_remote_code=True \
--tasks leaderboard \
--apply_chat_template \
--fewshot_as_multiturn \
--write_out \
--batch_size auto \
--show_configevalplus.evaluate --model "RedHatAI/Qwen3-Next-80B-A3B-Thinking-NVFP4" \
--dataset "humaneval" \
--backend vllm \
--tp 2 \
--greedy
evalplus.evaluate --model "RedHatAI/Qwen3-Next-80B-A3B-Thinking-NVFP4" \
--dataset "mbpp" \
--backend vllm \
--tp 2 \
--greedy