Overview
torchao is a quantization library built for native PyTorch. It lets you shrink model weights to low-bit data types such as int4 and float8, which reduces memory use and speeds up both training and inference. It works out of the box with torch.compile() and FSDP2 across most HuggingFace PyTorch models.
It is aimed at ML engineers and researchers who train or serve large models and want to lower hardware costs without rewriting their model code. Applying quantization usually takes only a few extra lines, using the quantize_ function with a config object.
Within the inference and serving space, torchao covers the quantization and compression step. It is integrated as a quantization backend in tools like vLLM, SGLang, and HuggingFace Transformers, and also supports quantization-aware training (QAT) to recover accuracy lost during quantization.
What it does
- Quantize model weights to int4 with the quantize_ API and an Int4WeightOnlyConfig, for faster inference and lower memory use
- float8 training support for large models such as Llama-3.1-70B
- Quantization-aware training (QAT) to recover accuracy lost when quantizing
- Works with torch.compile() and FSDP2 across most HuggingFace PyTorch models
- Used as a quantization backend in vLLM, SGLang, and HuggingFace Transformers
- Supports CUDA, XPU, and CPU builds, plus low-bit ARM CPU kernels for linear and embedding ops
Getting started
Install torchao from pip, then quantize an existing PyTorch model's weights in place.
Install torchao
Install the latest stable release with pip.
pip install torchaoQuantize a model to int4
Use quantize_ with Int4WeightOnlyConfig to convert weights in place. The config differs slightly between CUDA and XPU.
import torch
from torchao.quantization import Int4WeightOnlyConfig, quantize_
if torch.cuda.is_available():
# quantize on CUDA
quantize_(model, Int4WeightOnlyConfig(group_size=32, int4_packing_format="tile_packed_to_4d", int4_choose_qparams_algorithm="hqq"))
elif torch.xpu.is_available():
# quantize on XPU
quantize_(model, Int4WeightOnlyConfig(group_size=32, int4_packing_format="plain_int32"))Read the quick start guide
See the official quick start guide and workflows docs for more configs, dtypes, and hardware-specific options.
Commands and code are distilled from the project's own documentation — always check the official repo for the latest.
When to use it
- Reduce the memory footprint and speed up inference of a large language model by quantizing its weights to int4
- Pre-train or fine-tune large models faster using float8 training
- Recover accuracy lost to quantization with quantization-aware training before serving
- Serve quantized models through vLLM, SGLang, or HuggingFace Transformers using torchao as the quantization backend
How torchao compares
torchao alongside other open-source quantization & compression tools AI/TLDR tracks, ranked by GitHub stars.
| Tool | Stars | What it does |
|---|---|---|
| BitNet | ★ 39.6k | Microsoft's inference framework for running extremely low-bit (1.58-bit) quantized LLMs efficiently on CPUs. |
| bitsandbytes | ★ 8.3k | A library that adds 8-bit and 4-bit quantization to PyTorch models, widely used for loading and fine-tuning LLMs in low memory. |
| AWQ | ★ 3.6k | The reference implementation of Activation-aware Weight Quantization for compressing LLMs to 4-bit with little accuracy loss. |
| LLM Compressor | ★ 3.5k | A library for applying quantization and sparsity methods to LLMs to produce compressed models that run faster in vLLM. |
| torchao | ★ 2.9k | PyTorch-native quantization and low-bit data types for faster training and inference |
| Intel Neural Compressor | ★ 2.7k | An Intel toolkit for compressing models through quantization, pruning, and distillation across multiple deep-learning frameworks. |
| GPTQModel | ★ 1.2k | A model quantization toolkit, successor to AutoGPTQ, that compresses LLMs with GPTQ, AWQ, and other methods across many chips. |
| Optimum Quanto | ★ 1k | A Hugging Face PyTorch toolkit for quantizing model weights and activations to formats like int8, int4, and float8. |