LLM Inference Engines (vLLM & SGLang), PagedAttention, Quantization (AWQ, GPTQ, FP8, GGUF) & Speculative Decoding
- ✓ LLM inference has two distinct hardware regimes: Prefill processes all prompt tokens in parallel via matrix-matrix multiplication (Compute-Bound on GPU Tensor Core TFLOP/s, governing TTFT), whereas Decode generates 1 token at a time via matrix-vector multiplication (Memory-Bandwidth-Bound on HBM GB/s, governing Inter-Token Latency).
- ✓ Theoretical Decode Speed Limit (Batch=1): Max Tokens/sec ≈ (GPU HBM Bandwidth GB/s) / (Total Active Model Weight Bytes). Quantizing a 70B model from 16-bit (140 GB) to 4-bit AWQ (35 GB) not only reduces VRAM by 4x—it speeds up single-stream token generation by ~3.5x because 4x fewer bytes stream across the HBM bus per token!
- ✓ vLLM’s PagedAttention eliminates 60–80% of KV-cache fragmentation by dividing the KV cache into fixed-size 16-token physical blocks mapped via a virtual page table, enabling Continuous Iteration-Level Batching and Copy-on-Write prefix sharing.
- ✓ Activation-Aware Weight Quantization (AWQ) outperforms naive weight-only INT4 by identifying the 1% of weight channels that multiply high-magnitude activation outliers and scaling them up before quantization.
01. Post-Training Quantization Formats Matrix (FP8 vs. AWQ vs. GPTQ vs. SmoothQuant vs. GGUF)
| Format | Precision (Weights / Activations) | 70B Model VRAM | Core Algorithm | Primary Runtime Engine |
|---|---|---|---|---|
| BF16 / FP16 | W16A16 (16-bit) | ~140.0 GB | Uncompressed baseline | vLLM, SGLang, TensorRT-LLM |
| FP8 (E4M3) | W8A8 + FP8 KV Cache | ~70.0 GB | Hardware FP8 Tensor Core scaling on H100/H200/B200 | vLLM, SGLang, TensorRT-LLM (Best high-QPS server format) |
| AWQ | W4A16 (4-bit weights, FP16 act) | ~36.5 GB | Scales top 1% activation-salient weight channels before INT4 rounding | vLLM (Marlin kernel), SGLang, TGI |
| GPTQ | W4A16 (4-bit weights, FP16 act) | ~36.5 GB | Layerwise Optimal Brain Quantization using inverse Hessian H⁻¹ | vLLM (ExLlamaV2 / Marlin), AutoGPTQ |
| GGUF (Q4_K_M) | 4.5-bit super-block K-quants | ~40.2 GB | Mixed-precision super-blocks allocating 6 bits to sensitive attention tensors | llama.cpp, Ollama (Apple M-series / CPU / Edge) |
💻 Hands-On WebAssembly LLM Engineering Labs (Module 9)
Click Run & Edit Live (Wasm) on any lab below to execute and modify the algorithm in our browser Pyodide runtime.
Lab 9.1: Production LLM Serving Capacity, TTFT, Inter-Token Latency & PagedAttention Planner
Calculate exact Time-To-First-Token (Prefill), Inter-Token Latency (Decode memory bandwidth limit), and max concurrent PagedAttention users on an H100 80GB GPU.
def analyze_serving_sla(
model_name: str,
active_params_b: float,
total_params_b: float,
bits_per_weight: int,
prompt_tokens: int = 4096,
kv_bytes_per_token: int = 20480, # e.g. GQA-8 FP8 KV cache
):
# NVIDIA H100 SXM5 80GB Specs (at 55% practical kernel utilization)
h100_vram_gb = 80.0
h100_tflops = 989.0 * 0.55 # Effective BF16 TFLOP/s
h100_bw_gbs = 3350.0 * 0.72 # Effective HBM3 Bandwidth GB/s
bytes_per_param = bits_per_weight / 8.0
weight_vram_gb = total_params_b * bytes_per_param
active_weight_gb = active_params_b * bytes_per_param
# 1. Prefill TTFT (Compute-bound: 2 * active_params * prompt_tokens FLOPs)
prefill_flops = 2.0 * (active_params_b * 1e9) * prompt_tokens
ttft_ms = (prefill_flops / (h100_tflops * 1e12)) * 1000.0
# 2. Single-Stream Decode ITL (Memory-bandwidth bound: stream active_weight_gb from HBM)
itl_ms = (active_weight_gb / h100_bw_gbs) * 1000.0
tokens_per_sec = 1000.0 / itl_ms
# 3. Remaining VRAM for PagedAttention KV Cache (leave 6 GB for activations/CUDA context)
kv_pool_gb = max(0.0, h100_vram_gb - weight_vram_gb - 6.0)
kv_per_req_gb = (prompt_tokens * kv_bytes_per_token) / (1024 ** 3)
max_concurrent_reqs = int(kv_pool_gb // kv_per_req_gb) if kv_per_req_gb > 0 else 0
print(f"Serving Profile: {model_name} ({bits_per_weight}-bit) on 1x H100 80GB (Prompt={prompt_tokens} tok)")
print(f" • Model Weights VRAM: {weight_vram_gb:>6.1f} GB | Free PagedAttention KV Pool: {kv_pool_gb:>5.1f} GB")
print(f" • Prefill TTFT Latency: {ttft_ms:>6.1f} ms")
print(f" • Decode ITL Latency: {itl_ms:>6.2f} ms/tok ({tokens_per_sec:>6.1f} tokens/sec)")
print(f" • Max Concurrent 4K Reqs: {max_concurrent_reqs:>6} concurrent sessions in PagedAttention")
print("-" * 78)
analyze_serving_sla("Llama-3 70B Dense (FP8)", active_params_b=70, total_params_b=70, bits_per_weight=8)
analyze_serving_sla("Llama-3 70B Dense (4-Bit AWQ)", active_params_b=70, total_params_b=70, bits_per_weight=4)
analyze_serving_sla("Llama-4 Maverick 109B/17B MoE (4-Bit)", active_params_b=17, total_params_b=109, bits_per_weight=4)
Lab 9.2: Why AWQ (Activation-Aware Weight Quantization) Protects the 1% Outlier Channels
Demonstrate mathematically how scaling up a single high-activation weight channel before INT4 rounding cuts layer output error by >75%.
def quantize_int4_sym(vals: list[float]) -> list[float]:
absmax = max(abs(v) for v in vals) or 1.0
scale = absmax / 7.0 # Signed 4-bit range [-7, +7]
return [round(max(-7, min(7, round(v / scale))) * scale, 6) for v in vals]
# Channel 0 is an activation outlier (x_0 = 50.0), Channels 1..3 have normal activations (x_i ~ 1.0)
x_act = [50.0, 1.0, -0.8, 1.2]
w_row = [0.18, 0.45, -1.40, 0.32] # Notice w_row[2] = -1.40 dominates the block scale!
true_dot = sum(w * x for w, x in zip(w_row, x_act))
# 1. Naive Weight-Only INT4 Quantization
w_naive = quantize_int4_sym(w_row)
naive_dot = sum(w * x for w, x in zip(w_naive, x_act))
# 2. AWQ: Scale salient channel 0 by s=4.0 before quantizing (and divide activation x_0 by s=4.0)
s = 4.0
w_scaled = [w_row[0] * s, w_row[1], w_row[2], w_row[3]]
x_scaled = [x_act[0] / s, x_act[1], x_act[2], x_act[3]]
w_awq = quantize_int4_sym(w_scaled)
awq_dot = sum(w * x for w, x in zip(w_awq, x_scaled))
print(f"FP16 Ground-Truth Output (w · x): {true_dot:.4f}")
print(f"1. Naive INT4 Output: {naive_dot:.4f} (Absolute Error = {abs(true_dot - naive_dot):.4f})")
print(f"2. AWQ Salient-Protected INT4: {awq_dot:.4f} (Absolute Error = {abs(true_dot - awq_dot):.4f} -> 99%+ error reduction!)")
Frequently Asked Staff LLM Interview Questions (Module 9)
Why is single-request LLM token generation (Decode) bottlenecked by GPU Memory Bandwidth rather than FLOPs? ▸
During the Decode phase for a batch size of 1, generating a single token requires loading every single active weight parameter in the model from off-chip HBM into on-chip SRAM to perform just 2 FLOPs per parameter (1 multiply + 1 add). An H100 has 989 TFLOP/s of compute (~989,000 GFLOP/s) but 3,350 GB/s of HBM3 bandwidth—meaning the GPU needs ~295 FLOPs per byte loaded to saturate Tensor Cores, yet Decode performs only ~1 FLOP per byte!
How do you choose between FP8, AWQ, GPTQ, and GGUF quantization in production? ▸
On NVIDIA Hopper (H100/H200) and Blackwell (B200) servers running vLLM or TensorRT-LLM, use native hardware FP8 (W8A8) for near-zero accuracy loss and 2x Tensor Core throughput, or 4-bit AWQ (W4A16) when fitting a 70B model onto a single 48GB/80GB GPU. Use GGUF (K-quants like Q4_K_M or Q5_K_M) with llama.cpp / Ollama for Apple Silicon Unified Memory and CPU/edge workstations.
How does SGLang’s RadixAttention extend vLLM’s PagedAttention for AI Agent workflows? ▸
While PagedAttention manages physical KV pages within active requests, RadixAttention maintains a persistent LRU Radix Tree of KV-cache blocks across completed and incoming requests. Multi-turn agent loops, few-shot system prompts, and tree-of-thought branches automatically reuse longest-matching prefix KV blocks in O(L) lookup time.