Segment Fault when exporting GPT-2 as ONNX format
Author: BorisPolonsky
Pull request: no

### System Info

- `transformers` version: 4.25.1
- Platform: Linux-5.15.79.1-microsoft-standard-WSL2-x86_64-with-debian-buster-sid
- Python version: 3.7.11
- Huggingface_hub version: 0.11.1
- PyTorch version (GPU?): 1.10.0 (True)
- Tensorflow version (GPU?): not installed (NA)
- Flax version (CPU?/GPU?/TPU?): not installed (NA)
- Jax version: not installed
- JaxLib version: not installed
- Using GPU in script?: <fill in>
- Using distributed or parallel set-up in script?: <fill in>

### Who can help?

_No response_

### Information

- [ ] The official example scripts
- [X] My own modified scripts

### Tasks

- [ ] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [ ] My own task or dataset (give details below)

### Reproduction

1. launch container with `pytorch/pytorch:1.10.0-cuda11.3-cudnn8-runtime`
2. install `transformers` and `onnxruntime` with `pip`
3. Execute the following script
```
# %% [markdown]
# ## Reference
# - [ONNX Tutorial by HuggingFace](https://huggingface.co/docs/transformers/serialization)
# - [Custom ONNX Config for HuggingFace Transformers](https://huggingface.co/docs/transformers/serialization#implementing-a-custom-onnx-configuration)
# 

# %%
import torch as t
from transformers import GPT2Tokenizer, GPT2Model
from transformers.models.gpt2 import GPT2Config, GPT2OnnxConfig

# %%
tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
model = GPT2Model.from_pretrained('gpt2')

# %%
model.eval()

# %%
text = "Replace me by any text you'd like."
encoded_input = tokenizer(text, return_tensors='pt')
output = model(**encoded_input)
output

# %%
from pathlib import Path
from transformers.onnx import export
from transformers import AutoTokenizer, AutoModel

onnx_path = Path("gpt2-.onnx")
onnx_config = GPT2OnnxConfig(model.config)
print(list(onnx_config.outputs.keys()))
print(f"ONNX OP Seet: {onnx_config.default_onnx_opset}")
onnx_inputs, onnx_outputs = export(tokenizer, model, onnx_config, onnx_config.default_onnx_opset, onnx_path)
```
Log:
```
['last_hidden_state']
ONNX OP Seet: 13
/opt/conda/lib/python3.7/site-packages/torch/onnx/utils.py:90: UserWarning: 'enable_onnx_checker' is deprecated and ignored. It will be removed in the next PyTorch release. To proceed despite ONNX checker failures, catch torch.onnx.ONNXCheckerError.
  warnings.warn("'enable_onnx_checker' is deprecated and ignored. It will be removed in "
/opt/conda/lib/python3.7/site-packages/torch/onnx/utils.py:103: UserWarning: `use_external_data_format' is deprecated and ignored. Will be removed in next PyTorch release. The code will work as it is False if models are not larger than 2GB, Otherwise set to False because of size limits imposed by Protocol Buffers.
  warnings.warn("`use_external_data_format' is deprecated and ignored. Will be removed in next "
/opt/conda/lib/python3.7/site-packages/transformers/models/gpt2/modeling_gpt2.py:796: 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 batch_size <= 0:
Segmentation fault
(base) root@f2f3e984f0bd:/home/polonsky/Documents/model-infernce-demo# /opt/conda/bin/python /home/polonsky/Documents/model-infernce-demo/torchscript/gpt2_onnx.py
['last_hidden_state']
ONNX OP Seet: 13
/opt/conda/lib/python3.7/site-packages/torch/onnx/utils.py:90: UserWarning: 'enable_onnx_checker' is deprecated and ignored. It will be removed in the next PyTorch release. To proceed despite ONNX checker failures, catch torch.onnx.ONNXCheckerError.
  warnings.warn("'enable_onnx_checker' is deprecated and ignored. It will be removed in "
/opt/conda/lib/python3.7/site-packages/torch/onnx/utils.py:103: UserWarning: `use_external_data_format' is deprecated and ignored. Will be removed in next PyTorch release. The code will work as it is False if models are not larger than 2GB, Otherwise set to False because of size limits imposed by Protocol Buffers.
  warnings.warn("`use_external_data_format' is deprecated and ignored. Will be removed in next "
/opt/conda/lib/python3.7/site-packages/transformers/models/gpt2/modeling_gpt2.py:796: 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 batch_size <= 0:
Segmentation fault
```

### Expected behavior

Yield model in onnx for production use

Comments:
Make sure you have the optimum library installed to have the latest version of our ONNX export, and if the bug still persists, please open an issue in that repo as they will be able to help you :-) 

> Make sure you have the optimum library installed to have the latest version of our ONNX export, and if the bug still persists, please open an issue in that repo as they will be able to help you :-)

I've update the libraries via `pip3 install -U transformers optimum onnxruntime` and executed the script above and things ended up with `Segement Fault` despite having fewer warnings than before
```
['last_hidden_state']
ONNX OP Seet: 13
/opt/conda/lib/python3.7/site-packages/torch/onnx/utils.py:90: UserWarning: 'enable_onnx_checker' is deprecated and ignored. It will be removed in the next PyTorch release. To proceed despite ONNX checker failures, catch torch.onnx.ONNXCheckerError.
  warnings.warn("'enable_onnx_checker' is deprecated and ignored. It will be removed in "
/opt/conda/lib/python3.7/site-packages/torch/onnx/utils.py:103: UserWarning: `use_external_data_format' is deprecated and ignored. Will be removed in next PyTorch release. The code will work as it is False if models are not larger than 2GB, Otherwise set to False because of size limits imposed by Protocol Buffers.
  warnings.warn("`use_external_data_format' is deprecated and ignored. Will be removed in next "
/opt/conda/lib/python3.7/site-packages/transformers/models/gpt2/modeling_gpt2.py:794: 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 batch_size <= 0:
Segmentation fault
```

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.

Any response?

Hi @BorisPolonsky , the transformers ONNX export is not maintained anymore, and you should install `optimum` to get the latest updates related to the ONNX export, see https://huggingface.co/docs/optimum/main/en/exporters/onnx/usage_guides/export_a_model

```
pip install -U optimum
optimum-cli export onnx --model gpt2 gpt2_onnx/
```

If you encounter an issue with this command, feel free to open an issue in https://github.com/huggingface/optimum/issues and I will have a second look.

Thank you!

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.

Migrating to optimum.