Transformers onnx export error
Author: ZHANG-GuiGui
Pull request: no
Labels: name: External

name: Should Fix

## 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:
- Platform: Linux Ubuntu20.04
- Python version: 3.8
- PyTorch version (GPU?): Pytorch1.7.1 Cuda11.0
- Tensorflow version (GPU?): None
- Using GPU in script?: No
- Using distributed or parallel set-up in script?: No

### Who can help
<!-- 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

Library:

- benchmarks: @patrickvonplaten 
- pipelines: @LysandreJik

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)

 -->

## Information

I tried to export pytorch model with onnx following the tutorials here[https://huggingface.co/transformers/serialization.html]

## To reproduce

Steps to reproduce the behavior:

1.run python -m transformers.onnx --model=bert-base-cased onnx/bert-base-cased/
```
$ python -m transformers.onnx --model=bert-base-cased onnx/bert-base-cased/
Some weights of the model checkpoint at bert-base-cased were not used when initializing BertModel: ['cls.predictions.transform.dense.weight', 'cls.seq_relationship.bias', 'cls.seq_relationship.weight', 'cls.predictions.decoder.weight', 'cls.predictions.transform.LayerNorm.weight', 'cls.predictions.transform.LayerNorm.bias', 'cls.predictions.transform.dense.bias', 'cls.predictions.bias']
- This IS expected if you are initializing BertModel 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 BertModel 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.7.1
Overriding 1 configuration item(s)
	- use_cache -> False
Traceback (most recent call last):
  File "/home/ubuntu/anaconda3/envs/trans4.9/lib/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/home/ubuntu/anaconda3/envs/trans4.9/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/ubuntu/anaconda3/envs/trans4.9/lib/python3.8/site-packages/transformers/onnx/__main__.py", line 150, in <module>
    main()
  File "/home/ubuntu/anaconda3/envs/trans4.9/lib/python3.8/site-packages/transformers/onnx/__main__.py", line 141, in main
    onnx_inputs, onnx_outputs = export(tokenizer, model, onnx_config, args.opset, args.output)
  File "/home/ubuntu/anaconda3/envs/trans4.9/lib/python3.8/site-packages/transformers/onnx/convert.py", line 109, in export
    export(
  File "/home/ubuntu/anaconda3/envs/trans4.9/lib/python3.8/site-packages/torch/onnx/__init__.py", line 225, in export
    return utils.export(model, args, f, export_params, verbose, training,
  File "/home/ubuntu/anaconda3/envs/trans4.9/lib/python3.8/site-packages/torch/onnx/utils.py", line 85, in export
    _export(model, args, f, export_params, verbose, training, input_names, output_names,
  File "/home/ubuntu/anaconda3/envs/trans4.9/lib/python3.8/site-packages/torch/onnx/utils.py", line 632, in _export
    _model_to_graph(model, args, verbose, input_names,
  File "/home/ubuntu/anaconda3/envs/trans4.9/lib/python3.8/site-packages/torch/onnx/utils.py", line 409, in _model_to_graph
    graph, params, torch_out = _create_jit_graph(model, args,
  File "/home/ubuntu/anaconda3/envs/trans4.9/lib/python3.8/site-packages/torch/onnx/utils.py", line 379, in _create_jit_graph
    graph, torch_out = _trace_and_get_graph_from_model(model, args)
  File "/home/ubuntu/anaconda3/envs/trans4.9/lib/python3.8/site-packages/torch/onnx/utils.py", line 342, in _trace_and_get_graph_from_model
    torch.jit._get_trace_graph(model, args, strict=False, _force_outplace=False, _return_inputs_states=True)
  File "/home/ubuntu/anaconda3/envs/trans4.9/lib/python3.8/site-packages/torch/jit/_trace.py", line 1148, in _get_trace_graph
    outs = ONNXTracedModule(f, strict, _force_outplace, return_inputs, _return_inputs_states)(*args, **kwargs)
  File "/home/ubuntu/anaconda3/envs/trans4.9/lib/python3.8/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/ubuntu/anaconda3/envs/trans4.9/lib/python3.8/site-packages/torch/jit/_trace.py", line 125, in forward
    graph, out = torch._C._create_graph_by_tracing(
  File "/home/ubuntu/anaconda3/envs/trans4.9/lib/python3.8/site-packages/torch/jit/_trace.py", line 116, in wrapper
    outs.append(self.inner(*trace_inputs))
  File "/home/ubuntu/anaconda3/envs/trans4.9/lib/python3.8/site-packages/torch/nn/modules/module.py", line 725, in _call_impl
    result = self._slow_forward(*input, **kwargs)
  File "/home/ubuntu/anaconda3/envs/trans4.9/lib/python3.8/site-packages/torch/nn/modules/module.py", line 709, in _slow_forward
    result = self.forward(*input, **kwargs)
  File "/home/ubuntu/anaconda3/envs/trans4.9/lib/python3.8/site-packages/transformers/models/bert/modeling_bert.py", line 938, in forward
    input_shape = input_ids.size()
AttributeError: 'dict' object has no attribute 'size'
```




Comments:
Hi @ZHANG-GuiGui, 

Thanks for reporting the issue, I'm looking at it 🧐 

Hi, I'm also having this issue. 

`!python -m transformers.onnx --model=MyModel onnx/MyModelName/`

Extracting a GPT-2 model.

Hi @ZHANG-GuiGui, @johnpaulbin, 

This is indeed unsupported on PyTorch < 1.8.0.
We will submit a fix for this in order to raise a meaningful error when this happens.

Thanks again for raising the issue 🤗 

Hi @mfuntowicz , Thanks for your explication.
Is there any alternative way to export onnx model by using pytorch < 1.8.0 ?

You might be able to use our previous method `convert_graph_to_onnx.py`. 

You can find more information [here](https://huggingface.co/transformers/serialization.html#graph-conversion)

It works. Thanks 👍 

Closing the issue for now, feel free to reopen/create a new one if you have any further issue 👍🏻.