<h2 dir="auto"><g-emoji class="g-emoji" alias="bug" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f41b.png">🐛</g-emoji> Bug</h2>
<p dir="auto">ONNX exporter gets tripped by a model using <code class="notranslate">nn.Sequential</code> and <code class="notranslate">nn.Embedding</code> 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.</p>
<p dir="auto">The embedding table appears in initializers the second time (this time for the embedding layer).</p>
<h2 dir="auto">To Reproduce</h2>
<p dir="auto">Steps to reproduce the behavior:</p>
<ol dir="auto">
<li>Define a model:</li>
</ol>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="    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()"><pre class="notranslate"><code class="notranslate">    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()
</code></pre></div>
<ol start="2" dir="auto">
<li>Export:</li>
</ol>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="input = torch.LongTensor([2])
torch.onnx.export(model, input, &quot;foo.onnx&quot;)"><pre class="notranslate"><code class="notranslate">input = torch.LongTensor([2])
torch.onnx.export(model, input, "foo.onnx")
</code></pre></div>
<ol start="3" dir="auto">
<li>Check <code class="notranslate">foo.onnx</code> and notice it's malformed.</li>
</ol>
<h2 dir="auto">Expected behavior</h2>
<p dir="auto">No initializer for the user-provided input of the model.</p>
<h2 dir="auto">Environment</h2>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="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"><pre class="notranslate"><code class="notranslate">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
</code></pre></div>
<h2 dir="auto">Additional context</h2>
<p dir="auto"><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="432792695" data-permission-text="Title is private" data-url="https://github.com/pytorch/pytorch/issues/19227" data-hovercard-type="issue" data-hovercard-url="/pytorch/pytorch/issues/19227/hovercard" href="https://github.com/pytorch/pytorch/issues/19227">#19227</a> is another issue where the onnx export and <code class="notranslate">nn.Sequential</code> appear to be at odds.</p>
<p dir="auto">This bug can be worked around by inlining embedding definition to <code class="notranslate">nn.Sequetial</code> or by getting rid of <code class="notranslate">nn.Sequential</code> and chaining ops manually.</p>
<p dir="auto">Either way, the fact that a malformed file is outputted is not great.</p>
<h2 dir="auto"><g-emoji class="g-emoji" alias="bug" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f41b.png">🐛</g-emoji> Bug</h2>
<p dir="auto">The tracer in onnx exporter is failing with an assertion error in the exporter.</p>
<h2 dir="auto">To Reproduce</h2>
<p dir="auto">Steps to reproduce the behavior:</p>
<ol dir="auto">
<li>define a model:</li>
</ol>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="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()"><pre class="notranslate"><code class="notranslate">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()
</code></pre></div>
<ol start="2" dir="auto">
<li>run export:</li>
</ol>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="dummy_input = torch.tensor([2], dtype=torch.long)
dummy_input

torch.onnx.export(model, dummy_input, &quot;model.onnx&quot;, verbose=True)"><pre class="notranslate"><code class="notranslate">dummy_input = torch.tensor([2], dtype=torch.long)
dummy_input

torch.onnx.export(model, dummy_input, "model.onnx", verbose=True)
</code></pre></div>
<p dir="auto">fails with:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="[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:
--&gt; 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-&gt;inputs().size() &gt;= num_initializers` failed."><pre class="notranslate"><code class="notranslate">[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:
--&gt; 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-&gt;inputs().size() &gt;= num_initializers` failed.
</code></pre></div>
<h2 dir="auto">Expected behavior</h2>
<p dir="auto">Model is exported to ONNX</p>
<h2 dir="auto">Environment</h2>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="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"><pre class="notranslate"><code class="notranslate">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
</code></pre></div>
<h2 dir="auto">Additional context</h2>
<p dir="auto">As a workaround one can inline layer definitions into the sequence:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="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()"><pre class="notranslate"><code class="notranslate">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()
</code></pre></div>