[WIP] Refactor Attention Modules #11685
Draft
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?
Draft PR to address some issues with the attention modules.
Moves a number of transformer related blocks/objects in the attention module that would have a better home under models/transformers
Moves the definition of the Attention module into
attention.py
, rather than have it live inattention_prcessors.py
.We have a very large number of processors, but with Attention Dispatcher #11368 we should no longer need a good chunk of them and they can be deprecated. I think with these changes we would end up with ~3 processors per model (Attn, IPAdapter, PAG)
Make it so that we can bump up our minimum supported Torch version to
>=2.0
and use theF.sdpa
API for all processors.There was some discussion around naming of the processors:
https://huggingface.slack.com/archives/C065E480NN9/p1737130514639479
We landed on calling the processors something like AttnProcessorsSDPA, but with Attention Dispatcher #11368 we no longer need to use a dedicated processor per backend, so I think it's okay to just have the class be named AttnProcessor.
Move processor definitions into the model files, so we don't end up with very large files containing all processors.
Introduce AttentionModuleMixin that contains all common methods related to attention operations. New attention modules would inherit from this class.
Introduce an
AttentionMixin
for models so that methods likeset_processor
are not duplicated across models. Although we can probably just add the methods of this Mixin toModelMixin
Using Flux as an example here to show how we can define a single Processor to support both fused/unfused qkv projections.
Fixes # (issue)
Before submitting
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.