Repository navigation
CPU backend: install the every_n_layers gradient checkpointing router - #16730
Conversation
…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.
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. 🎉 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". |
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.
|
@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". |
Summary
On a host with no GPU,
import unslothloadsunsloth/_cpu_init.pyand neverunsloth/models/_utils.py, sopatch_gradient_checkpointing_enable_kwargswas never installed on that route. Selective checkpointing withevery_n_layersthen broke in several ways (tiny Qwen3, 4 decoder layers, checkpoint calls per layer, expected[2, 0, 2, 0]):SFTConfig(gradient_checkpointing_kwargs = {"every_n_layers": 2})[2, 2, 2, 2], silently[2, 0, 2, 0]gradient_checkpointing_enable(gradient_checkpointing_kwargs = {"every_n_layers": 2})[2, 0, 2, 0]every_n_layers = 2[2, 0, 2, 0]Change
_skip_aware_flag,_apply_every_n_layers, the kwargs patch) move fromunsloth/models/_utils.pyinto a new light module,unsloth/_gc_kwargs.py(functools / inspect only, transformers imported lazily).models/_utils.pyimports them back, so the GPU route is unchanged._cpu_init.pyinstalls the patch.The keyword form surviving
for_training(True)andget_peft_modelon the CPU route also needs a matching Unsloth Zoo change toFastCPUModel(it re-enables checkpointing even when it is already on); that is a separate PR there.Testing
tests/test_gc_every_n_cpu_route.pytests/test_gc_every_n_cpu_route.py,test_gc_enable_kwargs.py,test_gc_skip_auto.py,test_block_swap_install.pyCPU 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
every_n_layersx parallel layouts was not run on CPU.