Unit 10 of 11 · about 4 hours · implement

Divisibility failures and single-process reconstruction oracles

A divisible shape does not prove correct ownership, and an uneven shape is not automatically illegal. Explicit policies and independent oracles separate implementation limits from mathematical limits.

You'll be able to

  • Validate sequence, feature and head ownership under an explicit equal-shard, uneven-shard or padding policy.
  • Implement independent single-process forward, loss and gradient oracles for TP and CP layouts.
  • Catch wrong shard order, repeated bias, invalid targets and missing gradient sums with negative tests.

Covers

Uneven sequence and head divisibility failuresSingle-process reconstruction and gradient oracle tests
1

Read

In this order, only the parts listed

0 of 3 done

  1. Uneven DTensor shards and metadata checksPyTorch Tensor Parallel Tutorial

    Shard: torch.chunk semantics and experimental uneven-sharding note; DTensor.from_local: run_check, shape and stride

    Read these entries only. Uneven placement support does not imply every module or collective accepts ragged inputs.

  2. Equal-shard assertions and a linear oracle suitePicotron - Tensor and Context Parallelism

    Reference weight transfer; forward comparisons; backward weight, bias and input-gradient checks

    Also inspect constructor divisibility assertions in https://github.com/huggingface/picotron/blob/59714b1bf25541dbeb6f5a721ed15f6d0768e786/picotron/tensor_parallel/tensor_parallel.py . Port the CUDA/NCCL harness to CPU/Gloo as described in Practice.

  3. Dense CP reconstruction and its missing checksTorchFeather - TP and Ring Attention Minimal Examples

    main: sequence partition, dense causal SDPA reference and output comparison

    Reuse the dense reference, not the ring implementation, as the oracle. Add gradients and uneven-token cases in your deterministic simulation; the shipped suite checks neither.

2

Focus on

  • Record the ownership policy before launching. Picotron requires equal linear/vocabulary shards. Whole-head GQA without KV replication requires TP degree to divide query and KV head counts; retain the mapping checks from the attention unit.
  • DTensor uneven sharding follows torch.chunk, not a balanced remainder policy. On four ranks, ten elements have lengths [3, 3, 3, 1]. A custom CP simulation may use different boundaries if it preserves every logical token exactly once.
  • Reconstruct by logical indices and compare against ordinary PyTorch with the same global weights, inputs, masks, positions and loss denominator. Reusing the sharded mask builder in both paths can hide the same bug.
  • Compare input and reconstructed parameter gradients, not only output. Start with zero dropout and torch.float64; then add indexed RNG replay and justify lower-precision tolerances separately.
Common mistakes · 3
  • Assuming torch.chunk assigns the remainder one element at a time. Its shard boundaries can differ from a balanced split.
  • Padding tokens without excluding padded keys and padded loss entries. The padded execution then changes the objective.
  • Testing only all-ones upstream gradients. Add a nonuniform or nonlinear scalar loss to expose indexing errors.
3

Try it yourself

Closed-book, before checking anything

Closed-book, simulate causal attention for ten tokens over four CPU shards with boundaries [0, 3, 6, 9, 10]. Compare outputs and query/key/value gradients against dense attention under squared-output sum loss. Separately reconstruct torch.arange(10.) from four Gloo DTensor shards and check lengths [3, 3, 3, 1]. Reject four-way equal KV-head sharding for eight query heads and two KV heads. Swap two output shards and remove one gradient contribution; each mutation must fail a specific oracle assertion rather than hang in a collective.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.Must a ten-token sequence be rejected on four CP ranks?Answer

    Only if the implementation requires equal shards and has no padding policy. An uneven implementation can be correct with explicit boundaries, valid masks and compatible communication.

  2. 2.Why can a correct output hide an incorrect backward pass?Answer

    Missing input-gradient sums or remote KV-gradient returns do not change forward values. Compare each derivative under the same logical loss.

  3. 3.What makes the dense path an independent oracle?Answer

    It uses ordinary full tensors and independently declared global visibility and positions. It does not reproduce the partitioned algorithm's ownership logic.

5

Practice

  • Picotron: tensor-parallel linear forward/backward tests

    At commit 59714b1, port tests/test_tensor_parallel.py and run it with four CPU processes

    Use CPU tensors, Gloo and torch.manual_seed(42) instead of the utility import; replace the column test's exact torch.eq with torch.testing.assert_close. Keep identical reference weights and inputs across TP partners. Run the port with torchrun --nproc-per-node 4 and your file path. The section mini-project covers uneven CP, invalid targets, padding and ownership mutations in trainlab.

You're done when

The CPU linear suite and uneven attention output/gradient oracle pass, and each declared illegal layout or injected ownership error fails before performance measurement.

Only when you can do this without notes.

Leads to

Notes

Resources · 3 for this unit, 10 more in the section
  • Picotron - Tensor and Context ParallelismHugging Face · required

    Read tensor_parallel/tensor_parallel.py, tp_communications.py, context_parallel/context_parallel.py and cp_communications.py. tests/test_tensor_parallel.py hardcodes NCCL and CUDA; port it to Gloo on CPU and compare with torch.testing.assert_close, because its exact equality check fails when sharded and full matmuls round differently.

  • TorchFeather - TP and Ring Attention Minimal ExamplesTorchFeather · required

    Run minimal_examples/block_matrix_multiply.py, then tp_swiglu.py and tp_attention.py with 2 processes and ring_attention.py with 4, all on Gloo on CPU; each checks its output and gradients against ordinary tensors. Change one placement or mask and watch the check fail.

  • PyTorch Tensor Parallel TutorialPyTorch · required

    Use the tutorial to map ColwiseParallel, RowwiseParallel, SequenceParallel, PrepareModuleInput and loss parallelism to the ownership model you derived, with the DTensor reference at https://docs.pytorch.org/docs/2.14/distributed.tensor.html for Shard, Replicate, Partial and redistribute. Add assertions around every layout transition.

Also in this section