File size: 14,920 Bytes
18601cf 36a1463 18601cf 6dd2748 a150c6d 1d86c6f ae7fb3c 36a1463 2499a43 72fc30b 2499a43 36a1463 1d86c6f 36a1463 1d86c6f 36a1463 2499a43 36a1463 1d86c6f 36a1463 e88cded 36a1463 72fc30b 320ca52 36a1463 0b59b6f 36a1463 574af31 1d86c6f 574af31 1d86c6f 0609aff 1d86c6f 36a1463 1d86c6f 36a1463 1d86c6f 36a1463 1d86c6f 36a1463 6dd2748 1d86c6f 36a1463 6dd2748 36a1463 1d86c6f 6dd2748 36a1463 6dd2748 abbab01 36a1463 18601cf 36a1463 18601cf 1d86c6f 99caebc 36a1463 18601cf 36a1463 18601cf 36a1463 18601cf 36a1463 18601cf 36a1463 18601cf 36a1463 18601cf e23ee47 abbab01 04850a1 18601cf 36a1463 18601cf 36a1463 18601cf 36a1463 18601cf 36a1463 18601cf 36a1463 18601cf 36a1463 18601cf 36a1463 88d970d 36a1463 18601cf 36a1463 1d86c6f 88d970d 36a1463 18601cf 36a1463 18601cf 36a1463 18601cf 36a1463 18601cf 36a1463 18601cf 36a1463 18601cf 36a1463 18601cf 36a1463 18601cf 36a1463 18601cf 36a1463 1d86c6f 36a1463 18601cf 36a1463 1d86c6f 36a1463 18601cf 36a1463 18601cf 36a1463 18601cf 36a1463 18601cf 36a1463 18601cf 36a1463 |
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 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 |
---
base_model: Qwen/Qwen3-8B
tags:
- transformers
- torchao
- qwen3
license: apache-2.0
language:
- en
---
This repository hosts the **Qwen3-8B** model quantized with [torchao](https://huggingface.co/docs/transformers/main/en/quantization/torchao)
using int4 weight-only quantization and the [awq](https://arxiv.org/abs/2306.00978) algorithm.
This work is brought to you by the PyTorch team. This model can be used directly or served using [vLLM](https://docs.vllm.ai/en/latest/) for 53% VRAM reduction (7.82 GB needed)
and 1.34x speedup on H100 GPUs for batch size 1. The model is calibrated with 10 samples from `mmlu_abstract_algebra` task to recover the accuracy for `mmlu_abstract_algebra` specifically.
AWQ-INT4 improves the accuracy of `mmlu_abstract_algebra` of INT4 from 55 to 56, while the bfloat16 baseline is 58.
# Inference with vLLM
Install vllm nightly and torchao nightly to get some recent changes:
```
# please make sure uv is installed
pip install uv
# please use python 3.12
uv pip install --pre torchao torch vllm fbgemm_gpu_genai --index-url https://download.pytorch.org/whl/nightly/cu128
```
## Serving
Then we can serve with the following command:
```Shell
# Server
export MODEL=pytorch/Qwen3-8B-AWQ-INT4
VLLM_DISABLE_COMPILE_CACHE=1 vllm serve $MODEL --tokenizer $MODEL -O3
```
```Shell
# Client
curl http://localhost:8000/v1/chat/completions -H "Content-Type: application/json" -d '{
"model": "pytorch/Qwen3-8B-AWQ-INT4",
"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
}'
```
Note: please use `VLLM_DISABLE_COMPILE_CACHE=1` to disable compile cache when running this code, e.g. `VLLM_DISABLE_COMPILE_CACHE=1 python example.py`, since there are some issues with the composability of compile in vLLM and torchao,
this is expected be resolved in pytorch 2.8.
# Inference with Transformers
Install the required packages:
```Shell
pip install uv
uv pip install git+https://github.com/huggingface/transformers@main
uv pip install accelerate
uv pip install --pre torchao torch fbgemm_gpu_genai --index-url https://download.pytorch.org/whl/nightly/cu128
```
Example:
```Py
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "pytorch/Qwen3-8B-AWQ-INT4"
# 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("")
content = tokenizer.decode(output_ids[index:], skip_special_tokens=True).strip("")
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 accelerate
# please use python 3.12
pip install --pre torchao torch vllm --index-url https://download.pytorch.org/whl/nightly/cu128
```
Use the following code to get the quantized model:
```Py
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, TorchAoConfig
model_id = "Qwen/Qwen3-8B"
model_to_quantize = "Qwen/Qwen3-8B"
from torchao.quantization import Int4WeightOnlyConfig, quantize_, ModuleFqnToConfig
from torchao.prototype.awq import (
AWQConfig,
)
from torchao._models._eval import TransformerEvalWrapper
model = AutoModelForCausalLM.from_pretrained(
model_to_quantize,
device_map="auto",
torch_dtype=torch.bfloat16,
)
tokenizer = AutoTokenizer.from_pretrained(model_id)
# Note: this is only compatible with H100
base_config = Int4WeightOnlyConfig(group_size=128)
# for A100, please use the following for base_config:
# base_config = Int4WeightOnlyConfig(group_size=128, int4_packing_format="tile_packed_to_4d", int4_choose_qparams_algorithm="hqq")
linear_config = AWQConfig(base_config, step="prepare")
# skip quantizing lm_head since it has different definition in vllm and transformers
quant_config = ModuleFqnToConfig({"_default": linear_config, "lm_head": None})
# your selected tasks, see https://github.com/EleutherAI/lm-evaluation-harness/blob/main/docs/new_task_guide.md for adding tasks to lm-eval
tasks = ["mmlu_abstract_algebra"]
calibration_limit = 10
max_seq_length = 2048
quantize_(
model,
quant_config,
)
TransformerEvalWrapper(
model=model,
tokenizer=tokenizer,
max_seq_length=max_seq_length,
).run_eval(
tasks=tasks,
limit=calibration_limit,
)
linear_config = AWQConfig(base_config, step="convert")
quant_config = ModuleFqnToConfig({"_default": linear_config, "lm_head": None})
quantize_(model, quant_config)
quantized_model = model
linear_config = AWQConfig(base_config, step="prepare_for_loading")
quant_config = ModuleFqnToConfig({"_default": linear_config, "lm_head": None})
quantized_model.config.quantization_config = TorchAoConfig(quant_config)
# Push to hub
USER_ID = "YOUR_USER_ID"
MODEL_NAME = model_id.split("/")[-1]
save_to = f"{USER_ID}/{MODEL_NAME}-AWQ-INT4"
quantized_model.push_to_hub(save_to, safe_serialization=False)
tokenizer.push_to_hub(save_to)
# Manual Testing
prompt = "Hey, are you conscious? Can you talk to me?"
messages = [
{
"role": "system",
"content": "",
},
{"role": "user", "content": prompt},
]
templated_prompt = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
)
print("Prompt:", prompt)
print("Templated prompt:", templated_prompt)
inputs = tokenizer(
templated_prompt,
return_tensors="pt",
).to("cuda")
generated_ids = quantized_model.generate(**inputs, max_new_tokens=128)
output_text = tokenizer.batch_decode(
generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False
)
print("Response:", output_text[0][len(prompt):])
```
Note: to `push_to_hub` you need to run
```Shell
pip install -U "huggingface_hub[cli]"
huggingface-cli login
```
and use a token with write access, from https://huggingface.co/settings/tokens
# Model Quality
We rely on [lm-evaluation-harness](https://github.com/EleutherAI/lm-evaluation-harness) to evaluate the quality of the quantized model.
| Benchmark | | | |
|----------------------------------|----------------|------------------------|---------------------------|
| | Qwen/Qwen3-8B | jerryzh168/Qwen3-8B-INT4-skip_lm_head | pytorch/Qwen3-8B-AWQ-INT4 |
| mmlu_abstract_algebra | 58 | 55 | 56 |
Note that we only calibrate on a single `mmlu_abstract_algebra` task instead of the entire `mmlu` task since `mmlu` contains many different types of tasks and calibrating
on all of them does not necessarily improve the accuracy for all the tasks, since it's harder to faithfully represent the distribution of data from all types of tasks with a selected
small calibration sample data.
Note: we skipped quantization for `lm_head` because in transformers lm_head is a `Linear` but in vllm lm_head becomes [ParallelLMHead](https://github.com/vllm-project/vllm/blob/3e903b6cb4292ca1425a37cb809c1e3cddfdadcb/vllm/model_executor/models/qwen3.py#L294)
and the linear weight no longer works there.
<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-8B --tasks bbh --device cuda:0 --batch_size 8
```
## AWQ-INT4
```Shell
export MODEL=pytorch/Qwen3-8B-AWQ-INT4
lm_eval --model hf --model_args pretrained=$MODEL --tasks bbh --device cuda:0 --batch_size 8
```
</details>
# Peak Memory Usage
## Results
| Benchmark | | | |
|------------------|----------------|--------------------------------|--------------------------------|
| | Qwen/Qwen3-8B | jerryzh168/Qwen3-8B-INT4-skip_lm_head | pytorch/Qwen3-8B-AWQ-INT4 |
| Peak Memory (GB) | 16.47 | 7.82 (53% reduction) | 7.82 (53% reduction) |
<details>
<summary> Reproduce Peak Memory Usage Results </summary>
We can use the following code to get a sense of peak memory usage during inference:
```Py
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, TorchAoConfig
# use "Qwen/Qwen3-8B" or "pytorch/Qwen3-8B-AWQ-INT4"
model_id = "pytorch/Qwen3-8B-AWQ-INT4"
quantized_model = AutoModelForCausalLM.from_pretrained(model_id, device_map="cuda:0", torch_dtype=torch.bfloat16)
tokenizer = AutoTokenizer.from_pretrained(model_id)
torch.cuda.reset_peak_memory_stats()
prompt = "Hey, are you conscious? Can you talk to me?"
messages = [
{
"role": "system",
"content": "",
},
{"role": "user", "content": prompt},
]
templated_prompt = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
)
print("Prompt:", prompt)
print("Templated prompt:", templated_prompt)
inputs = tokenizer(
templated_prompt,
return_tensors="pt",
).to("cuda")
generated_ids = quantized_model.generate(**inputs, max_new_tokens=128)
output_text = tokenizer.batch_decode(
generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False
)
print("Response:", output_text[0][len(prompt):])
mem = torch.cuda.max_memory_reserved() / 1e9
print(f"Peak Memory Usage: {mem:.02f} GB")
```
</details>
# Model Performance
## Results (H100 machine)
| Benchmark (Latency) | | | |
|----------------------------------|----------------|---------------------------|---------------------------|
| | Qwen/Qwen3-8B | jerryzh168/Qwen3-8B-INT4-skip_lm_head | pytorch/Qwen3-8B-AWQ-INT4 |
| latency (batch_size=1) | 2.46s | 1.40s (1.76x speedup) | 1.83s (1.34x speedup) |
<details>
<summary> Reproduce Model Performance Results </summary>
## Setup
Get vllm source code:
```Shell
git clone [email protected]:vllm-project/vllm.git
```
Install vllm
```
VLLM_USE_PRECOMPILED=1 pip install --editable .
```
Run the benchmarks under `vllm` root folder:
## benchmark_latency
### baseline
```Shell
export MODEL=Qwen/Qwen3-8B
vllm bench latency --input-len 256 --output-len 256 --model $MODEL --batch-size 1
```
### AWQ-INT4
```Shell
export MODEL=pytorch/Qwen3-8B-AWQ-INT4
VLLM_DISABLE_COMPILE_CACHE=1 vllm bench latency --input-len 256 --output-len 256 --model $MODEL --batch-size 1
```
## benchmark_serving
We benchmarked the throughput in a serving environment.
Download sharegpt dataset:
```Shell
wget https://huggingface.co/datasets/anon8231489123/ShareGPT_Vicuna_unfiltered/resolve/main/ShareGPT_V3_unfiltered_cleaned_split.json
```
Other datasets can be found in: https://github.com/vllm-project/vllm/tree/main/benchmarks
Note: you can change the number of prompts to be benchmarked with `--num-prompts` argument for `benchmark_serving` script.
### baseline
Server:
```Shell
export MODEL=Qwen/Qwen3-8B
vllm serve $MODEL --tokenizer $MODEL -O3
```
Client:
```Shell
export MODEL=Qwen/Qwen3-8B
python benchmarks/benchmark_serving.py --backend vllm --dataset-name sharegpt --tokenizer $MODEL --dataset-path ./ShareGPT_V3_unfiltered_cleaned_split.json --model $MODEL --num-prompts 1
```
### AWQ-INT4
Server:
```Shell
export MODEL=pytorch/Qwen3-8B-AWQ-INT4
VLLM_DISABLE_COMPILE_CACHE=1 vllm serve $MODEL --tokenizer $MODEL -O3 --pt-load-map-location cuda:0
```
Client:
```Shell
export MODEL=pytorch/Qwen3-8B-AWQ-INT4
python benchmarks/benchmark_serving.py --backend vllm --dataset-name sharegpt --tokenizer $MODEL --dataset-path ./ShareGPT_V3_unfiltered_cleaned_split.json --model $MODEL --num-prompts 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.
|