Skip to content

CPU backend: install the every_n_layers gradient checkpointing router - #16730

Merged
danielhanchen merged 4 commits into
mainfrom
dh/cmp-mem-cpu-every-n
Nov 5, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
dh/cmp-mem-cpu-every-n

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

On a host with no GPU, import unsloth loads unsloth/_cpu_init.py and never unsloth/models/_utils.py, so patch_gradient_checkpointing_enable_kwargs was never installed on that route. Selective checkpointing with every_n_layers then broke in several ways (tiny Qwen3, 4 decoder layers, checkpoint calls per layer, expected [2, 0, 2, 0]):

how every_n_layers is passed transformers 5.17, before transformers 4.57.6, before after (both)
SFTConfig(gradient_checkpointing_kwargs = {"every_n_layers": 2}) works (natively) [2, 2, 2, 2], silently [2, 0, 2, 0]
gradient_checkpointing_enable(gradient_checkpointing_kwargs = {"every_n_layers": 2}) ValueError: Unexpected keyword arguments ValueError [2, 0, 2, 0]
keyword every_n_layers = 2 works TypeError [2, 0, 2, 0]

Change

  • The router and its helpers (_skip_aware_flag, _apply_every_n_layers, the kwargs patch) move from unsloth/models/_utils.py into a new light module, unsloth/_gc_kwargs.py (functools / inspect only, transformers imported lazily). models/_utils.py imports them back, so the GPU route is unchanged.
  • _cpu_init.py installs the patch.
  • Three tests that load these helpers by AST now read the new file.

The keyword form surviving for_training(True) and get_peft_model on the CPU route also needs a matching Unsloth Zoo change to FastCPUModel (it re-enables checkpointing even when it is already on); that is a separate PR there.

Testing

suite base head
new tests/test_gc_every_n_cpu_route.py FAIL PASS
tests/test_gc_every_n_cpu_route.py, test_gc_enable_kwargs.py, test_gc_skip_auto.py, test_block_swap_install.py 28 failed, 9 errors (the updated AST tests read the new module) 99 passed, 10 skipped

CPU gloo composition run (2 ranks, tiny Qwen3, 3 steps, loss / grads / params vs one process): gradient checkpointing (torch, Unsloth reentrant, Unsloth non-reentrant offload) passes under DDP, FSDP2, TP and PP, eager and block compile.

Not covered

  • GPU: the GPU route already installed the patch; this PR only moves the code, but no GPU run was made here.
  • every_n_layers x parallel layouts was not run on CPU.

…ing router

On a CPU-only host import unsloth loads unsloth/_cpu_init.py and never
unsloth/models/_utils.py, so patch_gradient_checkpointing_enable_kwargs was
not installed there. gradient_checkpointing_enable(gradient_checkpointing_kwargs
= {"every_n_layers": N}) raised "Unexpected keyword arguments: every_n_layers"
at the first forward (also TrainingArguments on transformers < 5.17), and a
keyword every_n_layers did not survive a flag rewrite.

Move the router and its helpers into unsloth/_gc_kwargs.py (functools /
inspect only, transformers imported lazily), import them back in
models/_utils.py, and install it from _cpu_init.py. Tests that load the
helpers by AST read the new file.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 5, 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-11-05T06:00:13.704392Z 87fa708 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. 🎉

Reviewed commit: 0e262b51ed

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

The other moved helpers are read only inside unsloth/_gc_kwargs.py, and the
source lint flags re-imported names nothing in _utils.py uses.
@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: 87fa7080dd

ℹ️ 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 f450da3 into main Nov 5, 2026
@danielhanchen
danielhanchen deleted the dh/cmp-mem-cpu-every-n branch November 5, 2026 07:32
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