You are using repetitive indices, which will write to the same index in an undefined order.
You can check it by comparing the shape of the indices and their unique length:
index = torch.stack(index)
u = index.unique(dim=1)
print(index.shape)
> torch.Size([3, 262144])
print(u.shape)
> torch.Size([3, 261021])