Skip to content

fix(models): Update Flash Attention detection for version 2.7+ - #4361

Closed
BillionClaw wants to merge 2 commits into
unslothai:mainfrom
BillionClaw:clawoss/fix/flash-attn-detection-4270
Closed

BillionClaw wants to merge 2 commits into
unslothai:mainfrom
BillionClaw:clawoss/fix/flash-attn-detection-4270

Conversation

@BillionClaw

Copy link
Copy Markdown

The Flash Attention availability check tried to import internal functions (flash_attn_gpu/flash_attn_cuda) from flash_attn.flash_attn_interface, which may not exist in flash-attn 2.7+.

Changed to first try importing the public API (flash_attn_func) from flash_attn, which is the actual function used by Unsloth throughout the codebase. Falls back to legacy imports for older flash-attn versions.

This fixes false negatives where flash-attn 2.7+ was incorrectly detected as broken, causing unnecessary fallback to XFormers and reduced GPU utilization.

Fixes #4270

The Flash Attention availability check tried to import internal functions
(flash_attn_gpu/flash_attn_cuda) from flash_attn.flash_attn_interface,
which may not exist in flash-attn 2.7+.

Now first tries to import the public API (flash_attn_func) from flash_attn,
which is the actual function used by Unsloth. Falls back to legacy imports
for older flash-attn versions.

Fixes unslothai#4270
@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

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refines the Flash Attention availability detection mechanism within Unsloth. By prioritizing the import of the public flash_attn_func for newer Flash Attention versions (2.7+) and providing a fallback for older versions, it eliminates false negatives that previously caused the system to incorrectly bypass Flash Attention, thereby improving GPU utilization and overall performance.

Highlights

  • Flash Attention Detection: Updated the logic for detecting Flash Attention availability to correctly identify and utilize versions 2.7 and above.
  • Backward Compatibility: Implemented a fallback import mechanism to ensure continued compatibility with older Flash Attention versions.
  • False Negative Fix: Resolved an issue where Flash Attention 2.7+ was incorrectly detected as unavailable, preventing unnecessary fallback to XFormers and improving GPU utilization.
Changelog
  • unsloth/models/_utils.py
    • Modified the Flash Attention import logic to first attempt importing flash_attn_func for newer versions, then fall back to flash_attn_gpu or flash_attn_cuda for older versions.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@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 correctly updates the Flash Attention availability check. My review includes two main points: first, to use specific exceptions (ImportError) instead of bare except: blocks for better error handling, and second, to refactor the duplicated code for checking Flash Attention into a helper function to improve maintainability.

Comment thread unsloth/models/_utils.py
Comment on lines 877 to +886
try:
# See https://github.com/unslothai/unsloth/issues/1437
from flash_attn.flash_attn_interface import flash_attn_gpu
from flash_attn import flash_attn_func

_ = flash_attn_func # Ensure import is valid
except:
from flash_attn.flash_attn_interface import flash_attn_cuda
# Fall back to legacy import check (for older flash-attn versions)
# See https://github.com/unslothai/unsloth/issues/1437
try:
from flash_attn.flash_attn_interface import flash_attn_gpu
except:

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.

medium

It's better to catch specific exceptions rather than using a bare except:. This prevents catching unexpected errors (like KeyboardInterrupt) and makes the code's intent clearer. Using except ImportError: would be more appropriate here.

Suggested change
try:
# See https://github.com/unslothai/unsloth/issues/1437
from flash_attn.flash_attn_interface import flash_attn_gpu
from flash_attn import flash_attn_func
_ = flash_attn_func # Ensure import is valid
except:
from flash_attn.flash_attn_interface import flash_attn_cuda
# Fall back to legacy import check (for older flash-attn versions)
# See https://github.com/unslothai/unsloth/issues/1437
try:
from flash_attn.flash_attn_interface import flash_attn_gpu
except:
try:
from flash_attn import flash_attn_func
_ = flash_attn_func # Ensure import is valid
except ImportError:
# Fall back to legacy import check (for older flash-attn versions)
# See https://github.com/unslothai/unsloth/issues/1437
try:
from flash_attn.flash_attn_interface import flash_attn_gpu
except ImportError:
from flash_attn.flash_attn_interface import flash_attn_cuda

Comment thread unsloth/models/_utils.py
Comment on lines 930 to +943
# Check for CUDA linking errors "undefined symbol: _ZNK3c106SymIntltEl"
try:
# Try importing the actual functions used by Unsloth first (for flash-attn 2.7+)
# See https://github.com/unslothai/unsloth/issues/4270
try:
# See https://github.com/unslothai/unsloth/issues/1437
from flash_attn.flash_attn_interface import flash_attn_gpu
from flash_attn import flash_attn_func

_ = flash_attn_func # Ensure import is valid
except:
from flash_attn.flash_attn_interface import flash_attn_cuda
# Fall back to legacy import check (for older flash-attn versions)
# See https://github.com/unslothai/unsloth/issues/1437
try:
from flash_attn.flash_attn_interface import flash_attn_gpu
except:

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.

medium

This logic for checking Flash Attention availability is duplicated from the cuda device block above (lines 874-920). To improve maintainability, this should be refactored into a shared helper function.

Also, please apply the same fix here as suggested for the previous block: use specific except ImportError: instead of bare except:.

@BillionClaw

Copy link
Copy Markdown
Author

Hi! Just checking in on this PR. Is there anything needed from my side to help move it forward?

@BillionClaw

Copy link
Copy Markdown
Author

Closing per ClawOSS blocklist — repo has 100% closure rate and no maintainer engagement.

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] GPU power is low during Qwen3-4B training

1 participant