Estimate peak parameter memory for fully sharded training with prefetch
Why it matters Sizing fully sharded training runs starts with this peak: gathered layers, not the shards, often decide whether a model fits.
Fully sharded data parallel keeps only 1 / N of every layer's parameters on each GPU and all-gathers a full layer just before using it. Prefetching the next layer while computing the current one hides communication but holds two full layers at once.
Write fsdp_peak(layer_params, world, bytes_per_param, prefetch=True). Each rank always stores its shards: sum(layer_params) / world parameters. During the forward pass, layer i is gathered in full; with prefetch, layer i + 1 is gathered at the same time (if it exists). Return the peak bytes per rank: (shards + max over i of the gathered parameters at step i) * bytes_per_param.
def fsdp_peak(layer_params, world, bytes_per_param, prefetch=True)Example
layers [100, 400, 200], world 4, 2 bytes, prefetch gives 1550.0 (shards 175, worst step 400 + 200 = 600 params, (175 + 600) x 2 bytes).
Understand the idea
Fully sharded training is like a group of friends sharing a big library: each keeps a few pages of every book at home, and before reading a chapter they quickly gather the full chapter from everyone. Prefetching means you also gather the next chapter while reading the current one, so you never wait, but you hold two chapters at once. Worked example: layers of 100, 400 and 200 parameters on 4 GPUs. Each GPU stores 700 / 4 = 175 parameters of shards. Step 0 holds 100 + 400, step 1 holds 400 + 200 = 600 (worst), step 2 holds 200. Peak = (175 + 600) x 2 bytes = 1550 bytes. Rule: peak = (sum / N + max_i(p_i + p_{i+1})) x bytes. Activations and optimizer state are separate and must be added for a full estimate.