Repository: huggingface/transformers

Issue:
AutoModelForMaskedLM not working when using MBartForConditionalGeneration architecture.
## Environment info
- `transformers` version: 4.2.1
- Platform: Linux-4.4.0-197-generic-x86_64-with-glibc2.10
- Python version: 3.8.5
- PyTorch version (GPU?): 1.7.1 (True)
- Tensorflow version (GPU?): not installed (NA)
- Using GPU in script?: No
- Using distributed or parallel set-up in script?: No

### Who can help
@patrickvonplaten @LysandreJik 

## Information

Model I am using (Bert, XLNet ...): BARThez, MBART

## To reproduce
```python
from transformers import (
    BarthezTokenizer,
    AutoModelForMaskedLM,
    MBartForConditionalGeneration
)

barthez_tokenizer = BarthezTokenizer.from_pretrained("moussaKam/barthez")
barthez_model = AutoModelForMaskedLM.from_pretrained("moussaKam/barthez")
```
error:
```
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-11-cef569c55032> in <module>
      9 
     10 barthez_tokenizer = BarthezTokenizer.from_pretrained("moussaKam/barthez")
---> 11 barthez_model = AutoModelForMaskedLM.from_pretrained("moussaKam/barthez")
     12 
     13 input_ids = torch.tensor(

~/anaconda3/envs/transformers/lib/python3.8/site-packages/transformers/models/auto/modeling_auto.py in from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs)
   1123                 pretrained_model_name_or_path, *model_args, config=config, **kwargs
   1124             )
-> 1125         raise ValueError(
   1126             "Unrecognized configuration class {} for this kind of AutoModel: {}.\n"
   1127             "Model type should be one of {}.".format(

ValueError: Unrecognized configuration class <class 'transformers.models.mbart.configuration_mbart.MBartConfig'> for this kind of AutoModel: AutoModelForMaskedLM.
Model type should be one of LayoutLMConfig, DistilBertConfig, AlbertConfig, BartConfig, CamembertConfig, XLMRobertaConfig, LongformerConfig, RobertaConfig, SqueezeBertConfig, BertConfig, MobileBertConfig, FlaubertConfig, XLMConfig, ElectraConfig, ReformerConfig, FunnelConfig, MPNetConfig, TapasConfig.
```

The model works as expected when using `MBartForConditionalGeneration` instead of `AutoModelForMaskedLM`.

After checking I see that the public model [MBart](https://huggingface.co/facebook/mbart-large-cc25/blob/main/config.json) itself is using `BartForConditionalGeneration` as default architecture, is that normal?  


Discussion:
Hey @moussaKam, 

Thanks for your issue! My opinion here is the following:

- `MBartForConditionalGeneration` should not work with `AutoModelForMaskedLM`, but only with `AutoModelForSeq2SeqLM` -> it's not a Bert-like autoencoding model, but an encoder-decoder model.
- You are completely right in that `MBartForConditionalGeneration` should have `MBartForConditionalGeneration` in its config and not `Bart...`. This should however not make a difference when loading the model with `AutoModelForSeq2SeqLM.from_pretrained(...)` -> I'll change that!

@LysandreJik what do you think?
Hi @moussaKam, I agree with @patrickvonplaten that `MBartForConditionalGeneration` should not work with `AutoModelForMaskedLM` but only with `AutoModelForSeq2SeqLM`.

I can confirm that 
```py
>>> from transformers import AutoModelForSeq2SeqLM
>>> model = AutoModelForSeq2SeqLM.from_pretrained("moussaKam/barthez")
```
works correctly.

Also agree with you that the configuration looks better thanks to [huggingface.co#88467f](https://huggingface.co/facebook/mbart-large-cc25/commit/88467fef84ba338740dc562dec3a105c2b14de9f)!
Yes @LysandreJik  @patrickvonplaten you are completely right, however the [inference API](https://huggingface.co/moussaKam/barthez?text=Paris+est+la+%3Cmask%3E+de+la+France.) is using `AutoModelForMaskedLM` for some reason, and returning the following error:
```
⚠️ Unrecognized configuration class for this kind of AutoModel: AutoModelForMaskedLM. Model type should be one of LayoutLMConfig, DistilBertConfig, AlbertConfig, BartConfig, CamembertConfig, XLMRobertaConfig, LongformerConfig, RobertaConfig, SqueezeBertConfig, BertConfig, MobileBertConfig, FlaubertConfig, XLMConfig, ElectraConfig, ReformerConfig, FunnelConfig, MPNetConfig, TapasConfig.
```

Is there anyway we can fix this issue?
That is problematic, indeed. Let me check what's going on.
Yes, same problem when using `pipeline`.

```python
from transformers import pipeline

pbase = pipeline(task="fill-mask", model="moussaKam/barthez")
src_text = ["Paris est la capitale de la <mask>"]
results = [x["token_str"] for x in pbase(src_text)]
```

```
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-12-399795ed06d8> in <module>
      1 from transformers import pipeline
      2 
----> 3 pbase = pipeline(task="fill-mask", model="moussaKam/barthez")
      4 src_text = ["Paris est la capitale de la <mask>"]
      5 results = [x["token_str"] for x in pbase(src_text)]

~/anaconda3/envs/transformers/lib/python3.8/site-packages/transformers/pipelines/__init__.py in pipeline(task, model, config, tokenizer, framework, revision, use_fast, **kwargs)
    403             )
    404 
--> 405         model = model_class.from_pretrained(model, config=config, revision=revision, **model_kwargs)
    406         if task == "translation" and model.config.task_specific_params:
    407             for key in model.config.task_specific_params:

~/anaconda3/envs/transformers/lib/python3.8/site-packages/transformers/models/auto/modeling_auto.py in from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs)
   1123                 pretrained_model_name_or_path, *model_args, config=config, **kwargs
   1124             )
-> 1125         raise ValueError(
   1126             "Unrecognized configuration class {} for this kind of AutoModel: {}.\n"
   1127             "Model type should be one of {}.".format(

ValueError: Unrecognized configuration class <class 'transformers.models.mbart.configuration_mbart.MBartConfig'> for this kind of AutoModel: AutoModelForMaskedLM.
Model type should be one of LayoutLMConfig, DistilBertConfig, AlbertConfig, BartConfig, CamembertConfig, XLMRobertaConfig, LongformerConfig, RobertaConfig, SqueezeBertConfig, BertConfig, MobileBertConfig, FlaubertConfig, XLMConfig, ElectraConfig, ReformerConfig, FunnelConfig, MPNetConfig, TapasConfig.
```

Resolving patch:
diff --git a/src/transformers/models/auto/modeling_auto.py b/src/transformers/models/auto/modeling_auto.py
--- a/src/transformers/models/auto/modeling_auto.py
+++ b/src/transformers/models/auto/modeling_auto.py
@@ -411,6 +411,7 @@
         (DistilBertConfig, DistilBertForMaskedLM),
         (AlbertConfig, AlbertForMaskedLM),
         (BartConfig, BartForConditionalGeneration),
+        (MBartConfig, MBartForConditionalGeneration),
         (CamembertConfig, CamembertForMaskedLM),
         (XLMRobertaConfig, XLMRobertaForMaskedLM),
         (LongformerConfig, LongformerForMaskedLM),


Test patch: