[BUG] Error in ShareBuffers Optimization

Wilbur: <h1><a name="error-description-1" class="anchor" href="#error-description-1"></a>Error Description</h1>
<p>I encountered a model that, when run in <code>onnxruntime</code> and <code>Glow</code> respectively, their outputs are distinct.</p>
<p>The output of <code>onnxruntime</code> is</p>
<pre><code class="lang-auto">[-7.4040246e-01 -1.0558732e+00 -5.1735401e-01  5.7158279e+00
 -6.1898971e-01  4.8943150e-01 -5.3290629e-01 -7.5109071e-01
 -1.0089742e+00 -9.7962457e-01 -2.0205033e-01 -5.6859505e-01
 -8.3097053e-01 -6.0609245e-01 -9.2153203e-01 -8.8401639e-01
 -8.7050164e-01 -9.2254686e-01  6.3326435e+00 -5.2634275e-01
 -2.1332520e-01 -3.9393574e-02 -8.1680071e-01 -7.4010009e-01
 -9.0249908e-01 -9.0031087e-01 -8.4391761e-01 -8.9972514e-01
  5.8119040e+00 -4.5583522e-01  5.3716774e+00 -7.6865733e-01
 -3.2050097e-01 -6.3881731e-01 -7.8147006e-01 -8.8674474e-01
 -8.7157512e-01 -7.5937271e-01  1.0185689e-03 -3.4555185e-01]
</code></pre>
<p>, whereas the output of <code>Glow</code> is</p>
<pre><code class="lang-auto">[-0.742133, -1.054049, -0.511033, 5.701681, -0.617301, 0.488157, -0.531513, -0.749289, -1.006998, -0.977479, -0.201742, -0.567398, -0.830031, -0.606550, -0.920508, -0.882669, -0.869515, -0.921491, 6.325564, -0.525662, -0.213413, -0.039500, -0.816155, -0.739523, -0.901777, -0.899409, -0.843245, -0.898985, 5.806691, -0.454686, 5.361451, -0.770803, -0.321736, -0.639595, -0.781732, -0.886845, -0.871701, -0.757481, 0.010961, -0.342509]
</code></pre>
<h1><a name="cause-analysis-2" class="anchor" href="#cause-analysis-2"></a>Cause Analysis</h1>
<p>According to the model definition, Node <code>Mul_1024</code> should take the output of node <code>Sub_1023</code> as input.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/5/c5b680e5ea3c8a30ec38803d5c7d3befb26acf84.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/c5b680e5ea3c8a30ec38803d5c7d3befb26acf84" title="model_def"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/c/5/c5b680e5ea3c8a30ec38803d5c7d3befb26acf84_2_522x500.jpeg" alt="model_def" data-base62-sha1="sd34z4PDxGliK009CN7GdtinflG" width="522" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/c/5/c5b680e5ea3c8a30ec38803d5c7d3befb26acf84_2_522x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/c/5/c5b680e5ea3c8a30ec38803d5c7d3befb26acf84_2_783x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/c/5/c5b680e5ea3c8a30ec38803d5c7d3befb26acf84.jpeg 2x" data-dominant-color="B3D1B5"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">model_def</span><span class="informations">807×772 65.4 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>If you use <code>-dump-ir-after-all-passes</code> option to view IR generated by <code>Glow</code>, you will see that after the <code>ShareBuffers</code> optimization pass</p>
<pre><code class="lang-auto">...
34 %Sub_1023__1 = elementsub @out %Sub_1023__1_res, @in %Sub_1023__1_res, @in %A1227_transposed
...
98 %Sub_2268__1 = elementsub @out %Sub_1023__1_res, @in %Sub_1023__1_res, @in %A1227_transposed
...
...
...
179 %Sub_1023__1_res__3 = tensorview @in %Sub_1023__1_res { Ty: float&lt;4 x 64 x 1 x 1&gt;, Offsets:[0, 0, 0, 0]} // Users: @in 180
180 %Mul_1024 = elementmul @out %Mul_1024_res, @in %Sub_1023__1_res__3, @in %Add_3784_res
...
</code></pre>
<p>, node <code>Sub_2268</code> writes its output to the buffer that is supposed to store the output of node <code>Sub_1023</code>. So in practice, in line <code>180</code>, the input to node <code>Mul_1024</code> is the output of node <code>Sub_2268</code>, which is the cause of the wrong output value.</p>
<h1><a name="how-to-reproduce-3" class="anchor" href="#how-to-reproduce-3"></a>How To Reproduce</h1>
<p>You can download the package at <a href="https://drive.google.com/file/d/1nu2g1hvD2jWtL_KA-k7BKan1O_vP69Gc/view?usp=sharing" class="inline-onebox" rel="noopener nofollow ugc">rep.zip - Google Drive</a>. After unzipping it, you can follow the steps in <code>README.md</code> to reproduce the results.</p>
<p>System Information:</p>
<p>Glow version: built from GitHub source <code>commit 07a82bd9fe97dfd2e8ea0f4742dce5ce86177c2b</code></p>
<p>onnxruntime version: <code>1.7.0</code></p>
<p>onnx version: <code>1.9.0</code></p>
<p>Operation system: Ubuntu 18.04LTS</p>
<p>CPU: Intel(R) Xeon(R) CPU E5-2683 v4 @ 2.10GHz 16 cores</p>
<p>BTW, we also report a similar issue (which seems also due to IR translation at <a href="https://discuss.pytorch.org/t/bug-error-in-ir-parsing/128709/2" class="inline-onebox">[BUG] Error in IR Parsing</a></p>