Post-Training Quantization (PTQ): Mathematical Foundations of Optimal Brain Surgeon, GPTQ Hessian Inversion, AWQ Salient Scaling, and SmoothQuant Outlier Migration

Post-Training Quantization (PTQ): Mathematical Foundations of Optimal Brain Surgeon, GPTQ Hessian Inversion, AWQ Salient Scaling, and SmoothQuant Outlier Migration Serving modern large language models at scale requires addressing severe hardware constraints. In autoregressive generation, decoding is fundamentally bounded by memory bandwidth rather than floating-point computation throughput. Each generated token requires reading every parameter from High Bandwidth Memory (HBM) into SRAM to perfo

10 min
Post-Training Quantization (PTQ): Mathematical Foundations of Optimal Brain Surgeon, GPTQ Hessian Inversion, AWQ Salient Scaling, and SmoothQuant Outlier Migration

Post-Training Quantization (PTQ): Mathematical Foundations of Optimal Brain Surgeon, GPTQ Hessian Inversion, AWQ Salient Scaling, and SmoothQuant Outlier Migration

Serving modern large language models at scale requires addressing severe hardware constraints. In autoregressive generation, decoding is fundamentally bounded by memory bandwidth rather than floating-point computation throughput. Each generated token requires reading every parameter from High Bandwidth Memory (HBM) into SRAM to perform matrix-vector multiplications. For a 70-billion parameter model in FP16 or BF16 precision, a single forward pass transfers 140 gigabytes of weights. On an NVIDIA H100 GPU with 3.35 terabytes per second of memory bandwidth, transferring these weights establishes a theoretical limit of roughly 24 tokens per second for batch size 1.

Post-Training Quantization (PTQ) addresses this memory wall by mapping high-precision floating-point parameters (FP16, BF16, or FP32) to low-bitwidth discrete representations (such as INT8, INT4, or FP8) without requiring extensive end-to-end retraining. Achieving low-bit quantization without degrading model perplexity requires rigorous mathematical compensation techniques.

Post-Training Quantization Mathematical Architecture and Precision Compression

Fundamentals of Uniform and Affine Quantization

The standard quantization operation maps continuous real-valued numbers into discrete integer bins.

Uniform Affine Quantization

Uniform affine (asymmetric) quantization maps a real-valued interval [xmin,xmax][x_{\min}, x_{\max}] to an unsigned bb-bit integer range [0,2b1][0, 2^b - 1] through a positive scale factor SS and an integer zero-point offset ZZ:

S=xmaxxmin2b1S = \frac{x_{\max} - x_{\min}}{2^b - 1}

Z=round(xminS)Z = \text{round}\left(-\frac{x_{\min}}{S}\right)

The quantization function Q(x)Q(x) applies scaling, rounding, offset addition, and clamping:

Q(x)=clip(round(xS)+Z,0,2b1)Q(x) = \text{clip}\left(\text{round}\left(\frac{x}{S}\right) + Z, 0, 2^b - 1\right)

The corresponding dequantization function reconstructs an approximate real value x^\hat{x}:

x^=S(Q(x)Z)\hat{x} = S \cdot (Q(x) - Z)

Symmetric Quantization

When data distributions are roughly centered around zero, symmetric quantization simplifies the mapping by setting the zero-point Z=0Z = 0. For signed bb-bit integers spanning [2b1,2b11][-2^{b-1}, 2^{b-1} - 1], the scale factor is derived from the maximum absolute value:

S=max(x)2b11S = \frac{\max(|x|)}{2^{b-1} - 1}

Q(x)=clip(round(xS),2b1,2b11)Q(x) = \text{clip}\left(\text{round}\left(\frac{x}{S}\right), -2^{b-1}, 2^{b-1} - 1\right)

x^=SQ(x)\hat{x} = S \cdot Q(x)

Symmetric quantization removes the overhead of subtracting zero points during matrix multiplication, enabling hardware integer Tensor Cores to execute integer multiply-accumulate (IMAC) operations directly.

Quantization Granularity

Quantization operates at various structural granularities:

  • Per-tensor quantization: A single scale factor SS is shared across an entire weight tensor or activation matrix. This minimizes metadata overhead but suffers when activation or weight distributions vary widely across channels.
  • Per-channel (per-column / per-row) quantization: An independent scale factor is assigned to each output channel (row) of a weight matrix WRdout×dinW \in \mathbb{R}^{d_{\text{out}} \times d_{\text{in}}}. This captures inter-channel variance without altering matrix multiplication hardware execution patterns.
  • Group-wise (sub-channel) quantization: Consecutive elements along the input channel dimension are partitioned into groups of size gg (typically g{32,64,128}g \in \{32, 64, 128\}), with an independent scale factor per group. Group-wise quantization limits the propagation of local outlier values, substantially improving accuracy at 3-bit and 4-bit precisions at the cost of additional scale parameters.

Optimal Brain Surgeon: Second-Order Error Minimization

Rounding weights to the nearest discrete integer independently (Round-To-Nearest, or RTN) causes quantization errors to compound across layers. To minimize the output distortion of a linear layer Y=WXY = W X, post-training quantization seeks to minimize the layer-wise Mean Squared Error (MSE):

L=12WXW^X22=12Tr((WW^)XXT(WW^)T)\mathcal{L} = \frac{1}{2} \| W X - \hat{W} X \|_2^2 = \frac{1}{2} \text{Tr}\left((W - \hat{W}) X X^T (W - \hat{W})^T\right)

Here, WRdout×dinW \in \mathbb{R}^{d_{\text{out}} \times d_{\text{in}}} denotes the original unquantized weight matrix, W^\hat{W} represents the quantized weight matrix, and XRdin×NX \in \mathbb{R}^{d_{\text{in}} \times N} represents the layer inputs collected from a small calibration dataset of unlabelled tokens.

The optimization can be decoupled across each row wR1×dinw \in \mathbb{R}^{1 \times d_{\text{in}}} of WW:

L(w)=12(ww^)H(ww^)T\mathcal{L}(w) = \frac{1}{2} (w - \hat{w}) H (w - \hat{w})^T

where the Hessian matrix HRdin×dinH \in \mathbb{R}^{d_{\text{in}} \times d_{\text{in}}} of second-order derivatives is given by:

H=w2L=2XXTH = \nabla_w^2 \mathcal{L} = 2 X X^T

Applying a second-order Taylor expansion around the unquantized optimal weight vector ww:

ΔL=L(w+Δw)L(w)=wLTΔw+12ΔwTHΔw\Delta \mathcal{L} = \mathcal{L}(w + \Delta w) - \mathcal{L}(w) = \nabla_w \mathcal{L}^T \Delta w + \frac{1}{2} \Delta w^T H \Delta w

Because the pre-trained weights ww represent an unconstrained local minimum of the layer loss, the first-order gradient vanishes: wL=0\nabla_w \mathcal{L} = 0. The change in error simplifies to:

ΔL12ΔwTHΔw\Delta \mathcal{L} \approx \frac{1}{2} \Delta w^T H \Delta w

In the classical Optimal Brain Surgeon (OBS) formulation, when a specific weight component wqw_q is quantized to w^q\hat{w}_q (enforcing the constraint eqTΔw=w^qwqe_q^T \Delta w = \hat{w}_q - w_q), the remaining unquantized weights are adjusted to compensate for the perturbation. Solving the constrained optimization problem via Lagrange multipliers yields the OBS weight update rule:

Δw=wqw^q[H1]qqH:,q1\Delta w = - \frac{w_q - \hat{w}_q}{[H^{-1}]_{qq}} H^{-1}_{:, q}

The resulting increase in reconstruction error is:

ΔL=12(wqw^q)2[H1]qq\Delta \mathcal{L} = \frac{1}{2} \frac{(w_q - \hat{w}_q)^2}{[H^{-1}]_{qq}}

Here, H:,q1H^{-1}_{:, q} denotes the qq-th column of the inverse Hessian matrix, and [H1]qq[H^{-1}]_{qq} is its qq-th diagonal element.

GPTQ: Efficient Second-Order Quantization at Scale

Direct application of Optimal Brain Surgeon to modern transformers is computationally intractable. For an input dimension din=8192d_{\text{in}} = 8192, computing and updating the full inverse Hessian matrix for each scalar weight parameter requires O(d4)O(d^4) operations, requiring hundreds of GPU hours per layer.

The GPTQ algorithm optimizes this process through three primary innovations:

1. Arbitrary Quantization Ordering

While classical OBS greedily selects the weight that minimizes ΔL\Delta \mathcal{L} at each step, GPTQ demonstrates that quantizing weights in a fixed, column-by-column order across all rows yields nearly identical perplexity. Fixing the column order allows the algorithm to share the inverse Hessian matrix computation across all output rows simultaneously.

2. Cholesky Decomposition and Inverse Hessian Lazy Updates

Let H=2XXT+λIH = 2 X X^T + \lambda I be the empirical Hessian regularized with damping factor λ=0.01mean(diag(H))\lambda = 0.01 \cdot \text{mean}(\text{diag}(H)) to prevent numerical instability. The inverse Hessian H1H^{-1} is computed once.

When quantizing a block of columns BB of size Δ=128\Delta = 128, the weights inside the block are quantized sequentially while tracking local updates. Once the entire block BB is quantized, the remaining unquantized columns RR of the weight matrix are updated in a single batched matrix-matrix multiplication:

W:,RW:,R(W:,BW^:,B)([HB,B1]1HB,R1)W_{:, R} \leftarrow W_{:, R} - (W_{:, B} - \hat{W}_{:, B}) \cdot \left([H^{-1}_{B, B}]^{-1} H^{-1}_{B, R}\right)

By utilizing Cholesky decomposition of H1H^{-1}, the updates leverage high-throughput GPU Tensor Cores, reducing the algorithmic complexity to O(d3)O(d^3) and quantizing a 175-billion parameter model in approximately four GPU hours.

3. Activation Order Heuristic (act-order)

In transformer architectures, certain activation channels have significantly higher variance. GPTQ incorporates the activation-order (act-order) heuristic: sorting the columns of WW and rows of XX by decreasing activation norm Xj,:2\|X_{j, :}\|_2 prior to Cholesky factorization. Quantizing channels with high curvature first ensures that remaining parameters have maximum capacity to compensate for quantization errors.

AWQ: Activation-Aware Weight Quantization

While GPTQ compensates for quantization error via inverse Hessian updates, Activation-aware Weight Quantization (AWQ) takes an alternative structural approach: protecting the most critical weight channels from quantization distortion entirely.

Salience of Activation Magnitude

Through empirical analysis of activation distributions across transformer layers, researchers observed that weight importance is not determined by weight magnitude W|W|, but by activation magnitude X|X|. Specifically:

  • The top 1% of channels with the highest average activation magnitudes carry the overwhelming majority of semantic information.
  • Quantizing these salient channels with Round-To-Nearest (RTN) leads to severe perplexity spikes.
  • Retaining only 1% of weights in FP16 precision (mixed-precision serving) eliminates perplexity degradation, but introduces irregular memory layouts that degrade GPU inference efficiency.

Per-Channel Equivalent Transformation

To protect salient channels without hardware-unfriendly mixed-precision execution, AWQ applies an equivalent per-channel affine transformation before uniform quantization.

For a linear layer Y=WXY = W X, an invertible diagonal scaling matrix S=diag(s1,s2,,sdin)S = \text{diag}(s_1, s_2, \dots, s_{d_{\text{in}}}) is introduced:

Y=WX=(WS)(S1X)=WXY = W X = (W S) (S^{-1} X) = W' X'

Quantizing the scaled weight matrix W=WSW' = W S and multiplying the dequantized weights by S1S^{-1} yields the effective output:

Y^=Q(WS)S1X\hat{Y} = Q(W S) S^{-1} X

When quantizing a scalar weight ww multiplied by scale factor ss, the rounding error Δw\Delta w is bounded by $\frac{1}{2} \Delta_{\text{quant}} = \frac{1}{2} \frac{\max(|w \cdot s|)}{2^{b-1}-1}$. When dequantized and multiplied back by s1s^{-1}, the effective perturbation on the layer output becomes:

ΔY=Δws1X=Δquant2s1X\Delta Y = \Delta w \cdot s^{-1} \cdot X = \frac{\Delta_{\text{quant}}}{2} \cdot s^{-1} \cdot X

Increasing sjs_j for salient channels reduces the effective output error contributed by channel jj.

AWQ determines the optimal per-channel scale vector ss by balancing activation magnitude and weight magnitude. The scale is parameterized as:

s=sXαs = s_X^\alpha

where sXRdins_X \in \mathbb{R}^{d_{\text{in}}} represents the average activation magnitude per channel:

sX,j=1Ni=1NXj,is_{X, j} = \frac{1}{N} \sum_{i=1}^N |X_{j, i}|

The hyperparameter α[0,1]\alpha \in [0, 1] controls the scaling strength. AWQ performs a fast grid search over α[0,1]\alpha \in [0, 1] (typically step size 0.05) on calibration data to minimize layer reconstruction error:

α=argminαWXQ(Wdiag(sXα))diag(sXα)X22\alpha^* = \arg\min_\alpha \| W X - Q(W \cdot \text{diag}(s_X^\alpha)) \cdot \text{diag}(s_X^{-\alpha}) X \|_2^2

Because SS is a static diagonal matrix, S1S^{-1} is folded directly into the preceding layer's bias, LayerNorm, or weight parameters during model loading. At inference time, the model executes standard uniform INT4/INT8 GEMM kernels without runtime latency penalties.

SmoothQuant: Migrating Activation Quantization Difficulty

Weight-only quantization (such as W4A16 or W8A16) reduces memory bandwidth pressure during decoding, but does not accelerate compute-bound prefill operations because activations remain in FP16 precision. Full integer quantization (W8A8 or W4A4) requires quantizing both weights and activations.

However, standard activation quantization fails in models exceeding 6.7 billion parameters due to systematic activation outliers.

The Activation Outlier Phenomenon

In large language models, activation outliers exhibit three distinct properties:

  1. They emerge systematically in a tiny fraction of channels (typically less than 0.1% of hidden dimensions).
  2. Their magnitudes can reach up to 100 times larger than average channel activations.
  3. They persist across all token positions in a sequence.

Because activation quantization must be computed dynamically per-token or per-tensor, extreme outliers stretch the quantization range [xmin,xmax][x_{\min}, x_{\max}], causing the vast majority of non-outlier activations to round to zero.

Mathematical Migration Formulation

SmoothQuant resolves this asymmetry by observing that while activations are difficult to quantize due to outliers, weight distributions are spatially uniform and easy to quantize. SmoothQuant migrates quantization difficulty from activations to weights through a per-channel smoothing factor sRdins \in \mathbb{R}^{d_{\text{in}}}:

Y=XW=(Xdiag(s)1)(diag(s)W)=X^W^Y = X W = (X \cdot \text{diag}(s)^{-1}) \cdot (\text{diag}(s) \cdot W) = \hat{X} \hat{W}

The smoothed activation X^=Xdiag(s)1\hat{X} = X \cdot \text{diag}(s)^{-1} and smoothed weight W^=diag(s)W\hat{W} = \text{diag}(s) \cdot W are quantized independently:

Y^=Q(X^)Q(W^)\hat{Y} = Q(\hat{X}) Q(\hat{W})

To distribute quantization difficulty equitably between activations and weights, the per-channel scale sjs_j is defined as:

sj=max(X:,j)αmax(Wj,:)1αs_j = \frac{\max(|X_{:, j}|)^\alpha}{\max(|W_{j, :}|)^{1 - \alpha}}

where max(X:,j)\max(|X_{:, j}|) is the maximum activation magnitude observed across calibration tokens for channel jj, max(Wj,:)\max(|W_{j, :}|) is the maximum absolute weight in the jj-th row of WW, and α[0,1]\alpha \in [0, 1] is the migration strength hyperparameter:

  • α=1\alpha = 1: Fully migrates activation scale to weights (per-channel activation quantization).
  • α=0\alpha = 0: Leaves activations untouched (equivalent to standard per-tensor activation quantization).
  • α=0.5\alpha = 0.5: Equitably splits dynamic range difficulty between activations and weights.

By selecting α=0.5\alpha = 0.5, SmoothQuant suppresses activation outliers below the clipping threshold while keeping weight magnitudes well within standard 8-bit dynamic ranges, enabling W8A8 matrix multiplication across INT8 Tensor Cores.

FP8 Formats and Microscaling (MX) Architecture

Recent hardware architectures (such as NVIDIA Hopper/Blackwell and AMD CDNA3) introduce native hardware support for 8-bit floating-point (FP8) arithmetic.

E4M3 vs. E5M2 Numerical Representations

The OCP (Open Compute Project) FP8 specification defines two distinct representations:

  • FP8-E4M3 (1 sign bit, 4 exponent bits, 3 mantissa bits): Provides higher numerical precision with a bounded dynamic range ([448,448][-448, 448]). E4M3 has only one NaN encoding and no representation for infinity, maximizing available numeric states. It is the preferred format for forward-pass weights and activations.
  • FP8-E5M2 (1 sign bit, 5 exponent bits, 2 mantissa bits): Matches the 5-bit exponent field of IEEE 754 FP16, providing an extended dynamic range ([57344,57344][-57344, 57344]) at the expense of mantissa precision. It accommodates large gradient scales during backward passes and training.

Microscaling (MXFP4 / MXFP6 / MXFP8) Formats

Microscaling partitions tensors into microscopic blocks (typically 32 elements). Each 32-element sub-vector shares a single 8-bit scale factor (E8M0), while individual elements are encoded in low-bit floating-point formats (such as FP4 or FP6). This eliminates global outlier distortion while maintaining high arithmetic density on next-generation hardware.

Comparative Mechanics and Deployment Profiles

Different PTQ strategies target distinct operational regimes:

  • GPTQ (Weight-Only INT4/INT3): Best suited for memory-bandwidth-bound token generation on single-GPU deployments where batch sizes are small (B4B \le 4). Weights are dequantized to FP16 in SRAM registers before computation.
  • AWQ (Weight-Only INT4/INT3): Provides equivalent or superior perplexity to GPTQ with faster calibration times and zero dependency on second-order Hessian inversions, making it the industry standard for on-device and edge deployment.
  • SmoothQuant (W8A8 INT8): Designed for high-throughput serving systems operating at large batch sizes (B32B \ge 32) where the prefill and decoding phases become compute-bound. Fully utilizes INT8 Tensor Cores.
  • Native FP8 (W8A8 FP8): Employs E4M3 for weights and activations with per-tensor or per-block scaling, delivering near-lossless FP16 parity with 2×2\times throughput speedup on modern Hopper/Blackwell hardware.

Post-training quantization represents a foundational discipline in modern LLM systems engineering, reconciling parameter scaling with physical hardware memory hierarchies.

Sources

Written by

More to read

  • LLM Guardrails and Runtime Safety in Production: Comparing NeMo Guardrails, Guardrails AI, Meta Llama Guard, and Lakera

    LLM Guardrails and Runtime Safety in Production: Comparing NeMo Guardrails, Guardrails AI, Meta Llama Guard, and Lakera Deploying large language models (LLMs) into production environments introduces runtime risks that offline evaluation and static system prompts cannot eliminate. User-facing applications face prompt injections, jailbreaks, data exfiltration, toxicity, hallucinations, and malformed structured outputs. Relying solely on system prompt instructions ("You are a helpful assistant tha

    1 min
  • Moonshot AI Seeks Up to 30% Revenue Share from Microsoft, Amazon, and Google to Host Kimi K3

    China-based artificial intelligence startup Moonshot AI is in early negotiations with Microsoft, Amazon Web Services (AWS), and Google Cloud regarding revenue-sharing agreements to host its open-weight Kimi K3 model on their respective cloud platforms, according to a report from Reuters. According to people familiar with the matter, Moonshot is seeking up to a 30% cut of all revenue generated from hosting and serving Kimi K3 on the US hyperscaler platforms. Commercial Licensing Clauses on Ope

    1 min
  • Agentic Memory Systems in Production: Comparing Mem0, Letta, Zep Graphiti, and Cognee Architecture, State Consolidation, Temporal Graphs, and Retrieval Latencies

    Large language models are inherently stateless across API calls. While context windows have expanded to hundreds of thousands or millions of tokens, stuffing entire interaction histories into prompt context degrades retrieval accuracy, inflates time-to-first-token (TTFT) latency, and creates linear or quadratic cost scaling per interaction turn. For production AI agents operating over days, weeks, or months, persistent memory is a necessary architectural layer. Production memory systems differ

    1 min