MLCrack

    ML SYSTEM DESIGN & MLOPS · INTERVIEW QUESTION

    What are the key techniques for making Transformers efficient in production? Explain KD, quantization, pruning, and graph optimization.

    🔴 advanced · Model Serving

    Hint: Think about the full optimization stack from model compression to runtime optimization.

    Answer

    Production transformer optimization stack:

    1. Knowledge Distillation: train a smaller student to mimic a larger teacher. Student achieves near-teacher accuracy with far fewer parameters. DistilBERT reduces BERT by ~40% retaining ~97% performance. Match soft logits (dark knowledge) and optionally intermediate features.

    2. Quantization: represent weights/activations with fewer bits (FP16→INT8→INT4). Post-training quantization (simple, some accuracy drop) vs QAT (simulates low-precision during training). 4-bit (QLoRA, GPTQ) allows billion-param models on single GPU. 2-8× memory reduction.

    3. Pruning: remove redundant weights. Unstructured (zero out small weights, harder for hardware) vs Structured (remove entire attention heads/neurons/layers, hardware-friendly). Often combined with retraining.

    4. Graph Optimization: operator fusion (MatMul+BiasAdd+LayerNorm into one kernel), kernel specialization (TensorRT, ONNX Runtime), static graph optimization. Lowers latency 20-40%.

    Combined: this stack yields 3-10× faster inference, 2-5× lower memory, often <1% accuracy drop.