Very weird behavior when converting to torch.tensor

Rafay_Khan: <p>I’m pretty new to PyTorch so, please excuse me if this question is too remedial. I have the following code for inference of my trained model which takes an image, does some pre-processing on it, converts to a tensor and finally performs a forward pass through the network.</p>
<pre><code class="lang-auto">   img = np.array(Image.open(img_path))

        orig_size = img.shape[:2][::-1]

        img_inp = torch.tensor(prepare_img(img).transpose(2, 0, 1)[None]).float()
        if has_cuda:
            img_inp = img_inp.cuda()

        segm = net(img_inp)[0].data.cpu().numpy().transpose(1, 2, 0)
</code></pre>
<p>I have successfully trained this segmentation model on my desktop machine. The inference code runs perfectly on it, but when I clone it to my laptop it gives me the following error:</p>
<pre><code class="lang-auto">       img_inp = torch.tensor(prepare_img(img).transpose(2, 0, 1)[None]).float()
RuntimeError: unsupported operation: more than one element of the written-to tensor refers to a single memory location. Please clone() the tensor before performing the operation.
</code></pre>
<p>I can bypass this error on my laptop by passing the result of <code>prepare_img</code> as a copy but that leads to memory leakages.</p>
<p>PS. On my Laptop I have built PyTorch from source for GPU compute and the torch version is <code>1.5.0a0+55c382e</code>. I have a hunch that this is the problem.  My desktop machine has PyTorch installed simply from anaconda’s repo.</p>
<p>Any help on this will be greatly appreciated. Thanks</p>

albanD: <p>Hi,</p>
<p>The problem is most likely that you try ot modify inplace a Tensor that does not support it. These checks were added fairly recently to prevent users from seeing behavior that are usually unexpected.</p>
<p>Could you give a small code sample that reproduces the error please?</p>

Rafay_Khan: <p>Hey, <a class="mention" href="/u/alband">@albanD</a> thanks for the reply. You can check out the code in this <a href="https://github.com/DrSleep/light-weight-refinenet" rel="nofollow noopener">repo</a>. This is what I’m trying to re-purpose. Try running any one of the example notebooks. on PyTorch <code>1.5.0a0+55c382e</code></p>

Rafay_Khan: <p>Seems like the main problem is when the <code>torch.tensor</code> is converted to <code>float</code>. When it’s removed at least the  Run-time error is gone(appears instead when tensor is converted to <code>.cuda()</code>) but now how do I change to <code>torch.float</code> dtype?(already tried <code>.type(torch.float)</code>)</p>

G.M: <p>I think <code>.float</code> only works for scalarr, it’ll return the value as a python float. If u want to convert types for tensors, try <code>a = a.to(dtype=tc.flaot)</code></p>

Rafay_Khan: <p><a class="mention" href="/u/g.m">@G.M</a> Thanks for the reply. Still the same problem:</p>
<pre><code class="lang-auto">  img_inp = torch.tensor(prepare_img(img).transpose(2, 0, 1)[None]).to(dtype=torch.float)
RuntimeError: unsupported operation: more than one element of the written-to tensor refers to a single memory location. Please clone() the tensor before performing the operation.
</code></pre>

Rafay_Khan: <p>How come this works?</p>
<pre><code class="lang-auto">import numpy as np
a = np.ones(5)
b = torch.from_numpy(a).float()   # because we change the dtype 'b' contains a copy of  'a' 
np.add(a, 1, out=a) 

print(a, a.dtype)
&gt;&gt;&gt; [2. 2. 2. 2. 2.] float64

print(b, b.dtype)
&gt;&gt;&gt; tensor([1., 1., 1., 1., 1.]) torch.float32
</code></pre>
<p>Secondly, this works as expected:</p>
<pre><code class="lang-auto">import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a, 1, out=a) # changed to np array, changes the torch.tensor

print(a, a.dtype)
&gt;&gt;&gt; [2. 2. 2. 2. 2.] float64

print(b, )
&gt;&gt;&gt; tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
</code></pre>

G.M: <p>Do u have any in-place operations or operations like <code>expand</code> in ur code? I’ve experienced the issue with the usage of <code>expand</code>.</p>

Rafay_Khan: <p>Nope. Can’t find any <code>expand</code> commands. You can check out the <a href="https://github.com/RafayAK/light-weight-refinenet/blob/master/examples/notebooks/NYU.ipynb" rel="nofollow noopener">repo here</a></p>

G.M: <aside class="quote no-group" data-username="Rafay_Khan" data-post="1" data-topic="68389">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/rafay_khan/48/22389_2.png" class="avatar"> Rafay_Khan:</div>
<blockquote>
<pre><code class="lang-auto">RuntimeError: unsupported operation: more than one element of the written-to tensor refers to a single memory location. Please clone() the tensor before performing the operation.
</code></pre>
</blockquote>
</aside>
<p>Can u try creating the tensor like this?</p>
<pre><code class="lang-auto">img_inp = torch.from_numpy(prepare_img(img).transpose(2, 0, 1)[None]).to(dtype=torch.float,device=UR_DEVICE)
</code></pre>

albanD: <p>Hi,</p>
<p>The issue comes from the <code>[None]</code> I think. You change this line to first convert to a Tensor then <code>.unsqueeze(0)</code> to get the exact same result:</p>
<pre><code class="lang-auto">img_inp = torch.tensor(prepare_img(img).transpose(2, 0, 1)).float().unsqueeze(0)
</code></pre>

Rafay_Khan: <aside class="quote no-group" data-username="albanD" data-post="11" data-topic="68389">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/48/215_2.png" class="avatar"> albanD:</div>
<blockquote>
<p>repare_img(img).transpose(2,</p>
</blockquote>
</aside>
<p>Thanks a bung <a class="mention" href="/u/alband">@albanD</a> got it working</p>