support 3D attention mask in bert
Author: gathierry
Pull request: yes

# 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 #31036
Try to fix with minimal change as mentioned in #31302

3D attention masks are used in [GroundingDINO](https://github.com/IDEA-Research/GroundingDINO)


## 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://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#create-a-pull-request),
      Pull Request section?
- [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.
- [ ] Did you make sure to update the documentation with your changes? Here are the
      [documentation guidelines](https://github.com/huggingface/transformers/tree/main/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation).
- [x] Did you write any new necessary tests?


## Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

- text models: @ArthurZucker

<!-- 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
- speech models: @sanchit-gandhi
- graph models: @clefourrier

Library:

- flax: @sanchit-gandhi
- generate: @zucchini-nlp (visual-language models) or @gante (all others)
- pipelines: @Narsil
- tensorflow: @gante and @Rocketknight1
- tokenizers: @ArthurZucker
- trainer: @muellerzr and @SunMarc

Integrations:

- deepspeed: HF Trainer/Accelerate: @muellerzr
- ray/raytune: @richardliaw, @amogkam
- Big Model Inference: @SunMarc
- quantization (bitsandbytes, autogpt): @SunMarc

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:
Hi I updated the doc string. But I'm not familiar with how the `attention_mask` doc is overwritten. If I did it in a wrong way, could you help indicate where should be change? 
`make fix-copies` seems fine, but maybe I miss something.

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

Hi @ArthurZucker , could you give another review on the updated code? Thanks

Thanks for the comment @ArthurZucker. The use case is that Grounding DINO uses a 3D attention mask. Right now, all implementations have to creating a wrapper of `BertModel`. Accepting 3D mask is more straightforward but I'm also okay with the idea about only accepting 2d or 4d. Grounding DINO has to expand its attention mask beforehand though.
Do you want an `assert` in the beginning to make sure the input shape is either 2D or 4D?

I just want to make sure we "clarrify", as the 3D mask you are providing will be added to the attention scores, which shape cannot be other than 3d, meaning there is an implicit broadcast. Having a check like for Llama would be better IMO! 

Could you give a reference of the check in Llama, I can try to do the same

MB, in https://github.com/huggingface/transformers/blob/834ec7b1ccf9b82905d5803aca41e1744849dae7/src/transformers/models/llama/modeling_llama.py#L59-L109 we don't raise and error so let's go with that 

For BERT it will go to some common functions which makes it more like the PR #31302 

I just realize that if we use 4D attention mask, there will be an error at https://github.com/gathierry/transformers/blob/97903a6352fdc897e13bc4f221fc57aa73e2697b/src/transformers/models/bert/modeling_bert.py#L1113
because the function `get_extended_attention_mask` only accept 2D and 3D inputs https://github.com/gathierry/transformers/blob/51296712900f28023cecea6ff7333af4b3c81d30/src/transformers/modeling_utils.py#L1132

yeah we should not got down that code path. 4d mask should be untouched

Yeah but seems like 3D is compatible by `get_extended_attention_mask` from the beginning?

Okay, let's go with 3d then 😉 

Yes, thanks

thanks for your contribution! 🤗