Skip to content

🚨All attention refactor🚨 - #35235

Merged
Cyrilvallez merged 99 commits into
mainfrom
all-attention-refactor
Dec 18, 2024
Merged

Cyrilvallez merged 99 commits into
mainfrom
all-attention-refactor

Conversation

@ArthurZucker

@ArthurZucker ArthurZucker commented Dec 12, 2024 •

Copy link
Copy Markdown
Collaborator

What does this PR do?

Todo in this PR:

  • Cohere
  • Chameleon
  • DBRX
  • Gemma
  • Gemma2
  • GLM (modular donc rien à faire je crois)
  • gpt_neoX et GPT2
  • Granite
  • Jamba
  • JetMoe
  • Mimi
  • Mistral
  • Mixtral
  • Mllama
  • Moshi
  • Nemotron
  • OPT
  • Phi
  • Ph3
  • PhiMoe
  • Qwen2
  • qwen2Moe
  • qwen2VL
  • SableML
  • StartCoder2 -> Modular normalement oK
  • Idefics1,2,3
  • Olmo
  • Olmo2
  • Siglip
  • Whisper

@ArthurZucker
ArthurZucker force-pushed the all-attention-refactor branch from 0dc9253 to d1aa9ce Compare December 12, 2024 13:49
Comment thread src/transformers/modeling_utils.py Outdated
)


class GradientCheckpointLayer(torch.nn.Module):

ghost Dec 13, 2024

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This should help with kwargs as well

@Cyrilvallez
Cyrilvallez force-pushed the all-attention-refactor branch from 8b56823 to ecd814b Compare December 16, 2024 11:28
**kwargs,
) -> Tuple[torch.Tensor, None]:
if hasattr(module, "num_key_value_groups"):
key = repeat_kv(key, module.num_key_value_groups)

ghost Jun 30, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@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.

ghost Jul 1, 2025

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@yuanyao-nv Sadly, yes it is due to multiple reasons:

ghost Jul 1, 2025

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

On point!

ghost Jul 1, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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?

ghost Jul 2, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@vasqu @ArthurZucker any thoughts on the above questions? Thanks.

ghost Jul 15, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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?

ghost Jul 15, 2025

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yes, in general using flash_attention_2 is just the best / recommended way!

ghost Jul 15, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

ghost Jul 17, 2025

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think #35235 (comment) still explains most reasons:

  • enable_gqa is 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_gqa works 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.

ghost Jul 17, 2025

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

FYI, there is #39412 which partially enables this kwarg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.