Add sdpa for Beit
Author: OmarManzoor
Pull request: yes
Labels: name: Vision

name: SDPA

name: run-slow

# 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 -->

Related to #28005

- Adds sdpa for Beit model
- Also extends the support of sdpa to Data2VecVision model to ensure consistency


## Who can review?

@qubvel 

<!-- 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
- vision models: @amyeroberts, @qubvel
- speech models: @ylacombe, @eustlb
- graph models: @clefourrier

Library:

- flax: @sanchit-gandhi
- generate: @zucchini-nlp (visual-language models) or @gante (all others)
- pipelines: @Rocketknight1
- tensorflow: @gante and @Rocketknight1
- tokenizers: @ArthurZucker
- trainer: @muellerzr and @SunMarc
- chat templates: @Rocketknight1

Integrations:

- deepspeed: HF Trainer/Accelerate: @muellerzr
- ray/raytune: @richardliaw, @amogkam
- Big Model Inference: @SunMarc
- quantization (bitsandbytes, autogpt): @SunMarc @MekkCyber

Documentation: @stevhliu

HF projects:

- accelerate: [different repo](https://github.com/huggingface/accelerate)
- datasets: [different repo](https://github.com/huggingface/datasets)
- diffusers: [different repo](https://github.com/huggingface/diffusers)
- rust tokenizers: [different repo](https://github.com/huggingface/tokenizers)

Maintained examples (not research project or legacy):

- Flax: @sanchit-gandhi
- PyTorch: See Models above and tag the person corresponding to the modality of the example.
- TensorFlow: @Rocketknight1

 -->


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

> Here is the [code](https://gist.github.com/qubvel/6c62984bf43e40efbc850fc4fbdf4d92), can you run it with your env to > > check? Also my code might be not the best way to benchmark, would appreciate if you share your code!

Maybe your script might be better than what I am using but nevertheless here is the script I used for inference. 
[inference_image_benchmark](https://gist.github.com/OmarManzoor/fc02a514258d9b8e9e0138cb18e7bcd0)
I basically just modified the script that we have been using for the text models.



Here are the results using your script

### BeitForImageClassification

|   Image batch size |   Eager (s/iter) | Eager CI, %   |   SDPA (s/iter) | SDPA CI, %   |   SDPA speedup |
|-------------------:|-----------------:|:--------------|----------------:|:-------------|---------------:|
|                  1 |            0.012 | ±0.5%         |           0.011 | ±0.3%        |          1.135 |
|                  4 |            0.013 | ±0.2%         |           0.011 | ±0.2%        |          1.181 |
|                 16 |            0.045 | ±0.1%         |           0.035 | ±0.1%        |          1.3   |
|                 32 |            0.088 | ±0.1%         |           0.067 | ±0.1%        |          1.322 |


## Environment:

```console
Python version: 3.10.14 (main, Jul 23 2024, 15:53:02) [GCC 9.4.0]
Transformers version: 4.47.0.dev0
Torch version: 2.5.1+cu124
GPU: NVIDIA GeForce RTX 2060 SUPER
```

Ok, looks good, can you update documentation benchmarks then? Maybe it's worth adding memory stats to my script and rerunning it

> Ok, looks good, can you update documentation benchmarks then? Maybe it's worth adding memory stats to my script and rerunning it

Okay I think we can use your inference benchmarks but for the training part I think we can keep the current benchmarks? Also did you have a look at the script I shared, is there something that is not quite correct in that?

@qubvel Do the slow tests still need to be run?

@ArthurZucker please review when you have bandwidth!

@dvrogozh Thank you for verifying

@ArthurZucker Can this be merged?

> Hey all! Sorry for the delay, we are in the middle of a huge refactoring in #35235, which is why I wanted to wait a bit, but good work should be rewarded, so let's merge this! Thanks for being patient 🤗

Thank you!