export BART model to ONNX failed with [Segmentation fault (core dumped)]
Author: PanQiWei
Pull request: no

## Environment info
<!-- You can run the command `transformers-cli env` and copy-and-paste its output below.
     Don't forget to fill out the missing fields in that output! -->

- `transformers` version:`v4.10.0-dev0`
- Platform:Ubuntu 18.04.3 LTS
- `Python` version:`v3.8.11`
- `PyTorch` version (GPU?):`v1.9.0-cu102`(TRUE)
- `Tensorflow` version (GPU?):`None`
- `onnx` version:`v1.10.1`
- `onnxruntim` version:`v1.8.1`
- Using GPU in script?:`False`
- Using distributed or parallel set-up in script?:`False`

### Who can help
@patrickvonplaten @patil-suraj
<!-- Your issue 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: @Rocketknight1

Library:

- benchmarks: @patrickvonplaten
- deepspeed: @stas00
- ray/raytune: @richardliaw, @amogkam
- text generation: @patrickvonplaten
- tokenizers: @LysandreJik
- trainer: @sgugger
- pipelines: @LysandreJik

Documentation: @sgugger

Model hub:

- for issues with a model report at https://discuss.huggingface.co/ and tag the model's creator.

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

 -->

## Information

Model I am using **BART**:

The problem arises when using:
* [ ] the official example scripts: (give details below)
* [x] my own modified scripts following the **command line example** given in the official [Export transformers models](https://huggingface.co/transformers/serialization.html#onnx-onnxruntime) document.

## To reproduce

Steps to reproduce the behavior:

1.run the following command line in console:
```
python -m transformers.onnx --model="lidiya/bart-large-xsum-samsum" --feature=default "lidiya-bart-large-xsum-samsum"
```

<details>
<summary>Full log</summary>
<pre>
Some weights of the model checkpoint at lidiya/bart-large-xsum-samsum were not used when initializing BartModel: ['lm_head.weight', 'final_logits_bias']
- This IS expected if you are initializing BartModel from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).
- This IS NOT expected if you are initializing BartModel from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).
Using framework PyTorch: 1.9.0+cu102
Overriding 1 configuration item(s)
        - use_cache -> False
/root/miniconda3/envs/speedup/lib/python3.8/site-packages/transformers/models/bart/modeling_bart.py:212: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  if attn_weights.size() != (bsz * self.num_heads, tgt_len, src_len):
/root/miniconda3/envs/speedup/lib/python3.8/site-packages/transformers/models/bart/modeling_bart.py:218: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  if attention_mask.size() != (bsz, 1, tgt_len, src_len):
/root/miniconda3/envs/speedup/lib/python3.8/site-packages/transformers/models/bart/modeling_bart.py:249: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  if attn_output.size() != (bsz * self.num_heads, tgt_len, self.head_dim):
/root/miniconda3/envs/speedup/lib/python3.8/site-packages/transformers/models/bart/modeling_bart.py:863: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  if input_shape[-1] > 1:
Validating ONNX model...
Traceback (most recent call last):
Segmentation fault (core dumped)
</pre>
</details>
<!-- If you have code snippets, error messages, stack traces please provide them here as well.
     Important! Use code tags to correctly format your code. See https://help.github.com/en/github/writing-on-github/creating-and-highlighting-code-blocks#syntax-highlighting
     Do not use screenshots, as they are hard to read and (more importantly) don't allow others to copy-and-paste your code.-->

## Expected behavior
Exporting **BART** model to onnx successfully and can be run on onnxruntime to generate correct results.

<!-- A clear and concise description of what you would expect to happen. -->


Comments:
Hello @PanQiWei!

I have successfully exported the model with your command:

```
Validating ONNX model...
        -[✓] ONNX model outputs' name match reference model ({'last_hidden_state', 'encoder_last_hidden_state'}
        - Validating ONNX Model output "last_hidden_state":
                -[✓] (2, 8, 1024) matches (2, 8, 1024)
                -[✓] all values close (atol: 0.0001)
        - Validating ONNX Model output "encoder_last_hidden_state":
                -[✓] (2, 8, 1024) matches (2, 8, 1024)
                -[✓] all values close (atol: 0.0001)
All good, model saved at: lidiya-bart-large-xsum-samsum/model.onnx
```

Would you mind mentioning the versions of PyTorch and onnxruntime you have installed in your environment? Thank you!

Hi @LysandreJik !

First of all, thank you for your replay!

I'm currently using ```pytorch==1.9.2``` with cuda version 10.2 and ```onnxruntime==1.8.1```

For 🤗transformers I tried both ```v1.9.2``` and ```the unrealesed version by installing from source```.

I tried saveral times to export Bart model into ONNX but all got the failed information as given above.

A segmentation fault isn't easy to debug, but I wonder if this isn't a memory error under the hood. Are you using a google colab?

Can you try exporting the following model, which is much smaller, to see if this succeeds or not? `sshleifer/distilbart-cnn-12-6`

I'm using GPU provided by my company, which is a RTX2080 GPU.

I ran the command and replace model name with ```sshleifer/distilbart-cnn-12-6```, this time the error message changed, as shown below:

```
Using framework PyTorch: 1.9.0+cu102
Overriding 1 configuration item(s)
        - use_cache -> False
/root/miniconda3/envs/gpc/lib/python3.8/site-packages/transformers/models/bart/modeling_bart.py:212: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  if attn_weights.size() != (bsz * self.num_heads, tgt_len, src_len):
/root/miniconda3/envs/gpc/lib/python3.8/site-packages/transformers/models/bart/modeling_bart.py:218: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  if attention_mask.size() != (bsz, 1, tgt_len, src_len):
/root/miniconda3/envs/gpc/lib/python3.8/site-packages/transformers/models/bart/modeling_bart.py:249: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  if attn_output.size() != (bsz * self.num_heads, tgt_len, self.head_dim):
/root/miniconda3/envs/gpc/lib/python3.8/site-packages/transformers/models/bart/modeling_bart.py:863: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  if input_shape[-1] > 1:
Validating ONNX model...
Floating point exception (core dumped)
```

Hmmm I'm failing at reproducing :( I have the following versions, could you try installing them to see if it changes something?
```
onnx                          1.9.0
onnxruntime                   1.8.1
torch                         1.9.0
```

I can also upload the converted model to the hub under a repository if that's helpful for you.

I re-install the libraries with the versions as yours, but still faild. 😢 

It would be wonderful and thankful if you could upload the converted model! ❤️ 

Again, thank you for your help! 😄

Hello again! I've uploaded the converted model here: https://huggingface.co/lysandre/onnx-bart/tree/main (search for model.onnx)

Thanks soooo much!! 😆 It's my first time to try an ONNX model, can't wait to see the improvement in my tasks, thank you! ❤️