Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Understanding Model Quantization: INT8/INT4 and Its Impact

Introduction

Welcome back! In our previous lesson, we explored batching as a crucial scheduling optimization for LLM inference. We saw how moving from static to continuous batching allows for much higher GPU utilization and system throughput by eliminating head-of-line blocking. Batching fundamentally addresses the problem of under-utilization when inference is memory-bound.

Today, we shift our focus from scheduling to the model itself. We will investigate another powerful technique that directly attacks the memory and compute bottlenecks: model quantization. This is a model compression technique that can dramatically improve inference performance.

This lesson will address the learning outcome: Explain model quantization (e.g., INT8/INT4) and its effect on performance, memory usage, and accuracy. We will cover the theoretical underpinnings of quantization, its practical implementation strategies, and the critical trade-offs involved in designing a quantized inference pipeline.

Why Quantize? The Impact on System Performance

At its core, quantization is the process of reducing the numerical precision of a model's parameters (weights) and, in some cases, its intermediate calculations (activations). Typically, models are trained using 32-bit floating-point numbers (FP32). Quantization converts these numbers into lower-precision formats, most commonly 8-bit integers (INT8), but also 16-bit floats (FP16/BF16) or even 4-bit integers (INT4).

This seemingly simple conversion has three profound effects on system performance, which directly address the core challenges of inference at scale.

Model Quantization: Concepts, Methods, and Why It Matters

To start, let's get a high-level overview of the benefits of quantization from this NVIDIA Developer Blog post. It provides a concise summary of why this technique is so important.

Please read the section titled 'The benefits of quantization'. Focus on the three key advantages it discusses: memory usage, inference speed, and energy consumption.

As the reading highlights, the benefits are:

  1. Reduced Memory Footprint: This is the most direct benefit. Converting a model from FP32 to INT8 reduces its size by a factor of 4. A 7-billion parameter model that requires ~28 GB in FP32 would only require ~7 GB in INT8. This makes it possible to fit larger models onto a single GPU or to deploy models on resource-constrained edge devices.

    Bit Precision Comparison
    This chart illustrates the dramatic reduction in model size as numerical precision decreases from FP32 down to INT4.

  2. Increased Inference Speed (Throughput): The speedup comes from two sources:

    • Reduced Memory Bandwidth Pressure: Since the data types are smaller, less data needs to be moved from the GPU's high-bandwidth memory (HBM) to its on-chip compute units. As we've discussed, LLM inference is often memory-bound, so this reduction can significantly improve performance.
    • Faster Compute Hardware: Modern processors, especially GPUs, have specialized hardware designed to perform low-precision integer arithmetic much faster than high-precision floating-point arithmetic. For example, NVIDIA's Tensor Cores provide a massive throughput boost for INT8 and INT4 matrix operations.

    GPU Inference Throughput with Quantization
    This chart shows the inference throughput for a ResNet-50 model on NVIDIA GPUs. Notice the significant increase in images per second when moving from INT8 to even lower-precision INT4, demonstrating the performance gains from specialized hardware.

  3. Lower Energy Consumption: Faster computation and reduced data movement naturally lead to lower power draw, which is a critical consideration in large-scale datacenter deployments.

Of course, these benefits come with a crucial trade-off: a potential loss of accuracy. The main challenge in quantization is to minimize this accuracy degradation while maximizing the performance gains.

The Mechanics of Quantization: From Floats to Integers

To understand how to manage the accuracy-performance trade-off, we need to look at the mechanics of the conversion process. The goal is to find a mapping from a range of high-precision floating-point numbers to a discrete set of low-precision integers.

The most common mapping is an affine transformation, defined by two parameters: a scale (S) and a zero-point (Z).

The general formula to quantize a floating-point value x to an integer q is:

To de-quantize q back to an approximate float x', the formula is:

  • Scale (S): A positive floating-point number that determines the step size of the quantization. It maps the range of the original tensor to the target integer range.
  • Zero-Point (Z): An integer that ensures the real value 0.0 is mapped precisely to an integer in the quantized range. This is important for operations like padding.

There are two main schemes based on this formula:

  1. Asymmetric (Affine) Quantization: This scheme uses both S and Z. It can map any arbitrary input range [min_val, max_val] to the target integer range (e.g., [0, 255] for unsigned INT8). The zero-point Z corresponds to the value 0.0 in the float range.

  2. Symmetric Quantization: This is a simplified version where the zero-point Z is fixed to 0. This is only suitable when the input float range is symmetric around zero (e.g., [-max_abs, +max_abs]). The mapping is simpler (q = round(x/S)), which can reduce computational overhead. Most model weights are naturally distributed around zero, making this a common choice for them.

The following video provides an excellent, detailed walkthrough of these formulas and concepts.

Quantization explained with PyTorch - Post-Training Quantization, Quantization-Aware Training

This video by Umar Jamil offers a very clear, step-by-step explanation of the mathematics behind asymmetric and symmetric quantization. It includes concrete numerical examples that make the concepts easy to grasp.

Please watch from 09:03 to 20:54. The first part (up to 12:02) explains the high-level goal: performing matrix operations with integers and then de-quantizing. The main part (from 13:35 onwards) details the formulas for asymmetric and symmetric quantization, explaining how the scale (S) and zero-point (Z) are calculated and used.

Practical Considerations: Calibration and Granularity

The theory is clear, but how do we find the optimal S and Z values for a given model? This process is called calibration.

Calibration Strategies

The choice of the float range [min_val, max_val] used to calculate the scale factor is critical. A naive approach can be highly sensitive to outliers.

  • Min-Max Calibration: Sets the range using the absolute minimum and maximum values in the tensor. This is simple but problematic if there are extreme outliers, as they will stretch the range, leaving most of the values clustered into a small number of quantized bins. This increases the overall quantization error for the majority of the values.
  • Percentile Calibration: A more robust method that sets the range based on a percentile of the data's distribution (e.g., the 1st and 99.9th percentiles). This ignores extreme outliers, leading to a more faithful representation of the bulk of the data, at the cost of "clipping" the outlier values to the min/max of the quantized range.

The following video segment explains this critical issue with outliers.

Quantization explained with PyTorch - Post-Training Quantization, Quantization-Aware Training

Let's return to the same video to understand how outliers impact quantization and how different calibration strategies mitigate this.

Watch the segment from 29:47 to 35:48. It demonstrates the problem with the min-max strategy when outliers are present and shows how the percentile strategy provides a much better result for the non-outlier values.

Quantization Granularity

Another key design choice is the granularity at which the S and Z parameters are calculated and applied.

  • Per-Tensor: A single S and Z are used for an entire weight tensor (e.g., for one layer). This is the simplest approach.
  • Per-Channel (or Per-Group): A separate S and Z are calculated for each output channel of a linear or convolutional layer. This is more complex but often yields higher accuracy, as it can adapt to the different value distributions in different parts of the tensor. This is a classic accuracy-vs-complexity trade-off.

Major Quantization Approaches: PTQ vs. QAT

There are two main high-level strategies for applying quantization to a neural network. Your choice between them depends on the desired balance of accuracy, implementation simplicity, and computational cost.

A Comprehensive Study on Quantization Techniques for Large ...

This research paper provides a good formal overview of these techniques and their trade-offs, which will complement the more practical video explanation to follow.

Please read the latter half of Section I ('Introduction') and all of Section V ('Quantization Techniques'). Section I introduces the distinction between Post-Training Quantization (PTQ) and Quantization-Aware Training (QAT). Section V provides short descriptions of each, along with other related techniques like weight-only quantization.

1. Post-Training Quantization (PTQ)

As the name implies, PTQ is applied to a model after it has been fully trained. It's a popular and straightforward method.

  • How it works:
    1. Start with a pre-trained FP32 model.
    2. Run a small, representative "calibration dataset" through the model to collect statistics on the range of activation values for each layer.
    3. Use these statistics (and the static weight values) to compute the S and Z parameters for each tensor you want to quantize.
    4. Convert the model's weights to the lower-precision format (e.g., INT8). These quantized weights are stored for inference.
  • Pros: Simple, fast, and does not require access to the original training dataset or pipeline.
  • Cons: Can lead to significant accuracy degradation, especially at very low bit-widths (e.g., INT4), because the model was never optimized to handle the noise introduced by quantization.

2. Quantization-Aware Training (QAT)

QAT integrates the quantization process into the model training or fine-tuning loop, making the model robust to precision loss.

  • How it works:
    1. During training, "fake quantization" nodes are inserted into the model graph.
    2. In the forward pass, these nodes simulate the effect of quantization: they take a float value, quantize it, and then immediately de-quantize it (x -> round(x/S)+Z -> S*(q-Z)). This introduces quantization error (rounding, clipping) into the activations that the subsequent layers see.
    3. In the backward pass, the model weights are updated (still in FP32) based on the loss computed from these "noisy" activations. This forces the model to learn weights that are inherently more robust to quantization.
    4. A technical detail: The round() function has a zero gradient almost everywhere, which would block backpropagation. QAT uses a trick called the Straight-Through Estimator (STE), which simply approximates the gradient of the rounding function as 1, allowing gradients to flow back through the fake quantization node.
  • Pros: Almost always results in higher accuracy than PTQ, especially for very low bit-widths.
  • Cons: Much more complex. It requires a full training pipeline, access to data, and significantly more computation than PTQ.

The following segments from the video provide a fantastic walkthrough of both PTQ and QAT, complete with PyTorch code examples.

Quantization explained with PyTorch - Post-Training Quantization, Quantization-Aware Training

This final segment from the 'Quantization explained' video will walk you through implementing both PTQ and QAT. Given your background in PyTorch, the code examples should be particularly insightful.

Please watch from 35:48 to the end (50:14). The first part (35:48 - 43:09) covers Post-Training Quantization (PTQ), showing how observers collect statistics to create the quantized model. The second part (43:09 - 50:14) explains Quantization-Aware Training (QAT), detailing how fake quantization is used during training and why the Straight-Through Estimator (STE) is necessary. The visual explanation of why QAT seeks 'wider' loss minima is an excellent conceptual takeaway.

Advanced Quantization for LLMs

While the principles of PTQ and QAT are universal, applying them to modern Large Language Models (LLMs) with billions of parameters introduces unique challenges. Researchers have found that as models scale, prominent activation outliers emerge that can severely degrade accuracy under standard PTQ.

This has led to the development of more sophisticated PTQ algorithms that are better suited for LLMs. You will often encounter names like:

  • GPTQ: Uses approximate second-order information (the Hessian) to selectively quantize weights in a way that minimizes the reconstruction error of the layer's output.
  • AWQ (Activation-aware Weight Quantization): Protects a small fraction of "salient" weights that are most important for performance by analyzing activation statistics, allowing the rest to be aggressively quantized.

A deep dive into these is beyond this lesson's scope, but it is important to know they exist as advanced, LLM-specific PTQ techniques that build on the foundations we've covered.

Conclusion

In this lesson, we have explored model quantization as a powerful optimization for efficient inference. By reducing the numerical precision of a model, we can achieve significant system-level benefits.

Key Takeaways:

  • Effects of Quantization: Quantization to lower bit-widths (like INT8/INT4) reduces model memory footprint, increases inference speed by leveraging specialized hardware and reducing memory bandwidth, but can potentially degrade model accuracy.
  • Core Mechanism: The process relies on an affine mapping from a float range to an integer range using a scale (S) and zero-point (Z). The calculation of these parameters (calibration) and the scope of their application (granularity) are key design decisions.
  • PTQ vs. QAT: Post-Training Quantization (PTQ) is a simple, fast method applied after training, but can be less accurate. Quantization-Aware Training (QAT) is more complex and computationally expensive but integrates quantization simulation into training to produce a more robust and accurate final model.
  • The Accuracy-Performance Trade-off: Every aspect of quantization, from the choice of calibration method to the PTQ/QAT decision, involves managing the trade-off between inference performance and model accuracy.

Preview of the Next Lesson:

We have now covered two major optimizations for inference at scale: batching (scheduling) and quantization (model compression). Both help alleviate the memory bandwidth bottleneck. In our next lesson, we will focus on another critical memory component specific to transformer models: the KV-cache. We will address the learning outcome: Describe the KV-cache mechanism in Transformer inference and its memory implications for long-context models. Understanding how to manage this cache is the next piece of the puzzle in designing high-performance LLM inference systems.

Can't find a good explanation? Sign up and we'll make it for you

Sign up