How to ensure the correctness of the torch script

username: marc_q
cooked: <p>Hello!<br>
Following the official tutorial, I am trying to convert a torch modle to a torch script. I am worried about how to ensure the correctness of the torch script. When convert a pytorch model to torch script via tracing(resnet18 as an example), official code shows to use a example input torch.rand(1,3,224,224) to get a traced_script_module. And then, feed the module a torch.ones(1,3,224,224) to get a output tensor. But I get different outputs when feed same torch.ones(1,3,224,224). Just because the example torch.rand(1,3,224,224)s are different. How to ensure that the results are correct, when feed real images.<br>
thanks.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/5/e519612a54acc7efd6966d6a56422441351cf36d.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/e519612a54acc7efd6966d6a56422441351cf36d" title="res"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/e/5/e519612a54acc7efd6966d6a56422441351cf36d_2_690x402.jpeg" alt="res" data-base62-sha1="wGHIoXYyPrzz2wvccgTBNkNzmwt" width="690" height="402" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/e/5/e519612a54acc7efd6966d6a56422441351cf36d_2_690x402.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/e/5/e519612a54acc7efd6966d6a56422441351cf36d_2_1035x603.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/e/5/e519612a54acc7efd6966d6a56422441351cf36d_2_1380x804.jpeg 2x" data-dominant-color="EBEDF0"><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">res</span><span class="informations">1588×926 134 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>

username: ptrblck
cooked: <p>I cannot reproduce the issue using <code>resnet18</code> as seen here:</p>
<pre><code class="lang-python">model = models.resnet18()
x = torch.randn(1, 3, 224, 224)
model_traced = torch.jit.trace(model, x)

out = model(torch.ones(1, 3, 224, 224))
out_traced = model(torch.ones(1, 3, 224, 224))
print((out - out_traced).abs().max())
&gt; tensor(0., grad_fn=&lt;MaxBackward1&gt;)
</code></pre>
<p>In case you are using a different model, make sure the traced model uses the same “code path” as the eager model (e.g. in case data-dependent control flow is used inside the model or dropout etc.).</p>
<p>EDIT: the tracer should also raise a warning e.g. if dropout was used in <code>training</code> mode:</p>
<pre><code class="lang-python">TracerWarning: Trace had nondeterministic nodes. Did you forget call .eval() on your model?
</code></pre>

username: marc_q
cooked: <p>Thanks for your attention.<br>
I can reproduce this issue by run code twice.</p>
<pre><code class="lang-auto">model = torchvision.models.resnet18()
# An example input you would normally provide to your model's forward() method.
example = torch.rand(1, 3, 224, 224)
# Use torch.jit.trace to generate a torch.jit.ScriptModule via tracing.
traced_script_module = torch.jit.trace(model, example)

output = traced_script_module.forward(torch.ones(1,3,224,224))
traced_script_module.save("traced_resnet_model.pt")
#eg: first get: tensor([-0.0645,  0.1313, -0.3563, -0.0869,  0.3209], grad_fn=&lt;SliceBackward&gt;)
#   second get: tensor([-0.1508, -0.2974,  0.0963, -0.1596, -0.7377], grad_fn=&lt;SliceBackward&gt;)
print(output[0, :5])
</code></pre>
<p>Basing code above，i run it first get tensor [-0.0645,  0.1313, -0.3563, -0.0869,  0.3209], and i try to run it again, i will get tensor[-0.1508, -0.2974,  0.0963, -0.1596, -0.7377]. The only difference between two procedures are var example. it seems to generate diffenent traced script. Besides, i compared to binary traced_resnet_model.pts, they are absolutly different,as shown below.</p>
<pre><code class="lang-auto">&gt;&gt; cmp traced_resnet_model_1.pt traced_resnet_model_2.pt 
&gt;&gt; raced_resnet_model_1.pt traced_resnet_model_2.pt differ: char 51, line 1
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/7/578804416ce8178c6bbf2cc0f40580bc6b82ff67.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/578804416ce8178c6bbf2cc0f40580bc6b82ff67" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/7/578804416ce8178c6bbf2cc0f40580bc6b82ff67_2_690x158.jpeg" alt="image" data-base62-sha1="cukX9WHAPtvZvqtPaeH3kBvSbWf" width="690" height="158" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/7/578804416ce8178c6bbf2cc0f40580bc6b82ff67_2_690x158.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/5/7/578804416ce8178c6bbf2cc0f40580bc6b82ff67_2_1035x237.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/5/7/578804416ce8178c6bbf2cc0f40580bc6b82ff67_2_1380x316.jpeg 2x" data-dominant-color="EDC3C6"><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">image</span><span class="informations">1920×441 189 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>

username: ptrblck
cooked: <p>I don’t quite understand your posted code and the explanations, since the code doesn’t compare anything or do you mean you are getting different results by rerunning the entire script twice?<br>
In the latter case, this would be expected, as you are randomly initializing the <code>resnet18</code>. If you want to load the pretrained model, use <code>pretrained=True</code>, or load a custom <code>state_dict</code> otherwise.</p>

username: marc_q
cooked: <p>Wow, that’s it.<br>
I understand it. When I use the pre-trained model, the trace script becomes unique. and generate same result no matter how many times i run it. thank you for your patience！ <img src="https://discuss.pytorch.org/images/emoji/apple/grinning.png?v=10" title=":grinning:" class="emoji" alt=":grinning:"></p>