Emerging Directions in Machine Learning
Efficient ML
Read aloud is unavailable in this browser.
The Quest for Computational Efficiency
The remarkable capabilities of modern AI systems come at equally remarkable computational costs. Training GPT-4 reportedly required compute measured in tens of millions of dollars; inference for large language models demands specialized hardware and consumes substantial energy. As AI becomes ubiquitous—running on phones, embedded devices, and serving billions of users—efficiency becomes not just an optimization but a requirement.
Efficient ML is the research agenda dedicated to achieving the same (or better) capabilities with less compute, memory, and energy. This encompasses techniques applied across the entire ML pipeline: efficient architectures that compute less, training methods that converge faster, inference optimizations that speed up serving, and specialized hardware that performs more operations per watt.
The stakes are significant. Efficiency determines:
- Whether AI can run on edge devices (phones, sensors, vehicles)
- Whether companies can afford to serve AI to millions of users
- Whether AI development remains accessible beyond a few large labs
- Whether AI's environmental footprint is sustainable
By the end of this page, you will understand the key techniques for efficient ML including quantization, pruning, and knowledge distillation; efficient neural network architectures designed for speed and low resource usage; training efficiency methods that reduce compute requirements; hardware-software co-design for ML acceleration; and the tradeoffs involved in making ML systems more efficient.
The Efficiency Imperative
Understanding why efficiency matters requires grasping the scale of computation in modern ML and the constraints that limit deployment.
The Scale of Compute
Modern AI training and inference consume extraordinary resources:
- GPT-4 Training: Estimated at ~$100M in compute costs
- LLM Inference: A single ChatGPT query may require billions of floating-point operations
- Large-scale serving: Millions of queries per day at major tech companies
- Training compute doubling: Training compute for frontier models has doubled roughly every 6 months
At these scales, even small efficiency improvements translate to millions of dollars saved and significant environmental impact reduced.
Deployment Constraints
Real-world deployment imposes hard constraints:
Edge Devices:
- Limited memory (2-8GB on smartphones)
- Limited compute (mobile GPUs/NPUs)
- Battery constraints (power consumption matters)
- Latency requirements (real-time applications)
Cloud Economics:
- GPU/TPU costs dominate serving expenses
- Every millisecond of latency reduces user engagement
- Carbon footprint and sustainability concerns
- Availability of specialized hardware
Accessibility:
- Many organizations can't afford massive compute budgets
- Efficiency democratizes access to AI capabilities
- Enables research without massive resources
| Stage | Efficiency Challenge | Key Techniques |
|---|---|---|
| Architecture Design | Reduce compute per forward pass | Efficient attention, MoE, pruning-aware design |
| Training | Reduce total training compute | Mixed precision, gradient checkpointing, curriculum learning |
| Compression | Reduce model size | Quantization, pruning, distillation |
| Inference | Reduce latency and throughput | Batching, caching, speculative decoding |
| Hardware | Maximize ops/watt | Custom accelerators, sparsity support |
Efficiency research runs counter to the 'bigger is better' trend that has dominated recent ML. While scaling laws show that larger models trained on more data generally perform better, efficiency research asks: can we achieve the same capability with less? Often the answer is yes—through clever architecture design, training, and compression.
Quantization
Quantization reduces model size and accelerates inference by using lower-precision numerical representations. Instead of 32-bit floating-point (FP32) weights and activations, quantized models use 16-bit (FP16/BF16), 8-bit (INT8), or even 4-bit (INT4) or lower representations.
Why Quantization Works
Neural networks are surprisingly robust to precision reduction:
- Weights and activations often have limited dynamic range
- Noise tolerance built up during training accommodates quantization noise
- Overparameterization provides redundancy that absorbs precision loss
- Fine-grained patterns often aren't essential for task performance
Quantization Approaches
Post-Training Quantization (PTQ): Quantize a trained model without additional training:
- Dynamic quantization: Quantize weights offline; quantize activations on-the-fly during inference
- Static quantization: Calibrate quantization ranges on representative data; fix all quantization parameters
- Per-tensor vs per-channel: Finer-grained quantization (per-channel) preserves more precision
PTQ is simple but can degrade performance, especially at very low bit-widths.
Quantization-Aware Training (QAT): Simulate quantization during training, allowing the model to adapt:
- Insert fake quantization nodes that quantize then dequantize
- Use straight-through estimator for backward pass (gradients ignore quantization)
- Train normally; the model learns to be robust to quantization error
QAT typically preserves accuracy better than PTQ but requires retraining.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
# Conceptual quantization implementationimport torchimport torch.nn as nnimport torch.nn.functional as F def quantize_tensor(x: torch.Tensor, num_bits: int = 8) -> torch.Tensor: """ Symmetric quantization of tensor to num_bits. Maps floating-point values to integers in [-2^(b-1), 2^(b-1)-1] then back to floating point (simulating quantization effects). """ qmin = -(2 ** (num_bits - 1)) qmax = 2 ** (num_bits - 1) - 1 # Compute scale factor (per-tensor symmetric quantization) max_val = x.abs().max() scale = max_val / qmax if max_val > 0 else 1.0 # Quantize: round to integer, clip to valid range x_int = torch.round(x / scale).clamp(qmin, qmax) # Dequantize: convert back to float x_quant = x_int * scale return x_quant class QuantizedLinear(nn.Module): """ Linear layer with weight quantization. In practice, weights are stored as integers and dequantized during computation. Here we simulate this for illustration. """ def __init__(self, in_features: int, out_features: int, weight_bits: int = 8, activation_bits: int = 8): super().__init__() self.weight = nn.Parameter(torch.randn(out_features, in_features)) self.bias = nn.Parameter(torch.zeros(out_features)) self.weight_bits = weight_bits self.activation_bits = activation_bits def forward(self, x: torch.Tensor) -> torch.Tensor: # Quantize weights (simulating stored low-precision weights) weight_q = quantize_tensor(self.weight, self.weight_bits) # Quantize input activations x_q = quantize_tensor(x, self.activation_bits) # Matrix multiply (in practice, uses integer arithmetic) output = F.linear(x_q, weight_q, self.bias) return output class StraightThroughEstimator(torch.autograd.Function): """ Straight-through estimator for quantization-aware training. Forward: Apply quantization Backward: Pass gradients through unchanged (as if no quantization) """ @staticmethod def forward(ctx, x: torch.Tensor, num_bits: int = 8) -> torch.Tensor: return quantize_tensor(x, num_bits) @staticmethod def backward(ctx, grad_output: torch.Tensor) -> torch.Tensor: # Gradient passes through unchanged return grad_output, NoneModern Quantization for LLMs
Recent work has pushed quantization to extreme low bit-widths for large language models:
GPTQ: Data-free quantization using approximate second-order information:
- Quantize weights one column at a time
- Adjust remaining weights to compensate for quantization error
- Achieves good 4-bit quantization for LLMs
AWQ (Activation-aware Weight Quantization):
- Observes that a small subset of weights are critical (activated by important inputs)
- Scales these critical weights up before quantization, preserving precision where it matters
- State-of-the-art 4-bit and 3-bit quantization
GGML/llama.cpp Quantization:
- Block-wise quantization schemes (e.g., Q4_0, Q5_K_M)
- Optimized for CPU inference
- Enables running large models on consumer hardware
Mixed-Precision Quantization:
- Different layers or components at different precisions
- Sensitive layers (first, last, attention) at higher precision
- Less sensitive layers (feed-forward middle layers) at lower precision
Quantization Speedups:
- INT8 on modern GPUs: ~2x speedup over FP16
- INT4/INT3: Higher speedups but typically requires specialized kernels
- Memory reduction enables larger batch sizes, further increasing throughput
Advances in quantization have enabled running models like LLaMA-65B on consumer GPUs (24GB) through 4-bit quantization—models that would otherwise require 130+ GB at FP16. This has democratized access to large language models and enabled local, private inference on consumer hardware.
Pruning and Sparsity
Pruning removes unnecessary weights or structures from neural networks, creating sparse models that require less compute and memory. The key insight is that many parameters in neural networks are redundant or unimportant.
Types of Pruning
Unstructured (Weight) Pruning: Remove individual weights, creating irregular sparsity patterns:
- Most flexible: can prune any weight
- Achieves highest sparsity levels (90%+ zeros)
- Challenge: Requires hardware support for sparse computation
Structured Pruning: Remove entire structures (neurons, channels, layers, attention heads):
- Maintains regular dense matrices after pruning
- Works efficiently on standard hardware
- Typically lower achievable sparsity than unstructured
Block Sparsity: Remove regular blocks of weights (e.g., 4×4 blocks):
- Compromise between unstructured and structured
- Supported by some modern hardware (NVIDIA Ampere's 2:4 sparsity)
When to Prune
Post-Training Pruning:
- Train a dense model
- Identify unimportant weights (magnitude, gradient, etc.)
- Remove identified weights
- Fine-tune to recover accuracy
During-Training Pruning:
- Gradually increase sparsity during training
- Model adapts to pruning progressively
- Often better final accuracy
Importance Metrics
How do we decide which weights to prune?
Magnitude-Based:
Smaller weights contribute less to outputs:
importance(w) = |w|
Simple and often effective, but assumes magnitude reflects importance.
Gradient-Based:
Weights with small gradients matter less for the objective:
importance(w) = |w × ∂L/∂w|
Captures interaction between weight value and its sensitivity.
Hessian-Based:
Second-order information captures the loss increase from removing a weight:
importance(w) ≈ ½ w² H_{ww}
More accurate but computationally expensive.
Lottery Ticket Hypothesis
Frankle and Carlin's Lottery Ticket Hypothesis (2019) revealed a surprising property:
- Dense networks contain sparse subnetworks ('winning tickets') that can train to full accuracy
- These subnetworks exist at initialization
- Finding them requires training, pruning, and resetting weights to initial values
This suggests pruning doesn't just compress; it finds the essential structure hidden in overparameterized networks.
Sparse Training
Rather than pruning after training, train sparse models from the start:
SET (Sparse Evolutionary Training):
- Start with random sparse connectivity
- Periodically prune weak connections and grow new random ones
- Connectivity evolves during training
RigL:
- Dynamic sparse training with gradient-guided regrowth
- Prune smallest magnitudes, grow where gradients are largest
- Achieves strong results at high sparsity
A major challenge is converting theoretical sparsity to practical speedups. Unstructured 90% sparsity should give 10× speedup, but irregular memory access patterns often prevent this on GPUs designed for dense computation. Hardware-software co-design (structured sparsity, specialized kernels) is key to realizing sparsity benefits.
Knowledge Distillation
Knowledge distillation trains small, efficient 'student' models to mimic the behavior of large, accurate 'teacher' models. Rather than training on hard labels alone, students learn from the teacher's softer output distributions, which contain richer information about class relationships.
The Core Idea
A trained teacher model produces soft probability distributions over classes:
- Correct class: 0.85
- Similar class A: 0.10
- Similar class B: 0.04
- Other classes: 0.01 total
These soft labels contain more information than hard labels:
- Class relationships: A and B are more similar to the correct class
- Confidence levels: Some examples are easier than others
- Label smoothing: Soft targets reduce overfitting
Distillation Loss
The standard distillation objective combines:
-
Hard label loss: Cross-entropy with true labels
L_hard = CE(student_logits, true_labels) -
Soft label loss: KL divergence from teacher's softened distribution
L_soft = KL(softmax(student_logits/T), softmax(teacher_logits/T)) -
Combined:
L = α L_hard + (1-α) L_soft
The temperature T (typically 3-10) softens distributions further, revealing more structure in the teacher's outputs.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
# Knowledge Distillation Implementationimport torchimport torch.nn as nnimport torch.nn.functional as Ffrom typing import Tuple class DistillationLoss(nn.Module): """ Knowledge distillation loss combining hard labels and soft targets. L = alpha * CE(student, labels) + (1-alpha) * KL(student_soft, teacher_soft) """ def __init__(self, temperature: float = 4.0, alpha: float = 0.5): super().__init__() self.temperature = temperature self.alpha = alpha self.ce_loss = nn.CrossEntropyLoss() self.kl_loss = nn.KLDivLoss(reduction='batchmean') def forward( self, student_logits: torch.Tensor, teacher_logits: torch.Tensor, labels: torch.Tensor, ) -> Tuple[torch.Tensor, dict]: """ Compute distillation loss. Args: student_logits: Raw logits from student model teacher_logits: Raw logits from teacher model (detached) labels: Ground truth labels Returns: Total loss and dict of component losses """ # Hard label loss: student vs ground truth hard_loss = self.ce_loss(student_logits, labels) # Soft label loss: student vs teacher (both softened by temperature) student_soft = F.log_softmax(student_logits / self.temperature, dim=-1) teacher_soft = F.softmax(teacher_logits / self.temperature, dim=-1) soft_loss = self.kl_loss(student_soft, teacher_soft) # Scale by T^2 as gradients are 1/T^2 smaller with temperature soft_loss = soft_loss * (self.temperature ** 2) # Combined loss total_loss = self.alpha * hard_loss + (1 - self.alpha) * soft_loss return total_loss, { 'hard_loss': hard_loss.item(), 'soft_loss': soft_loss.item(), 'total_loss': total_loss.item(), } def distill_model( teacher: nn.Module, student: nn.Module, train_loader: torch.utils.data.DataLoader, epochs: int = 10, temperature: float = 4.0, alpha: float = 0.5, learning_rate: float = 1e-3,): """Train student model via distillation from teacher.""" teacher.eval() # Teacher in eval mode (no updates) student.train() criterion = DistillationLoss(temperature=temperature, alpha=alpha) optimizer = torch.optim.Adam(student.parameters(), lr=learning_rate) for epoch in range(epochs): total_loss = 0.0 for inputs, labels in train_loader: # Get teacher predictions (no gradients needed) with torch.no_grad(): teacher_logits = teacher(inputs) # Get student predictions student_logits = student(inputs) # Compute distillation loss loss, loss_dict = criterion(student_logits, teacher_logits, labels) # Update student optimizer.zero_grad() loss.backward() optimizer.step() total_loss += loss.item() print(f"Epoch {epoch+1}: Loss = {total_loss / len(train_loader):.4f}") return studentAdvanced Distillation Techniques
Feature/Intermediate Distillation: Match not just final outputs but intermediate representations:
- FitNets: Align hidden layer activations
- Attention transfer: Match attention maps between teacher and student
- Enables smaller students to mimic teacher's internal processing
Self-Distillation: A model distills knowledge from itself:
- Born-Again Networks: Train identical architectures from scratch using previous version as teacher
- Deep self-distillation: Earlier layers learn from deeper layers
- Often improves accuracy even without compression
Data-Free Distillation: Distill without access to original training data:
- Generate synthetic data to query the teacher
- Use generative models or adversarial training
- Important when original data is unavailable due to privacy or licensing
Distillation in Modern LLMs
Distillation is key to deploying efficient language models:
- DistilBERT: 40% smaller than BERT with 97% of performance
- TinyLLaMA: 1.1B model trained with distillation from larger LLaMA variants
- Instruction distillation: Smaller models trained on outputs from GPT-4 or Claude
- Synthetic data: Large models generate training data for smaller models
Why does distillation work better than training small models directly? Teacher outputs provide richer supervision than hard labels. The teacher's uncertainty (e.g., predicting 0.7 vs 0.99) conveys difficulty. Teachers trained on more data/compute transfer that knowledge to students. Essentially, distillation lets students benefit from the teacher's expensive training.
Efficient Architectures
Beyond compressing existing models, we can design architectures that are inherently more efficient. These architectures achieve accuracy comparable to larger models with fundamentally fewer operations.
Efficient Convolutional Networks
Depthwise Separable Convolutions: Standard 3×3 convolution with C_in inputs and C_out outputs: O(k² × C_in × C_out × H × W) ops.
Depthwise separable splits this into:
- Depthwise: Separate 3×3 conv per input channel
- Pointwise: 1×1 conv to mix channels
Result: O(k² × C_in × H × W + C_in × C_out × H × W)—roughly 8-9× fewer operations.
Used in: MobileNet, EfficientNet, ShuffleNet
Neural Architecture Search (NAS): Automatically search for efficient architectures:
- Define a search space of possible operations and connections
- Use reinforcement learning, evolutionary methods, or differentiable search
- Optimize for accuracy/efficiency tradeoffs
NAS has discovered architectures like EfficientNet that achieve state-of-the-art efficiency.
Efficient Attention
Self-attention has O(n²) complexity in sequence length—prohibitive for long sequences.
Linear Attention:
- Reformulate attention to avoid quadratic term
- Examples: Performers, Linear Transformer
- Trade some expressivity for O(n) complexity
Sparse Attention:
- Attend only to subset of positions
- Local windows (Longformer), strided patterns (Sparse Transformer)
- Reduce complexity while maintaining important connections
Mixture of Experts (MoE)
MoE architectures activate only a subset of parameters for each input:
- Multiple 'expert' subnetworks (typically feed-forward layers)
- A learned router selects which experts process each token
- Increases total parameters without proportional compute increase
Benefits:
- Larger effective capacity with same inference cost
- Experts can specialize for different input types
- Enables very large models (Mixtral, Switch Transformer) with reasonable serving costs
Challenges:
- Load balancing: Ensuring experts are used equally
- Communication overhead in distributed training
- Memory for storing all expert parameters
State Space Models (SSMs)
Recent alternatives to attention for sequence modeling:
Mamba:
- Selective state space model with input-dependent dynamics
- O(n) complexity in sequence length
- Competitive with transformers on many tasks
- Much faster inference for long sequences
SSMs may represent a path to efficient long-context modeling without attention's quadratic cost.
KV Cache Optimization
For autoregressive generation, key-value caching avoids recomputation:
Problem: KV cache grows linearly with sequence length, consuming memory
Solutions:
- Grouped query attention (GQA): Share KV heads across query heads
- Multi-query attention (MQA): Single set of KV for all query heads
- Sliding window attention: Limit context length for older tokens
- KV cache quantization: Store KV cache at lower precision
For edge deployment prioritize depthwise separable convs (vision) or small transformers/SSMs (sequence). For long contexts consider sparse attention or SSMs. For large models with limited inference budget explore MoE. For latency-sensitive applications profile carefully—theoretical FLOP counts don't always predict real-world latency.
Training Efficiency
Inference efficiency is crucial for deployment, but training efficiency determines development costs and accessibility. Training modern models requires enormous compute—making training more efficient expands who can participate in AI development.
Mixed Precision Training
Use lower-precision formats where possible:
FP16/BF16 Training:
- Store weights and compute forward/backward in 16-bit
- Maintain FP32 master weights for stable updates
- ~2× speedup on GPUs with tensor cores
Loss Scaling:
- FP16 has limited dynamic range
- Scale loss to prevent gradient underflow
- Dynamic loss scaling adjusts scale factor automatically
BF16 Advantages:
- Same dynamic range as FP32 (8 exponent bits)
- Less precision (7 mantissa bits vs 10 for FP16)
- Often more stable than FP16 without loss scaling
Gradient Checkpointing
Trade compute for memory:
- Don't store all activations during forward pass
- Recompute activations during backward pass as needed
- Reduces memory by O(√n) for n layers at cost of ~33% more compute
Enables training larger models or larger batch sizes with limited GPU memory.
Efficient Optimizers
AdaFactor:
- Reduces optimizer state memory
- Factorizes second-moment accumulator
- Comparable performance to Adam with ~50% optimizer memory
LAMB/LARS:
- Layer-wise adaptive learning rates
- Enable very large batch sizes without accuracy loss
- Scale training across many GPUs/TPUs
8-bit Optimizers:
- Store optimizer states in 8-bit
- Block-wise quantization for precision
- Near-identical training dynamics with 75% memory reduction
Data Efficiency
Make more of less data:
Curriculum Learning:
- Present examples in order of difficulty
- Easier examples first, harder examples later
- Can speed convergence and improve final performance
Core-set Selection:
- Identify most informative subset of training data
- Train on subset for similar performance
- Reduces data loading and processing overhead
Synthetic Data:
- Generate training data from models or simulations
- Augment scarce real data
- LLMs generating training data for other LLMs
Distributed Training Efficiency
Pipeline Parallelism:
- Split model across GPUs by layer
- Overlap computation and communication
- Challenges: pipeline bubbles, memory imbalance
Tensor Parallelism:
- Split individual layers across GPUs
- All-reduce for synchronization
- Good for very large layers (attention, large FFNs)
ZeRO Optimizer:
- Partition optimizer state, gradients, parameters across GPUs
- Each GPU stores full layer but 1/N of states
- Linear memory scaling with GPU count
Research continues pushing the training efficiency frontier. Recent work on 1-bit optimizers, no-optimizer training methods like weight-decay-only, and hardware-aware training schedules suggest significant room for further improvement. Halving training cost effectively doubles accessible model sizes for a fixed budget.
Hardware and Systems for Efficient ML
Algorithmic efficiency is only half the story. The other half is efficiently mapping algorithms to hardware. Hardware-software co-design can yield multiplicative improvements beyond what either alone achieves.
ML Hardware Landscape
GPUs (NVIDIA, AMD):
- Dominant for training and inference
- High memory bandwidth, massive parallelism
- Tensor cores for matrix multiply acceleration
- Evolving support for lower precision (FP8, INT4)
TPUs (Google):
- Custom ASIC for ML workloads
- Designed for systolic array matrix operations
- High memory bandwidth, efficient interconnects
- Available via Google Cloud
Specialized Accelerators:
- Cerebras: Wafer-scale chip with massive parallelism
- Graphcore: Intelligence Processing Units (IPUs) for sparse workloads
- SambaNova: Reconfigurable dataflow architecture
- Groq: Deterministic scheduling for low latency
Edge/Mobile:
- Apple Neural Engine, Qualcomm AI Engine
- Google Edge TPU
- Optimize for power efficiency over raw throughput
Key Hardware Metrics:
- FLOPS: Raw compute capability
- Memory bandwidth: Often the bottleneck for memory-bound operations
- Memory capacity: Limits model size
- Power efficiency: FLOPS/watt for deployment cost
- Latency: Critical for interactive applications
Inference Optimization Systems
Serving Frameworks:
vLLM:
- PagedAttention for efficient KV cache management
- Continuous batching for high throughput
- State-of-the-art LLM serving
TensorRT-LLM:
- NVIDIA's optimized LLM inference
- Kernel fusion, quantization support
- Multi-GPU/multi-node inference
llama.cpp:
- CPU-optimized inference for quantized models
- Enable running LLMs on laptops
- Support for various quantization formats
Key Optimization Techniques:
Kernel Fusion:
- Combine multiple operations into single kernel
- Reduce memory bandwidth by keeping intermediates in registers
- Critical for memory-bound operations
Batching Strategies:
- Static batching: Fixed batch size, wait for batch to fill
- Dynamic/continuous batching: Variable batch, add/remove requests
- Speculative scheduling: Predict future requests
Speculative Decoding:
- Use small draft model to generate candidate tokens
- Large model verifies in parallel
- Accept tokens quickly, reject rare bad predictions
- 2-3× speedup with minimal quality loss
- •Profile first — Identify actual bottlenecks before optimizing
- •Quantize aggressively — 4-bit or 8-bit usually works with minimal accuracy loss
- •Batch efficiently — Dynamic batching maximizes GPU utilization
- •Use optimized serving — vLLM, TensorRT-LLM, or similar
- •Consider speculative decoding — Easy 2× speedup for autoregressive models
- •Right-size hardware — Match hardware to actual workload requirements
- •Monitor continuously — Performance can degrade with changing traffic patterns
Maximum efficiency requires full-stack optimization: efficient algorithms, appropriate precision, optimized kernels, smart batching, and suitable hardware. A 4-bit quantized model with PagedAttention on TensorRT can be 10-100× more efficient than a naive FP32 implementation. The gains multiply across the stack.
KEEP LEARNING
Keep learning for free.
Read the complete lesson, save your notes and pick up where you left off with a free account. No card needed.