Skip to content

refactor flex attn to prefer flash if possible - #4734

Merged
danielhanchen merged 8 commits into
unslothai:mainfrom
Datta0:flex_refactor
Apr 1, 2026
Merged

danielhanchen merged 8 commits into
unslothai:mainfrom
Datta0:flex_refactor

Conversation

@Datta0

@Datta0 Datta0 commented Mar 31, 2026

Copy link
Copy Markdown
Contributor

The previous impl was setting it to flex_attn for almost every model causing the models to not use FA2. This change creates a preference hierarchy

@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.
Repo admins can enable using credits for code reviews in their settings.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@Datta0

Datta0 commented Mar 31, 2026

Copy link
Copy Markdown
Contributor Author

Re work of #4250 : refactor to avoid issues

danielhanchen and others added 3 commits April 1, 2026 05:01
- Restore gemma3n explicit eager default in vision.py so timm-based
  vision towers are not switched to sdpa by the new hierarchy.
- Re-stamp config to eager when the supports_sdpa guard fires and
  deletes kwargs["attn_implementation"], keeping config consistent.
- Export _set_attn_impl in __all__ so vision.py can use it.
- Return impl from _set_attn_impl for convenience.
When model_class is None (remote-code or unmapped configs), the old
code defaulted to sdpa when supports_sdpa was True. The new
determine_attention_implementation returns eager since all checks
gate on model_class is not None. Add explicit fallback to preserve
the old behavior.

@danielhanchen danielhanchen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you for the PR! The goal of this PR is to refactor the attention implementation selection from the narrow prefer_flex_attn_if_supported (which only returned flex or None) into a proper hierarchical selector determine_attention_implementation that picks the best available backend: FA2 > Flex > SDPA > Eager. As a summary, this PR centralizes attention backend selection into one function, eliminating the per-caller fallback logic that was previously scattered across llama.py and vision.py.

I pushed two fix commits on top of the PR:

  1. Gemma3N eager default -- restored the explicit eager override in vision.py for gemma3n so timm-based vision towers are not promoted to SDPA by the new hierarchy.

  2. Unmapped vision config fallback -- when model_class is None (remote-code or unmapped configs), the new function returns eager since all checks gate on model_class is not None. The old code defaulted to sdpa for these. Added explicit fallback to preserve that behavior.

  3. Config re-stamping -- when the supports_sdpa=False guard fires in vision.py and deletes kwargs["attn_implementation"], the config's _attn_implementation was still set to sdpa. Now re-stamped to eager for consistency.

Reviewed by 8 independent reviewers. Here are the consolidated findings:

Reviewers Severity Finding
6/8 Fixed Unmapped/remote-code vision configs regress from sdpa to eager when model_class=None
6/8 Fixed Gemma3N defaults to sdpa instead of eager in vision.py
6/8 Fixed Config not re-stamped when supports_sdpa guard fires
3/8 Not an issue prefer_flex_attn_if_supported removed from exports -- verified no downstream or unsloth_zoo callers exist
2/8 Intentional FA2 selected for flex-excluded models (mllama, gpt_oss) when FA2 installed -- FA2 support is gated by _supports_flash_attn_2 class attribute which is the canonical HuggingFace compatibility check
2/8 Intentional llama.py excluded models get sdpa instead of eager -- SDPA is well-tested and correct for these models
1/8 Not an issue FA2 forced on float32 -- Unsloth always loads in bf16/fp16, float32 path is not exercised
1/8 Nitpick sentence_transformer.py has its own SDPA logic not updated -- separate scope from this PR

@danielhanchen

Copy link
Copy Markdown
Member

/gemini review

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@danielhanchen
danielhanchen merged commit 256c6e4 into unslothai:main Apr 1, 2026
1 check passed
shibizhao pushed a commit to shibizhao/unsloth-npu that referenced this pull request Apr 7, 2026
Replaces prefer_flex_attn_if_supported (which only returned flex_attention or None) with determine_attention_implementation, a centralized hierarchy: FA2 > Flex > SDPA > Eager.

Changes:
- New determine_attention_implementation function in _utils.py with clear priority chain
- _set_attn_impl helper to stamp config consistently
- _FLEX_EXCLUDED_MODELS / _FLEX_EXCLUDED_PREFIXES for model-specific exclusions
- Gemma3N explicit eager override in vision.py (timm vision towers)
- Preserved sdpa fallback for unmapped/remote-code vision configs
- Config re-stamped to eager when supports_sdpa guard fires

Co-authored-by: Datta Nimmaturi <[email protected]>
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.

2 participants