Fix and improve REALM fine-tuning
Author: qqaatw
Pull request: yes

# What does this PR do?

This PR 
1. adds `block_embedding_to` function to `RealmForOpenQA`, which allows users to send `block_emb` variable to a specific device. 
3. adds an input argument `block_mask` for reader that replaces previous used `token_type_ids` as the candidate mask. As a result, the reader logits at the second `[SEP]` position of the sequence will be masked out, which would prevent `[SEP]` from occurring on the predicted answer spans and matches the original TF implementation.

Their difference:
```
sentence: "[CLS] Hello How are you? [SEP] Good Job [SEP]"
token_type_ids = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1] # the last 1 is the second [SEP] token
block_mask     = [0, 0, 0, 0, 0, 0, 0, 1, 1, 0]
```

<!--
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 # (issue)


## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [ ] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/master/CONTRIBUTING.md#start-contributing-pull-requests),
      Pull Request section?
- [ ] 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.
- [ ] 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).
- [ ] 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.

@patrickvonplaten 

<!-- 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:
The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_15297). All of your documentation changes will be reflected on that endpoint.

@patrickvonplaten - The inference time of NQ evaluation dataset is `8:25` when `block_emb` is on GPU, while the inference time is `21:07` when `block_emb` is on CPU, which is roughly 3 times slower than on GPU. The test was performed on single RTX 2080Ti GPU.

Do you think we should provide an option to let users decide whether to send `block_emb` to GPU? Considering that much slower inference speed...

> evaluation

That is indeed a pretty big slow down...given such inference times I think we should have `block_emb` by default on the same device than the other parameters (so we should not overwrite the `.to(...)` method I think). I would be happy with adding a `def block_embedding_to(device)` method that lets someone easily move only `block_emb` to CPU/GPU - what do you think?

Considering most users would expect the model can be fine-tuned on single gpu with 12GiB memory as the paper states, I'm a bit worried that if we by default have `block_emb` on the same device (let's say a 2080Ti) as other parameters, when users call `block_embedding_to` to send it back to CPU, the cuda memory occupied by `block_emb` might not be able to be released as expected. 

To my knowledge, `torch.Tensor.to` will have the original tensor untouched and create a new tensor on the new device (so I think it's more like a copy rather than a transfer), and the way I know to release the memory occupied by the original tensor is delete the tensor until it is cleared by Python GC or by manually calling `torch.cuda.empty_cache()`. However, because I'm not very familiar with how PyTorch cuda context works, and I've seen in some circumstances that even though the tensor object was deleted and manually cleared, occupied spaces are still not be freed up, I don't have 100% confidence that the model can be trained on the GPU with 12GiB memory normally.

So to sum up, I think the most assured approach would be overriding `model.to` and adding a `def block_embedding_to(device)` as you suggest, this way users can choose whether they want to move `block_emb` to the GPU if they have one with big enough memory.

Does this make sense to you?

@patrickvonplaten - I noticed that the `block_records.npy` file has not been uploaded to model repos having an `openqa` suffix. Could you help with this so that people are able to fine-tune the model? Thanks.

> 

Hey @qqaatw,

Is it ok if we first move the repos to the official Google org and then add the indices? This should be a bit easier :-)



@patrickvonplaten Sure, but can I keep these checkpoints also on my namespace for testing (I can set private for them)? I will not be able to update checkpoints within Google org namespace right?

Hi @patrickvonplaten, do the above comments make sense to you? or I can just follow your guide. Sorry for the late update.