flash-attention-3 - #33522
Conversation
b6afd63 to
0976545
Compare
|
|
5aa58ab to
7ae105e
Compare
|
All models supporting FAv2 should now have FAv3 classes.
|
7ae105e to
bd6e9e7
Compare
|
All occurrences of Documentation and tests will be done next. |
bd6e9e7 to
fbf9bec
Compare
|
Some documentation and all tests are updated for FAv3. I'll run the tests on a H100 instance then mark this as ready for (initial) review. |
fbf9bec to
27edb62
Compare
|
Generally FAv3 tests are failing due to the small configurations used: Instead I've tested the majority of models from their examples, with a few exceptions like Gemma and Mistral that I need to request access to, and particularly large models such as Jamba that my instance doesn't have space to download. All of the tested models with examples are ok, with the exception of However this error also occurs with StableLM models are currently not supported due to num_attention_heads/ I've attached test reports, the numerical accuracy failures may need special care as per |
ArthurZucker
left a comment
There was a problem hiding this comment.
Wowowo super nice initiative thanks! 🔥
IMO since we already abstracted the flash attention API, let's try to keep it in flashAttentionLlama but maybe support flash_attention_3 in the attn_implementation for example! WDYT?
There was a problem hiding this comment.
Hey! As far as I can tell, the only diff is the forward function right?
There was a problem hiding this comment.
Yeah the difference between FlashAttention2 classes and FlashAttention3 is just the forward function, and lack of dropout/sliding window/softcap for FAv3. As you suggest we could support v3 in the existing classes instead using config.attn_implementation to select the appropriate function, happy to make this change if you think that's better.
There was a problem hiding this comment.
let's maybe replace flash_attention_forward by this one when flash attention3 is available WDYT?
There was a problem hiding this comment.
AFAIK FAv3 will be for Hopper GPUs only
27edb62 to
ba268ef
Compare
|
I've replaced the I've renamed the Note that while I was checking all In We could simplify the changes to Checks like |
4473129 to
4a34da8
Compare
|
Sliding window is now supported. |
|
Very great work 🚀 just a passerby who looked into the code :)
I'd be very pro this. It kinda looks misleading now with Personal preference: I'd go a step further and move the
Seems reasonable to me. Makes the code less verbose too. Lastly, Edit: Maybe raising a value error / warning if dropout or similar values are passed, would also be nice since now it's just silently ignoring them. |
4a34da8 to
9bcbe3f
Compare
|
I've removed I'll wait for input from a maintainer on changing the checks (
I assume this won't be merged until FAv3 is out of beta, at which point dropout and softcap should hopefully be supported, if not then I agree we should add an error/warning if they're used with FAv3. |
113afe4 to
a7f521c
Compare
a7f521c to
d50593a
Compare
d50593a to
66eefe5
Compare
|
Why not pull this request? I've been using it for months without a problem and have to hold back my transformers version to do so. |
|
@bn999 Just my thoughts but I think it's mainly due to fa3 still being in beta and stable support so far isn't really given - there is especially a lot of development in the last couple of weeks to support more architectures, head dims etc. Also, on another note, there also has been a refactor on the attn implementation side of transformers which most likely would force adjustments here. |
|
@vasqu To your first point, I keep up with the fa3 changes, and so far none have broken my use case. Also, if something does break, the user can just fall back to using fa2 as they do now. However, I can't comment on your second point. |
|
@ArthurZucker Yes I can pick this up again 🚀 |
What does this PR do?
This PR adds preliminary support for Flash Attention 3.
is_flash_attn_3_availablerequired a workaround in_is_package_availableaspackage_version = importlib.metadata.version(pkg_name)fails withimportlib.metadata.PackageNotFoundError: No package metadata was found for flash_attn_interface._supports_flash_attn_3and_check_and_enable_flash_attn_3added tomodeling_utils.py, near duplicate of_check_and_enable_flash_attn_2._flash_attention_3_forwardimplemented inmodeling_flash_attention_3_utils.py_flash_attention_forwardis now a unified interface for FAv2 and FAv3 controlled byuse_flash_attn_3which is passed fromFlashAttentionclasses based onconfig._attn_implementation == "flash_attention_3".sliding window(edit: sliding window is now supported) or softcap, and in FAv3flash_attn_func/flash_attn_varlen_funcreturn a tuple.attention_mask is not Noneandposition_ids is not Nonepaths depend on_upad_inputandprepare_fa2_from_position_idsrespectively,these are duplicated fromand are not included in FAv3 package therefore FAv3 depends onmodeling_flash_attention_utils.pyflash_attn, this is reflected inis_flash_attn_3_availablewhich checks foris_flash_attn_2_available.FLASH_ATTENTION_3_FP8for this purpose, we can probably add something likeattention_kwargsto model forwards to control this, or maybe another_attn_implementationtypeflash_attention_3_fp8, best to get reviews first and consensus on the best way to do it[1]Edit: added to other models, see comment below.flash_attention_3is added to Llama withLlamaFlashAttention3, similar toLlamaFlashAttention2with unsupported options like dropout and sliding window removed.See comment below._update_causal_maskis updated in various models due toutils/check_copies.py, and_supports_flash_attn_3is added in to some other models already for the same reason.Fixes #33373
Todo
Testattention_mask is not Noneandposition_ids is not NonepathsImplement FlashAttention3 classes for other modelsDone.DocumentationPartly done.Notes
Llama tested on H100 SXM with:
(shortened) responses
FP16:
FP8:
All other models will be tested after I've finished adding FlashAttention3 classes.Other models have been tested, see comment below.Who can review?
cc @ArthurZucker