Repository: huggingface/transformers

Issue:
small bug in RAG model
## 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.5.1
- Platform: Linux-5.4.0-66-generic-x86_64-with-glibc2.27
- Python version: 3.8.3
- PyTorch version (GPU?): 1.8.1+cu111 (True)

### Who can help
@ola13 

Models:

- rag

## Information

Model I am using (Bert, XLNet ...): RAG

The problem arises when using:
* [ ] the official example scripts: seems to be a bug in https://github.com/huggingface/transformers/blob/master/src/transformers/models/rag/modeling_rag.py#L306
To pass my own question encoder the argument needs to be `question_encoder_model`, but the suffix ("model") gets trimmed because the len is taken according to `question_question_encoder_`.

```python
kwargs_question_encoder = {
            argument[len("question_question_encoder_") :]: value
            for argument, value in kwargs.items()
            if argument.startswith("question_encoder_")
        }
```

## To reproduce

Steps to reproduce the behavior:

```python
question_encoder = AutoModel.from_pretrained("any model")
rag_model = model_class.from_pretrained_question_encoder_generator(
        question_encoder_model=question_encoder, generator_pretrained_model_name_or_path=generator_name_or_path, config=rag_config
    )
```

<!-- 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

be able to pass a question encoder **model** and not just a config to the rag model.



Discussion:
This indeed looks like a typo! Thanks for your issue :-)

The kwargs should not be trimmed by `"question_question_encoder"`, but by `"question_encoder"`.

Would you like to open a PR to fix it? Otherwise I can do it as well :-)

Resolving patch:
diff --git a/src/transformers/models/rag/modeling_rag.py b/src/transformers/models/rag/modeling_rag.py
--- a/src/transformers/models/rag/modeling_rag.py
+++ b/src/transformers/models/rag/modeling_rag.py
@@ -245,7 +245,6 @@ def from_pretrained_question_encoder_generator(
         question_encoder_pretrained_model_name_or_path: str = None,
         generator_pretrained_model_name_or_path: str = None,
         retriever: RagRetriever = None,
-        *model_args,
         **kwargs
     ) -> PreTrainedModel:
         r"""
@@ -310,7 +309,7 @@ def from_pretrained_question_encoder_generator(
         """
 
         kwargs_question_encoder = {
-            argument[len("question_question_encoder_") :]: value
+            argument[len("question_encoder_") :]: value
             for argument, value in kwargs.items()
             if argument.startswith("question_encoder_")
         }
@@ -340,11 +339,15 @@ def from_pretrained_question_encoder_generator(
             if "config" not in kwargs_question_encoder:
                 from ..auto.configuration_auto import AutoConfig
 
-                question_encoder_config = AutoConfig.from_pretrained(question_encoder_pretrained_model_name_or_path)
+                question_encoder_config, kwargs_question_encoder = AutoConfig.from_pretrained(
+                    question_encoder_pretrained_model_name_or_path,
+                    **kwargs_question_encoder,
+                    return_unused_kwargs=True,
+                )
                 kwargs_question_encoder["config"] = question_encoder_config
 
             question_encoder = AutoModel.from_pretrained(
-                question_encoder_pretrained_model_name_or_path, *model_args, **kwargs_question_encoder
+                question_encoder_pretrained_model_name_or_path, **kwargs_question_encoder
             )
 
         generator = kwargs_generator.pop("model", None)
@@ -357,7 +360,10 @@ def from_pretrained_question_encoder_generator(
             if "config" not in kwargs_generator:
                 from ..auto.configuration_auto import AutoConfig
 
-                generator_config = AutoConfig.from_pretrained(generator_pretrained_model_name_or_path)
+                generator_config, kwargs_generator = AutoConfig.from_pretrained(
+                    generator_pretrained_model_name_or_path, **kwargs_generator, return_unused_kwargs=True
+                )
+
                 kwargs_generator["config"] = generator_config
 
             generator = AutoModelForSeq2SeqLM.from_pretrained(


Test patch:
diff --git a/tests/test_modeling_rag.py b/tests/test_modeling_rag.py
--- a/tests/test_modeling_rag.py
+++ b/tests/test_modeling_rag.py
@@ -1083,12 +1083,17 @@ def test_rag_token_from_pretrained(self):
                 "facebook/bart-large-cnn",
                 retriever=rag_retriever,
                 config=rag_config,
+                question_encoder_max_length=200,
+                generator_max_length=200,
             ).to(torch_device)
             # check that the from pretrained methods work
             rag_token.save_pretrained(tmp_dirname)
             rag_token.from_pretrained(tmp_dirname, retriever=rag_retriever)
             rag_token.to(torch_device)
 
+            self.assertTrue(rag_token.question_encoder.config.max_length == 200)
+            self.assertTrue(rag_token.generator.config.max_length == 200)
+
             with torch.no_grad():
                 output = rag_token(
                     input_ids,