MobileBertForSequenceClassification outputs super-high logits
Author: NadiaRom
Pull request: no
Labels: name: wontfix

## Environment info
<!-- You can run the command `transformers-cli env` and copy-and-paste its output below.
     Don't forget to fill out the missing fields in that output! -->
     
- `transformers` version: 4.0.0
- Platform: Ubuntu 20.04
- Python version: 3.8.5
- PyTorch version (GPU?): 1.7.0
- Tensorflow version (GPU?): None
- Using GPU in script?: True
- Using distributed or parallel set-up in script?: False

### Who can help
<!-- Your issue 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.
 
 albert, bert, GPT2, XLM: @LysandreJik 
 tokenizers: @mfuntowicz
 Trainer: @sgugger
 Speed and Memory Benchmarks: @patrickvonplaten
 Model Cards: @julien-c
 TextGeneration: @TevenLeScao 
 examples/distillation: @VictorSanh
 nlp datasets: [different repo](https://github.com/huggingface/nlp)
 rust tokenizers: [different repo](https://github.com/huggingface/tokenizers)
 Text Generation: @patrickvonplaten @TevenLeScao
 Blenderbot: @patrickvonplaten
 Bart: @patrickvonplaten
 Marian: @patrickvonplaten
 Pegasus: @patrickvonplaten
 mBART: @patrickvonplaten
 T5: @patrickvonplaten
 Longformer/Reformer: @patrickvonplaten
 TransfoXL/XLNet: @TevenLeScao
 RAG: @patrickvonplaten, @lhoestq
 FSMT: @stas00
 examples/seq2seq: @patil-suraj
 examples/bert-loses-patience: @JetRunner
 tensorflow: @jplu
 examples/token-classification: @stefan-it
 documentation: @sgugger
 -->
@LysandreJik 

## Information

Model I am using (Bert, XLNet ...): MobileBertForSequenceClassification

The problem arises when using:
* [ ] the official example scripts: (give details below)
* [x] my own modified scripts: please see an example below

The tasks I am working on is:
* [ ] an official GLUE/SQUaD task: (give the name)
* [x] my own task or dataset: simple text classification using documents from sec.gov

## To reproduce

Steps to reproduce the behavior:

I am training a whole-text classifier with MobileBert using MobileBertForSequenceClassification:
```{python}
from transformers import MobileBertForSequenceClassification, \
                                        MobileBertTokenizerFast

ARCH = 'google/mobilebert-uncased'

model = MobileBertForSequenceClassification.from_pretrained(ARCH).cuda()
tokenizer = MobileBertTokenizerFast.from_pretrained(ARCH)

x = tokenizer(['def hello(): return "world"', 'This is some test'],
              max_length=512,
              truncation=True,
              return_tensors='pt',
              padding='longest')

with torch.no_grad():
    l = model(**x.to(model.device)).logits 
```
Resulting model outputs are extremely high:
```
tensor([[ 3289181.7500, -2371234.0000],
        [ 3198336.7500, -1882639.8750]])
```
Loading model and tokenizer with Auto- classes gives the same result. 
When using pooled output from ModileBertModel with a custom linear head (BatchNorm1d+Dropout+Linear) everything works fine.

<!-- If you have code snippets, error messages, stack traces please provide them here as well.
     Important! Use code tags to correctly format your code. See https://help.github.com/en/github/writing-on-github/creating-and-highlighting-code-blocks#syntax-highlighting
     Do not use screenshots, as they are hard to read and (more importantly) don't allow others to copy-and-paste your code.-->

## Expected behavior

I expect logits to be near [-3, 3], but not in 6-7 digits.


Comments:
I see the same behavior when trying it out with IMDB classification. 
I solved it by passing `classifier_activation=True` for the `from_pretrained` function. 
[Documentation](https://huggingface.co/transformers/model_doc/mobilebert.html#mobilebertconfig) says it is `True` by default, however it does not seem like it. 
[EDIT] Apparently this changes the behavior of the pooling layer 

@hfawaz Thank you for solving this issue, it's very helpful

This issue has been automatically marked as stale and been closed because it has not had recent activity. Thank you for your contributions.

If you think this still needs to be addressed please comment on this thread.

> I see the same behavior when trying it out with IMDB classification. I solved it by passing `classifier_activation=True` for the `from_pretrained` function. [Documentation](https://huggingface.co/transformers/model_doc/mobilebert.html#mobilebertconfig) says it is `True` by default, however it does not seem like it. [EDIT] Apparently this changes the behavior of the pooling layer

I encountered the same issue and solved it by passing classifier_activation=True for the from_pretrained function. This should even be a bug in the transformers library. It is now 2025, and the result is still the same.