Introduction | Inference | Training | Installation |Composability | Prototype Features | Integrations | Videos | For Developers | License | Citation
torchao
accelerates PyTorch models with minimal code changes through advanced quantization and sparsification techniques. Optimize weights, gradients, activations, and more for both inference and training.
From the team that brought you the fast series
- 9.5x inference speedups for Image segmentation models with sam-fast
- 10x inference speedups for Language models with gpt-fast
- 3x inference speedup for Diffusion models with sd-fast
torchao
isn't just for inference - it delivers substantial speedups at scale, from up to 1.5x speedups on 512 GPU clusters, to 1.34-1.43x speedups on 2K H200 clusters with the latest torchao.float8
rowwise
torchao
works out-of-the-box with torch.compile()
and FSDP2
across most Hugging Face PyTorch models
torchao
delivers substantial performance gains with minimal code changes:
- INT4 Weight-Only Quantization: 2x throughput (180 vs 107 tokens/sec) with 60% less memory (6.88 GB vs 16.43 GB) on LLaMA-3-7B
- Float8 Dynamic Quantization: 53.88% speedup on Flux.1-Dev* and 27.33% speedup on CogVideoX-5b on H100 GPU with preserved quality
- INT4 + 2:4 Sparsity: 2.4x throughput (226 vs 95 tokens/sec) with 80% memory reduction (5.3GB vs 16.4GB) on LLaMA-3-8B
View detailed benchmarks | Learn about sparsity
Quantize any model with nn.Linear
layers (including HuggingFace models) in just one line:
from torchao.quantization.quant_api import quantize_, Int4WeightOnlyConfig
quantize_(model, Int4WeightOnlyConfig(group_size=128, use_hqq=True))
from transformers import TorchAoConfig, AutoModelForCausalLM
from torchao.quantization.quant_api import Int4WeightOnlyConfig
# Create quantization configuration
quantization_config = TorchAoConfig(quant_type=Int4WeightOnlyConfig(group_size=128, use_hqq=True))
# Load and automatically quantize
quantized_model = AutoModelForCausalLM.from_pretrained(
"microsoft/Phi-4-mini-instruct",
torch_dtype="auto",
device_map="auto",
quantization_config=quantization_config
)
Deploy quantized models with one command:
vllm serve pytorch/Phi-4-mini-instruct-int4wo-hqq --tokenizer microsoft/Phi-4-mini-instruct -O3
Benefits: 67% VRAM reduction and 12-20% speedup on A100 GPUs while maintaining quality.
Step-by-step quantization guide | Pre-quantized models
Post-training quantization can result in a fast and compact model, but may also lead to accuracy degradation. We recommend exploring Quantization Aware Training (QAT) to overcome this limitation. In collaboration with Torchtune, we've developed a QAT recipe that demonstrates significant accuracy improvements over traditional PTQ, recovering 96% of the accuracy degradation on hellaswag and 68% of the perplexity degradation on wikitext for Llama3 compared to post-training quantization (PTQ). And we've provided a full recipe here. For more details, please see the QAT README.
from torchao.quantization import (
quantize_,
Int8DynamicActivationInt4WeightConfig,
)
from torchao.quantization.qat import (
FakeQuantizeConfig,
FromIntXQuantizationAwareTrainingConfig,
IntXQuantizationAwareTrainingConfig,
)
# Insert fake quantization
activation_config = FakeQuantizeConfig(torch.int8, "per_token", is_symmetric=False)
weight_config = FakeQuantizeConfig(torch.int4, group_size=32)
quantize_(
my_model,
IntXQuantizationAwareTrainingConfig(activation_config, weight_config),
)
# Run training... (not shown)
# Convert fake quantization to actual quantized operations
quantize_(my_model, FromIntXQuantizationAwareTrainingConfig())
quantize_(my_model, Int8DynamicActivationInt4WeightConfig(group_size=32))
torchao.float8 implements training recipes with the scaled float8 dtypes, as laid out in https://arxiv.org/abs/2209.05433
With torch.compile
on, current results show throughput speedups of up to 1.5x on up to 512 GPU / 405B parameter count scale (details)
from torchao.float8 import convert_to_float8_training
convert_to_float8_training(m, module_filter_fn=...)
And for an end-to-minimal training recipe of pretraining with float8, you can check out torchtitan.
- Supercharging Training using float8 and FSDP2
- Efficient Pre-training of Llama 3-like model architectures using torchtitan on Amazon SageMaker
- Float8 in PyTorch
We've added support for semi-structured 2:4 sparsity with 6% end-to-end speedups on ViT-L. Full blog here
The code change is a 1 liner with the full example available here
from torchao.sparsity.training import SemiSparseLinear, swap_linear_with_semi_sparse_linear
swap_linear_with_semi_sparse_linear(model, {"seq.0": SemiSparseLinear})
Optimizers like ADAM can consume substantial GPU memory - 2x as much as the model parameters themselves. TorchAO provides two approaches to reduce this overhead:
- Quantized optimizers: Reduce optimizer state memory by 2-4x by quantizing to lower precision
from torchao.optim import AdamW8bit, AdamW4bit, AdamWFp8
optim = AdamW8bit(model.parameters()) # replace with Adam4bit and AdamFp8 for the 4 / fp8 versions
Our quantized optimizers are implemented in just a few hundred lines of PyTorch code and compiled for efficiency. While slightly slower than specialized kernels, they offer an excellent balance of memory savings and performance. See detailed benchmarks here.
- CPU offloading: Move optimizer state and gradients to CPU memory
For maximum memory savings, we support single GPU CPU offloading that efficiently moves both gradients and optimizer state to CPU memory. This approach can reduce your VRAM requirements by 60% with minimal impact on training speed:
optim = CPUOffloadOptimizer(model.parameters(), torch.optim.AdamW, fused=True)
optim.load_state_dict(ckpt["optim"])
torchao
makes liberal use of several new features in PyTorch, it's recommended to use it with the current nightly or latest stable version of PyTorch, see getting started for more details.
Install the stable release (recommended):
pip install torchao
Other options:
# Nightly build
pip install --pre torchao --index-url https://download.pytorch.org/whl/nightly/cu124
# Different CUDA versions
pip install torchao --index-url https://download.pytorch.org/whl/cu118 # CUDA 11.8
pip install torchao --index-url https://download.pytorch.org/whl/cpu # CPU only
USE_CPP=0 python setup.py develop # Skip C++/CUDA extensions
torch.compile
: A key design principle for us is composability - any custom dtype or memory layout should work with our compiler. We enable kernel implementations in PyTorch, CUDA, C++, or Triton. This allows researchers and engineers to start with high-level dtype and layout logic in pure PyTorch, then progressively optimize performance by implementing lower-level kernels as needed, while maintaining compatibility with the compile infrastructure.
FSDP2: Historically most quantization has been done for inference, there is now a thriving area of research combining distributed algorithms and quantization.
The best example we have combining the composability of lower bit dtype with compile and fsdp is NF4 which we used to implement the QLoRA algorithm. So if you're doing research at the intersection of this area we'd love to hear from you.
Our framework makes it straightforward to add tensor parallel support to your custom quantized tensor subclass. Check out our tensor parallel tutorial to see how a quantized tensor subclass can be extended to support column and row-wise tensor sharding while maintaining compatibility with torch.compile
.
The prototype directory contains experimental and upcoming features including:
- MX Training & Inference: MX formats with a native PyTorch POC
- Int8 Quantized Training: Low-precision training methods
- And more experimental features in development
These features are under active development and may change. We welcome contributions from researchers and developers!
⚠️ Note: Features in the prototype directory do not have BC guarantees and are subject to change.
We're also fortunate to be integrated into some of the leading open-source libraries including
- Hugging Face transformers with a builtin inference backend and low bit optimizers
- Hugging Face diffusers best practices with torch.compile and torchao in a standalone repo diffusers-torchao
- Mobius HQQ backend leveraged our int4 kernels to get 195 tok/s on a 4090
- TorchTune for our QLoRA and QAT recipes
- VLLM for LLM serving: usage
- SGLang for LLM serving: usage and the major PR.
- Keynote talk at GPU MODE IRL
- Low precision dtypes at PyTorch conference
- Slaying OOMs at the Mastering LLM's course
- Advanced Quantization at CUDA MODE
- Chip Huyen's GPU Optimization Workshop
- Cohere for AI community talk
We've added support for authoring and releasing custom ops that do not graph break with torch.compile()
. We have a few examples you can follow
- fp6 for 2x faster inference over fp16 with an easy to use API
quantize_(model, fpx_weight_only(3, 2))
- 2:4 Sparse Marlin GEMM 2x speedups for FP16xINT4 kernels even at batch sizes up to 256
- int4 tinygemm unpacker which makes it easier to switch quantized backends for inference
If you believe there's other CUDA kernels we should be taking a closer look at please leave a comment on this issue or feel free to contribute directly to the repo.
torchao
is released under the BSD 3 license.
If you find the torchao library useful, please cite it in your work as below.
@software{torchao,
title = {torchao: PyTorch native quantization and sparsity for training and inference},
author = {torchao maintainers and contributors},
url = {https://github.com/pytorch/torchao},
license = {BSD-3-Clause},
month = oct,
year = {2024}
}