Skip to content

index_add_ & index_copy_ do not properly check tensor sizes on GPU #4213

@yongjik

Description

@yongjik
A = torch.zeros(5, 4)
B = torch.arange(0, 9).view(3, 3)
C = torch.arange(0, 15).view(3, 5)
idxs = torch.LongTensor([0, 2, 4])

A.index_add_(0, idxs, B)
# RuntimeError: inconsistent tensor size, expected r_ [4], t [4] and src
  [3] to have the same number of elements, but got 4, 4 and 3 elements
  respectively at (...)/aten/src/TH/generic/THTensorMath.c:1008

A.index_add_(0, idxs, C)
# RuntimeError: inconsistent tensor size, expected r_ [4], t [4] and src
  [5] to have the same number of elements, but got 4, 4 and 5 elements
  respectively at (...)/aten/src/TH/generic/THTensorMath.c:1008

So far so good. But if we use CUDA...

A = torch.zeros(5, 4).cuda()
B = torch.arange(0, 9).view(3, 3).cuda()
C = torch.arange(0, 15).view(3, 5).cuda()
idxs = torch.LongTensor([0, 2, 4]).cuda()

A.index_add_(0, idxs, B)
print(A)
#    0  1  2  0
#    0  0  0  0
#    3  4  5  0
#    0  0  0  0
#    6  7  8  0
#   [torch.cuda.FloatTensor of size 5x4 (GPU 0)]

OK, this looks wrong...

A.zero_()
A.index_add_(0, idxs, C)
print(A)
#     0   1   2   3
#     4   0   0   0
#     5   6   7   8
#     9   0   0   0
#    10  11  12  13
#   [torch.cuda.FloatTensor of size 5x4 (GPU 0)]

Now this looks definitely wrong.

Increase C's dimension to something like (3, 500), and it overwrites other tensors or triggers asserts.

Same thing happens with index_copy_.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions