Torch.topk with k=0 leads to errors when used for indexing another tensor

The indices (empty tensor) returned by topk for k=0, leads to RuntimeError: CUDA error: device-side assert triggered when being used for selecting indexes within another tensor. However, when using an empty tensor (not from topk.indices) for selecting indexes to the another tensor works fine. Is this an expected behavior ? Note that the behavior with device="cpu" is correct.

Pytorch version 1.5.1. The code runs fine on older version (1.2.0)

Code to reproduce the above:

import torch
#device = "cpu"
device = "cuda:0"
x = torch.arange(1,10).float().to(device)

# Case - 1: use an empty cuda tensor for index
y = []
y_t = torch.LongTensor(y).to(device)

# Case - 2: use an empty cuda tensor from topk.indices for index
topk = torch.topk (x, k=0, largest=False)

print(y_t, topk.indices)
x[y_t] = 0.0
print(x)
x[topk.indices] = 0.0
print(x) 
1 Like

Thanks for reporting this issue! :slight_smile:
Would you mind creating an issue so that we could track and fix it, please?

Sure, I’ll create an issue with the details I have obtained. Thanks.

1 Like