Repository navigation
Fix NaN gradients from compiled flex attention on short static training batches - #12207
Conversation
Inductor lowers a static query length below 128 to flex_decoding, whose logsumexp gets a padded batch stride from comprehensive_padding. The flex backward template indexes LSE as contiguous, so with batch > 1 every row after the first reads a shifted LSE and the gradients blow up (Qwen3.5 LoRA on B200: 550 non-finite LoRA grads at step 0).
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 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: 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". |
LoRA training on Qwen3.5 (and any model Unsloth runs through flex attention) can produce inf / NaN gradients at step 0, while the loss looks normal. It happens when the first training-mode flex call of the process has a static query length below 128 and a batch size above 1. A short no-grad forward first hides it, which made it look like a warm-up problem.
Root cause
This is a PyTorch Inductor bug in compiled flex attention, not in the model code.
flex_decodingkernel.comprehensive_paddingpads its batch stride. For (2, 24, 121) the saved buffer isempty_strided_cuda((2, 24, 121), (2912, 121, 1))instead of stride 2904.off_chz2 = (off_zq * HQ + off_hq2) * Q_LEN). The lowering makesdeltacontiguous but notlogsumexp.PyTorch main has the same lowering, so a torch upgrade does not fix it.
Pure PyTorch repro (no Unsloth, no transformers; B200, torch 2.13, B=2, H=24, S=121, head_dim 256, causal block mask,
torch.compile(flex_attention)vs eager flex, relative error):kernel_options={"FORCE_USE_FLEX_ATTENTION": True}comprehensive_padding=Falsehead_dim 64 is affected as well (dq relative error 104).
This also explains the earlier observations: a short first forward makes the later length-121 call recompile with dynamic shapes, and the decoding kernel needs static shapes. Transformers recompiles flex when
trainingchanges, so a no-grad warm-up afterget_peft_modeldoes not help.Fix
Unsloth already wraps the registered transformers
flex_attentionfunction to passkernel_optionsfor large head dims. The wrapper now also setsFORCE_USE_FLEX_ATTENTION=Trueon calls that need a backward: grad enabled, a non-CPU device, and q / k / v requiring grad.BACKENDis left alone, because torch rejects it combined with the legacy flag. A caller's ownFORCE_USE_FLEX_ATTENTIONvalue wins.The proper fix belongs in Inductor (
require_contiguousonlogsumexpin the flex backward lowering); this avoids the broken kernel pairing until then.Results
unsloth/Qwen3.8-27B-NVFP4, B200, batch (2, 121), LoRA r=16, first step:unslothunslothdynamic=TrueTests
tests/test_flex_large_head_dim_kernel_options.pygains 4 tests:BACKENDor the caller's own flag value is respected;25 passed. With
unsloth/models/_utils.pyfrom main and the tests kept, 2 fail, includingdq relative error 47.6 vs eager flex attention.Not covered