torchao

TorchAO is an architecture optimization library for PyTorch, it provides high performance dtypes, optimization techniques and kernels for inference and training, featuring composability with native PyTorch features like torch.compile, FSDP etc. Some benchmark numbers can be found here.

Before you begin, make sure you have Pytorch version 2.5, or above, and TorchAO installed:

pip install -U torch torchao

Usage

Now you can quantize a model by passing a TorchAoConfig to from_pretrained(). Loading pre-quantized models is supported as well! This works for any model in any modality, as long as it supports loading with Accelerate and contains torch.nn.Linear layers.

from diffusers import FluxPipeline, FluxTransformer2DModel, TorchAoConfig

model_id = "black-forest-labs/Flux.1-Dev"
dtype = torch.bfloat16

quantization_config = TorchAoConfig("int8wo")
transformer = FluxTransformer2DModel.from_pretrained(
    model_id,
    subfolder="transformer",
    quantization_config=quantization_config,
    torch_dtype=dtype,
)
pipe = FluxPipeline.from_pretrained(
    model_id,
    transformer=transformer,
    torch_dtype=dtype,
)
pipe.to("cuda")

prompt = "A cat holding a sign that says hello world"
image = pipe(prompt, num_inference_steps=4, guidance_scale=0.0).images[0]
image.save("output.png")

TorchAO offers seamless compatibility with torch.compile, setting it apart from other quantization methods. This ensures one to achieve remarkable speedups with ease.

# In the above code, add the following after initializing the transformer
transformer = torch.compile(transformer, mode="max-autotune", fullgraph=True)

For speed/memory benchmarks on Flux/CogVideoX, please refer to the table here.

Additionally, TorchAO supports an automatic quantization API exposed with autoquant. Autoquantization determines the best quantization strategy applicable to a model by comparing the performance of each technique on chosen input types and shapes. This can directly be used with the underlying modeling components at the moment, but Diffusers will also expose an autoquant configuration option in the future.

The TorchAoConfig class accepts three parameters:

Supported quantization types

Broadly, quantization in the follow data types is supported: int8, float3-float8 and uint1-uint7. Among these types, there exists weight-only quantization techniques and weight + dynamic-activation quantization techniques.

Weight-only quantization refers to storing the model weights in a specific low-bit data type but performing computation in a higher precision data type, like bfloat16. This lowers the memory requirements from model weights, but retains the memory peaks for activation computation.

Dynamic Activation quantization refers to storing the model weights in a low-bit dtype, while also quantizing the activations on-the-fly to save additional memory. This lowers the memory requirements from model weights, while also lowering the memory overhead from activation computations. However, this may come at a quality tradeoff at times, so it is recommended to test different models thoroughly before settling for your favourite quantization method.

The quantization methods supported are as follows:

The “Documentation shorthands/Common speak” representation is simply the underlying storage dtype with the number of bits for storing activations and weights respectively.

Note that some quantization methods are aliases (for example, int8wo is the commonly used shorthand for int8_weight_only). This allows the usage of the quantization methods as specified in the TorchAO docs as-is, while also making it convenient to use easy to remember shorthand notations.

It is recommended to check out the official TorchAO Documentation for a better understanding of the available quantization methods and the exhaustive list of configuration options available.

Resources

< > Update on GitHub