File size: 10,078 Bytes
1f96ea2
 
9e5aed4
 
c285ed8
 
 
9e5aed4
 
 
 
 
 
1f96ea2
 
8712fc6
1f96ea2
fd13ccd
09188dc
1f0b7e0
c303d63
09188dc
 
 
1f0b7e0
09188dc
c303d63
09188dc
 
 
 
 
 
 
 
 
 
c303d63
1f96ea2
9e5aed4
 
c303d63
1f96ea2
c303d63
1f96ea2
c303d63
 
 
 
 
 
b18ead3
1f96ea2
c303d63
9e5aed4
 
 
 
 
 
 
 
 
 
c303d63
1f96ea2
9e5aed4
c303d63
9e5aed4
 
 
 
1f96ea2
9e5aed4
 
 
 
 
 
1f96ea2
9e5aed4
 
1f96ea2
9e5aed4
 
c303d63
b18ead3
c303d63
 
 
b18ead3
c303d63
 
2c27924
c303d63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b18ead3
 
 
 
 
9e5aed4
1f96ea2
fd13ccd
ccf920f
 
 
 
a3302a3
ccf920f
a3302a3
 
ccf920f
301e064
ccf920f
a3302a3
301e064
 
ccf920f
 
 
 
 
 
 
 
 
5d2fbb8
ccf920f
 
 
 
 
 
 
 
 
 
1f96ea2
edf5c51
ebfd887
3b10b05
ebfd887
 
8213695
ebfd887
 
ccf920f
ebfd887
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f96ea2
fd13ccd
15cca67
3b10b05
 
15cca67
 
8712fc6
 
15cca67
 
 
 
 
 
 
 
 
 
 
 
 
 
6b1120f
15cca67
 
1f96ea2
b3a7a47
 
 
 
 
 
 
 
 
fd13ccd
9e5aed4
1f96ea2
9e5aed4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
---
library_name: transformers
tags:
- torchao
- code
- math
- chat
license: apache-2.0
language:
- multilingual
base_model:
- Qwen/Qwen3-32B
pipeline_tag: text-generation
---

[Qwen3-32B](https://huggingface.co/Qwen/Qwen3-32B) model quantized with [torchao](https://huggingface.co/docs/transformers/main/en/quantization/torchao) float8 dynamic activation and float8 weight quantization (per row granularity), by PyTorch team. Use it directly, or serve using [vLLM](https://docs.vllm.ai/en/latest/) with 47% VRAM reduction, around 1.5x speedup and little to no accuracy impact on H100.

# Inference with vLLM
```Shell
# Server
VLLM_DISABLE_COMPILE_CACHE=1 vllm serve pytorch/Qwen3-32B-float8dq --tokenizer Qwen/Qwen3-32B -O3
```

```Shell
# Client
curl http://localhost:8000/v1/chat/completions -H "Content-Type: application/json" -d '{
  "model": "pytorch/Qwen3-32B-float8dq",
  "messages": [
    {"role": "user", "content": "Give me a short introduction to large language models."}
  ],
  "temperature": 0.6,
  "top_p": 0.95,
  "top_k": 20,
  "max_tokens": 32768
}'
```

# Inference with transformers

```Py
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

model_name = "pytorch/Qwen3-32B-float8dq"

# load the tokenizer and the model
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
    model_name,
    torch_dtype="auto",
    device_map="auto"
)

# prepare the model input
prompt = "Give me a short introduction to large language model."
messages = [
    {"role": "user", "content": prompt}
]
text = tokenizer.apply_chat_template(
    messages,
    tokenize=False,
    add_generation_prompt=True,
    enable_thinking=True # Switches between thinking and non-thinking modes. Default is True.
)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)

# conduct text completion
generated_ids = model.generate(
    **model_inputs,
    max_new_tokens=32768
)
output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist() 

# parsing thinking content
try:
    # rindex finding 151668 (</think>)
    index = len(output_ids) - output_ids[::-1].index(151668)
except ValueError:
    index = 0

thinking_content = tokenizer.decode(output_ids[:index], skip_special_tokens=True).strip("\n")
content = tokenizer.decode(output_ids[index:], skip_special_tokens=True).strip("\n")

print("thinking content:", thinking_content)
print("content:", content)
```

# Quantization Recipe

Install the required packages:

```Shell
pip install git+https://github.com/huggingface/transformers@main
pip install torchao
pip install torch
pip install accelerate
```

Use the following code to get the float8 model using torchao library:

```Py
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, TorchAoConfig

model_id = "Qwen/Qwen3-32B"
from torchao.quantization import Float8DynamicActivationFloat8WeightConfig, PerRow
quant_config = Float8DynamicActivationFloat8WeightConfig(granularity=PerRow())
quantization_config = TorchAoConfig(quant_type=quant_config)
quantized_model = AutoModelForCausalLM.from_pretrained(
    model_id, 
    device_map="auto", 
    torch_dtype=torch.bfloat16, 
    quantization_config=quantization_config,
)
tokenizer = AutoTokenizer.from_pretrained(model_id)
```

Optionally, upload to your HF hub
```Py
USER_ID = "YOUR_USER_ID"
MODEL_NAME = model_id.split("/")[-1]
save_to = f"{USER_ID}/{MODEL_NAME}-float8dq"
quantized_model.push_to_hub(save_to, safe_serialization=False)
tokenizer.push_to_hub(save_to)
```

# Model Quality
We rely on [lm-evaluation-harness](https://github.com/EleutherAI/lm-evaluation-harness) to evaluate the quality of the quantized model.

| Benchmark                        |                |                           |
|----------------------------------|----------------|---------------------------|
|                                  | Qwen3-32B      | Qwen3-32B-float8dq        | 
| **General**                      |                |                           |
| mmlu                             | 80.71          | 80.67                     |
| bbh                              | 37.49          | 38.01                     |
| **Multilingual**                 |                |                           |
| mgsm_en_cot_es                   | 58.4           | 52.0                      |   
| **Math**                         |                |                           |
| gpqa_main_zeroshot               | 41.96          | 42.63                     |
| **Overall**                      | 54.64          | 53.33                     |


<details>
<summary> Reproduce Model Quality Results </summary>

Need to install lm-eval from source:
https://github.com/EleutherAI/lm-evaluation-harness#install

## baseline
```Shell
lm_eval --model hf --model_args pretrained=Qwen/Qwen3-32B --tasks mmlu --device cuda:0 --batch_size 8
```

## float8 dynamic quantization (float8dq)
```Shell
export MODEL=pytorch/Qwen3-32B-float8dq
# or
# export MODEL=Qwen/Qwen3-32B
lm_eval --model hf --model_args pretrained=$MODEL --tasks mmlu --device cuda:0 --batch_size 8
```
</details>

# Memory Usage

| Memory (tested on H100)          |                |                               |
|----------------------------------|----------------|-------------------------------|
|                                  | Qwen3-32B      | Qwen3-32B-float8dq            | 
| Peak Memory                      | 65.72 GB       | 34.54 GB (47.44% reduction)   |

<details>
<summary> Reproduce Peak Memory Usage Results </summary>

Code
```Py
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

model_name = "Qwen/Qwen3-32B" # pytorch/Qwen3-32B-float8dq

# load the tokenizer and the model
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
    model_name,
    torch_dtype="auto",
    device_map="auto"
)

torch.cuda.reset_peak_memory_stats()

# prepare the model input
prompt = "Give me a short introduction to large language model."
messages = [
    {"role": "user", "content": prompt}
]
text = tokenizer.apply_chat_template(
    messages,
    tokenize=False,
    add_generation_prompt=True,
    enable_thinking=True # Switches between thinking and non-thinking modes. Default is True.
)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)

# conduct text completion
generated_ids = model.generate(
    **model_inputs,
    max_new_tokens=32768
)
output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist() 

# parsing thinking content
try:
    # rindex finding 151668 (</think>)
    index = len(output_ids) - output_ids[::-1].index(151668)
except ValueError:
    index = 0

thinking_content = tokenizer.decode(output_ids[:index], skip_special_tokens=True).strip("\n")
content = tokenizer.decode(output_ids[index:], skip_special_tokens=True).strip("\n")

print("thinking content:", thinking_content)
print("content:", content)


mem = torch.cuda.max_memory_reserved() / 1e9
print(f"Peak Memory Usage: {mem:.02f} GB")
```
</details>


# Model Performance


| Benchmark  (Tested on H100)      |                |                               |
|----------------------------------|----------------|-------------------------------|
|                                  | Qwen3-32B      | Qwen3-32B-float8dq            | 
| latency (batch_size=1)           | 9.1s           | 5.77s (1.58x speedup)         |
| latency (batch_size=128)         | 12.45s         | 8.40s (1.48x speedup)         |

<details>
<summary> Reproduce latency benchmarks </summary>
  
**1. Setup**
```Shell
git clone [email protected]:vllm-project/vllm.git
cd vllm
VLLM_USE_PRECOMPILED=1 pip install --editable .
```

**2. Latency benchmarking**
```Shell
export MODEL=Qwen/Qwen3-32B # or pytorch/Qwen3-32B-float8dq
VLLM_DISABLE_COMPILE_CACHE=1 python benchmarks/benchmark_latency.py --input-len 256 --output-len 256 --model $MODEL --batch-size 1
```
</details>

# Paper: TorchAO: PyTorch-Native Training-to-Serving Model Optimization
The model's quantization is powered by **TorchAO**, a framework presented in the paper [TorchAO: PyTorch-Native Training-to-Serving Model Optimization](https://huggingface.co/papers/2507.16099).

**Abstract:** We present TorchAO, a PyTorch-native model optimization framework leveraging quantization and sparsity to provide an end-to-end, training-to-serving workflow for AI models. TorchAO supports a variety of popular model optimization techniques, including FP8 quantized training, quantization-aware training (QAT), post-training quantization (PTQ), and 2:4 sparsity, and leverages a novel tensor subclass abstraction to represent a variety of widely-used, backend agnostic low precision data types, including INT4, INT8, FP8, MXFP4, MXFP6, and MXFP8. TorchAO integrates closely with the broader ecosystem at each step of the model optimization pipeline, from pre-training (TorchTitan) to fine-tuning (TorchTune, Axolotl) to serving (HuggingFace, vLLM, SGLang, ExecuTorch), connecting an otherwise fragmented space in a single, unified workflow. TorchAO has enabled recent launches of the quantized Llama 3.2 1B/3B and LlamaGuard3-8B models and is open-source at this https URL .

# Resources
*   **Official TorchAO GitHub Repository:** [https://github.com/pytorch/ao](https://github.com/pytorch/ao)
*   **TorchAO Documentation:** [https://docs.pytorch.org/ao/stable/index.html](https://docs.pytorch.org/ao/stable/index.html)

# Disclaimer
PyTorch has not performed safety evaluations or red teamed the quantized models. Performance characteristics, outputs, and behaviors may differ from the original models. Users are solely responsible for selecting appropriate use cases, evaluating and mitigating for accuracy, safety, and fairness, ensuring security, and complying with all applicable laws and regulations.

Nothing contained in this Model Card should be interpreted as or deemed a restriction or modification to the licenses the models are released under, including any limitations of liability or disclaimers of warranties provided therein.