Skip to content

Train in several processes with DDP (#17) - #68

Merged
AvnehSBhatia merged 4 commits into
exea-labs:mainfrom
jaztulsi:ddp
Nov 22, 2026
Merged

AvnehSBhatia merged 4 commits into
exea-labs:mainfrom
jaztulsi:ddp

Conversation

@jaztulsi

Copy link
Copy Markdown
Contributor

Closes #17.

What changed

torchrun --nproc_per_node N -m exealm.train.pretrain ... now trains in N processes (DDP: NCCL with one GPU per process, gloo on a CPU). A plain run is unchanged: same weights, same search choices and the same log on a 60-step run with search rounds and retrieval on.

The issue's three points:

  • Swarms: --batch is the total over processes. Every process draws the same batch and keeps its own rows, whole swarms only (a total that can't split into whole swarms per process is refused with a clear error). So N processes train on exactly the data one process would.
  • Retrieval: process 0's datastore keys go to every process after each refresh.
  • Exact resume: process 0 writes the checkpoints, and --resume auto works as before.

Two more things it needed to stay one model:

  • Search: a replaced slot gets fresh random weights and cleared Adam rows. Process 0 runs each round, then the others copy its circuits, weights, buffers and optimizer state (sync_from_rank0). Without this the processes drift apart after the first replacement.
  • Loss: each process's loss is weighted by its share of the targets, so the update equals one process's on the whole batch even when masked seam targets make the shares unequal.

Process 0 also evaluates, logs and talks to W&B.

Result (the issue's "done when")

tiny, synthetic data, 300 steps on a CPU, final validation bits per token:

seed 0 seed 1
1 process 2.1394 2.1727
2 processes (torchrun, gloo) 2.1392 2.1730

1 vs 2 processes differ by 0.0002 and 0.0003; seed 0 vs seed 1 by 0.033. With search rounds and retrieval on (60 steps), both make the same choices in all 10 search rounds. The Llama baseline gives the same numbers to 4 decimals.

The small remaining difference: each process updates its slots' running output size (the unit scale) from its own rows, and DDP copies process 0's buffers before each forward.

Not in this PR

FSDP, and a sharded datastore search (every process holds the whole datastore). Both stay on the to-do list.

Tests

tests/test_ddp.py (3 tests, about 90 s): two gloo processes started like torchrun, on the tiny preset with search rounds every 4 steps and retrieval on.

  • Both processes end with identical parameters. With the sync switched off, this test fails.
  • The search makes the same choices as one process, and validation stays within 0.01.
  • A run stopped at step 6 and resumed ends with bit-identical weights to one that ran straight through.
  • A batch that can't split into whole swarms is refused.

Full suite: 229 passed, 2 skipped (226 on main plus these 3), and every commit passes on its own.

Part of exea-labs#17. Not used yet; the next commit calls them.

- dist_setup joins the process group when torchrun started the run
  (WORLD_SIZE, RANK, LOCAL_RANK): NCCL with one GPU per process, gloo on
  the CPU. A plain run gets rank 0 of 1 and nothing changes.
- sync_from_rank0 runs after a search round on process 0. A replaced
  slot gets fresh random weights and cleared Adam rows on that process
  only, so the others take its circuits, weights, buffers and optimizer
  state. It stops with an error if the optimizer state no longer has the
  same layout on every process.
- broadcast_ copies one tensor from process 0, through the device for
  tensors that live off it (Adam's step).
Part of exea-labs#17. torchrun --nproc_per_node N -m exealm.train.pretrain ...
now trains in N processes; a plain run is unchanged (same weights, same
search choices and the same log, checked on a 60-step run with search
rounds and retrieval on).

- Data: --batch is the total over processes. Every process draws the same
  batch from the same generator and keeps its own rows. The total must
  split into whole swarms per process, so a swarm never spans two.
- Loss: each process's loss is weighted by its share of the targets, so
  the update is the one a single process would take on the whole batch,
  even when masked seam targets make the shares unequal. The logged train
  loss is the whole batch's.
- Search: process 0 runs each round; then every process copies its
  circuits, weights and optimizer state.
- Retrieval: process 0's datastore keys go to every process after each
  refresh.
- Process 0 evaluates, logs, talks to W&B and writes checkpoints, so
  --resume auto works as before. The start record notes the number of
  processes.
- find_unused_parameters, since RG slots read nothing without a library.
Part of exea-labs#17. tests/test_ddp.py starts two gloo processes on the CPU the
way torchrun does, on the tiny preset with search rounds every 4 steps
and retrieval on:
- both processes end with identical parameters, through every
  replacement and update (switching the sync off makes this fail);
- the search makes the same choices as one process on the same total
  batch, and validation stays within 0.01 bits per token;
- a two-process run stopped at step 6 and resumed ends with exactly the
  weights of one that ran straight through;
- a total batch that cannot split into whole swarms is refused.
Closes exea-labs#17. pretrain.py's docstring and docs/training.md say how to start
a run with torchrun and what each process does. Measured on the tiny
preset, 300 steps on a CPU: two processes match one process on the same
total batch to within 0.0003 bits per token (two seeds differ by 0.033),
and a two-process run resumes exactly. FSDP and a sharded datastore
search stay on the to-do list. The README ticks DDP and swarm-aware
multi-GPU training.
@AvnehSBhatia
AvnehSBhatia merged commit 63e90c2 into exea-labs:main Nov 22, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DDP training, testable on a CPU with two processes

2 participants