Torch.nonzero() leads to crash in PyTorch 1.2

Beinan_Wang: <p>The code snippet works fine in 1.1 but causes an error in 1.2:</p>
<blockquote>
<p>p = F.softmax(x, dim=1)<br>
m = y != self.ignore_index<br>
t = F.one_hot((y * m.byte()).long(), num_classes=self.num_classes).byte().permute(0,3,1,2)<br>
i = (p * (t * m.unsqueeze(1).byte()).float()).sum((0,2,3))<br>
u = ((p + t.float()) * m.unsqueeze(1).float()).sum((0,2,3)) - i<br>
v = u.nonzero()<br>
return -((i[v] / u[v]).mean()).log()</p>
</blockquote>
<p>I get the error message below:</p>
<blockquote>
<p>RuntimeError: range.second - range.first == t.size() INTERNAL ASSERT FAILED at /pytorch/torch/csrc/autograd/generated/Functions.cpp:55, please report a bug to PyTorch. inconsistent range for TensorList output</p>
</blockquote>
<p>I had to remove the usage of nonzero() like below to make the code work:</p>
<blockquote>
<p>p = F.softmax(x, dim=1)<br>
m = y != self.ignore_index<br>
t = F.one_hot((y * m.byte()).long(), num_classes=self.num_classes).byte().permute(0,3,1,2)<br>
i = (p * (t * m.unsqueeze(1).byte()).float()).sum((0,2,3))<br>
u = ((p + t.float()) * m.unsqueeze(1).float()).sum((0,2,3)) - i<br>
return -((i / u).mean()).log()</p>
</blockquote>
<p>I have two questions:<br>
(1) Why does the runtime error happen in 1.2 but not in 1.1?<br>
(2) By not using nonzero to prevent division by zero I have numeric instability in theory. However, in really the number that comes out from a softmax operation should not reach zero because the input can’t really reach negative infinity right?</p>

Nikronic: <p>Hi,</p>
<p>about the first question, could you please provide <code>x</code> and <code>y</code> to let me reproduce the issue?<br>
It would be great too if you show the specs of your environment by running<br>
<code>python</code> <a href="https://raw.githubusercontent.com/pytorch/pytorch/master/torch/utils/collect_env.py" rel="nofollow noopener">collect_env.py</a></p>
<p>And about the second question, in practice I think you are right, you should not get such a number in your network, so softmax would never output zero.</p>
<p>Good luck<br>
Nik</p>

Beinan_Wang: <p>The code snippet comes from a loss function for a semantic segmentation network. I modified it a little so it can reproduce the same runtime error alone:</p>
<blockquote>
<p>import torch<br>
import torch.nn as nn<br>
import torch.nn.functional as F</p>
<p>def loss_func(p, y, ignore_index=255, num_classes=19):<br>
&nbsp;&nbsp;&nbsp;&nbsp; p = F.softmax(x, dim=1)<br>
&nbsp;&nbsp;&nbsp;&nbsp; m = y != ignore_index<br>
&nbsp;&nbsp;&nbsp;&nbsp; t = F.one_hot((y * m.byte()).long(), num_classes=num_classes).byte().permute(0,3,1,2)<br>
&nbsp;&nbsp;&nbsp;&nbsp; i = (p * (t * m.unsqueeze(1).byte()).float()).sum((0,2,3))<br>
&nbsp;&nbsp;&nbsp;&nbsp; u = ((p + t.float()) * m.unsqueeze(1).float()).sum((0,2,3)) - i<br>
&nbsp;&nbsp;&nbsp;&nbsp; v = u.nonzero()<br>
&nbsp;&nbsp;&nbsp;&nbsp; return -((i[v] / u[v]).mean()).log()</p>
<p>x = torch.rand(1,19,1024,2048, requires_grad=True).to(‘cuda’).float()<br>
y = torch.randint(0, 18, (1,1024,2048)).to(‘cuda’).byte()<br>
y[0][0][0] = 255</p>
<p>loss = loss_func(x, y)<br>
loss.backward()</p>
</blockquote>
<p>Below is the output from collect_env.py:</p>
<blockquote>
<p>Collecting environment information…<br>
PyTorch version: 1.2.0<br>
Is debug build: No<br>
CUDA used to build PyTorch: 10.0.130</p>
<p>OS: Ubuntu 18.04.2 LTS<br>
GCC version: (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0<br>
CMake version: version 3.13.1</p>
<p>Python version: 3.6<br>
Is CUDA available: Yes<br>
CUDA runtime version: 10.0.130<br>
GPU models and configuration: GPU 0: GeForce GTX 1080 Ti<br>
Nvidia driver version: 415.27<br>
cuDNN version: /usr/local/cuda-10.0/targets/x86_64-linux/lib/libcudnn.so.7.5.0</p>
<p>Versions of relevant libraries:<br>
[pip3] efficientnet-pytorch==0.4.0<br>
[pip3] numpy==1.16.4<br>
[pip3] torch==1.2.0<br>
[pip3] torchfile==0.1.0<br>
[pip3] torchsummary==1.5.1<br>
[pip3] torchvision==0.4.0<br>
[conda] Could not collect</p>
</blockquote>

Nikronic: <p>Actually, If you do not use backward, and just try to use forward feed, <code>nonzero()</code> won’t throw any errors. The issue is related to <code>backward</code> semantics. Currently, I cannot really figure out what is happening. Could you create and issue in <a href="https://github.com/pytorch/pytorch/issues" rel="nofollow noopener">pytorch github page</a>?</p>
<p>By the way, you can use code format in forum by putting ``` at the start and the end of your code section.</p>

Beinan_Wang: <p>Thanks, I created an issue.</p>