Sync Batch Norm & Sharded State
Keep stats and weights consistent.
Sync Batch Norm & Sharded State is a free Deep Learning Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Deep Learning Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Small-Batch Problem
Batch norm computes stats from each GPU's local batch. Split across many GPUs, each per-device batch shrinks and those stats get noisy.
Enter SyncBatchNorm
SyncBatchNorm fixes this by computing mean and variance across all GPUs together, as if it saw the full global batch.
Convert in One Call
You do not rewrite layers by hand. One helper converts every BatchNorm in your model to its synced version.
import torch.nn as nn
model = nn.SyncBatchNorm.convert_sync_batchnorm(model)Convert Before Wrapping
Order matters: call the conversion before you wrap the model in DDP, so the synced layers are the ones DDP manages.
model = nn.SyncBatchNorm.convert_sync_batchnorm(model)
model = DDP(model, device_ids=[local_rank])It Costs Communication
Syncing stats means an extra all-reduce at every batch norm layer. Use it when small per-GPU batches hurt accuracy, not by default.
The Memory Wall
Plain DDP copies the full model, gradients, and optimizer state onto every GPU. For huge models that redundancy wastes memory fast.
Shard the State
Sharding splits those tensors across GPUs so each device stores only a slice. Together the GPUs still hold the whole model.
Meet FSDP
PyTorch's FullyShardedDataParallel shards parameters, gradients, and optimizer state. It lets you train models far larger than one GPU's memory.
from torch.distributed.fsdp import FullyShardedDataParallel as FSDP
model = FSDP(model)Gather Just in Time
FSDP gathers each layer's full weights only when it is needed for compute, then frees them again. That keeps peak memory low.
ZeRO Stages
Sharding comes in levels, called ZeRO stages: shard optimizer state, then gradients, then parameters. More sharding saves more memory.
Pick the Right Tool
If your model fits per GPU, plain DDP is simplest. When it does not, reach for FSDP to shard state and keep going.
Quick Check
Decide what each technique is really for.
Recap
You met two consistency tools: SyncBatchNorm keeps stats global across GPUs, and FSDP shards state so giant models fit. Use each only when you need it.
Frequently asked questions
Is the “Sync Batch Norm & Sharded State” lesson free?
Yes — the full text of “Sync Batch Norm & Sharded State” is free to read here on the web, and the Deep Learning Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Deep Learning Academy course, upgrade to CoddyKit PRO.
What will I learn in “Sync Batch Norm & Sharded State”?
Keep stats and weights consistent. You practise Deep Learning Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Deep Learning Academy?
No prior experience is required. Deep Learning Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Sync Batch Norm & Sharded State” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Deep Learning Academy lesson?
Yes. Every Deep Learning Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Data vs Model Parallelism
- DistributedDataParallel Basics
- Sync Batch Norm & Sharded State
- Launch Jobs with torchrun