[JIT] [Mobile] Wrong substitution of aten::to

zetyquickly: <p>Hello again,</p>
<p>As I’ve introduced here (<a href="https://discuss.pytorch.org/t/jit-scripted-model-aten-to-failed-on-mobile/60759" class="inline-onebox">[JIT] Scripted model aten::to failed on Mobile</a>) there might be a bug inside TorchScript scripting or inside mobile library.<br>
I’ve found out that any <code>aten::to</code> scripted from Python code to JIT comes with extra parameter inside function call. Let me show an example, here is python code and and the place where interpreter finds an error</p>
<pre><code class="lang-auto">Compiled from code at /root/anaconda2/envs/pytorch-nightly/lib/python3.7/site-packages/torchvision/ops/boxes.py:68:14
            in decreasing order of scores
        """
        if boxes.numel() == 0:
            return torch.empty((0,), dtype=torch.int64, device=boxes.device)
        # strategy: in order to perform NMS independently per class.
        # we add an offset to all the boxes. The offset is dependent
        # only on the class idx, and is large enough so that boxes
        # from different classes do not overlap
        max_coordinate = boxes.max()
        offsets = idxs.to(boxes) * (max_coordinate + 1)
                  ~~~~~~~ &lt;--- HERE
        boxes_for_nms = boxes + offsets[:, None]
        keep = nms(boxes_for_nms, scores, iou_threshold)
        return keep
</code></pre>
<p>here’s what is wrong</p>
<pre><code class="lang-auto">Arguments for call are not valid.
    The following operator variants are available:
      
      aten::to.other(Tensor self, Tensor other, bool non_blocking=False, bool copy=False) -&gt; (Tensor):
      Expected at most 4 arguments but found 5 positional arguments.
      
      aten::to.dtype(Tensor self, int dtype, bool non_blocking=False, bool copy=False) -&gt; (Tensor):
      Expected at most 4 arguments but found 5 positional arguments.
      
      aten::to.device(Tensor self, Device device, int dtype, bool non_blocking=False, bool copy=False) -&gt; (Tensor):
      Expected a value of type 'Device' for argument 'device' but instead found type 'Tensor'.
      
      aten::to.dtype_layout(Tensor self, *, int dtype, int layout, Device device, bool pin_memory=False, bool non_blocking=False, bool copy=False) -&gt; (Tensor):
      Argument dtype not provided.
      
      aten::to(Tensor(a) self, Device? device, int? dtype=None, bool non_blocking=False, bool copy=False) -&gt; (Tensor(b|a)):
      Expected a value of type 'Optional[Device]' for argument 'device' but instead found type 'Tensor'.
      
      aten::to(Tensor(a) self, int? dtype=None, bool non_blocking=False, bool copy=False) -&gt; (Tensor(b|a)):
      Expected a value of type 'Optional[int]' for argument 'dtype' but instead found type 'Tensor'.
      
      aten::to(Tensor(a) self, bool non_blocking=False, bool copy=False) -&gt; (Tensor(b|a)):
      Expected a value of type 'bool' for argument 'non_blocking' but instead found type 'Tensor'.
    
    The original call is:
    at code/__torch__/torchvision/ops/boxes.py:16:9
      if torch.eq(torch.numel(boxes), 0):
        _3 = ops.prim.device(boxes)
        _1, _2 = True, torch.empty([0], dtype=4, layout=None, device=_3, pin_memory=None, memory_format=None)
      else:
        _1, _2 = False, _0
      if _1:
        _4 = _2
      else:
        max_coordinate = torch.max(boxes)
        _5 = torch.to(idxs, boxes, False, False, None)
             ~~~~~~~~ &lt;--- HERE
        offsets = torch.mul(_5, torch.add(max_coordinate, 1, 1))
        _6 = torch.slice(offsets, 0, 0, 9223372036854775807, 1)
        boxes_for_nms = torch.add(boxes, torch.unsqueeze(_6, 1), alpha=1)
        _4 = __torch__.torchvision.ops.boxes.nms(boxes_for_nms, scores, iou_threshold, )
      return _4
</code></pre>
<p>For some reason JIT provided code with extraneous parameter <code>None</code>. It will do it even we provide all named parameters to define which overriden function should be used.</p>

zetyquickly: <p>Notice that please. There’s high probability of bug. TorchScript produces <code>aten::to</code> with extra <code>None</code> parameter</p>

zetyquickly: <p>I am still coming across this issue. TorchScript <code>script</code> produces redundant <code>None</code> parameter</p>

David_Reiss: <p>Sorry for the late reply.  In the future, please post in the “mobile” category so the mobile developers will see them.  Do you have a script or notebook that we can use to reproduce this issue?</p>