Compression via Distillation & Low-Rank Factorization
Combining compression techniques: task-specific distillation for deployment, low-rank factorization (SVD, LoRA at inference), weight tying, vocabulary reduction, and progressive compression strategies for minimal quality loss.
Prerequisites
Table of Contents
- Learning Objectives
- Notation
- Core Intuition
- Low-Rank Factorization
- SVD-Based Compression
- LoRA as Inference Compression
- Weight Tying & Parameter Sharing
- Vocabulary Reduction
- Progressive Compression
- Combined Compression Strategies
- Common Pitfalls
- Summary
- Exercises
Learning Objectives
- Apply SVD to decompose weight matrices with controlled rank.
- Explain how LoRA enables post-hoc compression of fine-tuned models.
- Design vocabulary reduction strategies for domain-specific deployment.
- Construct progressive compression pipelines (pruning + quantization + distillation).
- Estimate quality-compression tradeoffs for a given model and budget.
Notation
- — weight matrix
- — target rank for decomposition
- — -th singular value
Core Intuition
A trained weight matrix is often approximately low-rank — most of its "information" lives in a small number of directions. By decomposing with rank , we reduce parameters from to — potentially 4-8x compression with minimal quality loss. Combined with pruning and quantization, we achieve 10-50x total compression.
Knowledge Distillation for Compression
Low-Rank Factorization
Replace with two smaller matrices:
Parameter reduction:
- Original: parameters.
- Factorized: parameters.
- Compression ratio: .
- For : compression = .
Compute reduction: costs FLOPs. costs — same compression ratio.
SVD-Based Compression
Truncated SVD: .
Optimal rank-r approximation (Eckart-Young theorem):
Explained variance ratio:
Practical: Choose such that EVR (retain 99% of information).
Per-layer rank selection: Different layers may have different effective ranks. Sensitivity analysis: measure loss increase per layer when compressed to rank .
LoRA as Inference Compression
During fine-tuning: .
For deployment: Merge: .
- No inference overhead (just a standard dense layer).
- But: full parameter count restored.
Alternative — keep factored:
- Store in low precision (INT4) + in FP16.
- During inference: .
- Quality of FP16 with memory closer to INT4.
QLoRA insight: Quantize base weights aggressively; keep LoRA in full precision. The LoRA compensates for quantization error.
Weight Tying & Parameter Sharing
Embedding tying: Share input embedding and output projection:
For vocabulary , dimension : saves parameters.
Cross-layer sharing (ALBERT): All transformer layers share parameters:
- 12-layer BERT → 1 set of parameters applied 12 times.
- 12x parameter reduction.
- Quality: 5-10% degradation but with 12x fewer parameters.
Partial sharing: Share attention parameters across layers, unique FFN per layer. Better quality/compression tradeoff.
Vocabulary Reduction
Problem: Vocabulary embedding is often 10-20% of total parameters. For multilingual models, many tokens are irrelevant for specific deployments.
Domain-specific reduction:
- Analyze target domain token frequency.
- Remove tokens with zero frequency in domain.
- Retrain embedding for remaining tokens.
Example: 128K general vocabulary → 32K domain-specific:
- Embedding reduction: 4x (512M → 128M parameters for 4096-dim).
- Quality: maintained for domain tasks; degraded for out-of-domain.
Progressive Compression
Apply compression techniques sequentially, each building on previous:
Recommended order:
- Structured pruning (remove channels/heads) → smaller architecture.
- Low-rank factorization (SVD on remaining layers) → fewer parameters.
- Quantization (INT8/INT4 on factored model) → smaller memory.
- Distillation (fine-tune compressed model with teacher guidance) → recover quality.
Each step: Apply → fine-tune → validate. Then proceed to next.
Typical results (70B → deployment):
- Pruning (50% heads): 70B → 40B effective.
- Low-rank (r=512): 40B → 25B parameters.
- Quantization (INT4): 25B × 4B → 12.5GB memory.
- Distillation: recover 1-2% lost accuracy.
- Total: 6x memory reduction with less than 3% quality loss.
Combined Compression Strategies
| Technique | Compression | Quality Impact | Speed Impact |
|---|---|---|---|
| Pruning (50%) | 2x params | 1-3% loss | 1.5-2x faster |
| Low-rank (r/4) | 2-4x params | 1-2% loss | 2-3x faster |
| INT4 quantization | 4x memory | 1-2% loss | 2x faster |
| Distillation | N/A (recovery) | +1-2% recovery | N/A |
| Combined | 16-32x | 3-5% total loss | 6-12x faster |
Common Pitfalls
Pitfall 1. Applying SVD without fine-tuning afterward. Raw SVD compression causes noticeable quality loss. Always fine-tune the factored model for a few hundred steps.
Pitfall 2. Same rank for all layers. Attention layers typically need higher rank (more complex transformations) than FFN layers (more redundant). Use per-layer rank selection.
Pitfall 3. Vocabulary reduction without verifying tokenization coverage. If important domain terms become out-of-vocabulary after reduction, the model can't process them. Always check coverage on domain test set.
Summary
- Low-rank factorization: ; 4-8x compression for rank-deficient layers.
- SVD: Optimal rank- approximation; choose by explained variance.
- LoRA for deployment: Keep base in INT4, adapter in FP16.
- Weight tying: Share embeddings across input/output; save 10-20% parameters.
- Vocabulary reduction: Remove unused tokens for domain deployment.
- Progressive compression: Prune → factorize → quantize → distill (10-30x total).
Exercises
Exercise 1. For a 4096×4096 weight matrix with singular values decaying as : find the rank needed for 99% explained variance.
Exercise 2. Compare the memory and compute of: (a) dense 4096×4096 in FP16, (b) factored rank-512 in FP16, (c) dense 4096×4096 in INT4.
Exercise 3. Design a progressive compression pipeline for LLaMA-7B targeting deployment on a smartphone (4GB RAM). Specify each step and expected quality.
Exercise 4. For ALBERT-style cross-layer sharing: prove that the effective capacity is NOT reduced by the sharing factor (layers can still learn different functions via different inputs).
Exercise 5. Compare vocabulary reduction strategies for deploying a general LLM to: (a) medical domain, (b) legal domain, (c) code-only. How much parameter savings in each case?