Repository: huggingface/transformers

Issue:
Instantiating a model from `pipeline()` ignores `model_kwargs` parameter
## Environment info

- `transformers` version: 4.8.1
- Platform: Linux-3.10.0-1160.31.1.el7.x86_64-x86_64-with-centos-7.9.2009-Core
- Python version: 3.7.9
- PyTorch version (GPU?): 1.8.1+cu102 (True)
- Tensorflow version (GPU?): not installed (NA)
- Flax version (CPU?/GPU?/TPU?): not installed (NA)
- Jax version: not installed
- JaxLib version: not installed
- Using GPU in script?: no
- Using distributed or parallel set-up in script?: no

### Who can help

This should be a one-line fix, so I will be submitting a PR shortly.

## Information

Model I am using: `gpt2` (not model-specific issue, though)

The problem arises when using:
* [ ] the official example scripts: (give details below)
* [x] my own modified scripts: (give details below)

The tasks I am working on is:
* [ ] an official GLUE/SQUaD task: (give the name)
* [x] my own task or dataset: (give details below)

## To reproduce

Steps to reproduce the behavior:

1. Ensure the model cache is already populated with the correct model by running the following code:

```python
from transformers import AutoModelForCausalLM

_ = AutoModelForCausalLM.from_pretrained("gpt2", cache_dir="model_cache")
```

2. Put the following code in `test.py`:

```python
from transformers import pipeline

_ = pipeline("text-generation", model="gpt2", model_kwargs={"cache_dir": "model_cache"})
```

3. Run `time TRANSFORMERS_OFFLINE=1 python test.py` to force the cache to be hit
4. See that the following exception is returned:

```console
Cannot find the requested files in the cached path and outgoing traffic has been disabled. To enable model look-ups and downloads online, set 'local_files_only' to False.
Traceback (most recent call last):
  File "test.py", line 3, in <module>
    _ = pipeline("text-generation", model="gpt2", model_kwargs={"cache_dir": "model_cache"})
  File "venv/lib/python3.7/site-packages/transformers/pipelines/__init__.py", line 409, in pipeline
    model, model_classes=model_classes, config=config, framework=framework, revision=revision, task=task
  File "venv/lib/python3.7/site-packages/transformers/pipelines/base.py", line 136, in infer_framework_load_model
    model = model_class.from_pretrained(model, **kwargs)
  File "venv/lib/python3.7/site-packages/transformers/utils/dummy_tf_objects.py", line 991, in from_pretrained
    requires_backends(cls, ["tf"])
  File "venv/lib/python3.7/site-packages/transformers/file_utils.py", line 612, in requires_backends
    raise ImportError("".join([BACKENDS_MAPPING[backend][1].format(name) for backend in backends]))
ImportError:
TFGPT2LMHeadModel requires the TensorFlow library but it was not found in your environment. Checkout the instructions on the
installation page: https://www.tensorflow.org/install and follow the ones that match your environment.
```

(I edited the stack trace to remove the parts of the path outside the virtual environment.)

## Expected behavior

There should be no output because the model should be loaded from the cache without issues.


Discussion:

Resolving patch:
diff --git a/src/transformers/pipelines/__init__.py b/src/transformers/pipelines/__init__.py
--- a/src/transformers/pipelines/__init__.py
+++ b/src/transformers/pipelines/__init__.py
@@ -406,7 +406,13 @@ def pipeline(
     # Will load the correct model if possible
     model_classes = {"tf": targeted_task["tf"], "pt": targeted_task["pt"]}
     framework, model = infer_framework_load_model(
-        model, model_classes=model_classes, config=config, framework=framework, revision=revision, task=task
+        model,
+        model_classes=model_classes,
+        config=config,
+        framework=framework,
+        revision=revision,
+        task=task,
+        **model_kwargs,
     )
 
     model_config = model.config


Test patch:
diff --git a/tests/test_pipelines_token_classification.py b/tests/test_pipelines_token_classification.py
--- a/tests/test_pipelines_token_classification.py
+++ b/tests/test_pipelines_token_classification.py
@@ -61,6 +61,13 @@ def _test_pipeline(self, token_classifier: Pipeline):
             for key in output_keys:
                 self.assertIn(key, result)
 
+    @require_torch
+    def test_model_kwargs_passed_to_model_load(self):
+        ner_pipeline = pipeline(task="ner", model=self.small_models[0])
+        self.assertFalse(ner_pipeline.model.config.output_attentions)
+        ner_pipeline = pipeline(task="ner", model=self.small_models[0], model_kwargs={"output_attentions": True})
+        self.assertTrue(ner_pipeline.model.config.output_attentions)
+
     @require_torch
     @slow
     def test_spanish_bert(self):