[In progress] Add warning padding attention mask
Author: anruijian
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 #16136 


## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [ ] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#start-contributing-pull-requests),
      Pull Request section?
- [ ] 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).
- [ ] 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.

<!-- 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 and @younesbelkada
- vision models: @amyeroberts
- speech models: @sanchit-gandhi
- graph models: @clefourrier

Library:

- flax: @sanchit-gandhi
- generate: @gante
- pipelines: @Narsil
- tensorflow: @gante and @Rocketknight1
- tokenizers: @ArthurZucker
- trainer: @sgugger

Integrations:

- deepspeed: HF Trainer: @stas00, Accelerate: @pacman100
- ray/raytune: @richardliaw, @amogkam

Documentation: @sgugger, @stevhliu and @MKhalusova

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: @sgugger
- TensorFlow: @Rocketknight1

 -->


Comments:
cc @gante 

Thank you for the comment!

Based on my understanding, [this line of code](https://github.com/huggingface/transformers/pull/21916/files#diff-6b72b98c4c2dcfc6cc606843917733f5d858374fbc22a735ff483bbc0c1e63eaR1054) enables the checking process only once during the forward pass, so it should not significantly impact performance.

The current warning method only issue warnings when `attention_mask` is necessary (due to the presence of padding tokens in the input), but no `attention_mask` is provided. In other cases where `attention_mask` is not required, no warning is issued. The additional checking on special tokens allows a more detailed warning message. 

I agree that your suggested method is more concise and efficient, but it may generate warnings when `attention_mask` is not needed. 

Since it's my first time contributing to the community, I don't have a strong opinion towards either solution. The original work is by @ydshieh and @patrickvonplaten. Perhaps they have additional insights and can suggest a more effective solution.

> Why not a simple logger.warning_once()

This is recently introduced :-)

@anruijian It checks `input_ids` until there is a batch in which a `pad_token_id` exists. If a user is working on a problem where  they have no `pad_token_id` on their data and they don't pass the `attention_mask`, there is a check made every forward pass. I'd strongly advocate for a simple warning when the `attention_mask` is not passed 🤗 

As a side note, we have related problems at other points in the code base. Getting into the habit of passing the `attention_mask` would really make everyone happier!

@gante Just to confirm before updating the PR, we are going to remove `warn_if_pad_token_in_input_ids_no_attention_mask` method and use `logger.warning_once` in `forward()`:
```python
def forward(...):
    ...
    if not attention_mask:
        logger.warning_once(
            "\nWe strongly recommend passing an `attention_mask` to avoid possibly incorrectly computing the"
            " attention weights. "
        )
    ...
```

@anruijian correct :) I would add a short example in the warning, such as `(e.g. to correctly mask the pad tokens)`, but I'll leave that up to you!

@gante 
```python
def forward(...):
    ...
    if not attention_mask:
        logger.warning_once(
            "\nWe strongly recommend passing an `attention_mask` to avoid possibly incorrectly computing the"
            " attention weights. Example to correctly mask the pad tokens: model(input_ids, attention_mask=attention_mask)."
            " See https://huggingface.co/docs/transformers/v4.23.1/en/troubleshooting#incorrect-output-when-padding-tokens-arent-masked for more details."
        )
    ...
```
Does this example look good to you? I also link the official doc on the issue. Not sure if it's too long. Let me know what you think about this. Thanks!


@anruijian sounds good to me! (A minor nit: the link is for v4.23 of the docs, should be `https://huggingface.co/docs/transformers/troubleshooting#incorrect-output-when-padding-tokens-arent-masked` instead)

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.