Infer padding in GPT2ForSequenceClassification when inputs_embeds and attention_mask are given
# 🚀 Feature request

Infer padding in `GPT2ForSequenceClassification` when `inputs_embeds` and `attention_mask` are given

## Motivation

When `config.pad_token_id is not None` and `inputs_embeds` are passed instead of `input_ids`, the forward method of `GPT2ForSequenceClassification` raises a warning telling that _"GPT2ForSequenceClassification will not detect padding tokens in `inputs_embeds`. Results may be unexpected if using padding tokens in conjunction with `inputs_embeds.`"_.

This happens because you have nowhere to look for the padding tokens start index when `input_ids` is not given. As a consequence, the last position (of the tensor) is taken as the classification token.

In practice, one may need to pass a batch of `inputs_embeds` with different sequence lengths and padded and I think there are options to solve or workaround this.

## Your contribution

One alternative that comes to my mind is to infer the sequence lengths from the attention mask (only when we don't have `input_ids`)
For instance:
a) Optimistic approach: the sequence length is given by the position of the last 1 in the corresponding row of the attention mask
or
b) Not so optimistic approach: the sequence length is given by the position of the last 1 in the attention mask only if all the previous positions are 1's and only if this is the case for every row of the attention mask; in any other case, `sequence_lengths = -1` and the warning would be raised.

Examples for approach b):

```
attention_mask = [
   [1, 1, 1, 1, 1],
   [1, 1, 1, 0, 0]
]
```
=> We infer sequence lengths = [5, 3] (in the code I think it should be [4, 2] actually)

```
attention_mask = [
   [0, 1, 1, 1, 1],
   [1, 1, 1, 0, 0]
]
```
=> We don't infer anything, sequence lengths = [-1, -1] (in the code I think it's just `sequence_lengths = -1` as broadcasting happens)


For reference, the modification would be done here: https://github.com/huggingface/transformers/blob/8a69e023bf81160e64848b256441e64a4cb47992/src/transformers/models/gpt2/modeling_gpt2.py#L1406-L1411

Do you see it reasonable or is it too much of an assumption?

A safer alternative is to add a new parameter that is only used when `inputs_embeds' are passed, but I'm not sure it's justified enough just for this specific case.

I can submit a PR that implements this or a different solution.

Thanks in advance.
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.