

TITLE
RobertaTokenizerFast is around 10 times slower than BertTokenizerFast #510

COMMENTS
6

REACTIONS
+1: 0
-1: 0
laugh: 0
hooray: 0
heart: 0
rocket: 0
eyes: 0

BODY
## 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: 3.3.1
- Platform: Linux-5.4.38-t2.el7.x86_64-x86_64-with-centos-7.7.1908-Core
- Python version: 3.7.9
- PyTorch version (GPU?): 1.4.0+cu100 (True)
- Tensorflow version (GPU?): 2.3.1 (True)
- Using GPU in script?: <fill in>
- Using distributed or parallel set-up in script?: <fill in>

### Who can help

@mfuntowicz
<!-- 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
 Translation: @sshleifer
 Summarization: @sshleifer
 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: @mariamabarham
 Bart: @sshleifer
 Marian: @sshleifer
 T5: @patrickvonplaten
 Longformer/Reformer: @patrickvonplaten
 TransfoXL/XLNet: @TevenLeScao
 RAG: @patrickvonplaten, @lhoestq
 FSTM: @stas00
 examples/seq2seq: @sshleifer
 examples/bert-loses-patience: @JetRunner
 tensorflow: @jplu
 examples/token-classification: @stefan-it
 documentation: @sgugger
 -->

## Information
Also opened this issue on the tokenizers repo: https://github.com/huggingface/tokenizers/issues/510

`RobertaTokenizerFast` with 60k vocab size is around 50 times slower  than the `BertTokenizerFast` for `transformers==3.3.1`. 

This is really slowing down the processing time for training the language model. Is there a suggested fix for this? 

Model I am using (Bert, XLNet ...): RobertaTokenizerFast

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

```python

In [37]: from transformers import BertTokenizerFast, RobertaTokenizerFast
    ...: 

In [38]: bert_tokenizer = BertTokenizerFast.from_pretrained(bert_tokenizer_dir, max_len=512, truncation=True)
    ...: roberta_tokenizer = RobertaTokenizerFast.from_pretrained(roberta_tokenizer_dir, max_len=512, truncation=True)
    ...: 

In [39]: line = "I am trying out this code for testing tokenizers and it is super good. Huge victory. But the difference in speed between BERT tokenizer and Robert
    ...: a tokenizers is quite slow."

In [40]: bert_tokenizer.encode_plus(line, add_special_tokens=True, truncation=True)
Out[40]: {'input_ids': [2, 51, 1881, 21557, 3212, 2937, 3590, 1945, 40605, 53576, 23981, 1013, 1985, 2179, 1943, 3863, 3841, 20, 38092, 65353, 20, 3180, 1931, 48800, 1822, 12679, 26777, 18732, 53576, 23981, 1985, 20839, 1016, 53576, 23981, 1013, 1943, 66009, 16390, 20, 3], 'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]}

In [41]: roberta_tokenizer.encode_plus(line, add_special_tokens=True, truncation=True)
Out[41]: {'input_ids': [0, 47, 979, 28120, 3145, 2924, 6628, 1046, 6747, 458, 618, 2063, 781, 719, 994, 1527, 891, 6074, 6464, 20, 550, 11852, 21228, 3085, 20, 16339, 898, 20420, 40977, 494, 24738, 34187, 444, 13841, 618, 2063, 27250, 994, 12086, 16449, 618, 2063, 781, 719, 891, 517, 1492, 23819, 20, 2], 'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]}

In [42]: %%timeit -n 10000
    ...: tokens = roberta_tokenizer.encode_plus(line, add_special_tokens=True, truncation=True)
    ...: 
10000 loops, best of 5: 9.09 ms per loop

In [43]: %%timeit -n 10000
    ...: tokens = bert_tokenizer.encode_plus(line, add_special_tokens=True, truncation=True)
    ...: 
10000 loops, best of 5: 181 µs per loop

In [44]: roberta_tokenizer.vocab_size
Out[44]: 60000

In [45]: bert_tokenizer.vocab_size
Out[45]: 100000


In [47]: import transformers

In [48]: import tokenizers

In [49]: transformers.__version__
Out[49]: '3.3.1'

In [50]: tokenizers.__version__
Out[50]: '0.8.1.rc2'

```

<!-- 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 to not be much difference here. Why is this the case? 

<!-- A clear and concise description of what you would expect to happen. -->
