Phase 4: Quantization & Compression [4000]¶
Table of Contents¶
- Overview
- Why Quantization Matters
- Module Structure
- Learning Path
- Cheat Sheets
- Key Takeaways
- Common Pitfalls
- Pro Tips
- Performance Benchmarks
- Related Experiments
- Assessment
- Related Topics
- Prerequisites
Overview¶
Maximizing constrained VRAM for 8B-70B model execution.
This phase covers quantization techniques to run larger models on limited hardware, enabling you to:
- Run 70B parameter models on consumer GPUs (RTX 3090/4090)
- Reduce memory footprint by 4-8x while maintaining accuracy
- Achieve faster inference through optimized formats
- Deploy models on CPU-only systems
Why Quantization Matters¶
The Memory Challenge¶
Model Size Comparison (FP16):
| Model | Parameters | FP16 Size | Typical GPU |
|---|---|---|---|
| Gemma-3-4B | 4B | 8 GB | RTX 3060 |
| Gemma-3-12B | 12B | 24 GB | RTX 3090 |
| Llama-3.1-70B | 70B | 140 GB | A100 40GB |
| CodeLlama-34B | 34B | 68 GB | A100 80GB |
| Mixtral-8x7B | 46.7B | 93.4 GB | 2xA100 |
After 4-bit Quantization:
| Model | Q4 Size | Reduction | Can Run On |
|---|---|---|---|
| Gemma-3-4B | 3.3 GB | 2.4x | RTX 3060 |
| Gemma-3-12B | 8.1 GB | 3.0x | RTX 3090 |
| Llama-3.1-70B | 43 GB | 3.3x | 2× RTX 4090 |
| CodeLlama-34B | 21 GB | 3.2x | RTX 4090 |
| Mixtral-8x7B | 29 GB | 3.2x | RTX 6000 |
Performance vs Accuracy Trade-offs¶
graph LR
A[FP32] -->|"100% Accuracy"| B[INT8]
B -->|"98% Accuracy"| C[INT4]
C -->|"95% Accuracy"| D[Q4_K_M]
D -->|"92% Accuracy"| E[Q4_K_S]
A[FP32] -->|1x Speed| B[INT8]
B -->|2x Speed| C[INT4]
C -->|3x Speed| D[Q4_K_M]
D -->|3.5x Speed| E[Q4_K_S]
style A fill:#f88
style E fill:#8f8
Module Structure¶
[4100] Low-Bit Quantization¶
| Document | Description | Time | Difficulty |
|---|---|---|---|
| 4101: GGUF Physics | CPU/GPU hybrid inference | 4h | Intermediate |
| 4102: EXL2 and AWQ | Extreme quantization for VRAM-only | 4h | Advanced |
| 4103: Double Quantization | BitsAndBytes 4-bit loading | 4h | Intermediate |
| 4104: MXFP4 and NVFP4 | Microscaling 4-bit: the gpt-oss formats | 4h | Advanced |
What You'll Learn:
- GGUF format internals and optimization strategies
- EXL2 quantile-based quantization for maximum accuracy
- AWQ activation-aware weight quantization
- Double quantization for memory efficiency
- MXFP4 and NVFP4 microscaling formats as working code
Hands-On Practice:
- Convert models to GGUF format
- Optimize EXL2 calibration datasets
- Implement AWQ quantization pipelines
- Apply double quantization to large models
- Quantize one weight MXFP4 and NVFP4 and compare the error
[4200] KV Cache Optimization¶
| Document | Description | Time | Difficulty |
|---|---|---|---|
| 4201: Context Window Physics | OOM errors in long sequences | 4h | Advanced |
| 4202: Speculative Decoding | Accelerating with draft models | 4h | Advanced |
| 4204: KV-Cache Paging and Prefix Caching | PagedAttention blocks, prefix caching, budgeted prefill | 4h | Advanced |
| 4203: Context Optimization | KV cache optimization | 6h | Intermediate |
What You'll Learn:
- KV cache memory management strategies
- Speculative decoding for 2-3x speedup
- Context window extension techniques
- PagedAttention for long sequences
Hands-On Practice:
- Implement KV cache quantization
- Set up speculative decoding pipelines
- Extend context windows beyond 32k
- Optimize memory for long conversations
[4300] Quantization Aware Training (QAT)¶
| Document | Description | Time | Difficulty |
|---|---|---|---|
| 4301: QAT Foundations | Fake quantization & calibration | 4h | Advanced |
| 4302: Fake Quantization | Simulated quantization during training | 4h | Advanced |
| 4303: QAT for Transformers | Layer-wise quantization strategies | 4h | Advanced |
| 4304: Low-bit QAT | Training with 4-bit weights | 4h | Advanced |
| 4305: Quantization Configuration | AutoGPTQ, GPTQ, AWQ configs | 4h | Intermediate |
What You'll Learn:
- Quantization-aware training workflows
- Fake quantization for seamless deployment
- Layer-wise quantization strategies
- Low-bit training (2-bit, 3-bit, 4-bit)
Hands-On Practice:
- Implement QAT pipelines for transformers
- Apply layer-wise quantization
- Train with fake quantization
- Deploy QAT models to production
[4400] Advanced Quantization Techniques¶
| Document | Description | Time | Difficulty |
|---|---|---|---|
| 4401: GPTQ | Accurate post-training quantization | 5h | Advanced |
| 4402: AWQ | Activation-aware quantization | 5h | Advanced |
| 4403: GGUF Format | llama.cpp file format and ecosystem | 5h | Intermediate |
| 4404: EXL2 Format | ExLlamaV2 format for GPU inference | 5h | Intermediate |
| 4405: Sparsity + Quantization | Combining pruning with quantization | 4h | Advanced |
| 4406: 1.58-bit Quantization | The frontier of extreme quantization | 3h | Advanced |
| 4407: Ternary & Binary Networks | {-1, 0, +1} weights | 4h | Advanced |
| 4408: Quantizing for Production | End-to-end quantization workflow | 3h | Advanced |
| 4409: Hardware-Specific Optimization | CPU, GPU, NPU, mobile | 3h | Advanced |
What You'll Learn:
- GPTQ for accurate post-training quantization
- AWQ activation-aware quantization
- GGUF and EXL2 ecosystem internals
- Extreme quantization (1.58-bit, ternary, binary)
- Hardware-specific optimizations
Hands-On Practice:
- Apply GPTQ to large language models
- Implement AWQ quantization pipelines
- Convert models to GGUF and EXL2
- Apply sparsity + quantization for 10x compression
- Optimize for specific hardware (NVIDIA, AMD, Apple Silicon)
Learning Path¶
Recommended Flow¶
graph TD
A[Start: Phase 3 Complete] --> B{Your Goal?}
B -->|Run 70B on RTX 3090| C[4100: Low-Bit Quantization]
B -->|Extend Context Window| D[4200: KV Cache Optimization]
B -->|Train Quantized Models| E[4300: QAT]
B -->|Extreme Compression| F[4400: Advanced Techniques]
C --> G[4101: GGUF Physics]
G --> H[4102: EXL2/AWQ]
D --> I[4201: Context Window]
I --> J[4202: Speculative Decoding]
E --> K[4301: QAT Foundations]
K --> L[4303: QAT for Transformers]
F --> M[4401: GPTQ]
M --> N[4405: Sparsity]
N --> O[4406: 1.58-bit]
style C fill:#e1f5fe
style D fill:#fff3e0
style E fill:#f3e5f5
style F fill:#fce4ec
Time Estimates¶
| Module | Reading | Practice | Total |
|---|---|---|---|
| 4100: Low-Bit Quantization | 16h | 4h | 20h |
| 4200: KV Cache Optimization | 18h | 3h | 21h |
| 4300: QAT | 20h | 8h | 28h |
| 4400: Advanced Techniques | 37h | 12h | 49h |
| Total | 91h | 27h | 118h |
Cheat Sheets¶
Single-page references for the surfaces this phase teaches:
- CHEAT-SHEET-009: Local Inference with llama.cpp & Ollama
- CHEAT-SHEET-015: Quantization Formats & Methods
- CHEAT-SHEET-017: KV Cache
- CHEAT-SHEET-062: KV Offload and Tiered Caching - the Recompute Bill, the Tier Ladder, and the Routing Gate
- CHEAT-SHEET-018: Speculative Decoding
- CHEAT-SHEET-020: Flash Attention
- CHEAT-SHEET-021: RoPE Positional Encoding
- CHEAT-SHEET-034: SGLang RadixAttention Serving
- CHEAT-SHEET-042: Extreme Quantization - the Method Choice, the File Choice, the Training-Time Frontier
- CHEAT-SHEET-043: 4-Bit Residuals - the Metadata Squeeze and the Hardware-Native Grid
- CHEAT-SHEET-044: Quantization-Aware Training - the Straight-Through Estimator and the Layer Map
- Volume 4: Quantization & Optimization - Quick Reference - the full command companion for this phase
Key Takeaways¶
You Will Learn¶
After completing this phase, you will be able to:
-
Run Large Models on Consumer Hardware
- Deploy 70B+ parameter models on RTX 3090/4090
- Reduce memory footprint by 4-8x
- Maintain 92-98% of original accuracy
-
Choose the Right Quantization Format
- GGUF for CPU/GPU hybrid systems
- EXL2 for maximum GPU performance
- AWQ for balanced accuracy and speed
-
Extend Context Windows
- Apply KV cache quantization
- Implement speculative decoding
- Use PagedAttention for long sequences
- Manage the cache with block tables, prefix caching, and token budgets
-
Train Quantized Models
- Implement quantization-aware training
- Apply layer-wise quantization
- Deploy QAT models to production
-
Push to Extreme Quantization
- Apply sparsity + quantization for 10x compression
- Experiment with 1.58-bit quantization
- Explore ternary and binary networks
Common Pitfalls¶
Pitfall 1: Memory Issues¶
Pitfall: Underestimating VRAM requirements for quantization
# Wrong: Not enough VRAM for calibration
python quantize.py --model meta-llama/Llama-3.1-70B-Instruct --calibration
# Error: CUDA out of memory
# Right: Reserve adequate VRAM for calibration
python quantize.py \
--model meta-llama/Llama-3.1-70B-Instruct \
--calibration \
--max-batch-size 1 \
--max-vram 20GB
Pitfall 2: Accuracy Loss¶
Pitfall: Using too aggressive quantization without validation
# Wrong: Direct 4-bit quantization without calibration
model = quantize(model, bits=4)
# Result: Significant accuracy drop
# Right: Calibrate with representative data
model = quantize(
model,
bits=4,
calibration_data=representative_dataset,
calibration_steps=256
)
Pitfall 3: Format Compatibility¶
Pitfall: Choosing incompatible format for your hardware
- EXL2 → Requires CUDA (NVIDIA only)
- GGUF Q4_K → Requires AVX2 (x86_64 only)
- AWQ → Requires GPU with tensor cores
+ GGUF → Most versatile (CPU + GPU + ARM)
+ EXL2 → Best for NVIDIA GPUs
+ AWQ → Balanced choice
Pitfall 4: Calibration Data Quality¶
Pitfall: Using poor calibration data
# Wrong: Random data for calibration
calibration_data = random_tokens(n=256)
# Right: Representative samples from target domain
calibration_data = load_target_domain_samples(n=256, diversity=True)
# Ensures quantization captures important activation patterns
Pro Tips¶
VRAM Optimization¶
Tip: Use gradient checkpointing during quantization calibration
import torch
from torch.utils.checkpoint import checkpoint
def calibrate_with_checkpointing(model, data, batch_size=32):
# Enables larger batch sizes with limited VRAM
for i in range(0, len(data), batch_size):
batch = data[i:i+batch_size]
with torch.amp.autocast("cuda"):
_ = model(batch) # Forward with checkpointing
torch.cuda.empty_cache() # Clear cache between batches
Accuracy Recovery¶
Tip: Apply layer-wise quantization for better accuracy
# Quantize attention layers less aggressively
quant_config = {
'attention': {'bits': 8}, # Higher precision
'ffn': {'bits': 4}, # Lower precision
'output': {'bits': 8}, # Higher precision
}
Speed Optimization¶
Tip: Combine quantization with speculative decoding
# 2-3x speedup with minimal accuracy loss
quantized_model = quantize(model, bits=4)
draft_model = load_model('draft-256m') # Small draft model
# Use speculative decoder
output = speculative_decode(
model=quantized_model,
draft_model=draft_model,
max_length=2048
)
Format Selection Guide¶
graph TD
A[Choose Quantization Format] --> B{Your Hardware?}
B -->|NVIDIA GPU| C{GPU Memory?}
B -->|CPU Only| D[GGUF Q4_K_M]
B -->|Apple Silicon| E[GGUF Q4_K_M]
B -->|AMD GPU| F[GGUF Q4_K_M]
C -->|<12GB| G[GGUF Q4_K_M]
C -->|12-24GB| H[EXL2 4-bit]
C -->|>24GB| I[EXL2 4-bit or AWQ]
style D fill:#8f8
style E fill:#8f8
style F fill:#8f8
style G fill:#dfd
style H fill:#dfd
style I fill:#ffd
Production Deployment¶
Tip: Use multiple quantization levels for different use cases
# Production configuration
models:
realtime:
format: GGUF Q4_K_M
bits: 4
use_case: "Low-latency inference"
batch:
format: EXL2
bits: 4
use_case: "High-throughput batch"
edge:
format: GGUF Q3_K_M
bits: 3
use_case: "Edge deployment"
Performance Benchmarks¶
Quantization Format Comparison¶
| Format | Model Size | VRAM Required | Speed | Accuracy | Best For |
|---|---|---|---|---|---|
| FP16 | 140 GB | 140 GB | 1.0x | 100% | Training |
| INT8 | 70 GB | 70 GB | 1.8x | 99% | Production |
| GGUF Q4_K_M | 42 GB | 42 GB | 2.5x | 96% | CPU/GPU hybrid |
| EXL2 4-bit | 38 GB | 38 GB | 3.2x | 97% | NVIDIA GPU |
| AWQ 4-bit | 40 GB | 40 GB | 3.0x | 98% | Balanced |
| GPTQ 4-bit | 39 GB | 39 GB | 3.1x | 98% | Post-training |
Hardware-Specific Performance¶
| Hardware | Best Format | Max Model (4-bit) | Speed |
|---|---|---|---|
| RTX 3060 (12GB) | GGUF Q4_K_M | Gemma-3-12B | 20 t/s |
| RTX 3090 (24GB) | EXL2 4-bit | Llama-3.1-70B | 35 t/s |
| RTX 4090 (24GB) | EXL2 4-bit | Llama-3.1-70B | 45 t/s |
| A100 40GB | EXL2 4-bit | Llama-3.1-70B | 80 t/s |
| A100 80GB | AWQ 4-bit | Llama-3.1-70B | 120 t/s |
| Apple M2 | GGUF Q4_K_M | Gemma-3-12B | 15 t/s |
Related Experiments¶
Hands-on Practice¶
-
- Convert Llama 3.1 to GGUF format
- Benchmark GGUF vs FP16
- Optimize GGUF settings
-
- Apply EXL2 quantization
- Compare EXL2 vs AWQ accuracy
- Optimize calibration datasets
-
- Extend context to 128k
- Implement KV cache quantization
- Benchmark memory usage
-
EXP_4202: Speculative Decoding
- Implement speculative decoding
- Benchmark speed improvements
- Tune draft model quality
-
4204: KV-Cache Paging and Prefix Caching
- Price contiguous versus paged reservation on eight concurrent sequences
- Trace a block table across appends into a fourth physical block
- Chunk a 2,048-token prefill under a 512-token iteration budget
Prerequisites¶
Before starting this phase, ensure you understand:
- PyTorch Basics (from 2200: Frameworks)
- Transformer Architecture (from 3400: Architectures)
- Model Training Loops (from 2400: Pretraining)
- Basic Linear Algebra (from 2100: Calculus)
See PREREQUISITES for details.
Assessment¶
Validate your knowledge with:
- Phase 4 Checkpoint - Module-by-module phase-exit review
- Phase 4 Quiz - Test your understanding (30 questions, 80% to pass)
- Phase 4 Practice - Hands-on exercises
Related Topics¶
- 4100: Low-bit Quantization - GGUF, EXL2, AWQ basics; MXFP4/NVFP4 microscaling formats
- 4200: KV Cache - Quantizing attention cache
- 4300: QAT - Quantization aware training
- 2300: Framework Engineering - Building quantization into frameworks
Next Steps¶
After completing this phase:
-
Apply to Your Projects
- Quantize your fine-tuned models
- Deploy to production with optimized formats
- Monitor accuracy and performance
-
Continue Learning
- Phase 5: Fine-tune quantized models
- Phase 6: Build RAG systems
- Phase 7: Create agentic AI systems
Module Duration: 118 hours (91 reading + 27 practice)
Difficulty: ⭐⭐⭐ Advanced
Ready to maximize your VRAM? Start with 4101: GGUF Physics or 4102: EXL2 and AWQ