Skip to content

Flex wrapper: keep the causal mask inside compiled blocks on torch < 2.10 - #16234

Merged
danielhanchen merged 6 commits into
unslothai:mainfrom
EvanZhuang:fix/flex-dropped-mask-compiled
Nov 4, 2026
Merged

danielhanchen merged 6 commits into
unslothai:mainfrom
EvanZhuang:fix/flex-dropped-mask-compiled

Conversation

@EvanZhuang

@EvanZhuang EvanZhuang commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Inside the per-layer compiled block (#10744, 2026.10.6), Dynamo on torch 2.8/2.9 evaluates hasattr(query, "dim") as False (fixed in torch 2.10, pytorch/pytorch#167651). _dropped_causal_mask therefore falls through and stock flex attention receives attention_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. On Qwen/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 with isinstance(query, torch.Tensor) (Dynamo reads that correctly on every version) and keeps the duck-typed dim() check for eager stand-ins. All four hasattr(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 x is_causal) match the merge base.

Tests

  • test_a_dropped_mask_is_still_causal_in_a_compiled_block in tests/test_flex_maskless_sdpa.py: checks a mask-less causal call under torch.compile(..., backend="eager") against SDPA is_causal=True (CUDA-only; fails without the fix on torch <= 2.9, passes with it).
  • Existing flex and block-compile suites on H100: 362 passed, 10 skipped on torch 2.9.1 (plus the 1 pre-existing test_flex_mask_graph_breaks.py failure) and 369 passed, 4 skipped on torch 2.14.1.
  • test_wrapper_injects_kernel_options_inside_a_compiled_block in tests/test_flex_large_head_dim_kernel_options.py (CPU): head_dim 512 kernel options reach a compiled call.
  • A100, torch 2.9.1: every tests/test_flex_*.py gives 268 passed with this branch. Before the _is_4d change, the new compiled kernel-options test failed. test_flex_mask_graph_breaks.py fails on 2.9.1 both before and after (pre-existing). torch 2.10.0: 271 passed on both.
  • FastModel LoRA in train mode with flex attention (tiny Qwen3.5 head_dim 256 and tiny Qwen3, A100, torch 2.9.1): changing only the last token moved 56,150,370 of 74,247,680 earlier logits at the merge base and 0 with this PR, and the loss matches the torch 2.10 value.

…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]>
@oobabooga

Copy link
Copy Markdown
Member

Reached Codex review convergence at oobabooga#449.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-04T16:00:15.184115Z 62d8fdf Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 1b59fd56e8

ℹ️ 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".

# Conflicts:
#	unsloth/models/_utils.py

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 62d8fdf2f1

ℹ️ 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".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants