Layout redistribution: a shard and a partial sum are different objects
Map DTensor placements to the required reconstruction and explain why identical local shapes do not determine semantics.
On this page
The question for today
What you’ll understand
- Distinguish Shard, Replicate, and Partial placements.
- Trace gather, reduction, reduce-scatter, and axis-to-axis redistribution.
- Preserve global shape and ownership metadata across local tensor boundaries.
A distributed tensor consists of a global logical value plus a placement on a device mesh. A local tensor’s shape alone cannot tell you whether it is a complete replica, a slice of different coordinates, or a contribution awaiting reduction. PyTorch 2.8 DTensor names these meanings Replicate, Shard, and Partial. Layout redistribution changes how the same logical value is represented, and sometimes completes a pending reduction. Choosing the wrong transition can produce plausible shapes with wrong values.
Name the logical value before choosing a collective
A row shard contains distinct global rows, so reconstructing the full tensor concatenates the owned rows. A sum-partial tensor contains contributions to the same coordinates, so reconstruction sums them. A replica already contains the full logical value. These are not three performance labels for interchangeable buffers. A linear layer’s input-feature products naturally create partial sums, while its output-feature partitions naturally create shards. Carry that placement information through the next operation rather than guessing from a printed local array.
In the documented one-mesh-dimension DTensor transitions, Shard to Replicate uses an all-gather, Partial with sum to Replicate uses an all-reduce, and sum-Partial to Shard uses reduce-scatter. Replicate to Shard can select local chunks without network traffic when replicas are consistent. Changing a shard axis can use all-to-all redistribution. These are version-scoped transition semantics; a larger composed plan can fuse or restructure movement, so its end-to-end communication count still depends on the actual layout graph.
Redistribution preserves coordinates, not just element count
A row-sharded matrix changing to column sharding must send each row owner’s appropriate column pieces to the new owners. Reshaping each local buffer cannot make remote coordinates appear. Conversely, all-reducing distinct row shards would add unrelated rows and destroy their global indexing. The operation needs global shape, shard axis, mesh dimension, and rank order. For uneven partitions, local sizes and offsets also matter; PyTorch 2.8 documents experimental limitations for uneven DTensor sharding, so test the selected operator rather than assuming support.
Converting a DTensor to local tensors can be useful at a kernel boundary, but it transfers responsibility for correct metadata and autograd mapping to the surrounding code. Creating a replicated interpretation from unequal local values does not make them replicas. Creating a sum-partial interpretation from already complete replicas can multiply the value by the mesh size when reduced. Validate both the forward transition and its gradient behavior; a wrong reverse mapping can train a different model even when reconstruction looks right.
Use a layout table at every distributed boundary
Write the global tensor shape, local shape, placement per mesh axis, and intended next placement in a compact table. Record whether a collective completes a sum or only moves coordinates. Check process-group membership and consistent collective order to avoid hangs that a local shape test cannot find. When a benchmark reports bytes, specify whether it counts local payload, sent bytes per rank, received bytes, or aggregate link traffic. Layout labels explain data meaning; a performance model then explains the cost of changing them.
The matrix lab provides a small invertible-transform analogy only. Reset, set a=2 and b=0, predict area scale 2, then Step. The chosen map changes geometric area, whereas a correct layout redistribution preserves the logical tensor’s values. This contrast helps distinguish representation changes from mathematical transformations. The lab does not gather shards, perform reductions, preserve DTensor metadata, or simulate a network; the worked array trace establishes the real redistribution semantics.
Work it through
Reduce-scatter completes a sum and distributes rows
Two ranks hold sum-partial 2×2 arrays P0=[[1,0],[0,1]] and P1=[[2,3],[4,5]]. The desired result is row-sharded evenly across the same two ranks.
The complete logical sum is T=[[3,3],[4,6]]. Both partial arrays referred to the same global coordinates; their values were contributions, not distinct row ownership.
A sum reduce-scatter along rows gives rank 0 row[3,3] and rank 1 row[4,6]. Each resulting shard has two elements, while the global tensor has four.
An all-reduce instead would give the full four-element T to both ranks. Concatenating the original partial arrays would create a4×2 structure and fail to compute the intended sum. The desired output placement determines which correct reduction result is needed.
Reduce-scatter combines values and assigns coordinate ownership. It is not merely a smaller all-gather or a local reshape.
The living notebook
Explore the model
Interactive lab · matrix
Representation changes are not value transforms
Reset, set a=2 and b=0, predict determinant 2, then Step. This geometric map changes values/area and is not a tensor-layout redistribution, all-gather, all-to-all, or reduce-scatter. Compare its transformation with the value-preserving logical layout trace.
- Area scale · det A
- 1.4
- Singular values · σmax, σmin
- 1.597, 0.876
- Condition number · κ₂(A)
- 1.823
The selected matrix is A = [[a,b],[0,1]]. Playback interpolates geometry between the identity and A; readouts always describe A. Its determinant is a and its singular values come from AᵀA. Positive a makes every supported matrix invertible. This two-dimensional family is not a general eigensolver or a learned network. 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 forA distributed layout describes where values live; it must not silently change which mathematical tensor they represent.
Pause & reason
Check your understanding
Your turn
Try it yourself
A4×2 tensor has rows[1,2],[3,4],[5,6],[7,8], initially row-sharded across two ranks. After column sharding, what should each rank own? Can local reshape alone achieve it?
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Each new owner needs one column from every old row owner.
Compare with the solution
Rank 0 should own column[1,3,5,7] and rank 1 column[2,4,6,8], each shaped 4×1 under this ordering. Each requires values held by the other initial row owner, so local reshaping alone cannot produce the correct redistribution. A supported communication transition must exchange those coordinate pieces.
Keep the idea
Take it with you
- Shard, Replicate, and Partial encode different global-value meanings.
- Gathering coordinates and summing contributions are different operations.
- Global shape, axes, process groups, and backward mappings belong in the layout contract.
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.