Attention can be None in ModernBertForSequenceClassification
Author: ashmikuz
Pull request: no

In the ModernBertForSequenceClassification class, the attention is never computed outside of the self.model (which is a ModernBertModel). Therefore when the attention is not used as input for the model the .unsqueeze() [here](https://github.com/huggingface/transformers/blob/86d7564611d21731fc004b4e79e472d48c4b0fec/src/transformers/models/modernbert/modular_modernbert.py#L1362) fails.
I solved this issue by assagning torch.ones(batch_size,seq_len) to the attention_mask, but I am not sure whether this is correct.
  

Comments:
Hi @ashmikuz, when no attention mask is passed then we can't really work out which positions are masked! Although we could add code to estimate this (like `attention_mask = input_ids != self.config.pad_token_id`), this is error-prone and I think a better solution is just to raise a clear error in this case, telling the user they have to pass an attention mask if they want to use `self.classifier_pooling == "mean"`.

Would you be interested in making a PR for that?

Hi @Rocketknight1 , @ashmikuz,

I had the same issue. Is anyone working on this? Otherwise I will raise a PR and add this RuntimeError:

`if self.config.classifier_pooling == "mean" and attention_mask is None:
            raise RuntimeError("Mean pooling requires an attention mask to properly compute the pooled output. Please provide an attention mask to indicate which tokens should be considered in the mean pooling calculation.")
`

Sorry I was quite busy in the last few days. Shouldn't it match how other models behave? As far as I understand, other models just print a warning and then create an attention mask from torch.ones, right?

Yes, you are right, I see the torch.ones for e.g. deberta [here](https://github.com/tom13878/transformers/blob/d7188ba600e36d3fd191b12e19f1b3bb81a8404f/src/transformers/models/deberta_v2/modeling_deberta_v2.py#L857)

I don't see the warning but I may have missed it ...

I'm working on a quick PR, just a moment and i'll send it. Hopefully it fixes the issue and is in line with other models.

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.

I'm sorry but the issue is still present, I just pulled the latest version and tested with this simple code:


```python
model_id = "answerdotai/ModernBERT-base"
model = transformers.AutoModelForSequenceClassification.from_pretrained(
    model_id,
    num_labels=2,
)

inputs = torch.full((8, 10), fill_value=model.config.pad_token_id)

model(inputs)
```

As things stand, the model crashes when it tries to unsqueeze the attention_mask, which is None. With the provided PR the issue is solved and a warning is shown:

```
We strongly recommend passing in an `attention_mask` since your input_ids may be padded. See https://huggingface.co/docs/transformers/troubleshooting#incorrect-output-when-padding-tokens-arent-masked.
```