<h2 dir="auto">RuntimeError: isTensor() ASSERT FAILED <g-emoji class="g-emoji" alias="bug" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f41b.png">🐛</g-emoji></h2>
<p dir="auto">We want to provide jitted versions of <a href="https://github.com/arraiyopensource/kornia">Kornia</a> functions and by doing so during the tests we found the following issue when tracing a <code class="notranslate">nn.Module</code> that internally calls a scripted version of a simple function of the library:</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="(base) 1 edgar@edgar-ThinkPad-T460:~$ python test_jit.py
test_jit.py:17: TracerWarning: Converting a tensor to a Python integer might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  return op_script(input, height, width)
Traceback (most recent call last):
  File &quot;test_jit.py&quot;, line 21, in &lt;module&gt;
    op_traced = torch.jit.trace(my_test_op, torch.rand(1,2,4,4))
  File &quot;/home/edgar/software/kornia/.dev_env/lib/python3.7/site-packages/torch/jit/__init__.py&quot;, line 688, in trace
    var_lookup_fn, _force_outplace)
  File &quot;/home/edgar/software/kornia/.dev_env/lib/python3.7/site-packages/torch/nn/modules/module.py&quot;, line 491, in __call__
    result = self._slow_forward(*input, **kwargs)
  File &quot;/home/edgar/software/kornia/.dev_env/lib/python3.7/site-packages/torch/nn/modules/module.py&quot;, line 481, in _slow_forward
    result = self.forward(*input, **kwargs)
  File &quot;test_jit.py&quot;, line 17, in forward
    return op_script(input, height, width)
RuntimeError: isTensor() ASSERT FAILED at /opt/conda/conda-bld/pytorch_1556653114079/work/aten/src/ATen/core/ivalue.h:209, please report a bug to PyTorch. (toTensor at /opt/conda/conda-bld/pytorch_1556653114079/work/aten/src/ATen/core/ivalue.h:209)
frame #0: c10::Error::Error(c10::SourceLocation, std::string const&amp;) + 0x45 (0x7f9d9e43ddc5 in /home/edgar/software/kornia/.dev_env/lib/python3.7/site-packages/torch/lib/libc10.so)
frame #1: &lt;unknown function&gt; + 0x96437d (0x7f9d9733137d in /home/edgar/software/kornia/.dev_env/lib/python3.7/site-packages/torch/lib/libtorch.so.1)
frame #2: torch::jit::tracer::getNestedValueTrace(c10::IValue const&amp;) + 0x41 (0x7f9d97593eb1 in /home/edgar/software/kornia/.dev_env/lib/python3.7/site-packages/torch/lib/libtorch.so.1)
frame #3: &lt;unknown function&gt; + 0xa5af9b (0x7f9d97427f9b in /home/edgar/software/kornia/.dev_env/lib/python3.7/site-packages/torch/lib/libtorch.so.1)
frame #4: &lt;unknown function&gt; + 0xa5b15b (0x7f9d9742815b in /home/edgar/software/kornia/.dev_env/lib/python3.7/site-packages/torch/lib/libtorch.so.1)
frame #5: &lt;unknown function&gt; + 0x458bf3 (0x7f9dcd772bf3 in /home/edgar/software/kornia/.dev_env/lib/python3.7/site-packages/torch/lib/libtorch_python.so)
frame #6: &lt;unknown function&gt; + 0x12d07a (0x7f9dcd44707a in /home/edgar/software/kornia/.dev_env/lib/python3.7/site-packages/torch/lib/libtorch_python.so)
&lt;omitting python frames&gt;
frame #28: &lt;unknown function&gt; + 0x3e73e6 (0x7f9dcd7013e6 in /home/edgar/software/kornia/.dev_env/lib/python3.7/site-packages/torch/lib/libtorch_python.so)
frame #29: &lt;unknown function&gt; + 0x44830b (0x7f9dcd76230b in /home/edgar/software/kornia/.dev_env/lib/python3.7/site-packages/torch/lib/libtorch_python.so)
frame #30: &lt;unknown function&gt; + 0x45b1c4 (0x7f9dcd7751c4 in /home/edgar/software/kornia/.dev_env/lib/python3.7/site-packages/torch/lib/libtorch_python.so)
frame #31: &lt;unknown function&gt; + 0x12d07a (0x7f9dcd44707a in /home/edgar/software/kornia/.dev_env/lib/python3.7/site-packages/torch/lib/libtorch_python.so)
frame #46: __libc_start_main + 0xe7 (0x7f9ddaaeab97 in /lib/x86_64-linux-gnu/libc.so.6)"><pre class="notranslate">(base) 1 edgar@edgar-ThinkPad-T460:<span class="pl-k">~</span>$ python test_jit.py
test_jit.py:17: TracerWarning: Converting a tensor to a Python integer might cause the trace to be incorrect. We can<span class="pl-s"><span class="pl-pds">'</span>t record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!</span>
<span class="pl-s">  return op_script(input, height, width)</span>
<span class="pl-s">Traceback (most recent call last):</span>
<span class="pl-s">  File "test_jit.py", line 21, in &lt;module&gt;</span>
<span class="pl-s">    op_traced = torch.jit.trace(my_test_op, torch.rand(1,2,4,4))</span>
<span class="pl-s">  File "/home/edgar/software/kornia/.dev_env/lib/python3.7/site-packages/torch/jit/__init__.py", line 688, in trace</span>
<span class="pl-s">    var_lookup_fn, _force_outplace)</span>
<span class="pl-s">  File "/home/edgar/software/kornia/.dev_env/lib/python3.7/site-packages/torch/nn/modules/module.py", line 491, in __call__</span>
<span class="pl-s">    result = self._slow_forward(*input, **kwargs)</span>
<span class="pl-s">  File "/home/edgar/software/kornia/.dev_env/lib/python3.7/site-packages/torch/nn/modules/module.py", line 481, in _slow_forward</span>
<span class="pl-s">    result = self.forward(*input, **kwargs)</span>
<span class="pl-s">  File "test_jit.py", line 17, in forward</span>
<span class="pl-s">    return op_script(input, height, width)</span>
<span class="pl-s">RuntimeError: isTensor() ASSERT FAILED at /opt/conda/conda-bld/pytorch_1556653114079/work/aten/src/ATen/core/ivalue.h:209, please report a bug to PyTorch. (toTensor at /opt/conda/conda-bld/pytorch_1556653114079/work/aten/src/ATen/core/ivalue.h:209)</span>
<span class="pl-s">frame #0: c10::Error::Error(c10::SourceLocation, std::string const&amp;) + 0x45 (0x7f9d9e43ddc5 in /home/edgar/software/kornia/.dev_env/lib/python3.7/site-packages/torch/lib/libc10.so)</span>
<span class="pl-s">frame #1: &lt;unknown function&gt; + 0x96437d (0x7f9d9733137d in /home/edgar/software/kornia/.dev_env/lib/python3.7/site-packages/torch/lib/libtorch.so.1)</span>
<span class="pl-s">frame #2: torch::jit::tracer::getNestedValueTrace(c10::IValue const&amp;) + 0x41 (0x7f9d97593eb1 in /home/edgar/software/kornia/.dev_env/lib/python3.7/site-packages/torch/lib/libtorch.so.1)</span>
<span class="pl-s">frame #3: &lt;unknown function&gt; + 0xa5af9b (0x7f9d97427f9b in /home/edgar/software/kornia/.dev_env/lib/python3.7/site-packages/torch/lib/libtorch.so.1)</span>
<span class="pl-s">frame #4: &lt;unknown function&gt; + 0xa5b15b (0x7f9d9742815b in /home/edgar/software/kornia/.dev_env/lib/python3.7/site-packages/torch/lib/libtorch.so.1)</span>
<span class="pl-s">frame #5: &lt;unknown function&gt; + 0x458bf3 (0x7f9dcd772bf3 in /home/edgar/software/kornia/.dev_env/lib/python3.7/site-packages/torch/lib/libtorch_python.so)</span>
<span class="pl-s">frame #6: &lt;unknown function&gt; + 0x12d07a (0x7f9dcd44707a in /home/edgar/software/kornia/.dev_env/lib/python3.7/site-packages/torch/lib/libtorch_python.so)</span>
<span class="pl-s">&lt;omitting python frames&gt;</span>
<span class="pl-s">frame #28: &lt;unknown function&gt; + 0x3e73e6 (0x7f9dcd7013e6 in /home/edgar/software/kornia/.dev_env/lib/python3.7/site-packages/torch/lib/libtorch_python.so)</span>
<span class="pl-s">frame #29: &lt;unknown function&gt; + 0x44830b (0x7f9dcd76230b in /home/edgar/software/kornia/.dev_env/lib/python3.7/site-packages/torch/lib/libtorch_python.so)</span>
<span class="pl-s">frame #30: &lt;unknown function&gt; + 0x45b1c4 (0x7f9dcd7751c4 in /home/edgar/software/kornia/.dev_env/lib/python3.7/site-packages/torch/lib/libtorch_python.so)</span>
<span class="pl-s">frame #31: &lt;unknown function&gt; + 0x12d07a (0x7f9dcd44707a in /home/edgar/software/kornia/.dev_env/lib/python3.7/site-packages/torch/lib/libtorch_python.so)</span>
<span class="pl-s">frame #46: __libc_start_main + 0xe7 (0x7f9ddaaeab97 in /lib/x86_64-linux-gnu/libc.so.6)</span></pre></div>
<h2 dir="auto">To Reproduce</h2>
<p dir="auto">Steps to reproduce the behavior:</p>
<ol dir="auto">
<li>Install kornia: <code class="notranslate">pip install git+https://github.com/arraiyopensource/kornia</code></li>
<li>Run the following script:</li>
</ol>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import torch
import torch.nn as nn
 
import kornia
 
@torch.jit.script
def op_script(input: torch.Tensor, height: int,
              width: int) -&gt; torch.Tensor:
    return kornia.normalize_pixel_coordinates(input, height, width)
 
class MyTestModule(nn.Module):
    def __init__(self):
        super(MyTestModule, self).__init__()
 
    def forward(self, input):
        height, width = input.shape[-2:]
        return op_script(input, height, width)
 
 
my_test_op = MyTestModule()
op_traced = torch.jit.trace(my_test_op, torch.rand(1,2,4,4))"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">torch</span>
<span class="pl-k">import</span> <span class="pl-s1">torch</span>.<span class="pl-s1">nn</span> <span class="pl-k">as</span> <span class="pl-s1">nn</span>
 
<span class="pl-k">import</span> <span class="pl-s1">kornia</span>
 
<span class="pl-en">@<span class="pl-s1">torch</span>.<span class="pl-s1">jit</span>.<span class="pl-s1">script</span></span>
<span class="pl-k">def</span> <span class="pl-en">op_script</span>(<span class="pl-s1">input</span>: <span class="pl-s1">torch</span>.<span class="pl-v">Tensor</span>, <span class="pl-s1">height</span>: <span class="pl-s1">int</span>,
              <span class="pl-s1">width</span>: <span class="pl-s1">int</span>) <span class="pl-c1">-&gt;</span> <span class="pl-s1">torch</span>.<span class="pl-v">Tensor</span>:
    <span class="pl-k">return</span> <span class="pl-s1">kornia</span>.<span class="pl-en">normalize_pixel_coordinates</span>(<span class="pl-s1">input</span>, <span class="pl-s1">height</span>, <span class="pl-s1">width</span>)
 
<span class="pl-k">class</span> <span class="pl-v">MyTestModule</span>(<span class="pl-s1">nn</span>.<span class="pl-v">Module</span>):
    <span class="pl-k">def</span> <span class="pl-en">__init__</span>(<span class="pl-s1">self</span>):
        <span class="pl-en">super</span>(<span class="pl-v">MyTestModule</span>, <span class="pl-s1">self</span>).<span class="pl-en">__init__</span>()
 
    <span class="pl-k">def</span> <span class="pl-en">forward</span>(<span class="pl-s1">self</span>, <span class="pl-s1">input</span>):
        <span class="pl-s1">height</span>, <span class="pl-s1">width</span> <span class="pl-c1">=</span> <span class="pl-s1">input</span>.<span class="pl-s1">shape</span>[<span class="pl-c1">-</span><span class="pl-c1">2</span>:]
        <span class="pl-k">return</span> <span class="pl-en">op_script</span>(<span class="pl-s1">input</span>, <span class="pl-s1">height</span>, <span class="pl-s1">width</span>)
 
 
<span class="pl-s1">my_test_op</span> <span class="pl-c1">=</span> <span class="pl-v">MyTestModule</span>()
<span class="pl-s1">op_traced</span> <span class="pl-c1">=</span> <span class="pl-s1">torch</span>.<span class="pl-s1">jit</span>.<span class="pl-en">trace</span>(<span class="pl-s1">my_test_op</span>, <span class="pl-s1">torch</span>.<span class="pl-en">rand</span>(<span class="pl-c1">1</span>,<span class="pl-c1">2</span>,<span class="pl-c1">4</span>,<span class="pl-c1">4</span>))</pre></div>

<h2 dir="auto">Expected behavior</h2>

<h2 dir="auto">Environment</h2>
<p dir="auto">PyTorch version: 1.1.0<br>
Is debug build: No<br>
CUDA used to build PyTorch: 10.0.130</p>
<p dir="auto">OS: Ubuntu 18.04.2 LTS<br>
GCC version: (Ubuntu 7.4.0-1ubuntu1~18.04) 7.4.0<br>
CMake version: version 3.10.2</p>
<p dir="auto">Python version: 3.7<br>
Is CUDA available: No<br>
CUDA runtime version: No CUDA<br>
GPU models and configuration: No CUDA<br>
Nvidia driver version: No CUDA<br>
cuDNN version: No CUDA</p>
<p dir="auto">Versions of relevant libraries:<br>
[pip3] numpy==1.16.2<br>
[pip3] torchgeometry==0.1.0<br>
[conda] mkl                       2019.3                      199<br>
[conda] mkl_fft                   1.0.13           py37h516909a_1    conda-forge<br>
[conda] mkl_random                1.0.4            py37hf2d7682_0    conda-forge<br>
[conda] pytorch                   1.1.0           py3.7_cuda10.0.130_cudnn7.5.1_0    pytorch<br>
[conda] torchvision               0.3.0           py37_cu10.0.130_1    pytorch</p>
<h2 dir="auto">Additional context</h2>
<h2 dir="auto"><g-emoji class="g-emoji" alias="bug" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f41b.png">🐛</g-emoji> Bug</h2>
<p dir="auto">I don't know which caused this problem.</p>
<h2 dir="auto">To Reproduce</h2>
<p dir="auto">Steps to reproduce the behavior:</p>
<ol dir="auto">
<li></li>
</ol>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.jit


@torch.jit.script
def resize_ref(x, shortpoint, method: str='bilinear', align_corners: bool=True):
    &quot;&quot;&quot;
    :type x: torch.Tensor
    :type shortpoint: torch.Tensor
    :type method: str
    :type align_corners: bool
    &quot;&quot;&quot;
    hw = shortpoint.shape[2:4]
    ihw = x.shape[2:4]
    if hw != ihw:
        x = F.interpolate(x, hw, mode=method, align_corners=align_corners)
    return x


class Net(nn.Module):
    def __init__(self):
        super().__init__()

    def forward(self, x):
        x2 = F.interpolate(x, scale_factor=2)
        y = resize_ref(x, x2)
        return y


a = torch.rand(1, 3, 6, 6)
net = Net()
net = torch.jit.trace(net, a)
b = net(a)
print(b.shape)
"><pre class="notranslate"><code class="notranslate">import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.jit


@torch.jit.script
def resize_ref(x, shortpoint, method: str='bilinear', align_corners: bool=True):
    """
    :type x: torch.Tensor
    :type shortpoint: torch.Tensor
    :type method: str
    :type align_corners: bool
    """
    hw = shortpoint.shape[2:4]
    ihw = x.shape[2:4]
    if hw != ihw:
        x = F.interpolate(x, hw, mode=method, align_corners=align_corners)
    return x


class Net(nn.Module):
    def __init__(self):
        super().__init__()

    def forward(self, x):
        x2 = F.interpolate(x, scale_factor=2)
        y = resize_ref(x, x2)
        return y


a = torch.rand(1, 3, 6, 6)
net = Net()
net = torch.jit.trace(net, a)
b = net(a)
print(b.shape)

</code></pre></div>
<h2 dir="auto">Expected behavior</h2>

<h2 dir="auto">Environment</h2>
<p dir="auto">Please copy and paste the output from our<br>
<a href="https://raw.githubusercontent.com/pytorch/pytorch/master/torch/utils/collect_env.py" rel="nofollow">environment collection script</a><br>
(or fill out the checklist below manually).</p>
<p dir="auto">You can get the script and run it with:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="wget https://raw.githubusercontent.com/pytorch/pytorch/master/torch/utils/collect_env.py
# For security purposes, please check the contents of collect_env.py before running it.
python collect_env.py"><pre class="notranslate"><code class="notranslate">wget https://raw.githubusercontent.com/pytorch/pytorch/master/torch/utils/collect_env.py
# For security purposes, please check the contents of collect_env.py before running it.
python collect_env.py
</code></pre></div>
<ul dir="auto">
<li>PyTorch Version (e.g., 1.0):1.1.0</li>
<li>OS (e.g., Linux):win10</li>
<li>How you installed PyTorch (<code class="notranslate">conda</code>, <code class="notranslate">pip</code>, source):pip</li>
<li>Build command you used (if compiling from source):</li>
<li>Python version:3.7</li>
<li>CUDA/cuDNN version:10</li>
<li>GPU models and configuration:gtx970m</li>
<li>Any other relevant information:</li>
</ul>
<h2 dir="auto">Additional context</h2>