Add ONNX support for MarianMT models
Author: lewtun
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 support to export MarianMT models in the ONNX format. The underlying logic builds on the awesome refactor / feature enhancement that @michaelbenayoun has implemented in #14358 & #14700 - ~we should rebase this branch on `master` once that PR is merged to simplify the diff in this PR.~ (Done)

Currently, this PR supports ONNX exports for the following "tasks" (i.e. uses):

* `default`, `default-with-past` => equivalent to exporting a pretrained `MarianModel`
* `seq2seq-lm`, `seq2seq-lm-with-past` => equivalent to exporting a pretrained `MarianMTModel`
* `causal-lm`, `causal-lm-with-past`=> equivalent to exporting a pretrained `MarianForCausalLM`

Note that in each case, the end user will have to implement their own `generate()` method with the ONNX model - see [this BART example](https://github.com/huggingface/transformers/tree/master/examples/onnx/pytorch/summarization) for what's involved.

I've also checked locally that the "slow" tests pass with:

```
RUN_SLOW=1 pytest tests/test_onnx_v2.py -k "marian" -rp
```

## Usage

Here's a quick example to show how this works:

```python
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
from transformers.models.marian import MarianOnnxConfig

model_ckpt = "Helsinki-NLP/opus-mt-en-de"
tokenizer = AutoTokenizer.from_pretrained(model_ckpt)
ref_model = AutoModelForSeq2SeqLM.from_pretrained(model_ckpt)
# Export model
feature = "seq2seq-lm"
onnx_path = f"onnx/{model_ckpt}-{feature}/"
# Run this from a Jupyter notebook
!python -m transformers.onnx --model={model_ckpt} --atol=1e-4 --feature={feature} {onnx_path}
# Test export with inputs
batch_size = 4
encoder_inputs = tokenizer(
    ["Studies have been shown that owning a dog is good for you"] * batch_size,
    return_tensors="np",
)
decoder_inputs = tokenizer(
    ["Studien haben gezeigt dass es hilfreich ist einen Hund zu besitzen"]
    * batch_size,
    return_tensors="np",
)
all_inputs = {
    "input_ids": encoder_inputs["input_ids"],
    "attention_mask": encoder_inputs["attention_mask"],
    "decoder_input_ids": decoder_inputs["input_ids"],
    "decoder_attention_mask": decoder_inputs["attention_mask"],
}
# Generate ONNX outputs
ort_session = ort.InferenceSession(f"{onnx_path}model.onnx")
onnx_config = MarianOnnxConfig(ref_model.config, task=feature)
onnx_named_outputs = list(onnx_config.outputs.keys())
onnx_outputs = ort_session.run(onnx_named_outputs, all_inputs)
```

**TODO**

- [x] Extend support for language modelling head
- [x] Investigate range of numerical tolerance between raw and ONNX models for a range of checkpoints
- [x] Ensure that ONNX models are compatible with ONNX Runtime
- [x] Verify whether past key values are supported

Closes #13823, #13854 




## 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.

<!-- 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:
There seems to be some sort of race condition happening in `run_tests_torch`:

```
_____________________________ ERROR collecting gw1 _____________________________
Different tests were collected between gw0 and gw1. The difference is:
--- gw0

+++ gw1
```

[This issue](https://github.com/pytest-dev/pytest-xdist/issues/366) has similar problems - perhaps a solution lies there.

Feel free to merge once you have taken care of the docs and the `# Copied from` statements :)

Thanks for the reviews @LysandreJik and @michaelbenayoun 🙏 !

I've fixed the docs by rebasing on `master` and added the `# Copied from:` snippets to the functions (I did not know about that trick!)

Will merge once all the test pass :)

The outputs decoding cannot get the correct result. How do you get the translation result