Repository navigation
Keep the load-time gradient checkpointing mode in for_training - #12629
Conversation
for_training() defaulted to use_gradient_checkpointing=True and set the flag on every module. On a model loaded with use_gradient_checkpointing=False, transformers' GradientCheckpointingLayer then tried to call a _gradient_checkpointing_func that gradient_checkpointing_enable() never installed, so the next training forward raised AttributeError. The default is now None, which restores the mode recorded at load / get_peft_model (True when nothing was recorded, as before), and a checkpointing layer without a checkpoint function is never switched on.
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 security review |
🛡️ Codex Security ReviewSecurity review completed. No security issues were found in this pull request. Reviewed commit: Only the user who started this review can view the report in Codex. ℹ️ About Codex security reviews in GitHubThis is an experimental Codex feature. Security reviews are triggered when:
Once complete, Codex will leave suggestions, or a comment if no findings are found. |
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 658bac09ec
ℹ️ 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".
| if use_gradient_checkpointing: | ||
| arm_gradient_checkpointing(model) |
There was a problem hiding this comment.
Install smart checkpointing when requesting Unsloth mode
When a model was prepared with checkpointing disabled and the caller later uses for_training(..., use_gradient_checkpointing="unsloth"), the earlier preparation has removed the smart-checkpointing patch, while this path only calls ordinary gradient_checkpointing_enable(). The layers are then labeled with the truthy string "unsloth", but their checkpoint function is the standard Transformers implementation, so the requested activation offloading and associated memory savings are absent. The new test only verifies that the function is callable and therefore misses this mode mismatch; this path must apply the Unsloth checkpointing configuration before arming the layers.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not changing this: the late enable gives real checkpointing (Qwen3-0.6B, 1.6k tokens: peak 4.18 GB off, 1.17 GB armed late, 1.04 GB with "unsloth" at load, same loss, every LoRA B gets gradients), so nothing is lost or wrong, only the activation offload is missing. Load-time "unsloth" also does not bind the zoo checkpoint on the layers (they keep torch.utils.checkpoint.checkpoint while the module-level function is patched), so re-running apply_unsloth_gradient_checkpointing mid-session would arm a different function than loading does. Loading with "unsloth" stays the way to get offloading.
… themselves, keep unrecorded load choices
…orwards as checkpointing
|
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". |
…thai#12629) for_training() no longer forces gradient checkpointing on: an omitted mode keeps the one chosen at load / get_peft_model, an explicit enable on a model loaded without checkpointing arms it through gradient_checkpointing_enable() (and turns use_cache off), and a layer without a checkpoint function is never switched on, so models loaded with use_gradient_checkpointing=False no longer crash with a missing _gradient_checkpointing_func.
Problem
Loading with
use_gradient_checkpointing = Falseand then callingFastLanguageModel.for_training(model)(orFastModel.for_training(model), which notebooks do after an inference cell) crashes on the next training step:for_trainingdefaulted touse_gradient_checkpointing = Trueand set that flag on every module. In transformers 5 the decoder layers areGradientCheckpointingLayers, which checkpoint themselves through_gradient_checkpointing_func. That attribute is only installed bygradient_checkpointing_enable(), which never ran because checkpointing was turned off at load. Training with checkpointing off works fine as long asfor_trainingis not called.Fix
for_training(model, use_gradient_checkpointing = None):Nonenow restores the mode recorded at load /get_peft_model(model._unsloth_gradient_checkpointing, the same value the GRPO trainer already restores). With nothing recorded, it follows whether loading armed the checkpointing layers, and models without any keep the old default ofTrue.True/"unsloth") on a model loaded without checkpointing now arms it first throughgradient_checkpointing_enable(), the same call the load path uses, instead of setting a flag with nothing behind it. It calls the outer model'sgradient_checkpointing_enable, so the per-model overrides Unsloth installs there (Gemma 3N / 4 reentrant, DeepSeek-V4.1 non-reentrant) still apply.GradientCheckpointingLayers and any module whose ownforwardcallsself._gradient_checkpointing_func(older and remote-code backbones). One that still has no checkpoint function (a model whosegradient_checkpointing_enable()refuses) is never switched on, so it trains without checkpointing instead of crashing.FastLlamaModel,FastBaseModeland theFastModelwrapper;FastDiffusionModel.get_peft_modelnow records its choice too, soFastModel.for_training(model)keeps it.use_cacheoff, as loading with checkpointing on does (for_inferencerestores it)."unsloth", so there is no activation offload (Qwen3-0.6B, 1.6k tokens: 1.17 GB armed late vs 1.04 GB with"unsloth"at load). Loading with"unsloth"stays the way to get offloading.Results
for_training(model)for_training)for_training(model)for_training(model, use_gradient_checkpointing = True), 1.6k tokensFastModel, GC off, thenfor_training(model)for_training(model)"unsloth"(default path)Every input
for_trainingover class (Llama path, generic path) x recorded mode (none,"unsloth",True,False) x layers armed or not x argument (omitted,True,"unsloth",False), 64 cells, main vs this PR:False, or nothing recorded and loaded without checkpointing)."unsloth"is restored as"unsloth"instead ofTrue(same behaviour, both truthy), and a recordedFalsefromget_peft_model(use_gradient_checkpointing = False)is now respected instead of being switched back on. The trainers already restore the recorded value at init, so that only differs in custom training loops.for_trainingper call on Qwen3-0.6B (2389 modules), interleaved: main 11.2 / 10.6 ms, this PR 10.5 / 11.1 ms.Tests
tests/test_for_training_gc_off.py: 26 tests (default keeps checkpointing off when it was off at load, an explicit enable arms it and every layer gets gradients, the default restores the recorded mode, unrecorded models keep their load-time choice, a backbone that calls the checkpoint function itself, the outergradient_checkpointing_enableoverride is used, a late enable turnsuse_cacheoff andfor_inferencerestores it, a source-unreadable forward counts as checkpointing,FastModelkeeps a diffusion adapter's recorded choice).tests/test_for_training_generation_flag.pyrebuildsfor_trainingfrom source with only the helpers in the same file, so it now also pulls the new helpers from_utils.py.for_training/ gradient checkpointing fail the same set on main and on this branch (only pre-existingtests/test_rms_rope_compile.pyfailures on this machine).