TracerWarning: Converting a tensor to a Python index might cause the trace to be incorrect...This means that the trace might not generalize to other inputs!

username: sercan
cooked: <p>I am trying to create trace file. Related code part is here:</p>
<pre><code>def center_crop(self, layer, target_size):
    _, _, layer_height, layer_width = layer.size()

    diff_y = (layer_height - target_size[0]) // 2
    diff_x = (layer_width - target_size[1]) // 2
    return layer[:, :, diff_y:(diff_y + target_size[0]), diff_x:(diff_x + target_size[1])]

def forward(self, x, bridge):

    up = self.up(x)
    crop1 = self.center_crop(bridge, up.shape[2:])
    out = torch.cat([up, crop1], 1)
    out = self.conv_block(out)
</code></pre>
<p>It gives the warning at this line:</p>
<blockquote>
<p>return layer[:, :, diff_y:(diff_y + target_size[0]), diff_x:(diff_x + target_size[1])]</p>
</blockquote>
<p>Then, I loaded this trace file in C++ and made prediction for a test image. Then I compared the result with the output in python. I observed that results are very different. I guess that the reason originates from that warning.</p>

username: driazati
cooked: <p>The tracer uses the example inputs you provide and records the operations. If a different input would result in a different operation, then this will not be captured by the tracer. Here it’s recording the arguments to the slice operation as constants (so it will always slice it with the values produced with the example inputs).</p>
<p>To get around this, put any code with data-dependent control flow inside a <a href="https://pytorch.org/docs/stable/jit.html#creating-torchscript-code" rel="nofollow noopener"><code>ScriptModule</code></a>, and then call that in your traced code.</p>

username: sercan
cooked: <p><a class="mention" href="/u/driazati">@driazati</a>  I added a helper function and wrote this code:</p>
<pre><code>@torch.jit.script
def center_slice_helper(layer, diff_y, diff_x, h_end, w_end):
   return layer[:, :, diff_y:h_end, diff_x:w_end]

def center_crop(self, layer, target_size):
    #_, _, layer_height, layer_width = layer.size()

    diff_y = (layer.shape[2] - target_size.shape[2]) // 2
    diff_x = (layer.shape[3] - target_size.shape[3]) // 2

    h_end = diff_y + target_size.shape[2]
    w_end = diff_x + target_size.shape[3]

    return center_slice_helper(layer, diff_y, diff_x, h_end, w_end)


def forward(self, x, bridge):

    up = self.up(x)
    crop1 = self.center_crop(bridge, up)
    out = torch.cat([up, crop1], 1)
    out = self.conv_block(out)

    return out
</code></pre>
<p>At this time, it gave this exception:</p>
<pre><code class="lang-auto">torch.jit.TracingCheckError: Tracing failed sanity checks!
Encountered an exception while running the Python function with test inputs.
Exception:
	center_slice_helper() expected value of type Tensor for argument 'diff_y' in position 1, but instead got value of type int.
	Value: 0
	Declaration: center_slice_helper(Tensor layer, Tensor diff_y, Tensor diff_x, Tensor h_end, Tensor w_end) -&gt; Tensor
</code></pre>
<p>I debugged the code and observed that x parameter of forward function has this type:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/0/0b5021d014ea169b8f4bab7344c8e58d542045be.png" data-download-href="https://discuss.pytorch.org/uploads/default/0b5021d014ea169b8f4bab7344c8e58d542045be" title="x_parameter"><img src="https://discuss.pytorch.org/uploads/default/original/2X/0/0b5021d014ea169b8f4bab7344c8e58d542045be.png" alt="x_parameter" data-base62-sha1="1C4VVHUtjDbo4mwDdNeCoEmJgzc" width="690" height="288" data-dominant-color="45494C"><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">x_parameter</span><span class="informations">932×390 8.66 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>How to handle this?</p>

username: sercan
cooked: <p><a class="mention" href="/u/driazati">@driazati</a> I have read the related part in documentation: <a href="https://pytorch.org/docs/stable/jit.html#mixing-tracing-and-scripting" rel="nofollow noopener">https://pytorch.org/docs/stable/jit.html#mixing-tracing-and-scripting</a></p>
<p>I understood that I should use MyPy-style type annotation. What is your opinion?</p>

username: driazati
cooked: <p>Our support for non-tensor types in tracing is pretty limited (see <a href="https://github.com/pytorch/pytorch/issues/14455" rel="nofollow noopener">#14455</a>), but your model looks like using script mode would work well. See something like:</p>
<pre><code class="lang-python">@torch.jit.script
def center_slice_helper(layer, diff_y, diff_x, h_end, w_end):
    # type: (Tensor, int, int, int, int) -&gt; Tensor
    return layer[:, :, diff_y:h_end, diff_x:w_end]

class M(torch.jit.ScriptModule):
    @torch.jit.script_method
    def center_crop(self, layer, target_size):
        #_, _, layer_height, layer_width = layer.size()

        diff_y = (layer.shape[2] - target_size.shape[2]) // 2
        diff_x = (layer.shape[3] - target_size.shape[3]) // 2

        h_end = diff_y + target_size.shape[2]
        w_end = diff_x + target_size.shape[3]

        return center_slice_helper(layer, diff_y, diff_x, h_end, w_end)

    @torch.jit.script_method
    def forward(self, x, bridge):
        crop1 = self.center_crop(bridge, x)
        return crop1

m = M()
</code></pre>

username: 111257
cooked: <p>Hello，I’ve been having this problem lately，How did you solve it？</p>