Quantization
Learn how lower-precision weights and activations reduce model memory, and how to evaluate the quality and speed tradeoffs.
Large Language Models store millions or billions of numerical parameters. Keeping every value at high precision consumes storage and device memory and increases the amount of data moved during inference. Quantization reduces numerical precision to make deployment more efficient.
The goal is not simply to make a file smaller. A useful quantized model must preserve acceptable task quality and run efficiently on the actual target hardware and software stack.
What Is Quantization?
Quantization represents model weights, activations, or cached values with fewer bits or a more compact numeric format. For example, a framework may convert selected 16-bit floating-point weights into 8-bit integers or 4-bit packed values.
Lower precision reduces the set of numbers that can be represented exactly. Values must be rounded or mapped to nearby quantized levels, introducing quantization error. Good methods control that error where it matters most.
Why Is Quantization Important?
- Reduce the storage required for model weights.
- Lower CPU, GPU, or accelerator memory use.
- Fit a model on hardware where the higher-precision version cannot run.
- Reduce memory bandwidth during inference.
- Increase throughput or reduce latency on supported kernels.
- Make some local, mobile, edge, and cost-sensitive deployments practical.
A smaller representation does not guarantee faster generation. Speed depends on optimized kernels, hardware instructions, batch size, context length, dequantization overhead, and whether the workload is limited by memory or computation.
Understanding Numerical Precision
| Format | Typical Bits per Value | Common Role |
|---|---|---|
| FP32 | 32 | Training baseline or numerically sensitive work |
| FP16 / BF16 | 16 | Training and inference on supported accelerators |
| INT8 | 8 | Efficient inference or selected training operations |
| INT4 | 4 | Aggressive weight compression for compatible runtimes |
Actual file and memory size also include metadata, scales, zero-points, padding, unquantized layers, runtime buffers, activations, and the attention key-value cache. The bit width alone does not tell the full resource story.
How Integer Quantization Works
A common approach maps a range of real values to a limited set of integers using a scale and sometimes a zero-point.
Quantize: q = round(x / scale) + zero_point
Dequantize: x_approx = scale x (q - zero_point)
x: original value
q: stored integer
scale: spacing between representable levelsValues outside the selected range may be clipped. Outliers can therefore determine a large scale and reduce precision for most normal values. Modern quantization methods use grouping, clipping, calibration, or optimization strategies to reduce this problem.
Symmetric and Asymmetric Quantization
Symmetric quantization places zero near the center of a balanced range and often uses no separate nonzero offset. Asymmetric quantization uses a zero-point to represent an unbalanced range more efficiently. Hardware and framework support influence the choice.
Per-Tensor, Per-Channel, and Group-Wise
One scale can represent an entire tensor, separate scales can represent output channels, or groups of values can share scales. Finer grouping often preserves quality better but adds metadata and implementation complexity.
What Can Be Quantized?
Weight-Only Quantization
Model weights are stored at lower precision while activations or computation use another format. This is common for LLM inference because weight memory and bandwidth are major costs.
Weight and Activation Quantization
Both weights and intermediate activations use reduced precision. This can unlock faster integer computation, but activations change with each input and can be harder to quantize accurately.
Key-Value Cache Quantization
Autoregressive Transformers cache attention keys and values for earlier tokens. Quantizing this cache can reduce memory during long-context generation, but compatibility and quality must be evaluated.
Main Quantization Approaches
Post-Training Quantization
Post-Training Quantization, or PTQ, converts a trained model without running a full new training process. It is often the fastest approach and may use calibration data or optimization that minimizes reconstruction error.
Dynamic Quantization
Dynamic methods determine some activation quantization parameters at runtime. This is easy to apply in certain frameworks but may add conversion overhead.
Static Quantization
Static methods use representative calibration data to estimate activation ranges before deployment. Results depend on how well that calibration data represents production inputs.
Quantization-Aware Training
Quantization-Aware Training, or QAT, simulates quantization effects during training so the model can adapt. It can preserve quality better for difficult low-precision targets but requires training compute, data, and a supported workflow.
Calibration Data
Calibration runs representative examples through the model to estimate numerical ranges or other quantization statistics. The dataset should cover real languages, prompt lengths, domains, formatting, and edge cases without containing unnecessary private information.
Calibration examples are not necessarily fine-tuning labels. Their job is to represent activation patterns, although some advanced methods may use additional objectives.
Python Memory Estimate
This example estimates idealized weight storage. It does not perform quantization and intentionally excludes runtime overhead.
def ideal_weight_size_gb(parameters: int, bits_per_weight: int) -> float:
total_bytes = parameters * bits_per_weight / 8
return total_bytes / (1024 ** 3)
parameter_count = 7_000_000_000
for bits in (32, 16, 8, 4):
size = ideal_weight_size_gb(parameter_count, bits)
print(f"{bits}-bit: {size:.2f} GiB of idealized weight storage")
# Real memory use is higher because runtimes also need scales,
# metadata, activations, cache, workspace, and other buffers.Quantization vs Fine-Tuning
| Area | Fine-Tuning | Quantization |
|---|---|---|
| Primary goal | Adapt behavior or task performance | Reduce numerical precision and deployment cost |
| What changes | Some or all trainable parameters | Representation of weights or runtime values |
| Data need | Labeled or curated training examples | May need representative calibration data |
| Main risk | Overfitting or unwanted behavior changes | Quality loss or unsupported slow execution |
| Typical stage | Model adaptation | Training or deployment optimization |
The methods can be combined. A model may be fine-tuned for a task and then quantized for serving. QLoRA takes another approach: it fine-tunes LoRA adapters while the frozen base model is held in a quantized representation.
Advantages
- Smaller model-weight files.
- Lower memory use during inference.
- Potentially faster loading and higher throughput.
- Ability to run larger models on a fixed device.
- Lower infrastructure cost for compatible workloads.
- More deployment options across local, edge, and cloud hardware.
Limitations and Risks
- Lower precision can reduce task accuracy and generation quality.
- Rare capabilities may degrade even when average benchmarks look stable.
- Not every model, layer, device, or framework supports every method.
- Unsupported formats may be slower because of conversion overhead.
- Calibration data may fail to represent production traffic.
- Aggressive quantization can amplify outlier and numerical issues.
- Different toolchains may use the same bit-width label for different formats.
- Quantization does not fix hallucinations, bias, privacy, or prompt injection.
Evaluation Workflow
- 1. Establish quality, latency, memory, and throughput baselines.
- 2. Quantize using a trusted tool and pinned configuration.
- 3. Run the same held-out task evaluation on both versions.
- 4. Test long contexts, languages, code, structured output, and important edge cases.
- 5. Measure first-token latency, generation speed, peak memory, throughput, and file size.
- 6. Compare results on the exact production hardware and runtime.
- 7. Red-team safety and security behavior for regressions.
- 8. Deploy gradually with monitoring and rollback support.
Best Practices
- Start from a tested higher-precision model and stable evaluation set.
- Choose the least aggressive precision that meets deployment needs.
- Use representative and legally permitted calibration data.
- Confirm optimized kernel support on target hardware before committing.
- Keep sensitive or numerically fragile layers at higher precision when testing supports it.
- Record the base model, revision, tokenizer, method, bit width, grouping, calibration data, runtime, and hardware.
- Validate quality and performance after every conversion or runtime update.
- Keep a higher-precision fallback and a reproducible build process.
Real-World Applications
- Local chat and coding assistants.
- Mobile and edge AI applications.
- Cloud inference with constrained accelerator memory.
- High-throughput support and document systems.
- Embedded and offline applications using suitable models.
- QLoRA and other resource-efficient adaptation workflows.
Why Learn Quantization?
Quantization connects numerical representation with real deployment constraints. It helps AI engineers reason about model size, memory bandwidth, hardware kernels, quality evaluation, latency, throughput, and production cost rather than treating a model file as a black box.