<p dir="auto">Looks like ONNX support does not support dataparallel models (in particular, model that is converted after nn.DataParallel(model).</p>
<p dir="auto">Here is a simple piece of code to repro the issue:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import os
import sys
import numpy as np
import pandas as pd
import torch
import torchvision.models as models
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torch.nn.init as init
import time
from torch.optim.lr_scheduler import ReduceLROnPlateau
from torch.autograd import Variable
import torchvision.transforms as transforms
from torch.utils.data import DataLoader, Dataset
from sklearn.metrics.ranking import roc_auc_score
from sklearn.model_selection import train_test_split
from PIL import Image
import multiprocessing

model = models.densenet.densenet121(pretrained=True)
model = nn.DataParallel(model)
dummy_input = Variable(torch.randn(10, 3, 224, 224)).cuda()
model = model.cuda()
torch.onnx.export(model, dummy_input, &quot;test.onnx&quot;, verbose=False, export_params=True)"><pre class="notranslate"><code class="notranslate">import os
import sys
import numpy as np
import pandas as pd
import torch
import torchvision.models as models
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torch.nn.init as init
import time
from torch.optim.lr_scheduler import ReduceLROnPlateau
from torch.autograd import Variable
import torchvision.transforms as transforms
from torch.utils.data import DataLoader, Dataset
from sklearn.metrics.ranking import roc_auc_score
from sklearn.model_selection import train_test_split
from PIL import Image
import multiprocessing

model = models.densenet.densenet121(pretrained=True)
model = nn.DataParallel(model)
dummy_input = Variable(torch.randn(10, 3, 224, 224)).cuda()
model = model.cuda()
torch.onnx.export(model, dummy_input, "test.onnx", verbose=False, export_params=True)
</code></pre></div>
<p dir="auto">The error message is:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-4-514806a4333a&gt; in &lt;module&gt;()
    386 dummy_input = Variable(torch.randn(10, 3, 224, 224)).cuda()
    387 model = model.cuda()
--&gt; 388 torch.onnx.export(model, dummy_input, &quot;test.onnx&quot;, verbose=False, export_params=True)
    389 
    390 #model.load_state_dict(chkpt['state_dict'])

~/python3env/lib/python3.5/site-packages/torch/onnx/__init__.py in export(model, args, f, export_params, verbose, training)
     73             only, so you will generally not need to set this to True.
     74     &quot;&quot;&quot;
---&gt; 75     _export(model, args, f, export_params, verbose, training)
     76 
     77 

~/python3env/lib/python3.5/site-packages/torch/onnx/__init__.py in _export(model, args, f, export_params, verbose, training)
    120                            &quot;something weird is happening in your model!&quot;)
    121 
--&gt; 122     _optimize_trace(trace)
    123     if verbose:
    124         print(trace)

~/python3env/lib/python3.5/site-packages/torch/onnx/__init__.py in _optimize_trace(trace)
     79     torch._C._jit_pass_peephole(trace)
     80     torch._C._jit_pass_lint(trace)
---&gt; 81     torch._C._jit_pass_onnx(trace)
     82     torch._C._jit_pass_lint(trace)
     83     torch._C._jit_pass_onnx_peephole(trace)

RuntimeError: untraced buffer"><pre class="notranslate"><code class="notranslate">---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-4-514806a4333a&gt; in &lt;module&gt;()
    386 dummy_input = Variable(torch.randn(10, 3, 224, 224)).cuda()
    387 model = model.cuda()
--&gt; 388 torch.onnx.export(model, dummy_input, "test.onnx", verbose=False, export_params=True)
    389 
    390 #model.load_state_dict(chkpt['state_dict'])

~/python3env/lib/python3.5/site-packages/torch/onnx/__init__.py in export(model, args, f, export_params, verbose, training)
     73             only, so you will generally not need to set this to True.
     74     """
---&gt; 75     _export(model, args, f, export_params, verbose, training)
     76 
     77 

~/python3env/lib/python3.5/site-packages/torch/onnx/__init__.py in _export(model, args, f, export_params, verbose, training)
    120                            "something weird is happening in your model!")
    121 
--&gt; 122     _optimize_trace(trace)
    123     if verbose:
    124         print(trace)

~/python3env/lib/python3.5/site-packages/torch/onnx/__init__.py in _optimize_trace(trace)
     79     torch._C._jit_pass_peephole(trace)
     80     torch._C._jit_pass_lint(trace)
---&gt; 81     torch._C._jit_pass_onnx(trace)
     82     torch._C._jit_pass_lint(trace)
     83     torch._C._jit_pass_onnx_peephole(trace)

RuntimeError: untraced buffer
</code></pre></div>
<p dir="auto">However if we remove the <code class="notranslate">model = nn.DataParallel(model)</code> line, i.e. change the code to the following:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="model = models.densenet.densenet121(pretrained=True)
dummy_input = Variable(torch.randn(10, 3, 224, 224)).cuda()
model = model.cuda()
torch.onnx.export(model, dummy_input, &quot;test.onnx&quot;, verbose=False, export_params=True)"><pre class="notranslate"><code class="notranslate">model = models.densenet.densenet121(pretrained=True)
dummy_input = Variable(torch.randn(10, 3, 224, 224)).cuda()
model = model.cuda()
torch.onnx.export(model, dummy_input, "test.onnx", verbose=False, export_params=True)
</code></pre></div>
<p dir="auto">It at least exports the model.</p>
<p dir="auto">I hope either we fix this bug (i.e. also support the model that is converted using nn.DataParallel(model), or provide an explicit error message saying multiple GPU model is not supported.</p>
<p dir="auto">BTW - for those who still want to export such a model, here is the workaround:<br>
<a href="https://stackoverflow.com/questions/44230907/keyerror-unexpected-key-module-encoder-embedding-weight-in-state-dict" rel="nofollow">https://stackoverflow.com/questions/44230907/keyerror-unexpected-key-module-encoder-embedding-weight-in-state-dict</a><br>
i.e. still loading the single GPU model and when loading the weights, change the dict name to exclude the string "module".</p>
<p dir="auto">When submitting a bug report, please include the following information (where relevant):</p>
<ul dir="auto">
<li>OS: Linux</li>
<li>PyTorch version: 0.3.1</li>
<li>How you installed PyTorch (conda, pip, source): pip</li>
<li>Python version: 3.5</li>
<li>CUDA/cuDNN version: 8.0/cuDNN 6</li>
<li>GPU models and configuration: Tesla K80</li>
<li>GCC version (if compiling from source): N/A</li>
</ul>
<p dir="auto">I have a custom model that's not very complicated<br>
It's just a combination of Conv2D, MaxPool2D, BatchNorm2D and Dropout2D and one ConvTranspose2D.</p>
<p dir="auto">I'm trying to save this model to file using the torch.onnx.export() function, but I get this error:</p>
<p dir="auto">*** RuntimeError: tuple appears in op that does not forward tuples (VisitNode at ../torch/csrc/jit/passes/lower_tuples.cpp:72)</p>
<p dir="auto">Any ideas are appreciated</p>
<p dir="auto">Platform: NVIDIA Jetson AGX Xavier<br>
PyTorch Version: 1.0.0a0+8601b33<br>
Python3</p>
<p dir="auto">Thanks,<br>
S</p>