T5: add SDPA and Flash Attention 2 support
Author: sergioperezcheco
Pull request: yes

<!-- ci-dashboard-badge:start -->
[![CI](https://transformers-ci.lor-e.huggingface.cool/badge/pr?pr=46946)](https://transformers-ci.lor-e.huggingface.cool/d/pytest-observability-by-pr/pytest-observability-branch?var-pr=46946)
<!-- ci-dashboard-badge:end -->

## 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

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

## After

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

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

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

<!-- ci-dashboard-recap:start -->

---

### CI recap

**Dashboard:** [View test results in Grafana](https://transformers-ci.lor-e.huggingface.cool/d/pytest-observability-by-pr/pytest-observability-branch?var-pr=46946)
**Latest run:** [28495379359:2](https://github.com/huggingface/transformers/actions/runs/28495379359)
**Result:** `success` | **Jobs:** 7 | **Tests:** 3,724 | **Failures:** 0 | **Duration:** 13m 54s

<!-- ci-dashboard-recap:end -->

cc @arthurzucker @cyrilvallez

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.

This competes with https://github.com/huggingface/transformers/pull/47014

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

Ha indeed, did not notice that the other one was from @jiqing-feng! Closing this one then in favor of https://github.com/huggingface/transformers/pull/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...)