Skip to content

fix: set HAS_FLEX_ATTENTION=True after successful import (closes #3650) - #5834

Closed
botbikamordehai2-sketch wants to merge 2 commits into
unslothai:mainfrom
botbikamordehai2-sketch:fix/issue-3650-1779962622
Closed

botbikamordehai2-sketch wants to merge 2 commits into
unslothai:mainfrom
botbikamordehai2-sketch:fix/issue-3650-1779962622

Conversation

@botbikamordehai2-sketch

Copy link
Copy Markdown

What

In unsloth/kernels/flex_attention.py, HAS_FLEX_ATTENTION is incorrectly set to False even after successfully importing flex_attention from torch.nn.attention.flex_attention. This causes the flex attention code path to never be used, always falling back to the slow attention path, which can cause maximum recursion depth errors with models like Gemma 3n.

Fix

Change HAS_FLEX_ATTENTION = False to HAS_FLEX_ATTENTION = True inside the try block after successfully importing and compiling flex attention.

Closes #3650

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request fixes a bug in unsloth/kernels/flex_attention.py where HAS_FLEX_ATTENTION was hardcoded to False even if the compilation of _flex_attention succeeded. It now correctly sets HAS_FLEX_ATTENTION to True upon successful compilation. There are no review comments, and I have no additional feedback to provide.

@Datta0

Datta0 commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

nice find. can you help us with what the attn_impl resolves to after the change for gemma3(n), gpt oss, gemma 4 etc?

@Datta0

Datta0 commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

Hey @botbikamordehai2-sketch apparently this is dead code and used nowhere. Can you check if this is being used anywhere? If not we can remove it altogether

@danielhanchen

Copy link
Copy Markdown
Member

HAS_FLEX_ATTENTION = False on the success path is intentional, not a bug. It was set deliberately in commit 6a1bef2 ('Disable Flex Attention', Oct 2024). Flipping it to True re-enables a disabled code path (used in unsloth/models/llama.py) with no tests, no benchmarks, and no discussion of why it was disabled, and it does not address #3650. If flex attention is to be re-enabled it needs a deliberate change with correctness and performance validation. Closing.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Gemma 3n - maximum recursion depth exceeded

3 participants