Fix FA performance regression
Author: andreasgoulas
Pull request: yes

<!-- ci-dashboard-badge:start -->
[![CI](https://transformers-ci.lor-e.huggingface.cool/badge/pr?pr=47134)](https://transformers-ci.lor-e.huggingface.cool/d/pytest-observability-by-pr/pytest-observability-branch?var-pr=47134)
<!-- ci-dashboard-badge:end -->

# What does this PR do?

<!--
Congratulations! You've made it this far! You're not quite done yet though.

Once merged, your PR is going to appear in the release notes with the title you set, so make sure it's a great title that fully reflects the extent of your awesome contribution.

Then, please replace this with a description of the change and which issue is fixed (if applicable). Please also include relevant motivation and context. List any dependencies (if any) that are required for this change.

Once you're done, someone will review your PR shortly (see the section "Who can review?" below to tag some potential reviewers). They may suggest changes to make the code even better. If no one reviewed your PR after a week has passed, don't hesitate to post a new comment @-mentioning the same persons---sometimes notifications get lost.
-->

<!-- Remove if not applicable -->

Fixes #46693 

How to test:
1. Checkout v5.2.0 and run the reproducer script below
2. Checkout the latest version and confirm that there is a performance regression
3. Apply this patch
4. Confirm that the performance regression has been resolved

```python
from transformers import Qwen3VLForConditionalGeneration, AutoProcessor
import torch

model = Qwen3VLForConditionalGeneration.from_pretrained(
    "Qwen/Qwen3-VL-2B-Thinking",
    dtype=torch.bfloat16,
    attn_implementation="flash_attention_2",
).to("cuda")

processor = AutoProcessor.from_pretrained("Qwen/Qwen3-VL-2B-Thinking")

messages = [
    [
        {
            "role": "user",
            "content": [
                {"type": "video", "video": torch.zeros(768, 3, 512, 512)},
                {"type": "text", "text": "placeholder"},
            ],
        }
    ],
    [
        {
            "role": "user",
            "content": [
                {"type": "video", "video": torch.zeros(768, 3, 512, 512)},
                {"type": "text", "text": "different placeholder"},
            ],
        }
    ],
]

inputs = processor.apply_chat_template(
    messages,
    tokenize=True,
    add_generation_prompt=True,
    do_sample_frames=False,
    return_dict=True,
    return_tensors="pt",
    padding=True,
    padding_side="left",
).to("cuda")

MAX_LEN = 16 * 1024

while True:
    start = torch.cuda.Event(enable_timing=True)
    end = torch.cuda.Event(enable_timing=True)

    start.record()
    out_ids = model.generate(
        **inputs, do_sample=False, min_length=MAX_LEN, max_length=MAX_LEN, use_cache=True
    )
    end.record()

    torch.cuda.synchronize()
    assert out_ids.size(0) == 2 and out_ids.size(1) == MAX_LEN

    cur_time = start.elapsed_time(end)
    print(cur_time)
```

## Code Agent Policy

- [x] I confirm that this is not a pure code agent PR.

## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [x] Did you read the [contributor guideline](https://huggingface.co/docs/transformers/contributing) and the
      [Pull Request](https://huggingface.co/docs/transformers/pr_checks) checks?
- [x] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? Please add a link
      to it if that's the case.
- [x] Did you make sure to update the documentation with your changes according to the [guidelines](https://github.com/huggingface/transformers/tree/main/docs)?
- [ ] Did you write any new necessary [tests](https://huggingface.co/docs/transformers/testing)?


## Who can review?

@vasqu

<!-- Your PR will be replied to more quickly if you can figure out the right person to tag with @

 If you know how to use git blame, that is the easiest way, otherwise, here is a rough guide of **who to tag**.
 Please tag fewer than 3 people.

Models:

- text models: @ArthurZucker @Cyrilvallez @vasqu
- vision models: @yonigozlan @molbap
- audio models: @eustlb @ebezzam @vasqu
- multimodal models: @zucchini-nlp
- graph models: @clefourrier

Library:

- generate: @zucchini-nlp (visual-language models) or @Cyrilvallez (all others)
- continuous batching: @remi-or @ArthurZucker @McPatate
- pipelines: @Rocketknight1
- tokenizers: @ArthurZucker and @itazap
- trainer: @SunMarc
- attention: @vasqu @ArthurZucker @CyrilVallez
- model loading (from pretrained, etc): @CyrilVallez
- distributed: @3outeille @ArthurZucker
- CIs: @ydshieh

Integrations:

- ray/raytune: @richardliaw, @amogkam
- Big Model Inference: @SunMarc
- quantization: @SunMarc
- kernels: @vasqu @drbh
- peft: @BenjaminBossan @githubnemo

Devices/Backends:

- AMD ROCm: @ivarflakstad
- Intel XPU: @IlyasMoutawwakil
- Ascend NPU: @ivarflakstad 

Documentation: @stevhliu

Research projects are not maintained and should be taken as is.

 -->

Comments:
@vasqu I have incorporated your suggestion and tested this fix on the latest main. I am not sure why the CI tests are failing but the PR should be ready.

<!-- 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=47134)
**Latest run:** [28968314204:2](https://github.com/huggingface/transformers/actions/runs/28968314204)
**Result:** `success` | **Jobs:** 15 | **Tests:** 170,113 | **Failures:** 0 | **Duration:** 19h 54m

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

Done, thanks!

Merging now, thanks :hugs: 

The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_47134). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.