Repository navigation
Skip the replayed LoRA MLP down projection under block compile - #18641
Conversation
On torch >= 2.11 block compile traces each decoder layer, so the eager thread-local token never fires. The block wrapper now sets a guarded global around a checkpoint replay of an eligible layer (the tagged LlamaDecoderLayer_fast_forward with the fused SwiGLU LoRA MLP and no forward hooks); the traced LoRA_MLP drops the down GEMM in that graph. The no-grad forward call trials the same skip graph, so each layer kind still compiles two graphs and later steps compile nothing.
|
@codex review |
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: 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". |
|
@codex review |
|
Codex Review: Didn't find any major issues. You're on a roll. 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 `Repo tests (CPU, rest)` is red on `main` (seen on #18650, reproduced on current `main`): ``` FAILED tests/test_autograd_inplace_outputs.py::test_no_inplace_mutation_of_returned_fresh_tensors AssertionError: unsloth/kernels/fast_lora.py:686 LoRA_MLP: i.fill_(_SKIPPED_FILL) ``` ## Cause #18607 (skip the LoRA MLP down projection in gradient checkpoint replays) builds the skipped output as `i = X.new_empty(X.shape)`. When the test-only `_SKIPPED_FILL` sentinel is set, it then calls `i.fill_(_SKIPPED_FILL)`. `i` is returned from `LoRA_MLP.forward`. The guard exists because torch 2.11 compiles a wrong gradient for an ATen in-place op on a fresh tensor a `torch.autograd.Function` forward returns (#14712), and the block-compile path (#18641) traces this forward. The guard is right, and the skip itself is right: only the mutation needs to go. ## Change - `unsloth/kernels/fast_lora.py`: allocate the skipped output already filled, `X.new_full(X.shape, _SKIPPED_FILL)`, when the sentinel is set. Otherwise it stays `X.new_empty(X.shape)`. The values are identical (`new_empty` + `fill_` and `new_full` both give a tensor of that shape full of that value), and production never sets the sentinel. There is no in-place op left on the returned tensor. ## Testing - `tests/test_autograd_inplace_outputs.py` fails on `main` with the line above and passes with this change. - `tests/test_gc_skip_replay_mlp_down.py` and `tests/test_autograd_inplace_outputs.py` on CPU: 20 passed, 6 skipped. The skipped six need CUDA. - Not run here: those six CUDA tests, which fill the skipped output with NaN to prove nothing reads it, and `test_gc_skip_replay_mlp_down_block_compile.py` (torch >= 2.11 with CUDA). This host has no local GPU for this session, and the remote attempt failed in dependency setup before any test ran. CI's GPU jobs will exercise them.
…rming layers (#18688) ## Summary `Repo tests (CPU, rest)` fails intermittently on current `main`. It is seen on #18650 and #18532, both based on recent `main`, and both runs include #18607 and #18641: ``` FAILED tests/test_gc_skip_replay_mlp_down.py::test_only_the_post_norm_free_layer_arms E AssertionError: assert ['LlamaDecode...e_at_514_178'] == ['LlamaDecode...fast_forward'] E Left contains one more item: '__resume_at_514_178' ``` ## Cause #18607 added this test. It collects every module-level function in `unsloth.models.{llama, gemma, ...}` whose source calls `arm_mlp_output_unread(` and expects exactly `LlamaDecoderLayer_fast_forward`. When an earlier test in the same xdist worker compiles that layer and hits a graph break, Dynamo parks the continuation function (`__resume_at_<offset>_<n>`) in the compiled function's module globals. That puts it in `unsloth.models.llama`. Its code object points at `LlamaDecoderLayer_fast_forward`'s lines, so `inspect.getsource` returns the same source, and it counts as a second arming layer. The test passes alone and fails only when such a compile ran first in the worker. The product code is right; the test's view of the module namespace was not. ## Change `tests/test_gc_skip_replay_mlp_down.py`: only count a function when its code name matches the name it is bound under (`fn.__code__.co_name == fn_name`). Dynamo's resume functions are bound as `__resume_at_*` but named `torch_dynamo_resume_in_*`, so they drop out. Every function defined in the module with `def` still matches. ## Testing - `tests/test_gc_skip_replay_mlp_down.py`: 18 passed, 6 skipped (the six need CUDA). - Reproduced the CI failure with a conftest that plants a resume function exactly as Dynamo does: same code with `co_name = "torch_dynamo_resume_in_LlamaDecoderLayer_fast_forward_at_514"`, bound as `__resume_at_514_178` in `unsloth.models.llama`. The test from `main` fails with `Left contains one more item: '__resume_at_514_178'`; this version passes. - The guard still does its job: planting a real second arming function in `unsloth.models.gemma` still fails it (`Left contains one more item: 'GemmaDecoderLayer_arming'`).
Summary
Follows up #18607 (and its zoo companion unslothai/unsloth-zoo#2630), both merged. #18607 skips the LoRA MLP down projection while a gradient checkpoint replays a Llama-style decoder layer, but only on the eager path. On torch >= 2.11, the default, block compile traces each decoder layer, so the eager thread-local token never fires and the skip runs 0 times. This PR brings the skip to the compiled block.
How
_block_compile._wrap.forward(eager, outside the compiled region) decides per call whether this is a checkpoint replay of exactly this layer (gradient_checkpoint_replay_module() is layer) and whether the layer is eligible:_unsloth_mlp_output_unread(onlyLlamaDecoderLayer_fast_forward, which Llama / Qwen2 / Qwen3 / Mistral use; its MLP output only feedsresidual + mlp(x));forwardisapply_lora_mlp_swiglu, with no accelerate hook or tiled MLP around it;fast_lora._BLOCK_SKIP_DOWNaround the compiled call. The tracedLoRA_MLP.forwardreads it, so dynamo guards on it, and the replay graph returns an unfilled tensor instead of runningact(e) * gand the down GEMM (plus its NF4 dequantize).checkedbit (2 = full, 4 = skip). For example, training without checkpointing or with a hooked layer compiles that graph on first use, through the usual strict compile and demotion path.LoRA_MLP's other branch, which always runs the down projection.UNSLOTH_GC_SKIP_MLP_DOWN=0turns it off, as in Skip the LoRA MLP down projection in gradient checkpoint replays #18607.Tests
tests/test_gc_skip_replay_mlp_down_block_compile.py(CPU, torch >= 2.11). It wraps the realQwen3DecoderLayerwith block compile's_wrap(aot_eager backend), uses real PEFT LoRA,LlamaDecoderLayer_fast_forwardand Unsloth's reentrant checkpoint, with the skipped output NaN-filled.Numbers
Colab, torch 2.14.1+cu130 (the default block compile path),
unsloth/Qwen3-4B-unsloth-bnb-4bit, LoRA r16 on all projections,use_gradient_checkpointing = "unsloth", bs1. Each arm takes 3 warmup steps, then the median of 8 steps. Base (#18607 head, equal to main for this code) and head are interleaved 3 times each; the spread is the range over the 3 runs. Every arm compiles cold (TORCHINDUCTOR_FORCE_DISABLE_CACHES=1).UNSLOTH_GC_SKIP_MLP_DOWN=0.tests/test_block_compile.pygive 115 passed, 2 skipped (both pre-existing). The zootests/test_gc_replay_module.pygives 8 passed.AMD gfx1151 (Linux + Windows 11, torch 2.11 ROCm)
UNSLOTH_GC_SKIP_MLP_DOWN=0; losses bitwise equal; the same graph and graph-break counts on both arms, no recompiles.