It seems BCELOSS does not do range check in torch-1.10.2 as torch-1.10.0+cu111

Hi, guys!
I find that nn.BCELoss does not do range check in torch-1.10.2 as torch-1.10.0+cu111, which may trigger error like " /…/Loss.cu: … [59,0,0] Assertion input_val >= zero && input_val <= one failed.".
So, I want to make it sure whether range check (from 0 to 1) is the official behavior of nn.BCELoss?

Your answer will be appreciated!

I cannot reproduce the issue using 1.10.2:

>>> criterion = nn.BCELoss()
>>> x = torch.randn(10, 1)
>>> y = torch.empty(10, 1).uniform_(0, 1)
>>> loss = criterion(x, y)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/miniforge3/envs/1.10.2_cu113/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1102, in _call_impl
    return forward_call(*input, **kwargs)
  File "/miniforge3/envs/1.10.2_cu113/lib/python3.8/site-packages/torch/nn/modules/loss.py", line 603, in forward
    return F.binary_cross_entropy(input, target, weight=self.weight, reduction=self.reduction)
  File "/miniforge3/envs/1.10.2_cu113/lib/python3.8/site-packages/torch/nn/functional.py", line 2915, in binary_cross_entropy
    return torch._C._nn.binary_cross_entropy(input, target, weight, reduction_enum)
RuntimeError: all elements of input should be between 0 and 1
>>> loss = criterion(x.cuda(), y.cuda())
>>> /pytorch/aten/src/ATen/native/cuda/Loss.cu:115: operator(): block: [0,0,0], thread: [1,0,0] Assertion `input_val >= zero && input_val <= one` failed.
/pytorch/aten/src/ATen/native/cuda/Loss.cu:115: operator(): block: [0,0,0], thread: [2,0,0] Assertion `input_val >= zero && input_val <= one` failed.
/pytorch/aten/src/ATen/native/cuda/Loss.cu:115: operator(): block: [0,0,0], thread: [5,0,0] Assertion `input_val >= zero && input_val <= one` failed.
/pytorch/aten/src/ATen/native/cuda/Loss.cu:115: operator(): block: [0,0,0], thread: [8,0,0] Assertion `input_val >= zero && input_val <= one` failed.
/pytorch/aten/src/ATen/native/cuda/Loss.cu:115: operator(): block: [0,0,0], thread: [9,0,0] Assertion `input_val >= zero && input_val <= one` failed.

>>> print(torch.__version__)
1.10.2+cu113

Both, the CPU and CUDA ops show the expected error.

1 Like

Thanks for your response. I will try how to reproduce my error case then.