Exporting model to onnx increases the model size
Author: hetpandya
Pull request: no

Hi, I'm trying to convert some models as mentioned below to onnx as follows:
ktrapeznikov/albert-xlarge-v2-squad-v2
albert-xlarge-v1
albert-xlarge-v2

The common issue with exporting all these models is that I get an exception that the protobuf size increases to more than 2gb while all these are less than 800mb. When I use the use_external_data_format=True flag, the exported model files(network layers as I searched in other issues) sum up to in gbs of size. For eg, the model ktrapeznikov/albert-xlarge-v2-squad-v2 is sized to 210mb but when I convert it to onnx using use_external_data_format flag, the model size sums up to 4gb. 

## Code example
```
from transformers import AutoTokenizer, AutoModelForQuestionAnswering
import torch

model_name = "ktrapeznikov/albert-xlarge-v2-squad-v2"

tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForQuestionAnswering.from_pretrained(model_name)
model.eval()
question = "what is google specialization"
text = "Google LLC is an American multinational technology company that specializes in Internet-related services and products, which include online advertising technologies, a search engine, cloud computing, software, and hardware."
encoding = tokenizer.encode_plus(question, text)
input_ids, attention_mask, token_type_ids = encoding["input_ids"],encoding["attention_mask"], encoding["token_type_ids"]

input_ids = torch.tensor([input_ids])
attention_mask = torch.tensor([attention_mask])
token_type_ids = torch.tensor([token_type_ids])

torch.onnx.export(
    model,
    (input_ids,attention_mask, token_type_ids),
    f"{model_name}.onnx",
    input_names = ['input_ids','attention_mask', 'token_type_ids'],
    output_names = ['qa_outputs'], 
    opset_version=12, ##opset has to be set to 12
    do_constant_folding=True,
    use_external_data_format=True,
    dynamic_axes = {
        'input_ids' : {0: 'batch', 1: 'sequence'},
        'attention_mask' : {0: 'batch', 1: 'sequence'}, 
        'token_type_ids' : {0: 'batch', 1: 'sequence'}, 
        'qa_outputs': {0: 'batch'}
    }
)
```
![image](https://user-images.githubusercontent.com/55797177/106251673-4549f780-623b-11eb-8735-9bf90f18bf4e.png)
![image](https://user-images.githubusercontent.com/55797177/106251758-63aff300-623b-11eb-81e7-defe45429eaf.png)


## System Info
PyTorch version: 1.7.0+cu101
CUDA used to build PyTorch: 10.1
ROCM used to build PyTorch: N/A

OS: Ubuntu 18.04.5 LTS (x86_64)
GCC version: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Clang version: 6.0.0-1ubuntu2 (tags/RELEASE_600/final)
CMake version: version 3.12.0

Python version: 3.6 (64-bit runtime)
Is CUDA available: False
CUDA runtime version: 10.1.243
GPU models and configuration: Could not collect
Nvidia driver version: Could not collect
cuDNN version: /usr/lib/x86_64-linux-gnu/libcudnn.so.7.6.5
HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:
[pip3] numpy==1.19.5
[pip3] torch==1.7.0+cu101
[pip3] torchsummary==1.5.1
[pip3] torchtext==0.3.1
[pip3] torchvision==0.8.1+cu101

Any help would be much appreciated.
Thanks in advance!

Comments:
I wouldn't be surprised that ONNX serializes each of the layers as independant layers when they're all repeated. I don't know enough about the ONNX export to know it that's the issue or what to do to fix it though.

Do you get similar increases in size with other models? With BERT for example?

Hi @LysandreJik , I faced the issue only on albert models so far, exporting to onnx for other BERT models worked fine and use them for prediction as well.

 I solved the issue using [this code](https://github.com/thehetpandya/onnx-shared-weights-remove/blob/main/onnx_remove_shared_weights.ipynb) that removes shared weights from the ONNX model.