Assertion `input_val >= zero && input_val <= one` failed

Junhao_Yan: <p>Hi, all<br>
Recently, I changed the cpu and motherboard of my PC. But when I tried to run the training code, I encountered this problem. I haven’t changed any codes on my scripts. So I’m wondering whether it’s caused by the CPU(Ryzen 7 3800xt)<br>
The “Traceback (most recent call last):” showed below changes everytime, sometime it happens on Conv, and sometime Batchnorm. But the assertion error happens everytime.<br>
I have tried to fix it by myself for several days. But I cannot find much information about this error. Thus, if anyone can help, I would really appreciate it.</p>
<p>/opt/conda/conda-bld/pytorch_1607370193460/work/aten/src/ATen/native/cuda/Loss.cu:102: operator(): block: [0,0,0], thread: [0,0,0] Assertion <code>input_val &gt;= zero &amp;&amp; input_val &lt;= one</code> failed.<br>
/opt/conda/conda-bld/pytorch_1607370193460/work/aten/src/ATen/native/cuda/Loss.cu:102: operator(): block: [0,0,0], thread: [1,0,0] Assertion <code>input_val &gt;= zero &amp;&amp; input_val &lt;= one</code> failed.<br>
/opt/conda/conda-bld/pytorch_1607370193460/work/aten/src/ATen/native/cuda/Loss.cu:102: operator(): block: [0,0,0], thread: [2,0,0] Assertion <code>input_val &gt;= zero &amp;&amp; input_val &lt;= one</code> failed.<br>
/opt/conda/conda-bld/pytorch_1607370193460/work/aten/src/ATen/native/cuda/Loss.cu:102: operator(): block: [0,0,0], thread: [3,0,0] Assertion <code>input_val &gt;= zero &amp;&amp; input_val &lt;= one</code> failed.<br>
/opt/conda/conda-bld/pytorch_1607370193460/work/aten/src/ATen/native/cuda/Loss.cu:102: operator(): block: [0,0,0], thread: [4,0,0] Assertion <code>input_val &gt;= zero &amp;&amp; input_val &lt;= one</code> failed.<br>
/opt/conda/conda-bld/pytorch_1607370193460/work/aten/src/ATen/native/cuda/Loss.cu:102: operator(): block: [0,0,0], thread: [5,0,0] Assertion <code>input_val &gt;= zero &amp;&amp; input_val &lt;= one</code> failed.<br>
/opt/conda/conda-bld/pytorch_1607370193460/work/aten/src/ATen/native/cuda/Loss.cu:102: operator(): block: [0,0,0], thread: [6,0,0] Assertion <code>input_val &gt;= zero &amp;&amp; input_val &lt;= one</code> failed.<br>
/opt/conda/conda-bld/pytorch_1607370193460/work/aten/src/ATen/native/cuda/Loss.cu:102: operator(): block: [0,0,0], thread: [7,0,0] Assertion <code>input_val &gt;= zero &amp;&amp; input_val &lt;= one</code> failed.<br>
Traceback (most recent call last):<br>
File “/home/jhyan/Scripts/PYTHON_PROJECT/FPN/train.py”, line 52, in <br>
main()<br>
File “/home/jhyan/Scripts/PYTHON_PROJECT/FPN/train.py”, line 36, in main<br>
operate.train()<br>
File “/home/jhyan/Scripts/PYTHON_PROJECT/FPN/operate.py”, line 90, in train<br>
self.opt_encoder.zero_grad()<br>
File “/home/jhyan/anaconda3/envs/domian-ada/lib/python3.6/site-packages/torch/optim/optimizer.py”, line 192, in zero_grad<br>
p.grad.zero_()</p>

ptrblck: <p>Based on the stack trace, <a href="https://github.com/pytorch/pytorch/blob/cd608fe59b70fa7cafb07110096b2e023a8b6e9c/aten/src/ATen/native/cuda/Loss.cu#L91">this check</a> is failing in <code>binary_cross_entropy_out_cuda</code> and points towards an invalid target. Could you check the min. and max. values of your current target tensor and make sure its values are in the range <code>[0, 1]</code>?</p>

Junhao_Yan: <p>Hi, ptrblck<br>
I use BCE for optimizing my GAN, and I create the target by myself with the following code so I pretty sure it’s within the range[0, 1]</p>
<pre><code class="lang-auto">                # for real samples
                if idx % 3 == 0:
                    label = torch.full([batch_size, ], 0.0, device=self.device)
                else:
                    label = torch.full([batch_size, ], 1.0, device=self.device)
                # for fake samples
                if idx % 3 == 0:
                    label.fill_(1.0)
                else:
                    label.fill_(0.0)
</code></pre>
<p>However, when I was debugging, I found the network outputted [[[[nan…, …]]]] when the assertion occured. It’s caused by the one of the conv layers, I put a screen shot and code below:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/6/0/601ca7109f1be6d629791b4af0d896e24e5e3e61.png" data-download-href="https://discuss.pytorch.org/uploads/default/601ca7109f1be6d629791b4af0d896e24e5e3e61" title="Screenshot from 2020-12-31 01-45-15"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/6/0/601ca7109f1be6d629791b4af0d896e24e5e3e61_2_690x82.png" alt="Screenshot from 2020-12-31 01-45-15" data-base62-sha1="dIfdXarXdMu0RCpBkPwndsp642R" width="690" height="82" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/6/0/601ca7109f1be6d629791b4af0d896e24e5e3e61_2_690x82.png, https://discuss.pytorch.org/uploads/default/optimized/3X/6/0/601ca7109f1be6d629791b4af0d896e24e5e3e61_2_1035x123.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/6/0/601ca7109f1be6d629791b4af0d896e24e5e3e61_2_1380x164.png 2x" data-dominant-color="F1F1F1"><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">Screenshot from 2020-12-31 01-45-15</span><span class="informations">2182×262 39.6 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><br>
As you can see “out” is the output from that conv layer, and the input of that layer is “x”, which is the image and I normalize it to range [-1, 1].<br>
the code of the conv layer is:</p>
<pre><code class="lang-auto">        self.in_convs = nn.Sequential(nn.Conv2dh3, 32, kernel_size=3, stride=2, padding=1, bias=False),
                                      nn.BatchNorm2d(32),
                                      nn.ReLU(inplace=True),
                                      nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1, bias=False),
                                      nn.BatchNorm2d(64),
                                      nn.ReLU(inplace=True))
</code></pre>
<p>I don’t know what is going on as I think my code is right. And before I change my cpu and motherboard. everything worked fine. Thank you so much</p>

ptrblck: <p>Your are right. The check is for the input, not the target, so I was blind.<br>
The NaNs would also explain the failure of the check, so you would need to debug, where these NaNs are coming from.<br>
Anomaly detection is useful in case the NaNs were created in the backward pass and might be helpful to isolate the issue. Also, check the inputs to the model for invalid values via <code>torch.isfinite</code> just as a sanity check.<br>
If this doesn’t help, check the loss for NaNs and all parameters of the model to further narrow down which operation creates these values.</p>

Junhao_Yan: <p>Thanks for your help</p>

Junhao_Yan: <p>Thanks for your advices. I have detected the bug with the Anomaly detection and torch.isfinite<br>
It’s caused by the cv2.resize(). I use the bilinear interpolation mode to resize the input target. The range of the input to the cv2.resize() is normal, but after the resize operation, the range will beome very very large, which is over the range of float32. Thus, the torch.from_numpy() will return nan or inf.<br>
I replace the bilinear with other mode or use the other resize mthod(transform.resize from skimage). Then everything will run smooth. However, thank you very much</p>

ptrblck: <p>That’s a bit strange. Do you know why OpenCV’s resize operation is “overshooting” some values? I would assume that the bilinear interpolation creates values “between” already existing ones.</p>

Junhao_Yan: <p>I don’t know that. I cannot find any references related to this issue.<br>
I call isinfinte(np.max(depth_img)) before and after cv2.resize(), and the return is the value within the range and inf or -inf when the error occurs. It’s so strange. As what I said my code ran smoothly before I change the cpu and the motherboard. So I think it’s the problem of the cpu or motherboard<br>
the stranger thing is the error occurs again not at the first epoch but later. God dam, the bug seems to have intelligence.</p>

ptrblck: <p>This seems to be a tricky issue indeed.<br>
Since you are seeing this bug after changing the CPU and by calling into OpenCV operations, you could check, if a BLAS library might be creating this issue.<br>
I don’t know which operations the resize method is exactly using, but assume that numpy is used and then maybe MKL.<br>
As a quick test you could run your script with:</p>
<pre><code class="lang-auto">MKL_DEBUG_CPU_TYPE=5
</code></pre>
<p>to force MKL to use AVX2 on AMD CPUs and check, if this issue is still visible.</p>

Junhao_Yan: <p>Adding the MKL_DEBUG_CPU_TYPE=5 to my script seems to solve the issue. I need to run more epoch to validate it. LOL. thank you so much</p>

ptrblck: <p>Cool that this workaround seems to work. I’m also trying to isolate this issue a bit more, as I’ve already seen in in the past couple of weeks, so could you post the resize operation, which created the NaNs?<br>
Were you able to run this op in a loop until these invalid values were returned using the AMD CPU?</p>

Junhao_Yan: <p>The code creates this error is</p>
<pre><code class="lang-auto">depth_im = cv2.resize(depth_img, (self.output_size, self.output_size), interpolation=cv2.INTER_LINEAR)
</code></pre>
<p>I will try to run this op in the for loop and see whehter the error will happen or not</p>

Junhao_Yan: <p>I put the op into the for loop, and it does have problem. I used the following code</p>
<pre><code class="lang-auto">import torch
import numpy as np
from operate import check_isfinite


def main():
    while True:
        in_img = torch.randint(0, 255, [512, 512, 3])
        output = cv2.resize(np.asarray(in_img, dtype=np.float), (256, 256), interpolation=cv2.INTER_LINEAR)
        output = torch.from_numpy(output)
        max_output = torch.max(output)
        check_isfinite("max_output", max_output)
        assert torch.isfinite(max_output)


if __name__ == "__main__":
    main()

</code></pre>
<p>I also found that the error occurs randomly enen though I add torch.random.manual_seed(0). It’s so strange</p>
<p>and the part of the output is:<br>
params name: max_output, is_finite: True<br>
tensor(247.5000, dtype=torch.float64)<br>
params name: max_output, is_finite: True<br>
tensor(245., dtype=torch.float64)<br>
params name: max_output, is_finite: True<br>
tensor(inf, dtype=torch.float64)<br>
params name: max_output, is_finite: False<br>
Traceback (most recent call last):<br>
File “/home/jhyan/Scripts/PYTHON_PROJECT/FPN/test_opencv.py”, line 17, in <br>
main()<br>
File “/home/jhyan/Scripts/PYTHON_PROJECT/FPN/test_opencv.py”, line 13, in main<br>
assert torch.isfinite(max_output)<br>
AssertionError</p>

ptrblck: <p>Thanks for the test. Is this error raised without the MKL env var, with, or in both cases?</p>

Junhao_Yan: <p>I have to say in both cases. Last time I ran my program under MKL env, it worked. But today the error appeared again</p>

Junhao_Yan: <p>I change the float type to be np.float32(the original is np.float), it works.  I also try np.float64. the error will occur.</p>

Junhao_Yan: <p>it’s the problem of the cpu. I have already claimed an return</p>

chaslie: <p>hi ptrblk,</p>
<p>I am also getting this error in the Binary cross entropy criteria. I have checked the output and targets are all between 0 &amp; 1, which they are. I have also had a look at the and i cannot find any NaN values. Is there anything else that can cause this?</p>
<p>cheers,</p>
<p>chaslie</p>

ptrblck: <p>You could rerun the code via <code>CUDA_LAUNCH_BLOCKING=1 python script.pt args</code> and make sure that the right line of code is shown, which raises the issue.<br>
If you are still seeing that the criterion is raising it, add <code>assert</code> statements before calculating the loss and check for the values of the model output and the targets.</p>

chaslie: <p>Hi Ptrblck,</p>
<p>thanks for this. I think i may be on to a winner, something was delivering a Nan to the BCE term. Think i may have solved it, will update with the solution in a day or so (depending on if the solution works <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"> )</p>
<p>thanks again for your help,</p>
<p>chaslie</p>