Problems with inconsistencies in assignment operations

As shown in the code demo below, I want to update the data at the specified location of data_store through tensor

import torch

device = "cuda:0"
device = "cpu"
data_store = torch.zeros([5,3]).to(device)
data_store_cp = torch.zeros([5,3]).to(device)
index = torch.randint(0,4,[100]).to(device)
data = torch.randn([index.size(0),3]).to(device)
data_store_cp[index.long()] = data
print(f"equal: {torch.equal(data_store, data_store_cp)}")    


device = "cpu"

I get “True”, however when I switch

device = "cuda:0"

I get “False”.
I wonder why the result is like this and how to modify it to make the result “Ture” under the cuda setting?
Thank you in advance.


pytorch: 1.9.0
cuda: 11.0

see torch.equals bug

Thanks for your reply!
But when I check the content in the data_store and data_store_cp, most of the time they are different.

Hi Yaqi!

At issue is that index contains many duplicate values so you are therefore
attempting to write to the same location in data_store multiple times in a
single assignment operation. Doing so is not supported by pytorch and is
not well defined.

Why you get different results from nearly identical assignment statements
is a bit of a mystery – pytorch is presumably taking advantage of the
freedom given it not to attempt to make the multiple write well defined to
perform some optimization (especially on the gpu).


K. Frank