RAG model card code not working in Colab
Author: rbownes
Pull request: no

## Environment info

- `transformers` version: 3.3.1
- Platform: Linux-4.19.112+-x86_64-with-Ubuntu-18.04-bionic
- Python version: 3.6.9
- PyTorch version (GPU?): 1.6.0+cu101 (True)
- Tensorflow version (GPU?): 2.3.0 (True)
- Using GPU in script?: Yes
- Using distributed or parallel set-up in script?: No
   

### Who can help
 
@julien-c @VictorSanh

## Information

Model I am using RAG

The problem arises when using:
* [X] the official example scripts: (give details below)

## To reproduce

Steps to reproduce the behavior:

1. Open a new colab notebook 
2. !pip install transformers
3. execute the RAG model example code 

```from transformers import RagTokenizer, RagRetriever, RagTokenForGeneration

tokenizer = RagTokenizer.from_pretrained("facebook/rag-token-nq")
retriever = RagRetriever.from_pretrained("facebook/rag-token-nq", index_name="exact", use_dummy_dataset=True)
model = RagTokenForGeneration.from_pretrained("facebook/rag-token-nq", retriever=retriever)

input_dict = tokenizer.prepare_seq2seq_batch("who holds the record in 100m freestyle", return_tensors="pt") 

generated = model.generate(input_ids=input_dict["input_ids"]) 
print(tokenizer.batch_decode(generated, skip_special_tokens=True)[0]) 
```
error traceback

``` 
NameError                                 Traceback (most recent call last)

<ipython-input-5-fcc46db034ee> in <module>()
      2 
      3 tokenizer = RagTokenizer.from_pretrained("facebook/rag-token-nq")
----> 4 retriever = RagRetriever.from_pretrained("facebook/rag-token-nq", index_name="exact", use_dummy_dataset=True)
      5 model = RagTokenForGeneration.from_pretrained("facebook/rag-token-nq", retriever=retriever)
      6 

2 frames

/usr/local/lib/python3.6/dist-packages/transformers/retrieval_rag.py in __init__(self, dataset_name, dataset_split, index_name, vector_size, index_path, use_dummy_dataset)
    218 
    219         logger.info("Loading passages from {}".format(self.dataset_name))
--> 220         self.dataset = load_dataset(
    221             self.dataset_name, with_index=False, split=self.dataset_split, dummy=self.use_dummy_dataset
    222         )

NameError: name 'load_dataset' is not defined
```


## Expected behavior

I would expect the model card example to be output:

# should give michael phelps => sounds reasonable

or something to this effect.


Comments:
You need to install `datasets` too for this model:
```
! pip install datasets
```
I'll work on some cleaner error messages.

Thanks for responding @sgugger !

Sadly that didn't fix it:

!pip install transformers
!pip install datasets
```

NameError                                 Traceback (most recent call last)

<ipython-input-2-fcc46db034ee> in <module>()
      2 
      3 tokenizer = RagTokenizer.from_pretrained("facebook/rag-token-nq")
----> 4 retriever = RagRetriever.from_pretrained("facebook/rag-token-nq", index_name="exact", use_dummy_dataset=True)
      5 model = RagTokenForGeneration.from_pretrained("facebook/rag-token-nq", retriever=retriever)
      6 

2 frames

/usr/local/lib/python3.6/dist-packages/transformers/retrieval_rag.py in __init__(self, dataset_name, dataset_split, index_name, vector_size, index_path, use_dummy_dataset)
    218 
    219         logger.info("Loading passages from {}".format(self.dataset_name))
--> 220         self.dataset = load_dataset(
    221             self.dataset_name, with_index=False, split=self.dataset_split, dummy=self.use_dummy_dataset
    222         )

NameError: name 'load_dataset' is not defined
```

Still resulted in the same error.

Did you try restarting the colab? `datasets` requires that if I'm not mistaken.

Problem persists after restarting. 

I tried it locally as well and I get the same error, but with a more verbose message.

```
NameError                                 Traceback (most recent call last)
<ipython-input-4-e0fde23b2cd7> in <module>
      2
      3 tokenizer = RagTokenizer.from_pretrained("facebook/rag-token-nq")
----> 4 retriever = RagRetriever.from_pretrained("facebook/rag-token-nq", index_name="exact", use_dummy_dataset=True)
      5 model = RagTokenForGeneration.from_pretrained("facebook/rag-token-nq", retriever=retriever)
      6

~/Library/Caches/pypoetry/virtualenvs/bbc-transformer-vt1pdFaV-py3.8/lib/python3.8/site-packages/transformers/retrieval_rag.py in from_pretrained(cls, retriever_name_or_path, **kwargs)
    306         question_encoder_tokenizer = rag_tokenizer.question_encoder
    307         generator_tokenizer = rag_tokenizer.generator
--> 308         return cls(
    309             config, question_encoder_tokenizer=question_encoder_tokenizer, generator_tokenizer=generator_tokenizer
    310         )

~/Library/Caches/pypoetry/virtualenvs/bbc-transformer-vt1pdFaV-py3.8/lib/python3.8/site-packages/transformers/retrieval_rag.py in __init__(self, config, question_encoder_tokenizer, generator_tokenizer)
    281             )
    282             if config.index_name == "legacy"
--> 283             else HFIndex(
    284                 config.dataset,
    285                 config.dataset_split,

~/Library/Caches/pypoetry/virtualenvs/bbc-transformer-vt1pdFaV-py3.8/lib/python3.8/site-packages/transformers/retrieval_rag.py in __init__(self, dataset_name, dataset_split, index_name, vector_size, index_path, use_dummy_dataset)
    218
    219         logger.info("Loading passages from {}".format(self.dataset_name))
--> 220         self.dataset = load_dataset(
    221             self.dataset_name, with_index=False, split=self.dataset_split, dummy=self.use_dummy_dataset
    222         )

NameError: name 'load_dataset' is not defined
```

I think I found why while fixing the error message. This also needs the faiss library: `! pip install faiss`.

@sgugger Confirmed.

```
!pip install transformers
!pip install datasets
!pip install faiss
```

give the expected behaviour. Thank you!

Working on having some clear error message for the next users in #7537 :-)
Thanks for flagging the problem!

Thanks for the help! :) 

@sgugger, I have the following versions of the packages installed : 
transformers==3.3.1
datasets==1.1.2
faiss==1.5.3
I still see the error.
It would be great if you could document which versions of faiss, datasets, and transformers works !

I imported datasets to see if it helps. Didn't.

on Google Colab, switch to a GPU runtime, then try with:
`!pip install faiss-gpu`
finally restart the runtime.

It worked for me :)

I have the same question, which is 
`ImportError                               Traceback (most recent call last)
[<ipython-input-7-d8ba1013a0e5>](https://localhost:8080/#) in <module>()
      6 
      7 tokenizer = RagTokenizer.from_pretrained("facebook/rag-token-nq")
----> 8 retriever = RagRetriever.from_pretrained("facebook/rag-token-nq", index_name="exact", use_dummy_dataset=True)
      9 model = RagTokenForGeneration.from_pretrained("facebook/rag-token-nq", retriever=retriever)
     10 

1 frames
[/usr/local/lib/python3.7/dist-packages/transformers/file_utils.py](https://localhost:8080/#) in requires_backends(obj, backends)
    846     failed = [msg.format(name) for available, msg in checks if not available()]
    847     if failed:
--> 848         raise ImportError("".join(failed))
    849 
    850 

ImportError: 
RagRetriever requires the 🤗 Datasets library but it was not found in your environment. You can install it with:
```
pip install datasets
```
In a notebook or a colab, you can install it by executing a cell with
```
!pip install datasets
```
then restarting your kernel.

Note that if you have a local folder named `datasets` or a local python file named `datasets.py` in your current
working directory, python may try to import this instead of the 🤗 Datasets library. You should rename this folder or
that python file if that's the case.

RagRetriever requires the faiss library but it was not found in your environment. Checkout the instructions on the
installation page of its repo: https://github.com/facebookresearch/faiss/blob/master/INSTALL.md and follow the ones
that match your environment.


---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.

To view examples of installing some common dependencies, click the
"Open Examples" button below.
---------------------------------------------------------------------------`
but either I add !pip install faiss-gpu or !pip install faiss is not useful.

> @sgugger Confirmed.
> 
> ```
> !pip install transformers
> !pip install datasets
> !pip install faiss
> ```
> 
> give the expected behaviour. Thank you!

I used above commands and it shows faiss is imported but still gives the import error for faiss.

ImportError                               Traceback (most recent call last)
[<ipython-input-5-c04f039ae844>](https://localhost:8080/#) in <cell line: 4>()
      2 
      3 tokenizer = RagTokenizer.from_pretrained("facebook/rag-token-nq")
----> 4 retriever = RagRetriever.from_pretrained("facebook/rag-token-nq", index_name="exact", use_dummy_dataset=True)
      5 model = RagTokenForGeneration.from_pretrained("facebook/rag-token-nq", retriever=retriever)
      6 

1 frames
[/usr/local/lib/python3.10/dist-packages/transformers/utils/import_utils.py](https://localhost:8080/#) in requires_backends(obj, backends)
   1012     failed = [msg.format(name) for available, msg in checks if not available()]
   1013     if failed:
-> 1014         raise ImportError("".join(failed))
   1015 
   1016 

ImportError: 
RagRetriever requires the faiss library but it was not found in your environment. Checkout the instructions on the installation page of its repo: https://github.com/facebookresearch/faiss/blob/master/INSTALL.md and follow the ones
that match your environment. Please note that you may need to restart your runtime after installation.
<img width="1002" alt="image" src="https://github.com/huggingface/transformers/assets/75541422/2d1d974e-e292-4b5d-acbc-ec2ae644ef3f">


> > @sgugger Confirmed.
> > ```
> > !pip install transformers
> > !pip install datasets
> > !pip install faiss
> > ```
> > 
> > 
> >     
> >       
> >     
> > 
> >       
> >     
> > 
> >     
> >   
> > give the expected behaviour. Thank you!
> 
> I used above commands and it shows faiss is imported but still gives the import error for faiss.
> 
> ImportError Traceback (most recent call last) in <cell line: 4>() 2 3 tokenizer = RagTokenizer.from_pretrained("facebook/rag-token-nq") ----> 4 retriever = RagRetriever.from_pretrained("facebook/rag-token-nq", index_name="exact", use_dummy_dataset=True) 5 model = RagTokenForGeneration.from_pretrained("facebook/rag-token-nq", retriever=retriever) 6
> 
> 1 frames [/usr/local/lib/python3.10/dist-packages/transformers/utils/import_utils.py](https://localhost:8080/#) in requires_backends(obj, backends) 1012 failed = [msg.format(name) for available, msg in checks if not available()] 1013 if failed: -> 1014 raise ImportError("".join(failed)) 1015 1016
> 
> ImportError: RagRetriever requires the faiss library but it was not found in your environment. Checkout the instructions on the installation page of its repo: https://github.com/facebookresearch/faiss/blob/master/INSTALL.md and follow the ones that match your environment. Please note that you may need to restart your runtime after installation. <img alt="image" width="1002" src="https://user-images.githubusercontent.com/75541422/248795710-2d1d974e-e292-4b5d-acbc-ec2ae644ef3f.png">

I am facing the same error. I tried the same solution, but it did not work

Install both `faiss-cpu` and `faiss-gpu` while using GPU runtime in Colab solves the issue.

> Install both `faiss-cpu` and `faiss-gpu` while using GPU runtime in Colab solves the issue.

Unluckily it still doesn't work.
![image](https://github.com/huggingface/transformers/assets/46794180/a1a661e5-5874-4881-86c0-d8ab20350470)

Error message is still the same:
![image](https://github.com/huggingface/transformers/assets/46794180/335a170a-6438-42ba-be95-4be78cf3c8f8)



> > Install both `faiss-cpu` and `faiss-gpu` while using GPU runtime in Colab solves the issue.
> 
> Unluckily it still doesn't work. ![image](https://private-user-images.githubusercontent.com/46794180/292166379-a1a661e5-5874-4881-86c0-d8ab20350470.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTEiLCJleHAiOjE3MDMxNTQ0MDEsIm5iZiI6MTcwMzE1NDEwMSwicGF0aCI6Ii80Njc5NDE4MC8yOTIxNjYzNzktYTFhNjYxZTUtNTg3NC00ODgxLTg2YzAtZDhhYjIwMzUwNDcwLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFJV05KWUFYNENTVkVINTNBJTJGMjAyMzEyMjElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjMxMjIxVDEwMjE0MVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWNlYjFjNGZmMTFkZDMzOWY0NjJjZTQ3ZjhiNjIzMzhlOWQ4ZDJkNGQ3YjFmY2U1NmYxMzcxMjUzYWViMjQ4ZTYmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.Ra-z56bokOuZndHRNhs-UMjpC0s2Ka12ZrBnFqOrIMw)
> 
> Error message is still the same: ![image](https://private-user-images.githubusercontent.com/46794180/292166693-335a170a-6438-42ba-be95-4be78cf3c8f8.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTEiLCJleHAiOjE3MDMxNTQ0MDEsIm5iZiI6MTcwMzE1NDEwMSwicGF0aCI6Ii80Njc5NDE4MC8yOTIxNjY2OTMtMzM1YTE3MGEtNjQzOC00MmJhLWJlOTUtNGJlNzhjZjNjOGY4LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFJV05KWUFYNENTVkVINTNBJTJGMjAyMzEyMjElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjMxMjIxVDEwMjE0MVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWRhNDk2ZDY5YWE0ZTZjM2I0NTExM2UzYTA4MzU0MjliYTZhY2E5YzJmNWQwNDYwNzhiMTgwMjIyNmUzNzMyMTYmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.N7P_pNHiFITjD6pi2ZO6xJ2S8bHccXBHsGzgfyLcfqw)

Mine works fine so no idea what happened on yours lol. I'll leave my Colab notebook here: https://colab.research.google.com/drive/1xnTBsOZxG5hJJppz5ozbpdGxGx7MnDXw?usp=sharing

Mine also works after restarting runtime. There was probably something wrong with dependencies

I struggled with this for a bit. I think the key is that you need to !pip install ALL of your dependencies and then restart the session. Or it may just be that after 20 attempts something else magically worked itself out. Good luck!

!pip install datasets evaluate transformers[sentencepiece]
!pip install faiss-cpu
!pip install faiss-gpu
# need to do a Runtime -> Restart Session after this and the previous cell - ensure you are using a GPU