Brevitas

repository·master·Indexed 23 days ago

https://github.com/xilinx/brevitas

A PyTorch library for neural network quantization supporting both post-training quantization (PTQ) and quantization-aware training (QAT). It provides quantized implementations of common PyTorch layers in the `brevitas.nn` module, including QuantConv, QuantRNN, and QuantLSTM. The library features Accumulator-Aware Quantization (A2Q and A2Q+), support for BNN-PYNQ models, and capabilities to export models to ONNX or QONNX formats. It includes specialized implementations for MobileNet V1, ProxylessNAS Mobile14, and VGG.

Tokens
47.4K
Snippets
110
Records
200
Agent score
80%

What's inside Brevitas

  1. Overview of Brevitas quantization modeling

    master

    Brevitas is a platform for modeling reduced precision hardware data-paths during training. It provides building blocks at various abstraction levels to support both research into new quantization-aware training (QAT) techniques and the practical application of existing techniques to models.

    Key features include:

    • Unified API: Supports a super-set of quantization schemes across different frameworks and compilers.
    • Inference Acceleration: Supports exporting to FINN, onnxruntime, or PyTorch's native quantized operators for specific layer and quantization combinations.
    • Quantization Style: Primarily implements affine quantization with a focus on uniform quantization. Note that non-uniform quantization is not supported out-of-the-box.
  2. Overview of ImageNet classification examples (PTQ and QAT)

    master

    The ImageNet examples are categorized into two main workflows:

    • PTQ (Post-Training Quantization): Demonstrates how to apply PTQ to models defined either 'by hand' (manually modified pretrained models) or 'programmatically' (starting from torchvision definitions).
    • QAT (Quantization Aware Training): Provides examples of running inference on networks trained with QAT. These examples focus on low-precision (< 8-bit) models designed for export and acceleration in downstream compilers. Note that the full training code for these specific QAT models is not included; only the quantized model definitions and inference examples are provided.

    Detailed information for each workflow can be found in the ptq and qat subdirectories. Quantized models defined by hand are located in the models folder.

  3. Explore Brevitas Example Sets

    master

    The src/brevitas_examples directory contains several specialized example sets for different quantization and deployment scenarios:

    • bnn_pynq: Focuses on simple Quantization Aware Training (QAT) at 1, 2, and 4 bits, specifically designed for deployment using the FINN framework.
    • imagenet_classification: Provides Post-Training Quantization (PTQ) examples and pretrained 4-bit QAT models for ImageNet classification tasks.
    • speech_to_text: Contains a pretrained 8-bit QAT MelGAN Vocoder intended for integration into text-to-speech pipelines.
    • super_resolution: Features pretrained 8-bit QAT models for increasing RGB image resolution using Accumulator-Aware Quantization (A2Q).
    • text_to_speech: Includes a pretrained 8-bit QAT QuartzNet model for Automatic Speech Recognition (ASR).
  4. Post Training Quantization (PTQ) in Brevitas

    master

    Brevitas provides Post Training Quantization (PTQ) features to quantize torchvision models or models manually defined with Brevitas quantized layers (e.g., MobileNet V1). This workflow can serve as a starting point for Quantization Aware Training (QAT) fine-tuning, particularly at lower precisions.

    There are two primary workflows provided:

    1. Evaluation script: Performs PTQ on a model given a name and quantization configuration, evaluates ImageNet top1 accuracy, and optionally exports the model to ONNX or TorchScript QCDQ.
    2. Benchmark suite: Tests various quantization configurations across selected models.

    Note: Programmatic quantization is experimental and may break on certain models or configurations. The implementation relies on torch.fx symbolic tracing, so the input model must support symbolic tracing.

  5. What is Qronos and how does it work?

    master

    Qronos (presented at ICLR 2026) is a Post-Training Quantization (PTQ) algorithm designed to address quantization errors in both weights and activations. Unlike standard PTQ techniques that solve a layerwise reconstruction problem in isolation, Qronos addresses the "mismatched" reconstruction problem by considering the potentially quantized inputs ($\tilde{X}$) from previously quantized layers.

    The algorithm quantizes weights sequentially using two alternating steps:

    1. Error Correction: Selecting a quantized weight that optimally corrects the current approximation error while holding other weights fixed.
    2. Error Diffusion: Updating the remaining unquantized weights to compensate for the accumulated rounding error.

    Qronos utilizes techniques like Cholesky decomposition and lazy batch updates to scale efficiently to large models.

  6. Overview of Learned Round quantization methods

    master

    The Learned Round implementation in Brevitas allows for learnable rounding during quantization. The experiments described in this directory compare Learned Round against several other techniques across two main phases:

    Stage 1: Transform (Preprocessing)

    • None: No preprocessing.
    • HIP: Hadamard-based incoherence processing.
    • MagR: Weight magnitude reduction.
    • QuaRot: Rotation-based outlier reduction.
    • SpinQuant: Cailey-optimized orthogonal rotations.

    Stage 2: Rounding

    • RTN: Round-To-Nearest.
    • GPTQ: GPTQ quantization.
    • Qronos: Qronos quantization.
    • Learned Round: Brevitas' implementation of learnable rounding.
  7. What is Learned Round and how does it work?

    master

    Learned Round is a post-training quantization (PTQ) technique that improves quantization quality by learning per-weight rounding decisions instead of using fixed round-to-nearest (RTN). While RTN minimizes weight reconstruction error, Learned Round optimizes for layer (or block-wise) output reconstruction loss, which is a better proxy for downstream accuracy.

    It formulates rounding as a continuous optimization problem by introducing learnable parameters into the rounding operator. These parameters are optimized using gradient-based methods over calibration data. This allows for joint optimization of rounding decisions across all weights within a block, effectively reducing block output error while mitigating overfitting.

    In Brevitas, Learned Round is unified to provide:

    • A common abstraction for learned rounding.
    • Flexible rounding parameterization and optimization strategies.
    • Seamless integration with existing PTQ pipelines (LLM and ImageNet entrypoints).

    It is compatible with all supported quantized data types (INT2, INT4, INT8, MXFP4, etc.) and can be composed with other PTQ techniques like QuaRot, SpinQuant, and MagR.

  8. Enable JIT compilation with BREVITAS_JIT

    master

    Brevitas uses custom torch.autograd.Function implementations. While Python implementations are used by default, you can enable C++ implementations to support end-to-end compilation of training-time quantizers when using JIT.

    To enable this, set the environment variable BREVITAS_JIT=1.

    Note on C++ Requirements: To use the C++ implementation, you must have a PyTorch compatible C++ compiler installed. Brevitas compiles the custom C++ autograd functions at runtime using torch.utils.cpp_extension.load(). This is primarily useful for distributed training scenarios.

  9. Accumulator-Aware Quantization (A2Q and A2Q+)

    master

    This directory provides implementations for advanced quantization techniques designed to avoid accumulator overflow:

  10. Share weight quantizers across layers

    master

    Brevitas allows you to share instances of weight quantizers among multiple layers. This forces the layers to share the same scale, zero-point, and bit-width. When a quantizer is shared, it looks at all participating weight tensors to determine a single global scale factor (e.g., based on the overall maximum value).

    This is designed to be eager-mode friendly; the quantizer is re-initialized appropriately whenever it is shared with a new layer.

  11. How custom quantizers are built with dependency injection

    master

    Advanced users can define entirely new quantization algorithms using Brevitas's dependency-injection system. Instead of using enums, you inherit from ExtendedInjector and define a tensor_quant module (which must be a brevitas.jit.ScriptModule) and a proxy_class (which returns a QuantTensor).

    The tensor_quant module is assembled by matching its keyword arguments to the attributes defined in your ExtendedInjector class. This allows for highly modular construction of quantization logic (rounding, scaling, clamping, etc.).

    from brevitas.inject import ExtendedInjector
    from brevitas.proxy import ActQuantProxyFromInjector
    
    class MyCustomQuantizer(ExtendedInjector):
        proxy_class = ActQuantProxyFromInjector
        tensor_quant = RescalingIntQuant 
        int_quant = IntQuant
        float_to_int_impl = RoundSte 
        # ... other components ...
  12. Supported PTQ techniques for Diffusion Quantization

    master

    The diffusion quantization entrypoint supports several Post-Training Quantization (PTQ) techniques for both integer and floating point quantization:

    • Activation Equalization: e.g., SmoothQuant, layerwise (requires addition of Mul ops).
    • Activation Calibration: Used for static activation quantization.
    • GPTQ
    • SVDQuant
    • Bias Correction