Converting ruGPT3 model (based on GPT2) to ONNX format
Author: Gooogr
Pull request: no
Labels: name: bug

### System Info

## Environment info
Use google colab with turned on GPU
- `transformers` version: 4.21.3
- Platform: Linux-5.4.188+-x86_64-with-Ubuntu-18.04-bionic
- Python version: 3.7.13
- Huggingface_hub version: 0.9.1
- PyTorch version (GPU?): 1.12.1+cu113 (True)
- Tensorflow version (GPU?): 2.8.2 (True)
- Flax version (CPU?/GPU?/TPU?): not installed (NA)
- Jax version: not installed
- JaxLib version: not installed
- Using GPU in script?: no (at least not call it directly)
- Using distributed or parallel set-up in script?: no


### Who can help?

@LysandreJik
@lewtun 

### Information

- [ ] The official example scripts
- [X] My own modified scripts

### Tasks

- [X] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [ ] My own task or dataset (give details below)

### Reproduction

I am trying  to export ruGPT3 model to ONNX format in google colab notebook
Code based on:
https://huggingface.co/docs/transformers/main/en/serialization#exporting-a-model-to-onnx
```
! pip install transformers[onnx] datasets sentencepiece >> pip_log.txt

from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, GPT2LMHeadModel
from onnxruntime import InferenceSession

# Load model and tokenizer
name = 'sberbank-ai/rugpt3medium_based_on_gpt2'
tokenizer  = AutoTokenizer.from_pretrained(name)
tokenizer.pad_token = tokenizer.eos_token
tokenizer.sep_token = tokenizer.eos_token
model = GPT2LMHeadModel.from_pretrained(name)

# Save to disk
tokenizer.save_pretrained("local-pt-checkpoint")
model.save_pretrained("local-pt-checkpoint")

# Run ONNX converter
! python -m transformers.onnx --model=local-pt-checkpoint  onnx/ --atol=2e-5 
```

I'm getting error

> Some weights of the model checkpoint at local-pt-checkpoint were not used when initializing GPT2Model: ['lm_head.weight']
> - This IS expected if you are initializing GPT2Model 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 GPT2Model 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.12.1+cu113
> Overriding 1 configuration item(s)
> 	- use_cache -> False
> /usr/local/lib/python3.7/dist-packages/transformers/models/gpt2/modeling_gpt2.py:808: 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:
> Traceback (most recent call last):
>   File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
>     "__main__", mod_spec)
>   File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
>     exec(code, run_globals)
>   File "/usr/local/lib/python3.7/dist-packages/transformers/onnx/__main__.py", line 107, in <module>
>     main()
>   File "/usr/local/lib/python3.7/dist-packages/transformers/onnx/__main__.py", line 94, in main
>     args.output,
>   File "/usr/local/lib/python3.7/dist-packages/transformers/onnx/convert.py", line 336, in export
>     return export_pytorch(preprocessor, model, config, opset, output, tokenizer=tokenizer, device=device)
>   File "/usr/local/lib/python3.7/dist-packages/transformers/onnx/convert.py", line 199, in export_pytorch
>     opset_version=opset,
>   File "/usr/local/lib/python3.7/dist-packages/torch/onnx/__init__.py", line 365, in export
>     export_modules_as_functions,
>   File "/usr/local/lib/python3.7/dist-packages/torch/onnx/utils.py", line 178, in export
>     export_modules_as_functions=export_modules_as_functions,
>   File "/usr/local/lib/python3.7/dist-packages/torch/onnx/utils.py", line 1084, in _export
>     dynamic_axes=dynamic_axes,
>   File "/usr/local/lib/python3.7/dist-packages/torch/onnx/utils.py", line 727, in _model_to_graph
>     graph, params, torch_out, module = _create_jit_graph(model, args)
>   File "/usr/local/lib/python3.7/dist-packages/torch/onnx/utils.py", line 602, in _create_jit_graph
>     graph, torch_out = _trace_and_get_graph_from_model(model, args)
>   File "/usr/local/lib/python3.7/dist-packages/torch/onnx/utils.py", line 518, in _trace_and_get_graph_from_model
>     model, args, strict=False, _force_outplace=False, _return_inputs_states=True
>   File "/usr/local/lib/python3.7/dist-packages/torch/jit/_trace.py", line 1175, in _get_trace_graph
>     outs = ONNXTracedModule(f, strict, _force_outplace, return_inputs, _return_inputs_states)(*args, **kwargs)
>   File "/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py", line 1130, in _call_impl
>     return forward_call(*input, **kwargs)
>   File "/usr/local/lib/python3.7/dist-packages/torch/jit/_trace.py", line 132, in forward
>     self._force_outplace,
>   File "/usr/local/lib/python3.7/dist-packages/torch/jit/_trace.py", line 118, in wrapper
>     outs.append(self.inner(*trace_inputs))
>   File "/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py", line 1130, in _call_impl
>     return forward_call(*input, **kwargs)
>   File "/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py", line 1118, in _slow_forward
>     result = self.forward(*input, **kwargs)
>   File "/usr/local/lib/python3.7/dist-packages/transformers/models/gpt2/modeling_gpt2.py", line 844, in forward
>     inputs_embeds = self.wte(input_ids)
>   File "/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py", line 1130, in _call_impl
>     return forward_call(*input, **kwargs)
>   File "/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py", line 1118, in _slow_forward
>     result = self.forward(*input, **kwargs)
>   File "/usr/local/lib/python3.7/dist-packages/torch/nn/modules/sparse.py", line 160, in forward
>     self.norm_type, self.scale_grad_by_freq, self.sparse)
>   File "/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py", line 2199, in embedding
>     return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
> IndexError: index out of range in self




### Expected behavior

Link to the model
[ruGPT3 medium based on GPT2](https://huggingface.co/sberbank-ai/rugpt3medium_based_on_gpt2)

Given that the model architecture is based on GPT2, I assumed that the script will be able to convert it automatically.
Unfortunately, the error logs are not too detailed and I don't know if it's even possible to solve this problem at the HuggingFace level. Can I convert it by setting custom ONNX configuration? Or probably there is another workaround?

Comments:
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.

Hello, I meet the same situation, have you solved it? @Gooogr