Views
No views yet
1from vllm import LLM, SamplingParams
2from transformers import AutoTokenizer
3
4model_id = "RedHatAI/Meta-Llama-3.1-8B-Instruct-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.modifiers.smoothquant import SmoothQuantModifier
7from llmcompressor.utils import dispatch_for_generation
8
9MODEL_ID = "meta-llama/Meta-Llama-3-8B-Instruct"
10
11# Load model.
12model = AutoModelForCausalLM.from_pretrained(MODEL_ID, torch_dtype="auto")
13tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
14
15DATASET_ID = "HuggingFaceH4/ultrachat_200k"
16DATASET_SPLIT = "train_sft"
17
18# Select number of samples. 512 samples is a good place to start.
19# Increasing the number of samples can improve accuracy.
20NUM_CALIBRATION_SAMPLES = 512
21MAX_SEQUENCE_LENGTH = 2048
22
23# Load dataset and preprocess.
24ds = load_dataset(DATASET_ID, split=f"{DATASET_SPLIT}[:{NUM_CALIBRATION_SAMPLES}]")
25ds = ds.shuffle(seed=42)
26
27def preprocess(example):
28 return {
29 "text": tokenizer.apply_chat_template(
30 example["messages"],
31 tokenize=False,
32 )
33 }
34
35ds = ds.map(preprocess)
36
37# Tokenize inputs.
38def tokenize(sample):
39 return tokenizer(
40 sample["text"],
41 padding=False,
42 max_length=MAX_SEQUENCE_LENGTH,
43 truncation=True,
44 add_special_tokens=False,
45 )
46
47ds = ds.map(tokenize, remove_columns=ds.column_names)
48
49# Configure the quantization algorithm and scheme.
50# In this case, we:
51# * quantize the weights to fp4 with per group 16 via ptq
52# * calibrate a global_scale for activations, which will be used to
53# quantize activations to fp4 on the fly
54smoothing_strength = 0.5
55recipe = [
56 SmoothQuantModifier(smoothing_strength=smoothing_strength),
57 QuantizationModifier(
58 ignore=["re:.*lm_head.*"],
59 config_groups={
60 "group_0": {
61 "targets": ["Linear"],
62 "weights": {
63 "num_bits": 4,
64 "type": "float",
65 "strategy": "tensor_group",
66 "group_size": 16,
67 "symmetric": True,
68 "observer": "mse",
69 },
70 "input_activations": {
71 "num_bits": 4,
72 "type": "float",
73 "strategy": "tensor_group",
74 "group_size": 16,
75 "symmetric": True,
76 "dynamic": "local",
77 "observer": "minmax",
78 },
79 }
80 },
81 )
82]
83
84# Save to disk in compressed-tensors format.
85SAVE_DIR = MODEL_ID.rstrip("/").split("/")[-1] + "-NVFP4"
86
87# Apply quantization.
88oneshot(
89 model=model,
90 dataset=ds,
91 recipe=recipe,
92 max_seq_length=MAX_SEQUENCE_LENGTH,
93 num_calibration_samples=NUM_CALIBRATION_SAMPLES,
94 output_dir=SAVE_DIR,
95)
96
97print("\n\n")
98print("========== SAMPLE GENERATION ==============")
99dispatch_for_generation(model)
100input_ids = tokenizer("Hello my name is", return_tensors="pt").input_ids.to("cuda")
101output = model.generate(input_ids, max_new_tokens=100)
102print(tokenizer.decode(output[0]))
103print("==========================================\n\n")
104
105model.save_pretrained(SAVE_DIR, save_compressed=True)
106tokenizer.save_pretrained(SAVE_DIR)
107| Category | Metric | Meta-Llama-3.1-8B-Instruct | Llama-3.1-8B-Instruct-NVFP4 (this model) | Recovery |
|---|---|---|---|---|
| OpenLLM V1 | arc_challenge_llama | 83.35 | 82.32 | 98.76 |
| gsm8k_llama | 78.17 | 79.30 | 101.45 | |
| hellaswag | 78.43 | 78.01 | 99.46 | |
| mmlu_llama | 69.37 | 65.95 | 95.07 | |
| mmlu_cot_llama | 72.86 | 68.60 | 94.15 | |
| truthfulqa_mc2 | 55.09 | 52.95 | 96.12 | |
| winogrande | 75.77 | 74.03 | 97.70 | |
| Average | 73.29 | 71.59 | 97.68 |
| Category | Metric | Meta-Llama-3.1-8B-Instruct | RedHatAI/Llama-3.1-8B-Instruct-NVFP4 (this model) | Recovery (%) |
|---|---|---|---|---|
| OpenLLM V2 | MMLU-Pro (5-shot) | 37.69 | 34.43 | 91.35 |
| IFEval (0-shot) | 80.94 | 79.98 | 98.81 | |
| BBH (3-shot) | 50.76 | 48.62 | 95.78 | |
| Math-|v|-5 (4-shot) | 22.05 | 14.65 | 66.44 | |
| GPQA (0-shot) | 28.44 | 27.94 | 98.24 | |
| MuSR (0-shot) | 38.10 | 37.83 | 99.29 | |
| Average | 43.00 | 40.58 | 94.37 | |
| Coding | HumanEval_64 pass@2 | 71.90 | 71.44 | 99.36 |
lm_eval \
--model vllm \
--model_args pretrained="RedHatAI/Meta-Llama-3.1-8B-Instruct-NVFP4",dtype=auto,add_bos_token=True,max_model_len=4096,tensor_parallel_size=1,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/Meta-Llama-3.1-8B-Instruct-NVFP4",dtype=auto,add_bos_token=True,max_model_len=4096,tensor_parallel_size=1,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/Meta-Llama-3.1-8B-Instruct-NVFP4",dtype=auto,add_bos_token=True,max_model_len=4096,tensor_parallel_size=1,enable_chunked_prefill=True,enforce_eager=True \
--tasks arc_challenge_llama \
--apply_chat_template \
--batch_size autolm_eval \
--model vllm \
--model_args pretrained="RedHatAI/Meta-Llama-3.1-8B-Instruct-NVFP4",dtype=auto,add_bos_token=True,max_model_len=4096,tensor_parallel_size=1,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/Meta-Llama-3.1-8B-Instruct-NVFP4",dtype=auto,add_bos_token=True,max_model_len=4096,tensor_parallel_size=1,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/Meta-Llama-3.1-8B-Instruct-NVFP4",dtype=auto,add_bos_token=True,max_model_len=4096,tensor_parallel_size=1,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/Meta-Llama-3.1-8B-Instruct-NVFP4",dtype=auto,add_bos_token=True,max_model_len=4096,tensor_parallel_size=1,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/Meta-Llama-3.1-8B-Instruct-NVFP4",dtype=auto,max_model_len=4096,tensor_parallel_size=1,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/Meta-Llama-3.1-8B-Instruct-NVFP4",dtype=auto,max_model_len=4096,tensor_parallel_size=1,enable_chunked_prefill=True,enforce_eager=True\
--apply_chat_template \
--fewshot_as_multiturn \
--tasks humaneval_64_instruct \
--batch_size auto