Unexpected behavior when using max() with cuda.ByteTensor


(Thomas Balestri) #1

I’m seeing some unexpected behavior using a cuda.ByteTensor with tensor.max(). I’m creating a binary mask from 2 cuda.FloatTensors (y,x) using y==x. This works fine. But when trying to retrieve the argmax using something like (y==x).max(dim=1)[1], it only returns the expected behavior on a cpu. I can only get the expected result on the gpu by converting the cuda.ByteTensor to a cuda.FloatTensor before calling the max operator: (y==x).type(cuda.FloatTensor).max(dim=1)[1]. Below I’ve provided a minimal example.

Python 2.7.13 |Continuum Analytics, Inc.| (default, Dec 20 2016, 23:09:15) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import torch
>>> print(torch.__version__)
0.2.0_4
>>> x = torch.Tensor( [ [0], [1], [1], [0], [0] ]).cuda()
>>> type(x)
<class 'torch.cuda.FloatTensor'>
>>> b_x = 1 == x
>>> type(b_x)
<class 'torch.cuda.ByteTensor'>
>>> b_x

 0
 1
 1
 0
 0
[torch.cuda.ByteTensor of size 5x1 (GPU 0)]

>>> b_x.max(dim=1)
(
 0
 1
 1
 0
 0
[torch.cuda.ByteTensor of size 5 (GPU 0)]
, 
 1
 0
 0
 1
 1
[torch.cuda.LongTensor of size 5 (GPU 0)]
)
>>> x = torch.Tensor( [ [0], [1], [1], [0], [0] ])
>>> type(x)
<class 'torch.FloatTensor'>
>>> b_x = 1 == x
>>> type(b_x)
<class 'torch.ByteTensor'>
>>> b_x.max(dim=1)
(
 0
 1
 1
 0
 0
[torch.ByteTensor of size 5]
, 
 0
 0
 0
 0
 0
[torch.LongTensor of size 5]
)
>>> x = torch.Tensor( [ [0], [1], [1], [0], [0] ]).cuda()
>>> b_x = 1 == x
>>> b_x.type(torch.cuda.FloatTensor).max(dim=1)
(
 0
 1
 1
 0
 0
[torch.cuda.FloatTensor of size 5 (GPU 0)]
, 
 0
 0
 0
 0
 0
[torch.cuda.LongTensor of size 5 (GPU 0)]
)
>>> 

(Alban D) #2

Hi,

That looks like a bug in the max implementation for ByteTensor on cuda indeed. :confused:
Could you open an issue on the pytorch main repo on github with this same reproducing example please?

Thanks for the report.


(Thomas Balestri) #3