Library

Volume 2 · GenAI Foundations & LLM Architecture

Notebook
Sign in

Section 5

Model Compression & Optimization

534 words · about 2 min

5.1 Quantization Deep Dive

Precision Memory (70B Model) Quality Impact Use Case
FP16 ~140 GB Baseline Production on cloud GPUs
INT8 (GPTQ) ~70 GB Minimal (<1% degradation) Production, fewer GPUs
INT4 (AWQ/GPTQ) ~35 GB Small (1-3% degradation) Cost-sensitive production
GGUF Q4 ~35 GB Moderate (2-5%) Local/edge deployment
  • GPTQ: Post-training quantization using approximate second-order information to minimize per-layer error; strong INT4/INT8 accuracy retention on GPU inference.
  • AWQ (Activation-aware Weight Quantization): Protects the small subset of “salient” weights most impacted by activation outliers, generally outperforming naive GPTQ at very low bit-widths.
  • GGML/GGUF: Quantization format designed for efficient CPU/edge inference (llama.cpp ecosystem), enabling large models to run on consumer hardware with acceptable latency.

5.2 Pruning

Removes redundant or low-importance weights/neurons/attention heads from a trained model to reduce size and compute. Structured pruning (removing entire channels/heads) is hardware-friendly and yields real speedups; unstructured pruning (removing individual weights) achieves higher sparsity ratios but often requires specialized sparse-matrix kernels to realize speed gains. Typically combined with a fine-tuning “recovery” pass to restore quality after pruning.

5.3 Knowledge Distillation

Trains a smaller “student” model to mimic a larger “teacher” model’s outputs — the student learns from the teacher’s soft probability distributions, which carry more information than hard labels alone.

Example workflow:

  1. Use a large model (teacher) to process a representative dataset and generate high-quality outputs
  2. Fine-tune a small model (student, e.g., 8B) on these teacher outputs
  3. The student model achieves 90-95% of teacher quality at a fraction of the inference cost
  4. Deploy the distilled model for production, high-volume inference

Trade-off: Upfront cost to generate teacher outputs, but ongoing savings of 80%+ per inference call — often breaking even within weeks at moderate-to-high volume.

5.4 Speculative Decoding

Covered in Section 3.3 — a small draft model proposes multiple tokens, verified in parallel by the large model, yielding 2-3x latency improvement for interactive workloads.

Section 5 — Interview Q&A

Q: Explain model distillation and how you’d use it to reduce inference costs. A: Distillation trains a smaller student model to replicate a larger teacher model’s behavior, learning from the teacher’s soft output distributions rather than just hard labels. A practical workflow: process a representative volume of production data through the large teacher model to generate high-quality reference outputs, then fine-tune a small open-source model (e.g., an 8B model) on those outputs. The distilled model typically reaches 90-95% of the teacher’s quality at roughly 1/20th the per-inference cost, and can be deployed on modest self-hosted infrastructure. The main cost is generating the teacher-labeled dataset upfront, but at meaningful production volume this breaks even in weeks and then compounds into ongoing savings.

Q: Compare GPTQ, AWQ, and GGUF for model compression. A: All three are post-training quantization approaches but target different deployment contexts. GPTQ minimizes per-layer quantization error using approximate second-order (Hessian) information — strong general-purpose INT4/INT8 GPU quantization. AWQ instead protects the weights most sensitive to activation outliers, generally preserving more quality than GPTQ at very aggressive (e.g., INT4) bit-widths. GGUF (built for the llama.cpp ecosystem) is optimized for CPU and edge inference rather than raw GPU throughput, trading some quality for portability to consumer hardware. In practice: use GPTQ/AWQ for self-hosted GPU serving at scale, GGUF for local/edge/on-device deployment.