Make Tokenizers Faster When There Are Many Additional Special Tokens
Author: gonglinyuan
Pull request: yes
Labels: name: wontfix

`PreTrainedTokenizer.unique_no_split_tokens` used to be a list that contains all special tokens.  During tokenization, the tokenizer will repeatedly check `if sub_text not in self.unique_no_split_tokens` or `if token not in self.unique_no_split_tokens`. List lookups will significantly slow down tokenization if the list is large, i.e., there are many additional special tokens added to `unique_no_split_tokens`. To resolve this issue, this commit will change `PreTrainedTokenizer.unique_no_split_tokens` to be an ordered dict (actually an ordered set, since all values are `None`), such that lookups can be done very efficiently while still keeping its original ordering.

Comments:
# [Codecov](https://codecov.io/gh/huggingface/transformers/pull/5909?src=pr&el=h1) Report
> Merging [#5909](https://codecov.io/gh/huggingface/transformers/pull/5909?src=pr&el=desc) into [master](https://codecov.io/gh/huggingface/transformers/commit/82dd96cae74797be0c1d330566df7f929214b278&el=desc) will **decrease** coverage by `0.13%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/huggingface/transformers/pull/5909/graphs/tree.svg?width=650&height=150&src=pr&token=9qOlN6Hb1c)](https://codecov.io/gh/huggingface/transformers/pull/5909?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master    #5909      +/-   ##
==========================================
- Coverage   78.49%   78.35%   -0.14%     
==========================================
  Files         146      146              
  Lines       26210    26211       +1     
==========================================
- Hits        20573    20538      -35     
- Misses       5637     5673      +36     
```


| [Impacted Files](https://codecov.io/gh/huggingface/transformers/pull/5909?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/transformers/tokenization\_utils.py](https://codecov.io/gh/huggingface/transformers/pull/5909/diff?src=pr&el=tree#diff-c3JjL3RyYW5zZm9ybWVycy90b2tlbml6YXRpb25fdXRpbHMucHk=) | `90.00% <100.00%> (+0.04%)` | :arrow_up: |
| [src/transformers/modeling\_tf\_openai.py](https://codecov.io/gh/huggingface/transformers/pull/5909/diff?src=pr&el=tree#diff-c3JjL3RyYW5zZm9ybWVycy9tb2RlbGluZ190Zl9vcGVuYWkucHk=) | `20.27% <0.00%> (-74.92%)` | :arrow_down: |
| [src/transformers/modeling\_openai.py](https://codecov.io/gh/huggingface/transformers/pull/5909/diff?src=pr&el=tree#diff-c3JjL3RyYW5zZm9ybWVycy9tb2RlbGluZ19vcGVuYWkucHk=) | `81.02% <0.00%> (-1.29%)` | :arrow_down: |
| [src/transformers/generation\_tf\_utils.py](https://codecov.io/gh/huggingface/transformers/pull/5909/diff?src=pr&el=tree#diff-c3JjL3RyYW5zZm9ybWVycy9nZW5lcmF0aW9uX3RmX3V0aWxzLnB5) | `85.96% <0.00%> (-0.26%)` | :arrow_down: |
| [src/transformers/modeling\_tf\_electra.py](https://codecov.io/gh/huggingface/transformers/pull/5909/diff?src=pr&el=tree#diff-c3JjL3RyYW5zZm9ybWVycy9tb2RlbGluZ190Zl9lbGVjdHJhLnB5) | `95.53% <0.00%> (+69.51%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/huggingface/transformers/pull/5909?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/huggingface/transformers/pull/5909?src=pr&el=footer). Last update [82dd96c...c90f92b](https://codecov.io/gh/huggingface/transformers/pull/5909?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.