Failed to finish "_jit_pass_onnx" when converting PyTorch model to ONNX

StevenTsai: <p>Hi,<br>
Recently, I tried to convert my PyTorch model (customized transformer) to an ONNX one, but the process hung during the conversion.<br>
I found that the hanging issue happend when running this line,</p>
<pre><code class="lang-auto">graph = _C._jit_pass_onnx(graph, operator_export_type)  # in pytorch/torch/onnx/utils.py
</code></pre>
<p>Currently, there is no error message from this problem. The program just hangs there.</p>
<p>My environment is Windows11 with 32GB RAM. My PyTorch version is 1.12.0. We can do the inference with PyTorch model without any problem.<br>
I used the following command for model conversion.</p>
<pre><code class="lang-auto">        torch.onnx.export(model, 
                          tuple(input_data_list), 
                          "onnx.onnx"), 
                          input_names = input_names,
                          output_names='output',
                          opset_version=16)
</code></pre>
<p>Please tell me if you know the root cause of the hanging issue. Thanks.</p>

StevenTsai: <p>I just solved the problem by myself.<br>
My PyTorch model contained recursive blocks, and I think the root cause might be the “cycle” in the graph. The solution is to convert the encoder and decoder parts separately.</p>

StevenTsai: <p>By the way, here is the source code (c++) of _jit_pass_onnx</p><aside class="onebox githubblob" data-onebox-src="https://github.com/pytorch/pytorch/blob/95d873855e6b5a7b44e102d3aec81d6db3215c0f/torch/csrc/jit/passes/onnx.cpp#L164">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/blob/95d873855e6b5a7b44e102d3aec81d6db3215c0f/torch/csrc/jit/passes/onnx.cpp#L164" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/pytorch/blob/95d873855e6b5a7b44e102d3aec81d6db3215c0f/torch/csrc/jit/passes/onnx.cpp#L164" target="_blank" rel="noopener nofollow ugc">pytorch/pytorch/blob/95d873855e6b5a7b44e102d3aec81d6db3215c0f/torch/csrc/jit/passes/onnx.cpp#L164</a></h4>



    <pre class="onebox"><code class="lang-cpp">
      <ol class="start lines" start="154" style="counter-reset: li-counter 153 ;">
          <li>  EliminateDeadCode(</li>
          <li>      block, true, DCESideEffectPolicy::ALLOW_DELETING_NODES_WITH_SIDE_EFFECTS);</li>
          <li>}</li>
          <li>
          </li>
<li>void PreprocessCaffe2Ops(std::shared_ptr&lt;Graph&gt;&amp; graph) {</li>
          <li>  preprocessCaffe2Ops(graph-&gt;block());</li>
          <li>  GRAPH_DUMP("After PreprocessCaffe2Ops: ", graph);</li>
          <li>}</li>
          <li>
          </li>
<li>// Transform PythonOps into Nodes that match ONNX semantics.</li>
          <li class="selected">std::shared_ptr&lt;Graph&gt; ToONNX(</li>
          <li>    std::shared_ptr&lt;Graph&gt;&amp; graph,</li>
          <li>    ::torch::onnx::OperatorExportTypes operator_export_type) {</li>
          <li>  auto constant_value_map = ConstantValueMap::getInstance();</li>
          <li>  ConstantValueMap::ClearMaps();</li>
          <li>  auto new_graph = std::make_shared&lt;Graph&gt;(graph-&gt;current_scope());</li>
          <li>  std::unordered_map&lt;Value*, Value*&gt; env;</li>
          <li>  try {</li>
          <li>    BlockToONNX(graph-&gt;block(), new_graph-&gt;block(), operator_export_type, env);</li>
          <li>  } catch (std::runtime_error&amp; ex) {</li>
          <li>    ONNX_LOG(</li>
      </ol>
    </code></pre>



  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>