Repository navigation
Block compile: transformers 5.24 cache contract, second hooked model, replay skip eligibility - #18986
Conversation
transformers 5.24 renamed the layer cache keyword from past_key_values to cache and stopped checkpointing a training-mode layer when grad is off (so generate() from a model left in train() keeps its cache). The block's checkpoint shim still checkpointed under no_grad and never cleared cache=, and _has_cache missed cache=, so a cached call ran the compiled block and could write the cache twice (8 keys against a 4-position mask). Also rebind accelerate-hooked layers of a second model whose layer class was already wrapped for an earlier model: the rebind only ran on the first wrap, so the second model kept the stale bound forward with no checkpointing.
…l__ overrides block_skip_down_eligible let an MLP with LoRA dropout or a lora_B bias through, though LoRA_MLP's dropout / bias branch always runs the down projection: the compiled block counted a skip in MLP_DOWN_SKIP_STATS that never happened and compiled a replay graph for the flag. Refuse them. _mlp_output_unobserved also accepted an MLP class overriding __call__, which can transform mlp(x) outside its forward; with a squaring __call__ the eager replay skip changed 29 of 43 gradients. Refuse those too.
Codex Review Summary: ✅ CompletedThis 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. Breezy! 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 checkpoint shim applied the transformers 5.24 rule (skip checkpointing when grad is off) on every version. On 5.23 the stock wrapper still checkpoints a training-mode layer under no_grad with use_cache=False and past_key_values=None, so a cache handed to the layer stays empty; the shim ran the layer as is and filled it (real LlamaDecoderLayer: 0 stock vs 3). Derive the contract from the transformers wrapper being replaced: the grad gate when its code calls is_grad_enabled, and only the cache keywords it clears. Unknown wrappers keep the 5.24 rule and clear every cache name. [skip ci]
|
@codex review |
|
Codex Review: Didn't find any major issues. Can't wait for the next one! 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". |
What
Follow-ups to #18811 and #18934 in block compile (
unsloth/models/_block_compile.py,unsloth/kernels/fast_lora.py):_checkpointing) now follows the transformers 5.24GradientCheckpointingLayerwrapper. It checkpoints only while grad is enabled, and it clears the renamedcache=keyword as well aspast_key_values=/layer_past=._has_cachealso treatscache=as a cache call, so cached calls keep the stock forward as they did withpast_key_values=.patch_block_compilerebinds accelerate-hooked layers of a model whose layer class was already wrapped for an earlier model. The installed forward keeps the forward it replaced (_unsloth_block_before) for that rebind.block_skip_down_eligiblerefuses MLPs whose gate / up / down adapters have a dropout module or alora_Bbias._mlp_output_unobservedrefuses an MLP class that overrides__call__.Why
cache. It also stopped checkpointing a training-mode layer underno_grad, sogenerate()on a model left intrain()keeps its cache. The shim still checkpointed underno_grad, never clearedcache=, and_has_cachemissedcache=. A cached call therefore ran the compiled block and could write the cache twice. The repro was a two-layer random Llama on transformers 5.24.0 behind accelerate dispatch hooks, CPU, torch 2.14.1. The block was installed withaot_eager:no_gradprefill + decode in train mode: main raisedThe size of tensor a (6) must match the size of tensor b (3)(6 cached keys against a 3-position mask), after 17 graphs and 49 breaks. With this change the cache lengths are 3 then 4 and the logits match stock transformers exactly (max delta 0), with 0 graphs._old_forwardonly when it wraps the class. A second model of the same layer class, dispatched before the first was patched, kept the stale bound wrapper. On transformers 5.23+ that means no checkpointing and no compiled block. Same repro, transformers 5.24: the second model's layer ran its projection once with no regional graph on main (no checkpoint replay). With this change it runs twice with one graph, the same as the first model.lora_Bbias takeLoRA_MLP's branch that always runs the down projection. The compiled block still passed the skip flag (an extra replay graph) and counted a skip inMLP_DOWN_SKIP_STATS. On Gemma GEGLU withlora_dropout=0.1, main counted 3 skips for 3 layers whose replay outputs were all finite (none skipped). An MLP subclass that overrides__call__can transformmlp(x)outside its forward. With a__call__that squares the output, main still skipped that layer's down projection in eager. The backward through the square then read the placeholder: 29 of 43 gradient tensors differed from skip-off, on both GEGLU forms and on Llama SwiGLU.Tests
Each new test fails on main (
632c437424) and passes here (CPU, torch 2.14.1):tests/test_block_compile.py::test_block_checkpoint_shim_keeps_the_5_24_cache_contract: main checkpointed underno_grad([1] == []).tests/test_block_compile.py::test_block_rebinds_hooked_layers_of_a_second_model: main ran the second model with no checkpoint ([] == [1]).tests/test_gc_skip_replay_mlp_down_gemma.py::test_mlp_call_override_keeps_down_projection[gemma_approx|gemma_exact|llama]: main counted 3 skips where 2 are allowed.tests/test_gc_skip_replay_mlp_down_gemma.py::test_compiled_replay_with_lora_dropout_counts_no_skip[gemma_approx|gemma_exact]: main reported a dropout MLP as skip-eligible.Suites, run with unsloth-zoo main for
gradient_checkpoint_replay_module:tests/test_block_compile.py,tests/test_gc_skip_replay_mlp_down.py,tests/test_gc_skip_replay_mlp_down_gemma.pyandtests/test_gc_skip_replay_mlp_down_block_compile.pyon transformers 4.57.6: 134 passed, 25 skipped (CUDA only).tests/test_block_compile.pyon transformers 5.23.0 (torch 2.15.0) and on 5.24.0: 81 passed, 9 skipped. The 2 failures in each venv also fail on main:vllm.sampling_paramsis missing there.Not covered
generate()from a QLoRA Llama on transformers 5.24 with block compile on, once on a single GPU and once withdevice_mapsplit across 2 GPUs. Also run a Gemma LoRA withlora_dropout > 0under block compile and check the graph count.torch.compile(fullgraph=True)around a hooked model still breaks on the block's inline flags inside the checkpoint. The FP8-LoRA per-instancelayer.forwardstill shadows accelerate's hook (this predates Block compile: keep gradient checkpointing on layers behind accelerate device-map hooks #18811).GPU check
Colab L4, transformers 5.24.0, QLoRA Llama-3.2-1B through FastModel with block compile on, left in train(): a no-grad prefill then one cached step raises
expanded size (12) must match (6)on the merge base; with this PR the cache goes 6 then 7. Gemma with lora_dropout=0.1: base marks the layer skip-eligible and compiles 3 graphs, this PR refuses and compiles 2. Unit tests: base 7 failed, head 129 passed.