Bug report: Relu() is broken for 2d tensors of type long

Hi
There seems to be a bug where relu() does not work for 2d tensors of type long.
Relu works as intended for 1d tensors but for 2d tensors, it seems to only work for really small tensors.
It might be me who is doing something weird here but for me at least it seems like a bug.

Code to replicate:

import torch

s = torch.relu(torch.LongTensor(6).random_(-5, 5))
print(s.min(), s.max()) # => tensor(0) tensor(4)

a = torch.relu(torch.LongTensor(2,2).random_(-5, 5))
print(a.min(), a.max()) # => tensor(0) tensor(3)

b = torch.relu(torch.LongTensor(10,10).random_(-5, 5))
print(b.min(), b.max()) # => tensor(-5) tensor(4)
print(b)

"""
tensor([[ 0, -4, -2,  0,  0,  0,  0,  0, -5,  0],
        [ 0,  0,  0, -2, -2, -1, -4, -5, -4,  0],
        [ 0, -2, -2,  0,  0, -2, -3, -3, -5,  0],
        [-5,  0, -3, -2,  0,  0, -4,  0, -5,  0],
        [-3, -4, -5, -1,  0, -1,  0,  0, -2,  0],
        [-4,  0,  0, -4,  0,  0, -5, -1, -1,  0],
        [ 0,  0, -4, -2, -1,  0, -2,  0, -2,  0],
        [-3,  0,  0, -4,  0,  0,  0,  0, -4,  0],
        [ 0,  0,  0,  0,  0,  0,  0,  0,  0, -5],
        [ 0,  0,  0,  0,  0,  0,  0,  4,  2,  2]])
"""

Which PyTorch version are you using?
This issue seems to be fixed in the current release.

The same for me. My version is 1.0.0.

Could you try to install the latest nightly build and see if this issue still occurs?
You can find the install instructions on the website. If you are using conda, it might be cleaner to create a virtual environment for the nightly build so that your current env won’t be touched.

I used conda to create a new virtual environment and installed the latest nightly build. It seems that this issue still occurs.

System: macOS Mojave 10.14.1
Python: 3.7.1
PyTorch: 1.0.0.dev20181227

>>> import torch
>>> print(torch.__version__)
1.0.0.dev20181227
>>> x = (torch.randn(4,4) * 10).long()
>>> x
tensor([[  4,  -6,   7,  -2],
        [-10,  -2,  13,   0],
        [  1,  -4, -17,   3],
        [  4,   2, -10,  -9]])
>>> torch.relu(x)
tensor([[  0,  -6,   0,  -2],
        [-10,  -2,   0,   0],
        [  0,  -4, -17,   0],
        [  0,   0, -10,  -9]])

I can’t reproduce this issue on Ubuntu 16.04 with PyTorch 1.0.0a0+dfad8b6 and 1.0.0.dev20181228.
Maybe this is a macOS related issue? I can’t test it unfortunately, so let’s see if someone might help us out here.

I can reproduce this on macOS. I’ll file a github issue so we can track this.

Edit: https://github.com/pytorch/pytorch/issues/15634

1 Like

posted a fix at https://github.com/pytorch/pytorch/pull/15635 . afaik it also affects linux (and I reproduced it).

1 Like