Use attention interface in RoFormerSelfAttention#46147
Closed
HamzaDogann wants to merge 2 commits into
Closed
Conversation
Replace hardcoded eager attention computation with `ALL_ATTENTION_FUNCTIONS` dispatch, enabling users to plug in custom attention backends (e.g. `flash_attention_2`, `sdpa`) via the standard `_attn_implementation` config field. Adds a local `eager_attention_forward` function as the default fallback that preserves existing behavior. Closes huggingface#46144
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates RoFormer’s self-attention to use the shared Transformers attention interface (ALL_ATTENTION_FUNCTIONS) instead of a hardcoded eager implementation, enabling alternative attention backends and custom attention implementations via config._attn_implementation.
Changes:
- Added a local
eager_attention_forwardfallback and switched RoFormerSelfAttention to dispatch throughALL_ATTENTION_FUNCTIONS.get_interface(...). - Stored
configand precomputedscalinginRoFormerSelfAttentionfor reuse by attention backends.
…lags - Pass an explicit `is_causal=False` and forward `output_attentions` to the attention interface, so `flash_attention_2`/`sdpa` no longer depend on a missing `module.is_causal` attribute (RoFormer precomputes a bidirectional mask, so no extra causal mask is needed). - Mark `eager_attention_forward` as `# Copied from` BERT so `make fixup` keeps it in sync with the shared implementation. - Declare `_supports_flash_attn`, `_supports_sdpa`, `_supports_flex_attn` and `_supports_attention_backend` on `RoFormerPreTrainedModel` so the new backends can actually be selected.
Contributor
|
[For maintainers] Suggested jobs to run (before merge) run-slow: roformer |
Member
|
When the issue creator says
This means you should not rush in and immediately vibe code a PR |
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.
RoFormer's self-attention was using a hardcoded eager implementation,
making it impossible to use alternative attention backends.
This PR replaces the hardcoded computation with
ALL_ATTENTION_FUNCTIONSdispatch and adds a local
eager_attention_forwardas the default fallback,preserving existing behavior while enabling
flash_attention_2,sdpa,and custom attention implementations via
_attn_implementation.Closes #46144