Repository navigation
🚨All attention refactor🚨 - #35235
🚨All attention refactor🚨#35235
Conversation
0dc9253 to
d1aa9ce
Compare
| ) | ||
|
|
||
|
|
||
| class GradientCheckpointLayer(torch.nn.Module): |
There was a problem hiding this comment.
This should help with kwargs as well
8b56823 to
ecd814b
Compare
| **kwargs, | ||
| ) -> Tuple[torch.Tensor, None]: | ||
| if hasattr(module, "num_key_value_groups"): | ||
| key = repeat_kv(key, module.num_key_value_groups) |
There was a problem hiding this comment.
@ArthurZucker Wondering if you know whether this repeat_kv is needed here? torch.nn.functional.scaled_dot_product_attention allows num heads to be different for q and k/v, and having this expansion just defeats the purpose of doing GQA.
There was a problem hiding this comment.
@yuanyao-nv Sadly, yes it is due to multiple reasons:
- GQA support was only later added to sdpa (we support up to torch 2.1)
- The GQA support in sdpa is very limited
- Quoting https://docs.pytorch.org/docs/stable/generated/torch.nn.functional.scaled_dot_product_attention.html "It currently works only for Flash_attention and math kernel on CUDA tensor"
- However, since we more often than not use a mask, we require the memory-efficient backend (fa cannot work with masks in sdpa) to avoid the least efficient math backend
There was a problem hiding this comment.
Thanks for the reply!
With regard to the flash attention support gap, how does introducing repeat_kv solve the issue? Does flash attention have better support for masked cases in non-GQA than GQA?
The reason I'm asking is that I'm trying to export models into FX graph and ONNX and I'm trying to preserve the SDPA op for Attention in the FX graph. The repeat_kv introduces additional ops that can lead to inefficiencies when a non-torch backend tries to parse such graphs.
I'm wondering if the use of repeat_kv should be configurable based on which backend is intended?
There was a problem hiding this comment.
@vasqu @ArthurZucker any thoughts on the above questions? Thanks.
There was a problem hiding this comment.
Another followup question (might be a naive one):
The use case you're referring to above seems to be attn_implementation="sdpa" and also explicitly requesting the FA backend (maybe via a context manager with sdpa_kernel(backends=[SDPBackend.FLASH_ATTENTION]):?)
However, if the user really intends to use FA, shouldn't they directly set attn_imlementation="flash_attention_2" or "flash_attention_3"? ie, why would they set attn_implementation="sdpa" and then request the FA kernel?
There was a problem hiding this comment.
Yes, in general using flash_attention_2 is just the best / recommended way!
There was a problem hiding this comment.
Thanks for confirming. In that case is there anything stopping us from removing repeat_kv from the sdpa_attention path? It sounds like if the user really wants to use the FA backend they should not need to use this path in the first place.
And this will make the exported fx graph and ONNX graph more efficient and easier for other backends to work with.
I'd be happy to submit a PR for this.
There was a problem hiding this comment.
I think #35235 (comment) still explains most reasons:
enable_gqais not available for earlier versions of torch (starts with 2.5.x)- If we have a mask, we need to avoid the math kernel (glad to see benchmarks to prove me wrong there) and instead use the xformers one (memory-efficient backend)
enable_gqaworks only with fa or math- If we have a mask, it already cuts out the fa kernel (torch internal restriction)
- Fallback to math kernel
- It's not about users requesting specific backends but moreso to avoid entering inefficient branches (math kernel) - if a user uses SDPA they should expect the more efficient backends...
The original flash attention is better in that way but SDPA is the standard attention as it's native torch.
There was a problem hiding this comment.
FYI, there is #39412 which partially enables this kwarg
What does this PR do?
Todo in this PR: