Indeed, this is faster than what I’ve suggested:
python -m timeit --setup="import torch;x = torch.rand(10 ** 5);not_idx = torch.randint(10 ** 5, (10 ** 5,))" "idx = torch.ones(10 ** 5).bool(); idx[not_idx] = False; x[idx]"