Assign stable samples to explicit training owners
Distinguish dataset identity, traversal position, rank ownership, and tail handling so scaling workers does not silently change the training experiment.
On this page
The question for today
What you’ll understand
- Define a sample identity independently of one worker’s local counter.
- Explain deterministic partitioning and its coverage conditions.
- Recognize padding and dropped tails as explicit visitation choices.
Four training workers report the expected batch count, yet each has read the same first quarter of a corpus. Throughput looks plausible while sample coverage is wrong. A correct input plan needs stable sample identities, a defined dataset snapshot, and an ownership rule that every participant interprets consistently. Counting worker outputs is insufficient. The experiment must say which underlying records were visited, whether repetition was intended, and how traversal changes when ranks, workers, or epoch boundaries change.
Identify content before assigning positions
A useful identity combines a versioned dataset snapshot with a stable record key or an unambiguous location within an immutable object. A pathname or row number alone can change meaning when files are replaced or filtered. Distinguish the source record from a transformed training occurrence: two random crops may share a source identity while having different augmentation identities. Preserve those relationships for auditing and replay. A worker-local counter identifies only a place in that worker's execution; it cannot prove that another worker did not emit the same source record under another counter.
Choose one canonical ordered manifest for index-based sharding, including a specified listing order and filtering policy. For fixed worker count W, assigning canonical index i to residue i modulo W partitions indices without overlap when each owner processes precisely its assigned set. Equal record counts do not imply equal decode costs or token counts. Hash-based ownership is another design, but its hash, key encoding, and snapshot must be fixed; do not depend on a process-randomized language hash. Changing W generally changes ownership, so elastic resizing needs a migration or restart rule rather than an assumption that local cursors remain meaningful.
Separate process ranks from loader workers
Distributed training ranks and loader subprocesses form different layers. A rank-level sampler can divide map-style indices while its local loader workers execute those requests. An iterable dataset often controls its own traversal and must explicitly combine rank and worker information to avoid repeating streams. PyTorch 2.9 documents that iterable dataset replicas need appropriate configuration; it does not automatically prove every custom iterator is correctly sharded. Validate ownership with small identifiable records before trusting an estimated loader length. Keep dataset construction and source listing consistent across participating ranks.
Tail behavior is part of the experiment. Some synchronous trainers require equal step counts, so a sampler may pad the index list with repeated samples or drop an incomplete tail. PyTorch 2.9 DistributedSampler exposes such a choice, and its epoch seed must be advanced with set_epoch when using its shuffled epoch traversal. Those policies differ from an uneven exact partition that visits every index once. Document whether repeated or omitted occurrences are permitted and how loss weighting and evaluation handle them. A constant batch count can conceal either policy, so record actual identities and multiplicities rather than infer coverage from the number of steps.
Test coverage and preserve the ownership contract
Check a short epoch by gathering sample identities and comparing observed multiplicities with the declared visitation policy. Include nondivisible dataset sizes, empty shards, multiple loader workers, and rank-count changes. Distinguish source records intentionally sampled with replacement from accidental duplicates caused by replicated iterators. Snapshot identity, ordering, partition policy, epoch, and relevant random state belong with restart metadata. Avoid silently relisting a changed object-store prefix after recovery, because a perfectly deterministic modulo rule over a different manifest is a different experiment.
The modular lab exposes a narrower ownership hazard. For each separate run, Reset, set both modulus m=4 and additive step a, predict the visited residues, then Play or Step. Step a=1 visits all four residues; a=2 visits only zero and two before repeating. This exact additive orbit is not a distributed sampler or a hash implementation. It illustrates why a stride through shard labels must be checked for complete coverage: a deterministic rule can repeat predictably while omitting valid owners. Real sample coverage still requires inspecting the actual manifest and emitted identities.
Work it through
Compare an exact partition with equal-length padding
A fictional canonical manifest has ten samples numbered zero through nine and three owners. First use owner(i)=i modulo 3 without padding. Then compare an equal-length traversal that pads to four occurrences per owner.
The exact residue sets are {0, 3, 6, 9}, {1, 4, 7}, and {2, 5, 8}, with sizes four, three, and three. Their union contains all ten distinct indices once.
Four occurrences for each of three owners requires twelve occurrences. A padded traversal therefore adds two repeated occurrences if it retains all ten original samples.
Both choices can be deterministic, but they have different multiplicities. Record which policy the trainer uses rather than interpreting twelve emitted items as twelve distinct source records.
The exact partition has ten distinct visits with unequal owner counts; the stated equal-length padded policy has twelve occurrences and two repeats.
The living notebook
Explore the model
Interactive lab · modular
A deterministic stride can omit shard labels
For each run: Reset, set both m=4 and step a, predict, then Play or Step. Compare a=1 with a=2. Their periods are four and two, respectively. This additive residue model is not a framework sampler, object hash, or full data-sharding simulator.
- Residue r0
- 0
- Orbit period · m/gcd(m,a)
- 12
- Distinct residues visited
- 1 of 12
Addition modulo m starts at zero: rₙ = na mod m. Playback advances two additions per second. The orbit has period m/gcd(m,a), including period one for a = 0. This is not a cryptographic security test or a primality algorithm. 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 forCheck the actual owner and sample coverage contract. Determinism alone does not imply that every intended label or sample is visited.
Pause & reason
Check your understanding
Your turn
Try it yourself
A canonical manifest has eleven samples and four residue owners with no padding. What are the owner counts? If an alternative pads to three occurrences per owner, how many repeated occurrences are added?
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Enumerate indices zero through ten, then compare twelve total slots with eleven records.
Compare with the solution
The residue counts are three, three, three, and two. The exact partition visits eleven records once. Padding to twelve occurrences adds one repeat under the stated retain-all policy; that repetition must be distinguished from a new sample.
Keep the idea
Take it with you
- Stable sample identity depends on a dataset snapshot and record meaning.
- Rank ownership, local worker traversal, and tail handling are distinct contracts.
- Audit multiplicities and coverage instead of relying only on batch counts.
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.