Clark Farming CompanySoftware Foundry

AI / Agent Reference · Reference

Quantization Theory

The mathematical foundations and practical techniques for reducing neural network precision — from FP32/FP16 floating point to INT8, INT4, and beyond. Quantization enables running large models on constrained hardware with minimal quality degradation. Neural networks are remarkably robust to precision reduction. Key reasons: Over-parameterization: Models have far more parameters than needed. Reducing precision removes…

draftneeds-review0 source links0 resolved links
wiki/wiki/ai-ml/quantization-theory.md

Answer

The mathematical foundations and practical techniques for reducing neural network precision — from FP32/FP16 floating point to INT8, INT4, and beyond. Quantization enables running large models on constrained hardware with minimal quality degradation. Neural networks are remarkably robust to precision reduction. Key reasons: Over-parameterization: Models have far more parameters than needed. Reducing precision removes…

Auto-generated neutral summary from the source page — needs human review before trusted use.

Evidence & Source Cards

No explicit artifact, library, or external source links found in this sample slice. Evidence state remains needs-review.

Source Excerpt

The mathematical foundations and practical techniques for reducing neural network precision — from FP32/FP16 floating point to INT8, INT4, and beyond. Quantization enables running large models on constrained hardware with minimal quality degradation.

Why Quantization Works

Neural networks are remarkably robust to precision reduction. Key reasons:

  1. Over-parameterization: Models have far more parameters than needed. Reducing precision removes redundancy.
  2. Weight distribution: Most weights are small. Large weights (outliers) dominate computation and can be preserved at higher precision.
  3. Activation saturation: Non-linearities (ReLU, GELU) saturate, making small precision differences irrelevant.
  4. Ensemble effect: Millions of small errors average out, preserving overall model behavior.

Floating Point Formats

FP32 (32-bit)

Sign (1 bit) | Exponent (8 bits) | Mantissa (23 bits)

FP16 (16-bit)

Sign (1 bit) | Exponent (5 bits) | Mantissa (10 bits)

BF16 (Brain Floating Point, 16-bit)

Sign (1 bit) | Exponent (8 bits) | Mantissa (7 bits)

Comparison

FormatRangePrecisionUse Case
FP32±3.4×10³⁸7 digitsTraining
FP16±655043 digitsInference (NVIDIA)
BF16±3.4×10³⁸2 digitsInference (AMD, Intel)
INT8±1273 digitsQuantized inference
INT4±81.5 digitsExtreme compression

Integer Quantization

Symmetric Quantization

Map floating-point range [-max, +max] to integer range [-127, +127] (INT8):

scale = max(|W|) / 127
W_int8 = round(W_fp32 / scale)
W_fp32 ≈ W_int8 × scale

Advantage: Simple, fast dequantization (multiply by scale).

Disadvantage: Wastes range if weights are asymmetric.

Asymmetric Quantization

Map floating-point range [min, max] to integer range [0, 255] (UINT8):

scale = (max - min) / 255
zero_point = round(-min / scale)
W_uint8 = round(W_fp32 / scale) + zero_point

Advantage: Better utilization of integer range.

Disadvantage: Slightly more complex dequantization.

Post-Training Quantization (PTQ)

Quantize a pre-trained model without additional training. Fastest approach, good for INT8.

Methods

Min-max calibration: Use a small calibration dataset to find min/max activation values.

For each layer:
  1. Run calibration data through layer
  2. Record min/max of activations and weights
  3. Compute scale and zero_point
  4. Quantize weights

Percentile calibration: Use percentiles (e.g., 99.9th) instead of min/max to handle outliers.

K-means clustering: Cluster weight values, assign cluster centers to integer levels.

Quality

TargetQuality vs FP16SpeedupMemory
INT898-99%1.5-2x50%
INT490-95%2-4x25%
INT270-85%4-8x12.5%

Quantization-Aware Training (QAT)

Simulate quantization effects during training. Better quality than PTQ, especially for INT4 and below.

How It Works

  1. Forward pass: Simulate quantized weights and activations
  2. Straight-through estimator (STE): Pass gradients through quantization (non-differentiable)
  3. Backward pass: Update full-precision weights with simulated gradients
W_quantized = round(W / scale) × scale  (forward)
∂L/∂W = ∂L/∂W_quantized  (STE: gradient passes through)

Quality

TargetQuality vs FP16Training TimeMemory
INT8 (QAT)99-99.5%1.5x PTQ50%
INT4 (QAT)95-97%2x PTQ25%

Advanced Quantization Techniques

AWQ (Activation-Aware Weight Quantization)

Identify and protect "outlier" weights that are important for model quality.

1. Analyze activation magnitudes across channels
2. Identify important channels (high activation magnitude)
3. Keep important channels at higher precision
4. Quantize remaining channels aggressively

Result: Better INT4 quality than uniform quantization.

SpQR (Sparsification + Quantization)

Combine weight sparsification with quantization:

1. Prune small weights (sparsification)
2. Quantize remaining weights
3. Result: Sparse + quantized model

Result: Higher compression than quantization alone.

OPTQ (Optimized Parameter-aware Text Quantization)

Iteratively select and quantize layers, optimizing for overall model quality:

1. Evaluate layer importance (sensitivity analysis)
2. Quantize least important layers first
3. Update importance scores
4. Repeat until target precision reached

Result: Better quality than layer-by-layer quantization.

NF4 (NormalFloat4) — QLoRA

4-bit normal浮点 format designed for QLoRA:

Quantization and Hardware

GPU Support

GPUINT8INT4FP8Tensor Cores
NVIDIA A100LimitedYes
NVIDIA H100Yes
NVIDIA RTX 4090LimitedYes
AMD MI300Yes
Apple M-seriesYes

CPU Support

CPUINT8INT4Vector Extensions
Intel 12th+ genLimitedAMX, AVX-512
Apple M-seriesSIMD
ARM Cortex-X4LimitedSVE2

Practical Guidance

When to Use Which Precision

ScenarioRecommendedReason
TrainingBF16 or FP32Numerical stability
Inference (GPU)FP16 or BF16GPU optimized, minimal quality loss
Inference (CPU)INT8CPU optimized, good quality
Edge devicesINT4 or INT8Memory and power constrained
Maximum compressionINT4 with AWQBest quality at 4-bit
Production chatINT8 or Q4_K_MBalance of quality and speed

Quality Preservation Tips

  1. Keep attention weights at higher precision (more sensitive to quantization)
  2. Use mixed precision (K-quantization: some layers Q8, others Q4)
  3. Calibrate with representative data (domain-matched calibration set)
  4. Evaluate before deploying (run benchmark prompts on quantized model)
  5. Keep FP16 original (for re-quantization if needed)

Source excerpt truncated at 220 of 225 lines. Open the canonical wiki path above for the full page.

Relationships

Outbound links

Referenced by

Tags

ai-mlquantizationcompressionfp16int8int4post-training