Add ONNX support for Pegasus
Author: pramodith
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.
-->

This PR adds ONNX support for the Pegasus model. Linked to https://github.com/huggingface/transformers/issues/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?
- [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/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.
@lewtun 

<!-- 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 documentation is not available anymore as the PR was closed or merged._

Hey @pramodith, this PR looks excellent! Did you try to convert a `Pegasus` model with this config?

I'm pinging @lewtun for reviewing.

@ChainYo  besides the pytests, I also tried executing the Onnx export and validation using this script. The difference between the pytorch tensors and the Onnx tensors was in the order of 1e-5. However, I did notice that the onnx export would fail when I set use_past=True, not sure if I need to do anything about that.

```from transformers import AutoConfig, AutoTokenizer, PegasusForCausalLM,PegasusForConditionalGeneration,PegasusModel
from transformers.models.pegasus import PegasusOnnxConfig
from transformers.onnx import export, validate_model_outputs
from pathlib import Path

def check_onnx_model(task):
    if task == "default":
        config = AutoConfig.from_pretrained("google/pegasus-large")
        model = PegasusModel.from_pretrained("google/pegasus-large")
        tokenizer = AutoTokenizer.from_pretrained("google/pegasus-large")
    elif task == "seq2seq-lm":
        config = AutoConfig.from_pretrained("google/pegasus-xsum")
        model = PegasusForConditionalGeneration.from_pretrained("google/pegasus-xsum", add_cross_attention=True)
        tokenizer = AutoTokenizer.from_pretrained("google/pegasus-xsum")
    else:
        config = AutoConfig.from_pretrained("google/pegasus-xsum")
        model = PegasusForCausalLM.from_pretrained("google/pegasus-xsum")
        tokenizer = AutoTokenizer.from_pretrained("google/pegasus-xsum")

    onnx_config = PegasusOnnxConfig(config, task=task, use_past=False)
    onnx_path = Path("model.onnx")
    onnx_inputs, onnx_outputs = export(tokenizer, model, onnx_config, onnx_config.default_onnx_opset, onnx_path)
    print(onnx_inputs)
    print(onnx_outputs)
    print(validate_model_outputs(onnx_config,tokenizer,model,onnx_path,onnx_outputs,onnx_config.atol_for_validation))

check_onnx_model("causal-lm")
```

> @ChainYo, besides the pytests, I also tried executing the Onnx export and validation using this script. The difference between the PyTorch tensors and the Onnx tensors was in the order of 1e-5. However, I did notice that the onnx export would fail when I set use_past=True, not sure if I need to do anything about that.

I'm not sure you can use past when you convert your model to ONNX. You have to rewrite the logic when using that feature with ONNX.

**Edit**: I never converted a model which uses `past`, but it could work if your config inherits from `OnnxSeq2SeqConfigWithPast`, which is the case.

> > @ChainYo, besides the pytests, I also tried executing the Onnx export and validation using this script. The difference between the PyTorch tensors and the Onnx tensors was in the order of 1e-5. However, I did notice that the onnx export would fail when I set use_past=True, not sure if I need to do anything about that.
> 
> I'm not sure you can use past when you convert your model to ONNX. You have to rewrite the logic when using that feature with ONNX.
> 
> **Edit**: I never converted a model which uses `past`, but it could work if your config inherits from `OnnxSeq2SeqConfigWithPast`, which is the case.

Gotcha! I'll wait for any comments by the reviewers in that case.

Hi, I just checked the failed test in the CI/CD pipeline, and it doesn't seem to come from your code. Could you please try to fetch/rebase your branch with the main upstream branch? We will see if it's solved.

[CI/CD error](https://app.circleci.com/pipelines/github/huggingface/transformers/44492/workflows/4da69f9f-f332-4ba8-97e7-3446ec6a559e/jobs/517244?invite=true#step-111-3911) 

`FAILED tests/models/bart/test_modeling_tf_bart.py::TFBartModelTest::test_decoder_model_past_large_inputs`

@ChainYo I've rebased and all the tests pass now!

> @ChainYo I've rebased, and all the tests pass now!

Super let's just wait for a reviewer now!