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.

Advanced

Prerequisites

Table of Contents

  1. Learning Objectives
  2. Notation
  3. Core Intuition
  4. Low-Rank Factorization
  5. SVD-Based Compression
  6. LoRA as Inference Compression
  7. Weight Tying & Parameter Sharing
  8. Vocabulary Reduction
  9. Progressive Compression
  10. Combined Compression Strategies
  11. Common Pitfalls
  12. Summary
  13. Exercises

Learning Objectives

  1. Apply SVD to decompose weight matrices with controlled rank.
  2. Explain how LoRA enables post-hoc compression of fine-tuned models.
  3. Design vocabulary reduction strategies for domain-specific deployment.
  4. Construct progressive compression pipelines (pruning + quantization + distillation).
  5. Estimate quality-compression tradeoffs for a given model and budget.

Notation

  • WRm×n\mathbf{W} \in \mathbb{R}^{m \times n} — weight matrix
  • rr — target rank for decomposition
  • σi\sigma_iii-th singular value

Core Intuition

A trained weight matrix W\mathbf{W} is often approximately low-rank — most of its "information" lives in a small number of directions. By decomposing WUVT\mathbf{W} \approx \mathbf{U}\mathbf{V}^T with rank rmin(m,n)r \ll \min(m,n), we reduce parameters from mnmn to r(m+n)r(m+n) — potentially 4-8x compression with minimal quality loss. Combined with pruning and quantization, we achieve 10-50x total compression.

Knowledge Distillation for Compression

Teacher92%Student71%3.3× compression | 30% params
Student
0.30
TeacherStudent
Explore: KD compression trains a small student to mimic a large teacher's soft outputs — achieving 3-10× size reduction with ~95% of teacher accuracy.

Low-Rank Factorization

Replace WRm×n\mathbf{W} \in \mathbb{R}^{m \times n} with two smaller matrices:

WAB,ARm×r,BRr×n.(1)\mathbf{W} \approx \mathbf{A}\mathbf{B}, \quad \mathbf{A} \in \mathbb{R}^{m \times r}, \mathbf{B} \in \mathbb{R}^{r \times n}. \tag{1}

Parameter reduction:

  • Original: mnmn parameters.
  • Factorized: r(m+n)r(m+n) parameters.
  • Compression ratio: mnr(m+n)\frac{mn}{r(m+n)}.
  • For m=n=4096,r=256m=n=4096, r=256: compression = 40962256×8192=8×\frac{4096^2}{256 \times 8192} = 8\times.

Compute reduction: Wx\mathbf{W}\mathbf{x} costs mnmn FLOPs. A(Bx)\mathbf{A}(\mathbf{B}\mathbf{x}) costs rn+rm=r(m+n)rn + rm = r(m+n) — same compression ratio.


SVD-Based Compression

Truncated SVD: W=UΣVTUrΣrVrT\mathbf{W} = \mathbf{U}\boldsymbol{\Sigma}\mathbf{V}^T \approx \mathbf{U}_r\boldsymbol{\Sigma}_r\mathbf{V}_r^T.

Optimal rank-r approximation (Eckart-Young theorem):

WUrΣrVrTF=i=r+1min(m,n)σi2.(2)\|\mathbf{W} - \mathbf{U}_r\boldsymbol{\Sigma}_r\mathbf{V}_r^T\|_F = \sqrt{\sum_{i=r+1}^{\min(m,n)} \sigma_i^2}. \tag{2}

Explained variance ratio:

EVR(r)=i=1rσi2i=1min(m,n)σi2.(3)\text{EVR}(r) = \frac{\sum_{i=1}^r \sigma_i^2}{\sum_{i=1}^{\min(m,n)} \sigma_i^2}. \tag{3}

Practical: Choose rr such that EVR(r)>0.99(r) > 0.99 (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 rr.


LoRA as Inference Compression

During fine-tuning: W=W0+ΔW=W0+BA\mathbf{W}' = \mathbf{W}_0 + \Delta\mathbf{W} = \mathbf{W}_0 + \mathbf{B}\mathbf{A}.

For deployment: Merge: Wmerged=W0+BA\mathbf{W}_{\text{merged}} = \mathbf{W}_0 + \mathbf{B}\mathbf{A}.

  • No inference overhead (just a standard dense layer).
  • But: full parameter count restored.

Alternative — keep factored:

  • Store W0\mathbf{W}_0 in low precision (INT4) + BA\mathbf{B}\mathbf{A} in FP16.
  • During inference: y=W0INT4x+B(Ax)\mathbf{y} = \mathbf{W}_0^{\text{INT4}}\mathbf{x} + \mathbf{B}(\mathbf{A}\mathbf{x}).
  • 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:

Wembed=WoutputT.(4)\mathbf{W}_{\text{embed}} = \mathbf{W}_{\text{output}}^T. \tag{4}

For vocabulary V=128KV=128K, dimension d=4096d=4096: saves V×d=512MV \times d = 512M 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:

  1. Analyze target domain token frequency.
  2. Remove tokens with zero frequency in domain.
  3. 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:

  1. Structured pruning (remove channels/heads) → smaller architecture.
  2. Low-rank factorization (SVD on remaining layers) → fewer parameters.
  3. Quantization (INT8/INT4 on factored model) → smaller memory.
  4. 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

TechniqueCompressionQuality ImpactSpeed Impact
Pruning (50%)2x params1-3% loss1.5-2x faster
Low-rank (r/4)2-4x params1-2% loss2-3x faster
INT4 quantization4x memory1-2% loss2x faster
DistillationN/A (recovery)+1-2% recoveryN/A
Combined16-32x3-5% total loss6-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: WAB\mathbf{W} \approx \mathbf{AB}; 4-8x compression for rank-deficient layers.
  • SVD: Optimal rank-rr approximation; choose rr 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 σi=100/i\sigma_i = 100/i: find the rank rr 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?