Skip to content

T5: add SDPA and Flash Attention 2 support - #46946

Closed
sergioperezcheco wants to merge 4 commits into
huggingface:mainfrom
sergioperezcheco:feat/t5-flash-attention-2
Closed

T5: add SDPA and Flash Attention 2 support#46946
sergioperezcheco wants to merge 4 commits into
huggingface:mainfrom
sergioperezcheco:feat/t5-flash-attention-2

Conversation

@sergioperezcheco

@sergioperezcheco sergioperezcheco commented Jun 28, 2026

Copy link
Copy Markdown

CI

What does this PR do?

T5 (and the whole T5 family — FLAN-T5, mT5, ByT5) has been the only major architecture in transformers without SDPA or Flash Attention support. Users hitting attn_implementation="sdpa" or "flash_attention_2" get a ValueError (see #46640). This PR fixes that by refactoring T5Attention to dispatch through ALL_ATTENTION_FUNCTIONS, matching how BERT, BART, Llama and others already work.

Partially addresses #26350 (community effort to add FA2 to more architectures).

There is an older attempt (#31167) but it uses the deprecated T5SdpaAttention subclass approach. Arthur asked there to switch to the ALL_ATTENTION_FUNCTIONS dispatch pattern — this PR does exactly that.

How it works

T5 attention is trickier than most models because of two things:

1. No scaling. T5 does not divide Q·K by √d (the original paper found it unnecessary with their initialization). The scaling=1.0 is passed explicitly so SDPA/flash kernels do not apply the default 1/√d.

2. Learned relative position bias. T5 adds a learned position_bias tensor to the attention scores before softmax. This bias has shape (batch, heads, q_len, k_len) and is additive.

  • SDPA handles this naturally — scaled_dot_product_attention accepts an additive attn_mask, so position_bias (with the causal/padding mask folded in) goes straight through.
  • Flash Attention 2 cannot accept arbitrary additive bias (only alibi_slopes for linear bias). When FA2 is selected and position_bias is present, we transparently fall back to SDPA, which still gets the memory-efficient fused kernel from PyTorch.

Changes

  • New module-level eager_attention_forward (mirrors the pattern in modeling_bart.py / modeling_bert.py)
  • T5Attention.forward dispatches through ALL_ATTENTION_FUNCTIONS.get_interface() instead of doing manual matmul → softmax → matmul
  • output_attentions=True forces eager (SDPA/FA do not return attention weights)
  • _supports_sdpa = True and _supports_flash_attn = True on T5PreTrainedModel

Verification

tests/models/t5/test_modeling_t5.py — 228 passed, 143 skipped, 0 failed

SDPA vs eager logits are bit-identical (max diff 0.0 on CPU with fp32). Also tested generation, KV cache, gradient checkpointing, encoder-only model, and cross-attention — all green.

Quick benchmark (T5-base config, seq_len=1024, CPU):

eager: avg 395ms
sdpa:  avg 378ms  (~4% faster; bigger gains expected on GPU with flash kernels)

Before

>>> model = T5ForConditionalGeneration.from_pretrained("t5-base", attn_implementation="sdpa")
ValueError: T5ForConditionalGeneration does not support Flash Attention 2 yet.

After

>>> model = T5ForConditionalGeneration.from_pretrained("t5-base", attn_implementation="sdpa")
# works, uses fused SDPA kernels

@sergioperezcheco
sergioperezcheco force-pushed the feat/t5-flash-attention-2 branch from 9b7aa2e to 0ac246e Compare June 30, 2026 05:11
Refactor T5Attention to use ALL_ATTENTION_FUNCTIONS dispatch system,
bringing T5 in line with BERT, BART, Llama and other models that already
support multiple attention backends.

Key design decisions:
- T5 does not scale Q*K by 1/sqrt(d_kv); pass scaling=1.0 explicitly
- position_bias is computed before dispatch and passed as additive
  attn_mask, which SDPA handles natively
- Flash Attention 2 cannot accept additive bias, so when FA2 is selected
  we transparently fall back to SDPA for correctness
- output_attentions=True forces eager path since SDPA/FA don't return
  attention probabilities

This resolves the long-standing gap where T5 was the only major
architecture without _supports_sdpa / _supports_flash_attn, causing
ValueError for users requesting these backends (e.g. issue huggingface#46640).

Partially addresses huggingface#26350.
The previous commit added SDPA/FA2 dispatch code to longt5, mt5,
pop2piano, and udop but forgot to import ALL_ATTENTION_FUNCTIONS,
sdpa_attention_forward, and define eager_attention_forward.

This fixes all 16 F821 'Undefined name' errors from the CI code
quality check.
…FUNCTIONS

T5 was the canonical 'legacy' example but now supports SDPA and FA2
through the modern AttentionInterface. Replaced it with FSMT as the
legacy model reference.
@sergioperezcheco
sergioperezcheco force-pushed the feat/t5-flash-attention-2 branch from 3b92c62 to 168d4d9 Compare July 1, 2026 05:19
@github-actions

github-actions Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: longt5, mt5, pop2piano, switch_transformers, t5, udop

@github-actions

github-actions Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 28495379359:2
Result: success | Jobs: 7 | Tests: 3,724 | Failures: 0 | Duration: 13m 54s

@Rocketknight1

Copy link
Copy Markdown
Member

cc @ArthurZucker @Cyrilvallez

@sergioperezcheco

Copy link
Copy Markdown
Author

Hi @ArthurZucker @Cyrilvallez, just a friendly ping on this PR. All CI tests are passing (3724 tests, 0 failures). Is there anything else you'd like me to address or clarify? Happy to make any adjustments.

@Cyrilvallez

Copy link
Copy Markdown
Member

This competes with #47014

@vasqu

vasqu commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator

Looks very agentic to me and the fact that FA support was added cries to me that it is either an agent or someone who hasn't even tested it. I know #47014 at least the contributors and it has a history from a previous PR, so much more serious and likely to be correct

@Cyrilvallez

Cyrilvallez commented Jul 9, 2026

Copy link
Copy Markdown
Member

Ha indeed, did not notice that the other one was from @jiqing-feng! Closing this one then in favor of #47014 for sure! (and indeed it has all the markers of just random agent code that we would waste time a lot of time to explain stuff to machine...)

@Cyrilvallez Cyrilvallez closed this Jul 9, 2026
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.

4 participants