Quantization-Aware Training (QAT) in NeMo Automodel
Quantization-Aware Training (QAT) in NeMo Automodel
NeMo Automodel supports Quantization-Aware Training (QAT) for Supervised Fine-Tuning (SFT) using TorchAO. QAT simulates quantization effects during training so model weights can adapt to the selected lower-precision scheme. Whether QAT improves task quality over post-training quantization (PTQ), and whether the converted model improves deployment efficiency, depends on the model, task, hardware, and inference backend; measure both approaches on the target workload.
What is Quantization-Aware Training?
Quantization-Aware Training introduces fake-quantization operations in the forward pass while keeping model weights trainable. This gives the model an opportunity to adapt to quantization error before the weights are converted to an actual quantized representation.
Benefits of QAT
- Adaptation during training: Model weights are optimized while fake quantization is active
- A PTQ alternative: QAT can be evaluated when PTQ does not meet the workload’s quality target
- Deployment experimentation: Converted models can be benchmarked with quantized kernels on the intended hardware and backend
- Explicit trade-off measurement: Validation quality, latency, throughput, and memory can be compared against BF16 and PTQ baselines
QAT vs. Post-Training Quantization
Requirements
To use QAT in NeMo Automodel, you need:
- Software: Use the repository’s locked environment, which includes TorchAO
- Hardware: An NVIDIA GPU and software stack supported by the selected TorchAO training and deployment kernels
- Training mode: Full-parameter BF16 SFT; the current recipe rejects QAT with PEFT and rejects models containing non-BF16 parameters
- Model dimensions: The model’s target
nn.Linearlayers must satisfy the selected TorchAO quantizer’s group and kernel shape constraints
Install TorchAO
TorchAO is a project dependency. Install the repository’s locked environment:
How QAT Works in NeMo Automodel
NeMo Automodel integrates TorchAO’s QAT quantizers into the training pipeline. During training:
- Model preparation: The quantizer replaces eligible linear layers with TorchAO QAT linear modules
- Forward pass: Eligible weights, and activations for 8da4w, pass through fake quantization
- Backward pass: Gradients flow through the fake quantization operations
- Weight updates: The optimizer updates the model weights; the current prepared modules do not add a separate set of trainable quantization parameters
Supported Quantization Schemes
NeMo Automodel supports two TorchAO QAT quantizers:
Int8 Dynamic Activation + Int4 Weight (8da4w-qat)
- Quantizer:
Int8DynActInt4WeightQATQuantizer - Activations: INT8 with dynamic quantization
- Weights: INT4 quantization
- Layer constraint: With the current default
padding_allowed=False, eligible linear layers must havein_featuresdivisible bygroupsize; incompatible layers remain unmodified
Int4 Weight-Only (4w-qat)
- Quantizer:
Int4WeightOnlyQATQuantizer - Activations: BF16 in the current integration
- Weights: INT4 quantization
- Layer constraint: TorchAO targets bias-free linear layers; preparation requires compatible input dimensions, and conversion requires
out_featuresdivisible by 8
The current QATConfig sets both model-weight precision and weight-scale precision to BF16. For the documented group sizes, a target layer’s in_features must be divisible by groupsize; the 4w path also requires divisibility by inner_k_tiles * 16 (128 with the default inner_k_tiles=8). Layers left unmodified by a quantizer remain BF16, so inspect the prepared model rather than assuming every linear layer was quantized.
Configuration
To enable QAT in your training configuration, you need to specify the quantizer in your YAML configuration file.
Basic Configuration
Int4 Weight-Only Configuration
Configuration Parameters
Fake Quantization Start Step
The recipe accepts fake_quant_after_n_steps as the requested activation threshold. In the current revision, however, the enable and disable helpers are called on top-level model parts without traversing their nested TorchAO QAT linear modules. Positive values therefore do not reliably delay fake quantization in those nested layers. Use 0 and do not rely on delayed activation until that traversal is implemented.
Training Workflow
1. Prepare Your Configuration
Create a YAML configuration file with QAT enabled:
2. Run Training
Launch training with your QAT-enabled configuration:
3. Monitor Training
During training, the model will:
- Apply fake quantization to eligible weights and, for 8da4w, activations
- Learn to minimize loss while accounting for quantization effects
- Produce checkpoints that can be converted to actual quantized models
4. Deploy Quantized Model
After training, convert the QAT checkpoint to a fully quantized model for deployment:
Performance Considerations
Training Performance
- Training time: Fake-quantization operations add work to each forward pass; benchmark step time against the BF16 baseline
- Memory usage: QAT modules change the training graph and state; measure peak allocated and reserved memory for the selected model and topology
- Convergence: Tune training duration and learning rate from validation results rather than assuming BF16 hyperparameters transfer unchanged
Inference Performance
After converting to actual quantization, benchmark the exact inference stack you plan to deploy. The encoded weights use lower precision, but scales, metadata, unmodified layers, kernel availability, and runtime packing all affect the end-to-end result.
- Speed: Report latency and throughput on the target hardware and backend; quantized storage alone does not guarantee faster kernels
- Memory: Measure serialized size and runtime peak memory because they include more than packed weight bits
- Accuracy: Evaluate the same task metrics for BF16, PTQ, and QAT checkpoints
When to Use QAT
Consider QAT when:
- PTQ misses a target: A measured PTQ quality result is below the workload requirement
- Training is available: You can run and validate an additional full-parameter SFT job
- The deployment stack supports the scheme: The target hardware and backend have compatible quantized kernels
When Not to Use QAT
Prefer a simpler baseline when:
- Establishing feasibility: PTQ can test the deployment scheme without another training run
- The backend lacks compatible kernels: Conversion would not exercise the intended quantized implementation
- Training resources are limited: QAT requires another full-parameter training run
- PTQ already meets the measured targets: Additional QAT training may not be justified
Best Practices
1. Start with Post-Training Quantization
Before investing in QAT, try post-training quantization to establish a baseline:
If accuracy is acceptable, you may not need QAT.
2. Choose the Right Quantization Scheme
- 8da4w-qat: Fake-quantizes eligible activations to INT8 and weights to INT4
- 4w-qat: Fake-quantizes eligible weights to INT4 while activations remain BF16
Train and benchmark both only when their layer-shape constraints and deployment kernels match your model; neither scheme is categorically more accurate or faster for every workload.
3. Tune Group Size
The groupsize parameter affects the granularity of quantization:
- Smaller groups (128): Finer weight quantization granularity with more scale metadata
- Larger groups (256): Coarser granularity with fewer scales
Both values must divide the target linear layer’s in_features. Compare validation metrics and deployment measurements before choosing one.
4. Monitor Validation Metrics
Track validation metrics closely during QAT training:
- Compare against full-precision baseline
- Watch for convergence issues
- Compare learning-rate choices using the same validation protocol
5. Start Fake Quantization Immediately
Use immediate fake quantization in the current revision:
Positive values are accepted by the recipe, but the current direct helper calls do not traverse nested TorchAO QAT linear modules, so they must not be relied upon to delay activation.
Accuracy vs. Efficiency Trade-offs
Measure the Trade-off
Do not assume a fixed accuracy loss, compression ratio, or inference speedup. Record the following for BF16, PTQ, and each QAT scheme you evaluate:
Optimization Strategies
If accuracy is below expectations:
- Tune training duration: Use validation curves to decide whether more steps help
- Tune learning rate: Compare a small set of learning rates against the BF16 training baseline
- Compare schemes: Evaluate 8da4w and 4w when both satisfy the model and backend constraints
- Compare group sizes: Smaller groups provide finer-grained quantization but add scale metadata
Limitations and Known Issues
Current Limitations
- SFT only: QAT is currently supported for Supervised Fine-Tuning tasks only
- BF16 only: The infrastructure rejects QAT when any model parameter is not BF16
- No PEFT: The fine-tuning recipe rejects configurations that combine QAT and PEFT
- Model compatibility: TorchAO only prepares eligible
nn.Linearlayers, and the 4w path has stricter bias and shape constraints - Training overhead: QAT adds computational overhead during training
Troubleshooting
Issue: Training diverges or doesn’t converge
Solution: Try these approaches:
- Compare lower learning rates against the BF16 baseline
- Compare supported group sizes whose values divide the target layer dimensions
- Verify your baseline model trains successfully without QAT
Issue: Accuracy is significantly worse than expected
Solution:
- Ensure you’re comparing against the same baseline (same training steps, data, etc.)
- Compare 8da4w quantization with 4w if both are supported by the deployment backend
- Compare supported group sizes whose values divide the target layer dimensions
- Use the validation curve to decide whether additional training steps help
Issue: Out of memory during training
Solution:
- Measure peak memory against the BF16 run to determine the added cost for this model
- Reduce batch size if needed
- Use gradient accumulation to maintain effective batch size