nm-research commited on
Commit
e3348f3
·
verified ·
1 Parent(s): 2d0756e

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +396 -3
README.md CHANGED
@@ -1,3 +1,396 @@
1
- ---
2
- license: apache-2.0
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ tags:
3
+ - fp4
4
+ - vllm
5
+ language:
6
+ - en
7
+ - de
8
+ - fr
9
+ - it
10
+ - pt
11
+ - hi
12
+ - es
13
+ - th
14
+ pipeline_tag: text-generation
15
+ license: apache-2.0
16
+ base_model: Qwen/Qwen3-8B
17
+ ---
18
+
19
+ # Qwen3-8B-NVFP4
20
+
21
+ ## Model Overview
22
+ - **Model Architecture:** Qwen/Qwen3-8B
23
+ - **Input:** Text
24
+ - **Output:** Text
25
+ - **Model Optimizations:**
26
+ - **Weight quantization:** FP4
27
+ - **Activation quantization:** FP4
28
+ - **Out-of-scope:** Use in any manner that violates applicable laws or regulations (including trade compliance laws). Use in languages other than English.
29
+ - **Release Date:** 6/25/2025
30
+ - **Version:** 1.0
31
+ - **Model Developers:** RedHatAI
32
+
33
+ This model is a quantized version of [Qwen/Qwen3-8B](https://huggingface.co/Qwen/Qwen3-8B).
34
+ It was evaluated on a several tasks to assess the its quality in comparison to the unquatized model.
35
+
36
+ ### Model Optimizations
37
+
38
+ This model was obtained by quantizing the weights and activations of [Qwen/Qwen3-8B](https://huggingface.co/Qwen/Qwen3-8B) to FP4 data type, ready for inference with vLLM>=0.9.1
39
+ This optimization reduces the number of bits per parameter from 16 to 4, reducing the disk size and GPU memory requirements by approximately 25%.
40
+
41
+ Only the weights and activations of the linear operators within transformers blocks are quantized using [LLM Compressor](https://github.com/vllm-project/llm-compressor).
42
+
43
+ ## Deployment
44
+
45
+ ### Use with vLLM
46
+
47
+ This model can be deployed efficiently using the [vLLM](https://docs.vllm.ai/en/latest/) backend, as shown in the example below.
48
+
49
+ ```python
50
+ from vllm import LLM, SamplingParams
51
+ from transformers import AutoTokenizer
52
+
53
+ model_id = "RedHatAI/Qwen3-8B-NVFP4"
54
+ number_gpus = 1
55
+
56
+ sampling_params = SamplingParams(temperature=0.6, top_p=0.9, max_tokens=256)
57
+
58
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
59
+
60
+ messages = [
61
+ {"role": "system", "content": "You are a pirate chatbot who always responds in pirate speak!"},
62
+ {"role": "user", "content": "Who are you?"},
63
+ ]
64
+
65
+ prompts = tokenizer.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
66
+
67
+ llm = LLM(model=model_id, tensor_parallel_size=number_gpus)
68
+
69
+ outputs = llm.generate(prompts, sampling_params)
70
+
71
+ generated_text = outputs[0].outputs[0].text
72
+ print(generated_text)
73
+ ```
74
+
75
+ vLLM aslo supports OpenAI-compatible serving. See the [documentation](https://docs.vllm.ai/en/latest/) for more details.
76
+
77
+ ## Creation
78
+
79
+ This model was created by applying [LLM Compressor with calibration samples from UltraChat](https://github.com/vllm-project/llm-compressor/blob/main/examples/quantization_w4a4_fp4/llama3_example.py), as presented in the code snipet below.
80
+
81
+ <details>
82
+
83
+ ```python
84
+ from datasets import load_dataset
85
+ from transformers import AutoModelForCausalLM, AutoTokenizer
86
+
87
+ from llmcompressor import oneshot
88
+ from llmcompressor.modifiers.quantization import QuantizationModifier
89
+ from llmcompressor.utils import dispatch_for_generation
90
+
91
+ MODEL_ID = "Qwen/Qwen3-8B"
92
+
93
+ # Load model.
94
+ model = AutoModelForCausalLM.from_pretrained(MODEL_ID, torch_dtype="auto")
95
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
96
+
97
+ DATASET_ID = "HuggingFaceH4/ultrachat_200k"
98
+ DATASET_SPLIT = "train_sft"
99
+
100
+ # Select number of samples. 512 samples is a good place to start.
101
+ # Increasing the number of samples can improve accuracy.
102
+ NUM_CALIBRATION_SAMPLES = 512
103
+ MAX_SEQUENCE_LENGTH = 2048
104
+
105
+ # Load dataset and preprocess.
106
+ ds = load_dataset(DATASET_ID, split=f"{DATASET_SPLIT}[:{NUM_CALIBRATION_SAMPLES}]")
107
+ ds = ds.shuffle(seed=42)
108
+
109
+ def preprocess(example):
110
+ return {
111
+ "text": tokenizer.apply_chat_template(
112
+ example["messages"],
113
+ tokenize=False,
114
+ )
115
+ }
116
+
117
+ ds = ds.map(preprocess)
118
+
119
+ # Tokenize inputs.
120
+ def tokenize(sample):
121
+ return tokenizer(
122
+ sample["text"],
123
+ padding=False,
124
+ max_length=MAX_SEQUENCE_LENGTH,
125
+ truncation=True,
126
+ add_special_tokens=False,
127
+ )
128
+
129
+ ds = ds.map(tokenize, remove_columns=ds.column_names)
130
+
131
+ # Configure the quantization algorithm and scheme.
132
+ # In this case, we:
133
+ # * quantize the weights to fp4 with per group 16 via ptq
134
+ # * calibrate a global_scale for activations, which will be used to
135
+ # quantize activations to fp4 on the fly
136
+ smoothing_strength = 0.8
137
+ recipe = [
138
+ SmoothQuantModifier(smoothing_strength=smoothing_strength),
139
+ QuantizationModifier(
140
+ ignore=["re:.*lm_head.*"],
141
+ config_groups={
142
+ "group_0": {
143
+ "targets": ["Linear"],
144
+ "weights": {
145
+ "num_bits": 4,
146
+ "type": "float",
147
+ "strategy": "tensor_group",
148
+ "group_size": 16,
149
+ "symmetric": True,
150
+ "observer": "mse",
151
+ },
152
+ "input_activations": {
153
+ "num_bits": 4,
154
+ "type": "float",
155
+ "strategy": "tensor_group",
156
+ "group_size": 16,
157
+ "symmetric": True,
158
+ "dynamic": "local",
159
+ "observer": "mse",
160
+ },
161
+ }
162
+ },
163
+ )
164
+ ]
165
+
166
+ # Save to disk in compressed-tensors format.
167
+ SAVE_DIR = MODEL_ID.rstrip("/").split("/")[-1] + "-NVFP4"
168
+
169
+ # Apply quantization.
170
+ oneshot(
171
+ model=model,
172
+ dataset=ds,
173
+ recipe=recipe,
174
+ max_seq_length=MAX_SEQUENCE_LENGTH,
175
+ num_calibration_samples=NUM_CALIBRATION_SAMPLES,
176
+ output_dir=SAVE_DIR,
177
+ )
178
+
179
+ print("\n\n")
180
+ print("========== SAMPLE GENERATION ==============")
181
+ dispatch_for_generation(model)
182
+ input_ids = tokenizer("Hello my name is", return_tensors="pt").input_ids.to("cuda")
183
+ output = model.generate(input_ids, max_new_tokens=100)
184
+ print(tokenizer.decode(output[0]))
185
+ print("==========================================\n\n")
186
+
187
+ model.save_pretrained(SAVE_DIR, save_compressed=True)
188
+ tokenizer.save_pretrained(SAVE_DIR)
189
+ ```
190
+ </details>
191
+
192
+ ## Evaluation
193
+
194
+ This model was evaluated on the well-known OpenLLM v1, OpenLLM v2 and HumanEval_64 benchmarks using [lm-evaluation-harness](https://github.com/neuralmagic/lm-evaluation-harness). The Reasoning evals were done using [ligheval](https://github.com/neuralmagic/lighteval).
195
+
196
+ ### Accuracy
197
+ <table>
198
+ <thead>
199
+ <tr>
200
+ <th>Category</th>
201
+ <th>Metric</th>
202
+ <th>Qwen/Qwen3-8B</th>
203
+ <th>Qwen3-8B-NVFP4 (this model)</th>
204
+ <th>Recovery</th>
205
+ </tr>
206
+ </thead>
207
+ <tbody>
208
+ <tr>
209
+ <td rowspan="7"><b>OpenLLM V1</b></td>
210
+ <td>arc_challenge</td>
211
+ <td>64.76</td>
212
+ <td>63.91</td>
213
+ <td>98.69</td>
214
+ </tr>
215
+ <tr>
216
+ <td>gsm8k</td>
217
+ <td>87.26</td>
218
+ <td>86.73</td>
219
+ <td>99.39</td>
220
+ </tr>
221
+ <tr>
222
+ <td>hellaswag</td>
223
+ <td>76.68</td>
224
+ <td>75.34</td>
225
+ <td>98.25</td>
226
+ </tr>
227
+ <tr>
228
+ <td>mmlu</td>
229
+ <td>74.97</td>
230
+ <td>73.07</td>
231
+ <td>97.47</td>
232
+ </tr>
233
+ <tr>
234
+ <td>truthfulqa_mc2</td>
235
+ <td>54.42</td>
236
+ <td>55.07</td>
237
+ <td>101.19</td>
238
+ </tr>
239
+ <tr>
240
+ <td>winogrande</td>
241
+ <td>71.43</td>
242
+ <td>68.43</td>
243
+ <td>95.80</td>
244
+ </tr>
245
+ <tr>
246
+ <td><b>Average</b></td>
247
+ <td><b>71.59</b></td>
248
+ <td><b>70.43</b></td>
249
+ <td><b>98.38</b></td>
250
+ </tr>
251
+ <tr>
252
+ <td rowspan="7"><b>OpenLLM V2</b></td>
253
+ <td>BBH (3-shot)</td>
254
+ <td>47.46</td>
255
+ <td>49.33</td>
256
+ <td>103.94</td>
257
+ </tr>
258
+ <tr>
259
+ <td>MMLU-Pro (5-shot)</td>
260
+ <td>34.64</td>
261
+ <td>27.49</td>
262
+ <td>79.36</td>
263
+ </tr>
264
+ <tr>
265
+ <td>MuSR (0-shot)</td>
266
+ <td>40.61</td>
267
+ <td>42.86</td>
268
+ <td>105.54</td>
269
+ </tr>
270
+ <tr>
271
+ <td>IFEval (0-shot)</td>
272
+ <td>87.89</td>
273
+ <td>87.65</td>
274
+ <td>99.73</td>
275
+ </tr>
276
+ <tr>
277
+ <td>GPQA (0-shot)</td>
278
+ <td>25.17</td>
279
+ <td>26.34</td>
280
+ <td>104.65</td>
281
+ </tr>
282
+ <tr>
283
+ <td>Math-|v|-5 (4-shot)</td>
284
+ <td>53.55</td>
285
+ <td>50.83</td>
286
+ <td>94.92</td>
287
+ </tr>
288
+ <tr>
289
+ <td><b>Average</b></td>
290
+ <td><b>48.22</b></td>
291
+ <td><b>47.42</b></td>
292
+ <td><b>98.33</b></td>
293
+ </tr>
294
+ <tr>
295
+ <td rowspan="1"><b>Coding</b></td>
296
+ <td>HumanEval_64 pass@2</td>
297
+ <td>86.51</td>
298
+ <td>85.32</td>
299
+ <td>98.62</td>
300
+ </tr>
301
+ <tr>
302
+ <td rowspan="4"><b>Reasoning</b></td>
303
+ <td>AIME24 (0-shot)</td>
304
+ <td>75.86</td>
305
+ <td>62.07</td>
306
+ <td>81.82</td>
307
+ </tr>
308
+ <tr>
309
+ <td>AIME25 (0-shot)</td>
310
+ <td>65.52</td>
311
+ <td>62.07</td>
312
+ <td>94.74</td>
313
+ </tr>
314
+ <tr>
315
+ <td>GPQA (Diamond, 0-shot)</td>
316
+ <td>59.90</td>
317
+ <td>54.82</td>
318
+ <td>91.51</td>
319
+ </tr>
320
+ <tr>
321
+ <td><b>Average</b></td>
322
+ <td><b>67.09</b></td>
323
+ <td><b>59.65</b></td>
324
+ <td><b>89.36</b></td>
325
+ </tr>
326
+ </tbody>
327
+ </table>
328
+
329
+
330
+
331
+
332
+ ### Reproduction
333
+
334
+ The results were obtained using the following commands:
335
+
336
+ <details>
337
+
338
+ ```
339
+ lm_eval \
340
+ --model vllm \
341
+ --model_args pretrained="RedHatAI/Qwen3-8B-NVFP4",dtype=auto,max_model_len=4096,tensor_parallel_size=2,enable_chunked_prefill=True,enforce_eager=True\
342
+ --apply_chat_template \
343
+ --fewshot_as_multiturn \
344
+ --tasks openllm \
345
+ --batch_size auto
346
+ ```
347
+
348
+
349
+ #### OpenLLM v2
350
+ ```
351
+ lm_eval \
352
+ --model vllm \
353
+ --model_args pretrained="RedHatAI/Qwen3-8B-NVFP4",dtype=auto,max_model_len=4096,tensor_parallel_size=2,enable_chunked_prefill=True,enforce_eager=True\
354
+ --apply_chat_template \
355
+ --fewshot_as_multiturn \
356
+ --tasks leaderboard \
357
+ --batch_size auto
358
+ ```
359
+
360
+ #### HumanEval_64
361
+ ```
362
+ lm_eval \
363
+ --model vllm \
364
+ --model_args pretrained="RedHatAI/Qwen3-8B-NVFP4",dtype=auto,max_model_len=4096,tensor_parallel_size=2,enable_chunked_prefill=True,enforce_eager=True\
365
+ --apply_chat_template \
366
+ --fewshot_as_multiturn \
367
+ --tasks humaneval_64_instruct \
368
+ --batch_size auto
369
+ ```
370
+
371
+ #### LightEval
372
+ ```
373
+ # --- model_args.yaml ---
374
+ cat > model_args.yaml <<'YAML'
375
+ model_parameters:
376
+ model_name: "RedHatAI/Qwen3-8B-NVFP4"
377
+ dtype: auto
378
+ gpu_memory_utilization: 0.9
379
+ tensor_parallel_size: 2
380
+ max_model_length: 40960
381
+ generation_parameters:
382
+ seed: 42
383
+ temperature: 0.6
384
+ top_k: 20
385
+ top_p: 0.95
386
+ min_p: 0.0
387
+ max_new_tokens: 32768
388
+ YAML
389
+
390
+ lighteval vllm model_args.yaml \
391
+ "lighteval|aime24|0,lighteval|aime25|0,lighteval|gpqa:diamond|0" \
392
+ --max-samples -1 \
393
+ --output-dir out_dir
394
+
395
+ ```
396
+ </details>