advanced28 min studyLesson 547 in Engineering

Parameter sharding: materialize only what the next operation needs

Trace FULL_SHARD parameter availability and all-gather traffic while retaining module-fit, shared-parameter, and optimizer-mapping constraints.

On this page

What you’ll understand

  • Trace the scoped FSDP FULL_SHARD forward and backward lifecycle.
  • Calculate an equal-shard all-gather receive volume.
  • Separate persistent state savings from a materialized module’s memory requirements.

Optimizer and gradient sharding still leave a full compute-parameter replica in the simplest earlier stages. Parameter sharding removes that persistent replication, then gathers the needed parameter unit before computation. We scope the concrete lifecycle to the PyTorch 2.9 FullyShardedDataParallel wrapper with FULL_SHARD, ordinary managed module execution, and no extra recomputation. DeepSpeed v0.18.2 ZeRO stage three uses related collection and repartitioning ideas but has its own module and external-parameter contracts. Other FSDP APIs or strategies must be read on their own terms.

Parameter availability is a time-dependent invariant

In the scoped FULL_SHARD lifecycle, parameter storage is sharded between uses. An all-gather unshards the relevant managed unit before its forward computation, and resharding follows forward. The unit is unsharded again before its backward computation and reshards afterward; gradients are synchronized and sharded through reduce-scatter, and owners update their optimizer state locally. These operations have stream and completion dependencies. Calling a submodule directly while bypassing its managing wrapper can violate the assumption that its needed parameters have first been materialized.

The managed unit matters. Wrapping the whole model as one unit can require a much larger full-parameter materialization than wrapping suitable submodules. Conversely, excessively small units can create many communication launches and harm overlap. DeepSpeed’s documented stage-three assumptions require the relevant submodule forward and backward to fit, with supported techniques needed when they do not. Parameter sharding does not divide activations automatically, and a large intermediate or one indivisible operator can still exceed device memory even when the persistent parameter shard is small.

Mstate,ideal=(bp+bg+bo)NWM_{\rm state,ideal}=\frac{(b_p+b_g+b_o)N}{W}
For balanced full sharding, N counts logical parameters and bp,bg,bo are explicitly inventoried bytes per parameter, gradient, and optimizer-owned state. W>0 owners gives an ideal persistent bytes-per-rank estimate. Activations, replicated buffers, materialized units, padding, and temporary allocations are excluded.↔ Scroll wide equations horizontally.

All-gather restores a logical parameter view

With equal shards of a unit, each rank already holds one fraction and receives the remaining fractions to reconstruct that unit’s logical parameter data. This is a data-volume model, not an algorithm-independent wall-clock formula. Topology, protocol, dtype, padding, overlapping collectives, and reuse policy change time and actual traffic. Distinguish bytes received by one rank from bytes sent, aggregate fabric traffic, and the size of the final full buffer. Confusing those counters can produce an apparently consistent but incorrect communication budget.

Do not retain arbitrary references to temporary full-parameter views and assume they remain valid after resharding. The wrapper can replace views during forward and backward. Shared or externally accessed parameters require the framework’s supported coordination so that the right data is gathered and ownership is preserved. Build optimizers against the supported wrapped representation and preserve canonical parameter identity in checkpoints. A logical parameter existing in the model definition does not mean every rank has its complete current data resident at every instant.

Vrecv=W−1WSV_{\rm recv}=\frac{W-1}{W}S
For one ideal equal-shard all-gather over W≥1 ranks, S is the full unit size in bytes and Vrecv is bytes a rank receives beyond its own shard. It excludes algorithm metadata, padding, repeated gathers, sends, and topology-specific traffic.↔ Scroll wide equations horizontally.

Choose a schedule, then verify its live representations

Trace one managed unit through forward, backward, reduction, update, and the next iteration. Record which representation is full, which is sharded, and which stream still uses each allocation. A strategy that keeps full parameters after forward can reduce the need for another gather but has a different memory schedule from FULL_SHARD. Reentrant computation or activation rematerialization can also change how many materializations occur. Count the actual events rather than transfer a two-gather illustration to every model and configuration.

Use the cache lab only as a reuse analogy. Reset, explicitly set capacity two and stride four, predict the outcome after one Step, then Step once. A separate Reset with capacity four and stride four gives a different reuse outcome after exactly one Step: 17 misses versus four misses and 13 hits. The toy starts empty and traces a four-address cycle over 17 requests. It does not model all-gather, managed parameter units, GPU allocations, or a legal FSDP retention policy; real schedule evidence determines whether reuse is possible and affordable.

Count per-rank receive volume for two materializations

A hypothetical managed parameter unit is 64 MiB and is evenly sharded across four ranks. Assume one ordinary FULL_SHARD forward and backward each require a fresh all-gather, without padding or extra recomputation. Count only per-rank received payload.

  1. Each persistent shard is 64/4=16 MiB. To form the full unit, a rank receives the other three shards, totaling 48 MiB for one all-gather.

  2. Under the explicit two-materialization assumption, forward and backward together receive 2×48=96 MiB per rank. The full 64-MiB buffer size is not the same as the received volume because the rank already owned one shard.

  3. This excludes sends, gradient reduce-scatter, protocol overhead, padding, and any extra gather caused by another execution path. It also does not state the simultaneous memory peak; that requires the lifetimes of shards, full buffers, activations, and prefetched units.

The result

The modeled received payload is 96 MiB per rank for two gathers. Neither persistent shard size nor receive volume alone describes the complete training memory peak.

Explore the model

Interactive lab

Interactive lab · cache

Retention and reuse are an analogy, not an FSDP policy

For independent runs use Reset → explicitly set BOTH C and s → predict → Step once. At t=.5,N=17, (2,4) yields 17 misses and (4,4) yields four misses and 13 hits. The fully associative LRU instrument contains no parameter units, all-gather, or GPU memory allocator.

Retention and reuse are an analogy, not an FSDP policyA fully associative LRU cache starts empty and requests address (i·s) mod 16. Hits move an address to the newest position; misses evict the oldest when full. Playback starts with 16 requests and adds two per second, capped at 4096 explicitly simulated requests. This toy stream is not a processor-cache benchmark. Current readings: Hits / requests: 0 / 16; Hit rate: 0%; Address-stream period: 16 addresses. All parameters are adjustable with the labeled controls below.Fully associative LRU cache after 16 deterministic requests: 0 hits and 16 misses.Most recent 16 requestsaddress = i × 3 mod 160M3M6M9M12M15M2M5M8M11M14M1M4M7M10M13MCurrent cache order · oldest → newest471013Latest address 13 · miss: inserted as newestToy address stream · period 16 · starts empty
Cached addressMiss / evictionMost recent request
Hits / requests
0 / 16
Hit rate
0%
Address-stream period
16 addresses
t = 0 s

A fully associative LRU cache starts empty and requests address (i·s) mod 16. Hits move an address to the newest position; misses evict the oldest when full. Playback starts with 16 requests and adds two per second, capped at 4096 explicitly simulated requests. This toy stream is not a processor-cache benchmark. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forReusing materialized data trades capacity for avoided work, but a legal sharded schedule must establish availability and lifetime explicitly.

Check your understanding

Which statement matches the scoped PyTorch 2.9 FULL_SHARD lifecycle?

Try it yourself

A 96-MiB unit is equally sharded across eight ranks. Under the same two-gather assumptions, calculate shard size and total per-rank received payload. Why does a tensor reference saved before wrapper execution need scrutiny?

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

One gather receives everything except the local shard; storage views can change as parameters are materialized and resharded.

Compare with the solution

The shard is twelve MiB. One gather receives 84 MiB, and two receive 168 MiB per rank under the stated payload model. This excludes all other communication and does not determine peak memory. A saved reference may point to a view that is no longer the current full parameter representation after wrapper-managed changes. Use the supported access and coordination interfaces, including external or shared-parameter handling where required, instead of assuming a complete parameter remains resident and valid on every rank.

Take it with you

  • Name the wrapper, sharding strategy, and managed materialization unit.
  • Count received payload separately from full buffers and aggregate fabric traffic.
  • Track parameter views and module-fit requirements across the actual execution schedule.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.