Skip to content

[bugfix] fix flash attention 2 unavailable error on Ascend NPU #39166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 7, 2025

Conversation

FightingZhen
Copy link
Contributor

What does this PR do?

#38972 introduce flash attention 3 into transformers. However, the modification introduce a bug when using flash attention 2 on Ascend NPU.

The core reason is due to function names mismatch:

Functions defined from transformers.integrations.npu_flash_attention:

if is_torch_npu_available():
from .integrations.npu_flash_attention import (
npu_apply_rotary_emb as apply_rotary_emb, # noqa: F401
)
from .integrations.npu_flash_attention import (
npu_flash_attn_func as flash_attn_func,
)
from .integrations.npu_flash_attention import (
npu_flash_attn_varlen_func as flash_attn_varlen_func,
)
from .integrations.npu_flash_attention import (
pad_input,
unpad_input,
)

Functions actually used:

elif attn_implementation == "flash_attention_2":
_flash_attn_varlen_func = flash_attn_2_varlen_func
_flash_attn_func = flash_attn_2_func
_pad_input = pad_input_fa2
_unpad_input = unpad_input_fa2
_is_fa3 = False

This PR is committed for solving this problem, by renaming flash attention 2 related functions (e.g. npu_flash_attn_func) from transformers.integrations.npu_flash_attention to correct names, which should contain _2_ symbol (e.g. flash_attn_2_func)

Fixes # (issue)
Not related.

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

@FightingZhen FightingZhen changed the title [bugfix] fix flash attention 2 error on Ascend NPU [bugfix] fix flash attention 2 unavailable error on Ascend NPU Jul 2, 2025
@FightingZhen
Copy link
Contributor Author

This PR is ready for merge @ArthurZucker @SunMarc, cc @EduardDurech

@EduardDurech
Copy link
Contributor

Thanks, I didn't have a device to test this on but looks like you did it correctly

@FightingZhen
Copy link
Contributor Author

Looking forward for review @ArthurZucker @SunMarc

Copy link
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

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

Makes sense, I think this deserves to be in the patch!

@ArthurZucker ArthurZucker added the for patch Tag issues / labels that should be included in the next patch label Jul 7, 2025
@ArthurZucker ArthurZucker enabled auto-merge (squash) July 7, 2025 12:50
@ArthurZucker ArthurZucker merged commit 00e9efc into huggingface:main Jul 7, 2025
26 checks passed
@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Cyrilvallez pushed a commit that referenced this pull request Jul 11, 2025
[bugfix] fix flash attention 2 error on Ascend NPU
SwiftAkira pushed a commit to SwiftAkira/transformers that referenced this pull request Jul 11, 2025
rjgleaton pushed a commit to rjgleaton/transformers that referenced this pull request Jul 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for patch Tag issues / labels that should be included in the next patch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants