[WIP][Feature]Support using flash-attention and flash-varlen-attention on Ascend NPU #36617
+240
−13
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Package
flash-attn
is not supported on Ascend NPU, even can not be installed. In that case, we can not use flash-attention or flash-varlen-attention with transformers naturally.Additionally, through Ascend FlashAttentionScore Document we find that Ascend
torch_npu
package has provided flash-attention and flash-varlen-attention apis on Ascend NPU, which can play the same role as the apis in packageflash-attn
.Therefore, this PR is for supporting using flash-attention and flash-varlen-attention on Ascend NPU. All modifications are controlled in using Ascend NPU.
Modifications
src/transformers/utils/npu_flash_attention_utils.py
to organize necessary functions for using flash-attention and flash-varlen-attention on Ascend NPU, part of codes are copied from flash-attn/bert_padding.py.is_flash_attn_2_available
,is_flash_attn_greater_or_equal_2_10
andis_flash_attn_greater_or_equal
3 functions returnTrue
when detectingtorch
andtorch_npu
available before detecting whether packageflash-attn
exists or not.index_first_axis
,pad_input
,unpad_input
,flash_attn_func
,flash_attn_varlen_func
5 functions online to replace same functions in packageflash-attn
when detectingtorch_npu
available.Fixes # (issue)
Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.