Repository navigation
refactor flex attn to prefer flash if possible - #4734
Conversation
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
|
Re work of #4250 : refactor to avoid issues |
- 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
left a comment
There was a problem hiding this comment.
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:
-
Gemma3N eager default -- restored the explicit
eageroverride in vision.py for gemma3n so timm-based vision towers are not promoted to SDPA by the new hierarchy. -
Unmapped vision config fallback -- when
model_classis None (remote-code or unmapped configs), the new function returnseagersince all checks gate onmodel_class is not None. The old code defaulted tosdpafor these. Added explicit fallback to preserve that behavior. -
Config re-stamping -- when the
supports_sdpa=Falseguard fires in vision.py and deleteskwargs["attn_implementation"], the config's_attn_implementationwas still set tosdpa. Now re-stamped toeagerfor 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 |
|
/gemini review |
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
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]>
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