Views
No views yet
1from vllm import LLM, SamplingParams
2from transformers import AutoTokenizer
3
4model_id = "RedHatAI/Llama-4-Maverick-17B-128E-Instruct-NVFP4"
5number_gpus = 2
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)1import torch
2from datasets import load_dataset
3from transformers import Llama4ForConditionalGeneration, Llama4Processor
4
5from llmcompressor import oneshot
6from llmcompressor.modifiers.quantization import QuantizationModifier
7
8# Select model and load it.
9model_id = "meta-llama/Llama-4-Maverick-17B-128E-Instruct"
10model = Llama4ForConditionalGeneration.from_pretrained(model_id, torch_dtype="auto")
11processor = Llama4Processor.from_pretrained(model_id)
12# MoE calibration is now handled automatically by the pipeline.
13# The `SequentialLlama4TextMoe` modules (from `llmcompressor.modeling.llama4`)
14# will be applied during calibration to enable
15# proper expert calibration and vLLM compatibility.
16# These replace the original `Llama4TextMoe` class from
17# `transformers.models.llama4.modeling_llama4`.
18
19DATASET_ID = "neuralmagic/calibration"
20NUM_CALIBRATION_SAMPLES = 20
21MAX_SEQUENCE_LENGTH = 8192
22
23ds = load_dataset(DATASET_ID, name="LLM", split=f"train[:{NUM_CALIBRATION_SAMPLES}]")
24
25
26def preprocess_function(example):
27 messgages = []
28 for message in example["messages"]:
29 messgages.append(
30 {
31 "role": message["role"],
32 "content": [{"type": "text", "text": message["content"]}],
33 }
34 )
35
36 return processor.apply_chat_template(
37 messgages,
38 return_tensors="pt",
39 padding=False,
40 truncation=True,
41 max_length=MAX_SEQUENCE_LENGTH,
42 tokenize=True,
43 add_special_tokens=False,
44 return_dict=True,
45 add_generation_prompt=False,
46 )
47
48
49ds = ds.map(preprocess_function, batched=False, remove_columns=ds.column_names)
50
51
52def data_collator(batch):
53 assert len(batch) == 1
54 return {
55 key: (
56 torch.tensor(value)
57 if key != "pixel_values"
58 else torch.tensor(value, dtype=torch.bfloat16).squeeze(0)
59 )
60 for key, value in batch[0].items()
61 }
62
63
64# Configure the quantization algorithm to run.
65recipe = QuantizationModifier(
66 targets="Linear",
67 scheme="NVFP4",
68 ignore=[
69 "re:.*lm_head",
70 "re:.*self_attn",
71 "re:.*router",
72 "re:.*vision_model.*",
73 "re:.*multi_modal_projector.*",
74 "Llama4TextAttention",
75 ],
76)
77
78# Apply algorithms.
79# due to the large size of Llama4, we specify sequential targets such that
80# only one MLP is loaded into GPU memory at a time
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 sequential_targets=["Llama4TextMLP"],
88 data_collator=data_collator,
89)
90
91
92# Save to disk compressed.
93SAVE_DIR = model_id.rstrip("/").split("/")[-1] + "-NVFP4"
94model.save_pretrained(SAVE_DIR)
95processor.save_pretrained(SAVE_DIR)
96| Category | Metric | Llama-4-Maverick-17B-128E-Instruct | Llama-4-Maverick-17B-128E-Instruct-NVFP4 (this model) | Recovery |
|---|---|---|---|---|
| OpenLLM V1 | arc_challenge_llama | 95.97 | 95.88 | 99.91 |
| gsm8k_llama | 96.13 | 96.06 | 99.93 | |
| mmlu_llama | 86.77 | 85.49 | 98.53 | |
| mmlu_cot_llama | 89.49 | 88.72 | 99.14 | |
| truthfulqa_mc2 | 68.23 | 68.42 | 100.28 | |
| winogrande | 77.98 | 77.74 | 99.69 | |
| hellaswag | ||||
| Average | 85.23 | |||
| OpenLLM V2 | BBH | 69.52 | ||
| MMLU-Pro | 62.83 | |||
| MuSR | 45.77 | |||
| IFEval | 89.45 | |||
| GPQA | 30.54 | |||
| Math-Hard | 64.95 | |||
| Average | 60.51 | |||
| Coding | HumanEval_64 (pass@2) | 88.88 |
lm_eval \
--model vllm \
--model_args pretrained="RedHatAI/Llama-4-Maverick-17B-128E-Instruct-NVFP4",dtype=auto,add_bos_token=True,max_model_len=4096,tensor_parallel_size=2,enable_chunked_prefill=True,enforce_eager=True \
--tasks mmlu_llama \
--apply_chat_template \
--fewshot_as_multiturn \
--batch_size autolm_eval \
--model vllm \
--model_args pretrained="RedHatAI/Llama-4-Maverick-17B-128E-Instruct-NVFP4",dtype=auto,add_bos_token=True,max_model_len=4096,tensor_parallel_size=2,enable_chunked_prefill=True,enforce_eager=True \
--tasks mmlu_cot_llama \
--apply_chat_template \
--fewshot_as_multiturn \
--batch_size autolm_eval \
--model vllm \
--model_args pretrained="RedHatAI/Llama-4-Maverick-17B-128E-Instruct-NVFP4",dtype=auto,add_bos_token=True,max_model_len=4096,tensor_parallel_size=2,enable_chunked_prefill=True,enforce_eager=True \
--tasks arc_challenge_llama \
--apply_chat_template \
--batch_size autolm_eval \
--model vllm \
--model_args pretrained="RedHatAI/Llama-4-Maverick-17B-128E-Instruct-NVFP4",dtype=auto,add_bos_token=True,max_model_len=4096,tensor_parallel_size=2,enable_chunked_prefill=True,enforce_eager=True \
--tasks gsm8k_llama \
--apply_chat_template \
--fewshot_as_multiturn \
--batch_size autolm_eval \
--model vllm \
--model_args pretrained="RedHatAI/Llama-4-Maverick-17B-128E-Instruct-NVFP4",dtype=auto,add_bos_token=True,max_model_len=4096,tensor_parallel_size=2,enable_chunked_prefill=True,enforce_eager=True \
--tasks hellaswag \
--apply_chat_template \
--fewshot_as_multiturn \
--batch_size autolm_eval \
--model vllm \
--model_args pretrained="RedHatAI/Llama-4-Maverick-17B-128E-Instruct-NVFP4",dtype=auto,add_bos_token=True,max_model_len=4096,tensor_parallel_size=2,enable_chunked_prefill=True,enforce_eager=True \
--tasks winogrande \
--apply_chat_template \
--fewshot_as_multiturn \
--batch_size autolm_eval \
--model vllm \
--model_args pretrained="RedHatAI/Llama-4-Maverick-17B-128E-Instruct-NVFP4",dtype=auto,add_bos_token=True,max_model_len=4096,tensor_parallel_size=2,enable_chunked_prefill=True,enforce_eager=True \
--tasks truthfulqa \
--apply_chat_template \
--fewshot_as_multiturn \
--batch_size autolm_eval \
--model vllm \
--model_args pretrained="RedHatAI/Llama-4-Maverick-17B-128E-Instruct-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/Llama-4-Maverick-17B-128E-Instruct-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