Issue 1:
🐛 Bug

ONNX exporter gets tripped by a model using nn.Sequential and nn.Embedding when the embedding layer is stored as a class property. The generated onnx file is malformed: it has an initializer for the user-defined input. The initializer used is the weight metrix of the embedding table.

The embedding table appears in initializers the second time (this time for the embedding layer).

To Reproduce

Steps to reproduce the behavior:

Define a model:

n = 8
dim = 10

class SomeModel(nn.Module):

def __init__(self):
super(SomeModel, self).__init__()
self.embedding = nn.Embedding(n, dim)
self.seq = nn.Sequential(
self.embedding,
nn.Linear(dim, 1),
nn.Sigmoid()
)

def forward(self, indices):
return self.seq(indices)

model = SomeModel()

Export:

input = torch.LongTensor([2])
torch.onnx.export(model, input, "foo.onnx")

Check foo.onnx and notice it's malformed.

Expected behavior

No initializer for the user-provided input of the model.

Environment

Collecting environment information...
PyTorch version: 1.0.0
Is debug build: No
CUDA used to build PyTorch: 9.0.176

OS: Ubuntu 16.04.6 LTS
GCC version: (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609
CMake version: Could not collect

Python version: 2.7
Is CUDA available: No
CUDA runtime version: No CUDA
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA

Versions of relevant libraries:
[pip] msgpack-numpy==0.4.3.1
[pip] numpy==1.14.1
[pip] torch==1.0.0
[conda] Could not collect

Additional context

#19227 is another issue where the onnx export and nn.Sequential appear to be at odds.

This bug can be worked around by inlining embedding definition to nn.Sequetial or by getting rid of nn.Sequential and chaining ops manually.

Either way, the fact that a malformed file is outputted is not great.

Issue 2:
🐛 Bug

The tracer in onnx exporter is failing with an assertion error in the exporter.

To Reproduce

Steps to reproduce the behavior:

define a model:

class SomeModel(nn.Module):

def __init__(self):
super(SomeModel, self).__init__()
dim = 5
self.emb = nn.Embedding(10, dim)
self.lin1 = nn.Linear(dim, 1)
self.seq = nn.Sequential(
self.emb,
self.lin1,
)

def forward(self, input):
return self.seq(input)

model = SomeModel()

run export:

dummy_input = torch.tensor([2], dtype=torch.long)
dummy_input

torch.onnx.export(model, dummy_input, "model.onnx", verbose=True)

fails with:

[redacted]/vendor/local/lib/python2.7/site-packages/torch/onnx/utils.pyc in _export(model, args, f, export_params, verbose, training, input_names, output_names, operator_export_type, export_type, example_outputs, propagate)
230 defer_weight_export = export_type is not ExportTypes.PROTOBUF_FILE
231 if export_params:
--> 232 proto, export_map = graph.export(params, _onnx_opset_version, defer_weight_export, operator_export_type)
233 else:
234 proto, export_map = graph.export([], _onnx_opset_version, False, operator_export_type)

RuntimeError: torch/csrc/jit/export.cpp:296: encodeBlock: Assertion `b->inputs().size() >= num_initializers` failed.

Expected behavior

Model is exported to ONNX

Environment

Collecting environment information...
PyTorch version: 0.4.1
Is debug build: No
CUDA used to build PyTorch: 9.0.176

OS: Ubuntu 16.04.6 LTS
GCC version: (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609
CMake version: Could not collect

Python version: 2.7
Is CUDA available: No
CUDA runtime version: No CUDA
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA

Versions of relevant libraries:
[pip] msgpack-numpy==0.4.3.1
[pip] numpy==1.14.1
[pip] torch==0.4.1
[conda] Could not collect

Additional context

As a workaround one can inline layer definitions into the sequence:

class SomeModel(nn.Module):

def __init__(self):
super(SomeModel, self).__init__()
dim = 5
self.seq = nn.Sequential(
nn.Embedding(10, dim),
nn.Linear(dim, 1),
)

def forward(self, input):
return self.seq(input)

model = SomeModel()