Add ONNX support for LayoutLMv3
Author: regisss
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 -->

This PR adds ONNX support for LayoutLMv3. Linked to #16308.


## 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/main/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.
- [x] Did you make sure to update the documentation with your changes? Here are the
      [documentation guidelines](https://github.com/huggingface/transformers/tree/main/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/main/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:
@NielsRogge The supported tasks are question answering, token classification and sequence classification. Is there any other use case that should be supported?

Also, the order of input arguments for the `forward` method of `LayoutLMv3ForSequenceClassification` and `LayoutLMv3ForQuestionAnswering` is different from `LayoutLMv3ForTokenClassification` and `LayoutLMv3Model`. This is taken care of in the ONNX config because I guess modifying it in `modeling_layoutlmv3.py` is not an option since it would break backwards compatibility right?

_The documentation is not available anymore as the PR was closed or merged._

@lewtun All slow tests passed

CI fails because of the following error:
```shell
Traceback (most recent call last):
  File "utils/check_repo.py", line 768, in <module>
    check_repo_quality()
  File "utils/check_repo.py", line 762, in check_repo_quality
    check_all_objects_are_documented()
  File "utils/check_repo.py", line 675, in check_all_objects_are_documented
    + "\n - ".join(undocumented_objs)
Exception: The following objects are in the public init so should be documented:
 - OptionalDependencyNotAvailable
 - dummy_scatter_objects
 - sys
```
It seems to come from the following line in `configuration_layoutlmv3.py`:
```python
from ...processing_utils import ProcessorMixin
```

Wow thanks a lot @sgugger for the clear explanation, it makes complete sense!

CI and slow tests all passed. It should be ready now @sgugger @lewtun 

Thanks!

@regisss Thank you for your great work, when convert layoutxlm  LayoutLMv2ForRelationExtraction to onnx, we are blocked by relation extraction layer for some reasons, can you try to export LayoutLMv2ForRelationExtraction model to onnx and give us for some help? gret thanks for you!

@NielsRogge Thanks for your great work, when I convert LayoutLMv2ForRelationExtraction to onnx, I can not export relation extraction layer to onnx, can you help me to solve it? because the deadline is coming for the project, I hope you can help me, Thank you very much.

> @regisss Thank you for your great work, when convert layoutxlm LayoutLMv2ForRelationExtraction to onnx, we are blocked by relation extraction layer for some reasons, can you try to export LayoutLMv2ForRelationExtraction model to onnx and give us for some help? gret thanks for you!

@githublsk Where did you find `LayoutLMv2ForRelationExtraction`? I cannot find it in Transformers

@regisss it is just in microsoft/unlim,please refer to the link:
https://github.com/microsoft/unilm/blob/master/layoutlmft/layoutlmft/models/layoutlmv2/modeling_layoutlmv2.py
![image](https://user-images.githubusercontent.com/77612906/176890500-ff03d43f-8140-44c6-aadc-4c746f4627f6.png)
it is useful for relation extraction, but when running onnx,some question occur as below:
![image](https://user-images.githubusercontent.com/77612906/176890726-94f76fa4-d928-4657-93d7-ccb5b1c80111.png)
the onnx graph is as below:
![image](https://user-images.githubusercontent.com/77612906/176890859-51f745fc-4c81-4e96-8759-e4c7fafb1f91.png)
I can not find any reason, which confused me, I meet the deadline for my project, it is so urgent....

@regisss if you have time, please help us, I am a newer to it, great thanks for you!

> @regisss if you have time, please help us, I am a newer to it, great thanks for you!

@githublsk Open an issue because it is not related to this PR. And provide the command/script you ran with the complete error message please, screenshots are not very helpful. 

@regisss Thank you for your great help, I open an issue in the link, can you help me? because the deadline is coming, it bothers me a lot, we hope you can help us to reslove it, great thanks.

https://github.com/huggingface/transformers/issues/17999

@githublsk 
how did you solve onnx convert error:
 **Exporting the operator bilinear to ONNX opset version 13 is not supported**
 super(BiaffineAttention, self).__init__()
        self.in_features = in_features
        self.out_features = out_features
        **self.bilinear = torch.nn.Bilinear(in_features, in_features, out_features, bias=False)**

@gjj123 replace torch.nn.Bilinear with this one
```
class Bilinear(nn.Module):
    def __init__(self, in1_features, in2_features, out_features):
        super(Bilinear, self).__init__()
        self.weight = torch.nn.Parameter(torch.zeros((in1_features, in2_features, out_features)))
        self.bias = torch.nn.Parameter(torch.zeros((out_features)))
        nn.init.xavier_uniform_(self.weight)

    def forward(self, x, y):
        t = x @ self.weight.permute(2,0,1)
        output = (t * y).sum(dim=2).t()
        return output
```