Views
No views yet
1from vllm import LLM, SamplingParams
2from transformers import AutoTokenizer
3
4model_id = "RedHatAI/MiniMax-M2.5-NVFP4"
5number_gpus = 1
6sampling_params = SamplingParams(temperature=1.0, top_p=0.95, top_k=40, min_p=0, max_tokens=256)
7
8messages = [
9 {"role": "user", "content": prompt}
10]
11
12tokenizer = AutoTokenizer.from_pretrained(model_id)
13
14messages = [{"role": "user", "content": "Give me a short introduction to large language model."}]
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)1import torch
2from datasets import load_dataset
3from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer
4
5from llmcompressor import oneshot
6from llmcompressor.modeling.minimax_m2 import ( # noqa: F401
7 CalibrationMiniMaxM2SparseMoeBlock,
8)
9from llmcompressor.modifiers.quantization import QuantizationModifier
10
11# Load the model
12model_id = "RedHatAI/MiniMax-M2.5-BF16"
13config = AutoConfig.from_pretrained(model_id, trust_remote_code=True)
14model = AutoModelForCausalLM.from_pretrained(
15 model_id, dtype=torch.bfloat16, config=config,trust_remote_code=True
16)
17tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
18# MoE calibration is handled automatically by the pipeline.
19# The `CalibrationMiniMaxM2SparseMoeBlock` modules (from
20# `llmcompressor.modeling.minimax_m2`) will be applied during calibration to enable
21# proper expert calibration. These replace the original
22# `MiniMaxM2SparseMoeBlock` class from
23# `transformers.models.minimax_m2.modeling_minimax_m2`.
24
25# Select calibration dataset.
26DATASET_ID = "HuggingFaceH4/ultrachat_200k"
27DATASET_SPLIT = "train_sft"
28
29# Select number of samples. 512 samples is a good place to start.
30# Increasing the number of samples can improve accuracy.
31NUM_CALIBRATION_SAMPLES = 512
32MAX_SEQUENCE_LENGTH = 2048
33
34# Load dataset and preprocess.
35ds = load_dataset(DATASET_ID, split=f"{DATASET_SPLIT}[:{NUM_CALIBRATION_SAMPLES}]")
36ds = ds.shuffle(seed=42)
37
38
39def preprocess(example):
40 return {
41 "text": tokenizer.apply_chat_template(
42 example["messages"],
43 tokenize=False,
44 )
45 }
46
47
48ds = ds.map(preprocess)
49
50
51# Tokenize inputs.
52def tokenize(sample):
53 return tokenizer(
54 sample["text"],
55 padding=False,
56 max_length=MAX_SEQUENCE_LENGTH,
57 truncation=True,
58 add_special_tokens=False,
59 )
60
61
62ds = ds.map(tokenize, remove_columns=ds.column_names)
63
64moe_ignores = [
65 "lm_head",
66 "re:.*block_sparse_moe.gate$",
67]
68
69# Experts live under `model.layers.*.block_sparse_moe.experts.<idx>.(w1|w2|w3)`.
70EXPERT_TARGET_REGEX = [
71 "re:.*block_sparse_moe\\.experts\\.\\d+\\.w1$",
72 "re:.*block_sparse_moe\\.experts\\.\\d+\\.w2$",
73 "re:.*block_sparse_moe\\.experts\\.\\d+\\.w3$",
74]
75
76recipe = QuantizationModifier(
77 targets=EXPERT_TARGET_REGEX,
78 scheme="NVFP4",
79 weight_observer="mse",
80 ignore= moe_ignores
81)
82
83
84# Apply algorithms.
85oneshot(
86 model=model,
87 dataset=ds,
88 processor=tokenizer,
89 recipe=recipe,
90 num_calibration_samples=NUM_CALIBRATION_SAMPLES,
91 max_seq_length=MAX_SEQUENCE_LENGTH,
92 sequential_targets=["MiniMaxM2DecoderLayer"],
93)
94
95# Save to disk compressed.
96SAVE_DIR = model_id.rstrip("/").split("/")[-1] + "-NVFP4"
97model.save_pretrained(SAVE_DIR, save_compressed=True)
98tokenizer.save_pretrained(SAVE_DIR)vllm serve RedHatAI/MiniMax-M2.5-NVFP4 --max-model-len 262144 --reasoning-parser deepseek_r1lm_eval --model local-chat-completions \
--tasks mmlu_pro_chat \
--model_args "model=RedHatAI/MiniMax-M2.5-NVFP4,max_length=262144,base_url=http://0.0.0.0:8000/v1/chat/completions,num_concurrent=64,max_retries=3,tokenized_requests=False,tokenizer_backend=None,timeout=1200" \
--num_fewshot 0 \
--apply_chat_template \
--gen_kwargs "do_sample=True,temperature=1.0,top_p=0.95,top_k=40,min_p=0.0,max_gen_toks=64000lm_eval --model local-chat-completions \
--tasks ifeval \
--model_args "model=RedHatAI/MiniMax-M2.5-NVFP4,max_length=262144,base_url=http://0.0.0.0:8000/v1/chat/completions,num_concurrent=64,max_retries=3,tokenized_requests=False,tokenizer_backend=None,timeout=1200" \
--num_fewshot 0 \
--apply_chat_template \
--gen_kwargs "do_sample=True,temperature=1.0,top_p=0.95,top_k=40,min_p=0.0,max_gen_toks=64000lm_eval --model local-chat-completions \
--tasks gsm8k_platinum_cot_llama \
--model_args "model=RedHatAI/MiniMax-M2.5-NVFP4,max_length=262144,base_url=http://0.0.0.0:8000/v1/chat/completions,num_concurrent=64,max_retries=3,tokenized_requests=False,tokenizer_backend=None,timeout=1200" \
--num_fewshot 0 \
--apply_chat_template \
--gen_kwargs "do_sample=True,temperature=1.0,top_p=0.95,top_k=40,min_p=0.0,max_gen_toks=640001model_parameters:
2 model_name: RedHatAI/MiniMax-M2.5-NVFP4
3 dtype: auto
4 gpu_memory_utilization: 0.9
5 max_model_length: 40960
6 generation_parameters:
7 temperature: 1.0
8 top_k: 40
9 min_p: 0.0
10 top_p: 0.95
11 max_new_tokens: 64000lighteval endpoint litellm lighteval_model_arguments.yaml \
"aime25|0,math_500|0,gpqa:diamond|0"| Benchmark | RedHatAI/MiniMax-M2.5-BF16 | RedHatAI/MiniMax-M2.5-NVFP4 | Recovery (%) |
|---|---|---|---|
| GSM8k Platinum (0-shot) | 95.15 | 93.91 | 98.70 |
| IfEval (0-shot) | 92.05 | 89.89 | 97.66 |
| AIME 2025 | 87.50 | 77.08 | 88.10 |
| GPQA diamond | 83.67 | 80.30 | 95.98 |
| Math 500 | 87.33 | 87.73 | 100.46 |
| MMLU Pro Chat | 80.83 | 80.08 | 99.07 |