Repository navigation
Flex wrapper: keep the causal mask inside compiled blocks on torch < 2.10 - #16234
Conversation
…2.10 On torch 2.8/2.9, Dynamo evaluates `hasattr(query, "dim")` as False inside compiled decoder blocks (fixed upstream in torch 2.10, pytorch#167651), so `_dropped_causal_mask` returns False and passes `attention_mask=None` (bidirectional) to stock flex attention for unpadded causal training rows. Check `isinstance(query, torch.Tensor)` instead. Co-authored-by: gemini-code-assist Signed-off-by: Yufan Zhuang <[email protected]>
|
Reached Codex review convergence at oobabooga#449. |
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: Didn't find any major issues. Chef's kiss. 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". |
# Conflicts: # unsloth/models/_utils.py [skip ci]
|
@codex review |
1 similar comment
|
@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
Inside the per-layer compiled block (#10744,
2026.10.6), Dynamo on torch 2.8/2.9 evaluateshasattr(query, "dim")asFalse(fixed in torch 2.10, pytorch/pytorch#167651)._dropped_causal_masktherefore falls through and stock flex attention receivesattention_mask=None(bidirectional attention) for unpadded training rows, such as SFTTrainer at batch size 1, on Qwen3.5/3.6 (head_dim=256) on H100. OnQwen/Qwen3.5-0.8B(FastModel fp32, 300 tokens, H100, torch 2.9.1), changing only the last token moves 74,246,486 of 74,247,680 earlier train-mode logits before this fix and 0 after (SFTTrainer step-1 loss 0.4505 vs 1.3346 causal; 1.3304 after). This PR adds_is_4d, which matches real tensors withisinstance(query, torch.Tensor)(Dynamo reads that correctly on every version) and keeps the duck-typeddim()check for eager stand-ins. All fourhasattr(query, "dim")checks in the flex wrapper now use it: the dropped causal mask and the head_dim > 256 kernel options were both lost inside compiled blocks on torch < 2.10, and the other two sit behind torch >= 2.10 device gates. Eager routing is unchanged: 672 of 672 predicate cells (query kind x key length x mask form xis_causal) match the merge base.Tests
test_a_dropped_mask_is_still_causal_in_a_compiled_blockintests/test_flex_maskless_sdpa.py: checks a mask-less causal call undertorch.compile(..., backend="eager")against SDPAis_causal=True(CUDA-only; fails without the fix on torch <= 2.9, passes with it).test_flex_mask_graph_breaks.pyfailure) and 369 passed, 4 skipped on torch 2.14.1.test_wrapper_injects_kernel_options_inside_a_compiled_blockintests/test_flex_large_head_dim_kernel_options.py(CPU): head_dim 512 kernel options reach a compiled call.tests/test_flex_*.pygives 268 passed with this branch. Before the_is_4dchange, the new compiled kernel-options test failed.test_flex_mask_graph_breaks.pyfails on 2.9.1 both before and after (pre-existing). torch 2.10.0: 271 passed on both.