feat: add optional flash attention kernels#236
Open
art-test-stack wants to merge 21 commits into
Open
Conversation
…to handle attention kernel
70890d4 to
c414176
Compare
…k - need to be run on Hopper GPUs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Hi,
Sorry, I have done something wrong that closed the previous PR related to this #229, and could not reopen it
TLDR; this PR addresses the case of using flash attention in the dot product with
attn_mask=Noneor for causal attention onkernels(not installingflash_attentionyet).Warning
This PR will need to be tested on cuda device (as I don't have my own, I could not; but it should theoretically offer faster performance for the same input-output couple)
flash_attncontrolAs mentioned in the last PR (#229)
AttentionConfiga control on attention implementation (inspired by (axolotl)[https://docs.axolotl.ai/docs/attention.html] as proposed.NOETHER_ATTN_IMPLto have a higher level controlTo disable flash attention mode, just set the environment variable
NOETHER_ATTN_IMPLto something different from "fast-attn". For example:module
Added
src/noether/modeling/modules/attention/_flash_attention.py.This module exposes the minimal functions used across the attention modules in this codebase:
flash_attn_func,flash_attn_qkvpacked_func,flash_attn_with_kvcache.The implementation prefers the external kernel when available, but falls back to implementations based on
torch.nn.functional.scaled_dot_product_attentionso semantics remain identical.Note
For all flash attention kernel calls, the signatures are from official implementation by Dao-AILab/flash-attention.
comments
To answer the comments that I have not answered in previous PR:
to @Ndles
Then I am thinking of implementing both interfaces in one—
kernelsis easier to install thanfa3orfa4I'm working on understanding the modules to have these tweaks minimal :)
to @HennerM
yes, but it comes quite from how
flash_attentionoptimizes the dot-product operation. For instance, PyTorch may usesfa2backend ifattn_mask=None.However, I m looking at how it is possible to "play" with other inputs, as the
flash_attentionViT module does here.todo suggestions
As TODOs (related to faster sdpa kernels) I have identified the following, which could be beneficial:
Dao-AILab/flash-attentionprovides an implementation for ViT here. Maybe it can be interesting to think around it to get faster implementation.is_causalshould be preferred as an argument for causal attention, instead of being given a causal masktorch.flex_attentionlooks like a good option for a custom mask, but maybe not compatible as it just came out with the last release (torch 2.12)Finally, sorry for the delay, I tried to do this asap. I am motivated to add other
flash_attnvariants, but without cuda device, I am afraid it is not very reliable for you.12/6 EDIT: added
flash-attention-3installation description with support14/6 EDIT:
I have added some tests, and now it should be fully testable on GPUs. Also, it is fully backward compatible as the default mode for
attn_implementationissdpa.Minor changes that are coming to be pushed—but these changes won't impact the core logic, which is done. So I let this PR as ready to review even if some changes will be needed:
'sdpa'and'flash_attention_3'modes (with pytest)attn_mode='flash_attention'NB: one very likely crash cause could be the input tensors that are not contiguous in
fast_attn_func.Also working on another PR from this one (as it will be a big one) for flex_attention. Will be quite big as it would support some kind of
attn_mask(actually, it gives as an argument some function that constructs the mask given the position in the matrix). However, it would make a very massive PR if I put it in here. Same for changing the whole tensor coordinate convention— PyTorch's convention (B, H, T, D)— or Flash Attention's convention (B, T, H, D).Looking forward to your feedback :))