Add `LayoutXLMTokenizer` and `LayoutXLMTokenizerFast`
Author: kingyiusuen
Pull request: yes

# What does this PR do?

<!--
Congratulations! You've made it this far! You're not quite done yet though.

Once merged, your PR is going to appear in the release notes with the title you set, so make sure it's a great title that fully reflects the extent of your awesome contribution.

Then, please replace this with a description of the change and which issue is fixed (if applicable). Please also include relevant motivation and context. List any dependencies (if any) that are required for this change.

Once you're done, someone will review your PR shortly (see the section "Who can review?" below to tag some potential reviewers). They may suggest changes to make the code even better. If no one reviewed your PR after a week has passed, don't hesitate to post a new comment @-mentioning the same persons---sometimes notifications get lost.
-->

<!-- Remove if not applicable -->

Fixes #13972 (issue)


## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [X] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/master/CONTRIBUTING.md#start-contributing-pull-requests),
      Pull Request section?
- [X] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? Please add a link
      to it if that's the case.
- [X] Did you make sure to update the documentation with your changes? Here are the
      [documentation guidelines](https://github.com/huggingface/transformers/tree/master/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/master/docs#writing-source-documentation).
- [X] Did you write any new necessary tests?


## Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

<!-- Your PR 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.

Models:

- albert, bert, xlm: @LysandreJik
- blenderbot, bart, marian, pegasus, encoderdecoder,  t5: @patrickvonplaten, @patil-suraj
- longformer, reformer, transfoxl, xlnet: @patrickvonplaten
- fsmt: @stas00
- funnel: @sgugger
- gpt2: @patrickvonplaten, @LysandreJik
- rag: @patrickvonplaten, @lhoestq
- tensorflow: @LysandreJik

Library:

- benchmarks: @patrickvonplaten
- deepspeed: @stas00
- ray/raytune: @richardliaw, @amogkam
- text generation: @patrickvonplaten
- tokenizers: @n1t0, @LysandreJik
- trainer: @sgugger
- pipelines: @LysandreJik

Documentation: @sgugger

HF projects:

- datasets: [different repo](https://github.com/huggingface/datasets)
- rust tokenizers: [different repo](https://github.com/huggingface/tokenizers)

Examples:

- maintained examples (not research project or legacy): @sgugger, @patil-suraj
- research_projects/bert-loses-patience: @JetRunner
- research_projects/distillation: @VictorSanh

 -->


Comments:
Nice! Let me know if you need any help.

> Nice! Let me know if you need any help.

Thanks!

The tokenizer in `'microsoft/layoutxlm-base'` was registered with `XLMRobertaTokenizer`, so the user will get a  warning that there is a mismatch when they do

```
tokenizer = LayoutXLMTokenizer.from_pretrained('microsoft/layoutxlm-base') 
```

Is there a way to avoid this?

Also, I keep getting a `ModuleNotFoundError: No module named 'sentencepiece'` error in `tests/test_processor_layoutlmv2.py`. I can't figure out what is wrong.



> The tokenizer in 'microsoft/layoutxlm-base' was registered with XLMRobertaTokenizer, so the user will get a warning

Yeah, that's because the config currently has an attribute `tokenizer_class` which is set to `XLMRobertaTokenizer` as can be seen [here](https://huggingface.co/microsoft/layoutxlm-base/blob/main/config.json). Once `LayoutXLMTokenizer`/`LayoutXLMTokenizerFast` are ready, we will upload the vocab files to the model repo, and remove the `tokenizer_class` attribute.

> > The tokenizer in 'microsoft/layoutxlm-base' was registered with XLMRobertaTokenizer, so the user will get a warning
> 
> Yeah, that's because the config currently has an attribute `tokenizer_class` which is set to `XLMRobertaTokenizer` as can be seen [here](https://huggingface.co/microsoft/layoutxlm-base/blob/main/config.json). Once `LayoutXLMTokenizer`/`LayoutXLMTokenizerFast` are ready, we will upload the vocab files to the model repo, and remove the `tokenizer_class` attribute.

Got it. I just have a failing test and a docstring error in the CI. Any advice on how to fix them?

Ok, I'll take a look later today.

@kingyiusuen I've fixed the docs issue, currently checking out the tests. One is failing, the boxes created between the slow and fast tokenizer aren't equal:

```
from transformers import LayoutXLMTokenizer, LayoutXLMTokenizerFast

tokenizer_p = LayoutXLMTokenizer.from_pretrained("microsoft/layoutxlm-base")
tokenizer_r = LayoutXLMTokenizerFast.from_pretrained("microsoft/layoutxlm-base")
question = "what's his name?"
words = ["a", "weirdly", "test"]
boxes = [[423, 237, 440, 251], [427, 272, 441, 287], [419, 115, 437, 129]]

encoding_p = tokenizer_p(question, words, boxes, padding="max_length", max_length=20)
encoding_r = tokenizer_r(question, words, boxes, padding="max_length", max_length=20)
for x,y in zip(encoding_p.bbox, encoding_r.bbox): print(x,y)

# this prints:
[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, 0, 0, 0] [0, 0, 0, 0]
[0, 0, 0, 0] [0, 0, 0, 0]
[1000, 1000, 1000, 1000] [1000, 1000, 1000, 1000]
[423, 237, 440, 251] [1000, 1000, 1000, 1000]
[427, 272, 441, 287] [423, 237, 440, 251]
[427, 272, 441, 287] [427, 272, 441, 287]
[419, 115, 437, 129] [427, 272, 441, 287]
[1000, 1000, 1000, 1000] [419, 115, 437, 129]
[0, 0, 0, 0] [1000, 1000, 1000, 1000]
[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]
```

The `input_ids` and `attention_mask` are equal. Decoding the `input_ids`, it seems that one adds 2 special 
 tokens in between  the question and the context? So the fast tokenizer seems correct.

UPDATE: fixed it in the slow tokenizer.

It might make sense to create a separate LayoutXLMProcessor. Will do this.

I've made all necessary changes, fixed all tests, implemented a new `LayoutXLMProcessor` and created new tests for it accordingly.

You can find my branch here: https://github.com/NielsRogge/transformers/tree/add-layoutxlm-fast-tokenizer

Should I open a PR on your branch? Or should I directly open a PR to HuggingFace Transformers?

> I've made all necessary changes, fixed all tests, implemented a new `LayoutXLMProcessor` and created new tests for it accordingly.
> 
> You can find my branch here: https://github.com/NielsRogge/transformers/tree/add-layoutxlm-fast-tokenizer
> 
> Should I open a PR on your branch? Or should I directly open a PR to HuggingFace Transformers?

Maybe you should directly open a PR to HuggingFace Transformers. You've done most of the heavy-lifting. This should be counted as your contribution. 😃