Skip to main content

Why the first of five test cases turned up on both workers’ lists

Deal five numbered cards to two people, taking turns. One person gets three cards and the other gets two. To give them equal numbers, you could copy the first card and deal it at the end. Or you could set the last card aside before you start.

PyTorch can make either choice when it divides test cases among workers. In our five-case example, one setting sent case 00 to both workers. The other left case 04 out altogether. If every result goes straight into a score, that choice changes which cases get a vote.

A case is one item to test. A worker handles a share of those items. The part of PyTorch that assigns the cases is called DistributedSampler. Each worker has a number, called its rank. We asked for the lists it would send to ranks 0 and 1, using case IDs 00 through 04. We also tried 00 through 05. With six cases, two workers can receive three cases apiece without copying or omitting one. Recorded assignments

What the two workers would receive

Our script worked out both workers’ assignments in a single process. The made-up cases could be looked up by position, and we kept them in their original order (shuffle=False). For each set of cases we compared only two sampler choices. In the five-case run drop_last=False filled an extra place, while drop_last=True dropped the last case.

After the assignment, another component, the DataLoader, grouped each worker’s cases into batches of 2. We used num_workers=0 and loader drop_last=False to keep a final short batch. It recorded the cases and batches each rank would get without starting a distributed process group or scoring a model. Replay script

Read the two rank columns together. Each set of brackets encloses a batch, a group of cases delivered together. In the first row rank 0 gets 00, 02 and 04. Rank 1 gets 01, 03 and another 00. Counting each delivery of a case as one row, six rows cover just five distinct IDs.

Assignments for lists of five and six cases with two ranks
Intended IDsSampler drop_lastRank 0 batchesRank 1 batchesTotal rowsUnique IDsExtra occurrencesMissing IDs
00–04False[00, 02] [04][01, 03] [00]65One extra 00None
00–04True[00, 02][01, 03]44None04
00–05False[00, 02] [04][01, 03] [05]66NoneNone
00–05True[00, 02] [04][01, 03] [05]66NoneNone

The sampler adjusted the list before it dealt the cases

For our unshuffled five-case list, drop_last=False told the sampler to fill a sixth place. It appended a copy of 00, then dealt alternating positions to the workers. Rank 0 got 00, 02 and 04. Rank 1 got 01, 03 and 00. The six rows therefore covered just five different cases.

With drop_last=True, the sampler shortened the list to four by leaving out 04. Each of the other cases appeared once. With six cases to start with, the length already divided evenly by two, so either setting delivered every case exactly once. PyTorch 2.9.1 sampler source

The DataLoader has its own setting with the same name, drop_last. It decides whether to discard a short final batch after each rank already has its list. In our first row the loader kept the one-case batches containing 04 and 00. Holding that choice fixed let us isolate the sampler’s effect. The replay ran on Windows with Python 3.13.17 and CPU PyTorch 2.9.1+cpu. PyTorch 2.9.1 DataLoader source

How often did each case reach the test?

  1. Start with a list of stable, unique IDs for the cases you mean to test. If each should count once, this list gives you the expected count for every ID.
  2. Log the ID and rank for every row delivered, then put the rank logs together without removing duplicates. For a check of the sampler alone, keep loader drop_last=False so incomplete batches reach the log.
  3. Count how often each ID appears and compare with the intended list in both directions. If the aim is to test every case once, a missing ID, an unexpected ID or a second occurrence each calls for an explanation.

If you are investigating a score, follow the IDs into the part that calculates it. Count how many case results it actually included. A later stage might have removed duplicates or filtered out cases after the loader delivered them.