Skip to content

checkpoint_skip_layers="auto" under torchrun: every rank keeps the smallest planned count - #15545

Merged
danielhanchen merged 8 commits into
mainfrom
dh/s9-auto-skip-dist
Oct 28, 2026
Merged

danielhanchen merged 8 commits into
mainfrom
dh/s9-auto-skip-dist

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

Problem

checkpoint_skip_layers = "auto" (or UNSLOTH_GC_KEEP_LAYERS=auto) measures the first step and then stops
checkpointing 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.install no longer refuses under torchrun. Each rank plans as on one GPU, then
_agree_across_ranks runs one all_reduce(MIN) on the count at the planning forward, which every rank reaches at the
same 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).

  • Tensor parallel and pipeline parallel models stay fully checkpointed (their ranks hold different layers), and a
    message says so.
  • If the process group is not initialized by the planning step under a distributed launch, every layer stays
    checkpointed, as before.
  • Single-process runs are unchanged: there is no collective and the same count.

Tests

  • tests/test_gc_skip_auto_dist_gloo.py::test_auto_agrees_on_the_smallest_count_across_ranks: 2 gloo ranks with room
    for 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.

config base (auto refused) ms head (auto planned) ms change peak alloc GB base / head last loss base / head
DDP, seq 512 bs 2 ga 2 (3 + 3 runs) 639.1 (3.8%) 490.1 (0.9%) -23.3% 1.63 / 2.62 0.7243, 0.7242, 0.7241 / 0.7241, 0.7242, 0.7243
native FSDP2, seq 512 (2 + 2 runs) 666.0 (3.1%) 510.9 (0.6%) -23.3% 1.60 / 2.59 0.7243, 0.7242 / 0.7242, 0.7242
DDP, seq 2048 (1 + 1 run) 3475.1 2283.6 -34.3% (single run, no A/A) 1.62 / 5.78 0.2049 / 0.2049

Engagement: base logs checkpoint_skip_layers = "auto" is single process only on every rank (12 / 12 rank logs).
Head logs keeps 28 of 28 decoder layers un-checkpointed on 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.

GPU config default GC ms (A/A spread) auto ms (spread) change peak alloc GB loss (last step)
Colab L4 bf16 seq 512 bs 2 ga 2 435.0 (2.0%, n=3) 301.0 (2.6%, n=3) -30.8% 1.37 -> 2.35 0.7248 / 0.7245
Colab L4 bf16 seq 2048 1208.6 (1.1%, n=3) 916.2 (1.5%, n=2) -24.2% 1.57 -> 5.72 0.1872 / 0.1871
Colab L4 bf16 Qwen3-4B seq 2048 5310.8 (0.9%, n=2) 4350.6 (0.5%, n=2) -18.1% 8.57 -> 16.54 (of 22.5) 0.2103 / 0.2103
Kaggle T4 fp16 seq 512 512.6 (0.7%, n=3) 393.6 (2.6%, n=3) -23.2% 1.59 -> 2.58 0.7248 / 0.7248
Kaggle T4 fp16 seq 2048 2605.3 (5.6%, n=3) 1998.5 (3.1%, n=3) -23.3% 1.59 -> 5.74 0.1864 / 0.1864

…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).
@danielhanchen danielhanchen self-assigned this Oct 27, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-27T12:55:58.194512Z 9413436 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 0702821b7e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 9413436af1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@danielhanchen
danielhanchen merged commit 570b9b8 into main Oct 28, 2026
36 of 38 checks passed
@danielhanchen
danielhanchen deleted the dh/s9-auto-skip-dist branch October 28, 2026 05:36
danielhanchen added a commit to Etherll/unsloth that referenced this pull request Oct 28, 2026
…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.
danielhanchen added a commit that referenced this pull request Oct 28, 2026
….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.
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.

1 participant