Repository navigation
checkpoint_skip_layers="auto" under torchrun: every rank keeps the smallest planned count - #15545
Conversation
…allest planned count auto refused any distributed run, so DDP and native FSDP2 users kept every layer checkpointed. Each rank now plans as on one GPU and one MIN all-reduce at the planning forward picks the count, so all ranks keep the same layers. Tensor / pipeline parallel models stay fully checkpointed (stages and shards hold different layers).
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep it up! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
… on CPU gloo Rank-local pre-train / eval / no-grad / inference-mode forwards keep both ranks on one collective; tensor / pipeline parallel and a missing process group take none; single process and a world-size-1 group plan alone; DDP (static_graph on/off, gradient accumulation with no_sync) and FSDP2 losses and weights match the fully checkpointed run.
…erance Skipping the recompute changes fp32 rounding by an ulp on torch 2.13 CPU. [skip ci]
|
@codex review |
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
…d helper (unslothai#15729) `tests/test_rendezvous_port_everywhere.py::test_distributed_tests_take_their_port_from_the_shared_helper` fails on main: `test_gc_skip_auto_dist_cells.py` and `test_gc_skip_auto_dist_gloo.py` (added in unslothai#15545) still bind their own `127.0.0.1:0` socket for the rendezvous port. unslothai#15679 added the check right after them, so they were never moved to `tests._rendezvous_port.free_port()`. Both `_free_port()` helpers now call the shared helper, and the unused `import socket` goes. - `test_rendezvous_port_everywhere.py`: 1 failed / 5 passed on main, 6 passed here. - `test_gc_skip_auto_dist_gloo.py`: passes (CPU gloo). `test_gc_skip_auto_dist_cells.py` needs a CUDA device for its NCCL cases; on a CPU-only host the same 5 fail on main and here ("No CUDA GPUs are available"), 8 pass. Tests only; no library or Studio code changes.
….join() (#15779) ### Problem Since #15545, `checkpoint_skip_layers = "auto"` under torchrun runs one `all_reduce(MIN)` on the planned layer count at the planning forward (the second training forward). That assumes every rank reaches that forward. Under `DDP.join()` (also `accelerate`'s `Accelerator.join_uneven_inputs(..., even_batches=False)`), a rank that runs out of inputs early sits in the join loop and only shadows DDP's own collectives. If it runs out before the planning forward, the planning `all_reduce` on the rank that is still training pairs with a join shadow collective instead. On gloo this aborts with `Received data size doesn't match expected size. Is there a distributed collective mismatch in your code?`. Before #15545 distributed runs never planned, so this configuration used to train fully checkpointed. HF `Trainer` and TRL never enter `DDP.join()`, so this only affects custom loops that use join with uneven inputs. ### Change `_AutoCheckpointSkip._agree_across_ranks` checks the DDP whose forward is running (`DistributedDataParallel._get_active_ddp_module()`). If it was put in an enabled join context, no collective is issued and every layer stays checkpointed, with a one line message. Every rank enters the join context, so every rank takes the same branch without communicating. Runs without join are unchanged. ### Tests `tests/test_gc_skip_auto_dist_cells.py::test_ddp_join_with_uneven_inputs_keeps_every_layer_checkpointed`: 2 gloo ranks, tiny Qwen3 under DDP with reentrant checkpointing, rank 0 has 1 batch and rank 1 has 3, inside `with ddp.join():`. On main the run fails with the collective mismatch above. With this change both ranks keep every layer checkpointed and losses and weights match a run without `"auto"`. `tests/test_gc_skip_auto_dist_cells.py`, `tests/test_gc_skip_auto_dist_gloo.py`, `tests/test_gc_skip_auto.py` on CPU (torch 2.13): 41 passed, 10 skipped (CUDA only). ### Not covered NCCL was not run (CPU gloo only). After a join context exits, DDP keeps its join config enabled, so a model that first plans after leaving a join also stays fully checkpointed.
Problem
checkpoint_skip_layers = "auto"(orUNSLOTH_GC_KEEP_LAYERS=auto) measures the first step and then stopscheckpointing as many decoder layers as fit. It refused every distributed run ("single process only"), so DDP and
native FSDP2 users kept every layer checkpointed and paid the full recompute. The refusal existed because each rank
would otherwise choose its own K.
Change
_AutoCheckpointSkip.installno longer refuses under torchrun. Each rank plans as on one GPU, then_agree_across_ranksruns oneall_reduce(MIN)on the count at the planning forward, which every rank reaches at thesame step. So every rank keeps the same number of layers, sized for the tightest rank (the same layers unless
offload_layers = "auto"offloaded different ones per rank).message says so.
checkpointed, as before.
Tests
tests/test_gc_skip_auto_dist_gloo.py::test_auto_agrees_on_the_smallest_count_across_ranks: 2 gloo ranks with roomfor 8 and 2 layers both keep the same 2 layers. On main the planner logic keeps [0..7] on rank 0 and [3, 7] on
rank 1, and the test fails.
tests/test_gc_skip_auto_dist_cells.py: 2 spawned gloo ranks with a 30 s timeout, so a collective only one rank takes fails instead of hanging. Single process (no collective, same count); ranks with room for different counts agree after one collective; rank 0 alone running an extra grad forward before step 1, or eval / no_grad / inference_mode forwards between steps; no second collective later; Unsloth TP / PP and an uninitialised process group stay fully checkpointed; DDP (static_graph off / on, gradient accumulation with no_sync) and FSDP2 (accumulation 1 and 2) match fully checkpointed training. torch 2.6: 38 passed, 12 skipped (no CPU FSDP2); 2.13 and 2.14: all pass.tests/test_gc_skip_auto.py: unchanged, 27 passed with the new file (10 CUDA-only skipped on CPU).GPU before / after (Kaggle T4x2, Qwen3-0.6B LoRA fp16, UNSLOTH_GC_KEEP_LAYERS=auto, torchrun 2 ranks)
Trees: main 8f390e4 (base) vs main 8f390e4 + this change (head), same kernel, same seed and data
(torch 2.14.1, transformers 5.17.0, TRL 1.15.0). Slowest-rank median step over the timed steps, runs interleaved
(DDP: B H H B B H; FSDP2: H B B H). Spread is (max - min) / median over the repeats of each arm.
Engagement: base logs
checkpoint_skip_layers = "auto" is single process onlyon every rank (12 / 12 rank logs).Head logs
keeps 28 of 28 decoder layers un-checkpointedon both ranks of every run (12 / 12), so the ranks agreed.Kernels per step: 11855 -> 9391 (DDP), because the recompute forward is gone.
Single-GPU reference: what auto buys where it already works (this PR does not change it)
Same harness, base = main 8f390e4 / ff308d0 with and without
UNSLOTH_GC_KEEP_LAYERS=auto, Qwen3-0.6B LoRA unless noted.