Layoutlm onnx support (Issue #13300)
Author: nishprabhu
Pull request: yes

# What does this PR do?

This PR extends ONNX support to LayoutLM as explained in https://huggingface.co/transformers/serialization.html?highlight=onnx#converting-an-onnx-model-using-the-transformers-onnx-package

Fixes Issue #13300


## 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.
- [ ] 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.

@NielsRogge @mfuntowicz @LysandreJik

Comments:
Looks almost ready, great work @nishprabhu!!
Thank you for your contribution.

Hi, any update about this? Any other changes required?
@LysandreJik @mfuntowicz @NielsRogge

LGTM! 

This is making the LayoutLM ONNX test fail for the following reason:

```
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <transformers.models.layoutlm.configuration_layoutlm.LayoutLMOnnxConfig object at 0x7f344811ceb0>
tokenizer = PreTrainedTokenizerFast(name_or_path='microsoft/layoutlm-base-uncased', vocab_size=30522, model_max_len=512, is_fast=T...okens={'unk_token': '[UNK]', 'sep_token': '[SEP]', 'pad_token': '[PAD]', 'cls_token': '[CLS]', 'mask_token': '[MASK]'})
batch_size = -1, seq_length = -1, is_pair = False
framework = <TensorType.PYTORCH: 'pt'>

    def generate_dummy_inputs(
        self,
        tokenizer: PreTrainedTokenizer,
        batch_size: int = -1,
        seq_length: int = -1,
        is_pair: bool = False,
        framework: Optional[TensorType] = None,
    ) -> Mapping[str, Any]:
        """
        Generate inputs to provide to the ONNX exporter for the specific framework
    
        Args:
            tokenizer: The tokenizer associated with this model configuration
            batch_size: The batch size (int) to export the model for (-1 means dynamic axis)
            seq_length: The sequence length (int) to export the model for (-1 means dynamic axis)
            is_pair: Indicate if the input is a pair (sentence 1, sentence 2)
            framework: The framework (optional) the tokenizer will generate tensor for
    
        Returns:
            Mapping[str, Tensor] holding the kwargs to provide to the model's forward function
        """
    
        input_dict = super().generate_dummy_inputs(tokenizer, batch_size, seq_length, is_pair, framework)
    
        # Generate a dummy bbox
        box = [48, 84, 73, 128]
    
        if not framework == TensorType.PYTORCH:
            raise NotImplementedError("Exporting LayoutLM to ONNX is currently only supported for PyTorch.")
    
        if not is_torch_available():
            raise ValueError("Cannot generate dummy inputs without PyTorch installed.")
        import torch
    
>       input_dict["bbox"] = torch.tensor(
            [
                [0] * 4,
                *[box] * seq_length,
                [self.max_2d_positions] * 4,
            ]
        ).tile(batch_size, 1, 1)
E       RuntimeError: Trying to create tensor with negative dimension -1: [-1, 2, 4]

```

Could you take a look @nishprabhu @michaelbenayoun?

Will skip this test in the meantime.

I think we have to import the compute_effective_axis_dimension function from the onnx module and compute the batch_size and seq_length dimensions in the configuration_layoutlm.py file. We are currently using -1 as the value for both dimensions which is causing the error. @michaelbenayoun @LysandreJik