← Back to Index

9. Scalability & Performance

Sharding for AI Systems

Data and compute sharding are essential when datasets or model parameters exceed single-machine capacity.

# Data Parallelism (simplified)
Worker 1: sees data[0:10K], computes gradients G1
Worker 2: sees data[10K:20K], computes gradients G2
Worker N: sees data[...], computes gradients GN
All-reduce: G = (G1 + G2 + ... + GN) / N
All workers apply G to their model copy

Distributed Training

StrategyModel SizeCommunicationFrameworks
Data ParallelismFits on one GPUGradients (frequent)PyTorch DDP, Horovod
Pipeline ParallelismFits on a few GPUsActivations (moderate)DeepSpeed, Megatron-LM
Tensor ParallelismVery large (100B+ params)Intermediate tensors (intra-node)Megatron-LM, FSDP
FSDP (Fully Sharded)Large (1B—100B params)All-gather (efficient)PyTorch FSDP

Model Compression

Smaller models mean lower latency and lower cost:

# Quantization impact
Model: Llama-7B
FP32: 28 GB GPU memory, 50ms per token
FP16: 14 GB GPU memory, 28ms per token
INT8:  7 GB GPU memory, 15ms per token

Inference Load Balancing

Cost-Performance Trade-offs

Every optimization decision involves a cost-benefit analysis:

TechniqueLatency ImprovementCost ImpactQuality Impact
Quantization (FP16)~2xLower (less GPU time)Minimal
Quantization (INT8)~4xLowerSmall (0.5—2%)
Distillation (10% size)~10xMuch lowerModerate (3—5%)
Dynamic batchingHigher throughputBetter GPU utilizationNo impact
Exercise: You need to serve a 70B parameter LLM at 100 requests/second with p99 latency under 1 second. You have 8 A100 GPUs (80GB each). Design the serving architecture: model parallelism strategy, quantization level, batch size, and number of replicas. Show your calculations.