Problems with inconsistencies in assignment operations

YaqiXia: <p>As shown in the code demo below, I want to update the data at the specified location of data_store through tensor</p>
<pre><code class="lang-python">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[index.long()]=data
data_store_cp[index.long()] = data
print(f"equal: {torch.equal(data_store, data_store_cp)}")    
</code></pre>
<p>When</p>
<pre><code class="lang-auto">device = "cpu"
</code></pre>
<p>I get “True”, however when I switch</p>
<pre><code class="lang-auto">device = "cuda:0"
</code></pre>
<p>I get “False”.<br>
I wonder why the result is like this and how to modify it to make the result “Ture” under the cuda setting?<br>
Thank you in advance.</p>
<p>Verision:</p>
<pre><code class="lang-auto">pytorch: 1.9.0
cuda: 11.0
</code></pre>

swap: <p>see torch.equals bug</p><aside class="onebox githubissue" data-onebox-src="https://github.com/pytorch/pytorch/issues/15900">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/issues/15900" target="_blank" rel="noopener nofollow ugc">github.com/pytorch/pytorch</a>
  </header>

  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue">
	  <svg width="60" height="60" class="github-icon" viewbox="0 0 14 16" aria-hidden="true"><path d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/pytorch/issues/15900" target="_blank" rel="noopener nofollow ugc">torch.equals bug</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2019-01-09" data-time="23:39:30" data-timezone="UTC">11:39PM - 09 Jan 19 UTC</span>
      </div>

        <div class="date">
          closed <span class="discourse-local-date" data-format="ll" data-date="2019-01-10" data-time="04:50:34" data-timezone="UTC">04:50AM - 10 Jan 19 UTC</span>
        </div>

      <div class="user">
        <a href="https://github.com/PetrochukM" target="_blank" rel="noopener nofollow ugc">
          <img alt="PetrochukM" src="https://avatars.githubusercontent.com/u/7424737?v=4" class="onebox-avatar-inline" width="20" height="20">
          PetrochukM
        </a>
      </div>
    </div>

    <div class="labels">
    </div>
  </div>
</div>

  <div class="github-row">
    <p class="github-body-container">## 🐛 Bug

Torch equals does not work.

## To Reproduce

```
&gt;&gt;&gt; import to<span class="show-more-container"><a href="" rel="noopener" class="show-more">…</a></span><span class="excerpt hidden">rch
&gt;&gt;&gt; t =torch.tensor([0.3000, 0.3000, 0.0000, 0.0000])
&gt;&gt;&gt; t1 =torch.tensor([0.3000, 0.3000, 0.0000, 0.0000])
&gt;&gt;&gt; torch.equal(t, t1)
True
&gt;&gt;&gt; t1 =torch.tensor([0.3000, 0.3000, 0.0000, 0.0000]) + 1 - 1
&gt;&gt;&gt; torch.equal(t, t1)
False
&gt;&gt;&gt; t1
tensor([0.3000, 0.3000, 0.0000, 0.0000])
```
## Expected behavior

That equality still works after some operations.

## Environment

```
Collecting environment information...
PyTorch version: 1.0.0
Is debug build: No
CUDA used to build PyTorch: None

OS: Mac OSX 10.14.1
GCC version: Could not collect
CMake version: Could not collect

Python version: 3.6
Is CUDA available: No
CUDA runtime version: No CUDA
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA

Versions of relevant libraries:
[pip] Could not collect
[conda] Could not collect
```</span></p>
  </div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>


YaqiXia: <p>Thanks for your reply!<br>
But when I check the content in the <code>data_store</code> and <code>data_store_cp</code>, most of the time they are different.</p>

KFrank: <p>Hi Yaqi!</p>
<aside class="quote no-group" data-username="YaqiXia" data-post="1" data-topic="154672" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/yaqixia/48/50722_2.png" class="avatar"> YaqiXia:</div>
<blockquote>
<pre data-code-wrap="python"><code class="lang-python">data_store[index.long()]=data
</code></pre>
</blockquote>
</aside>
<p>At issue is that <code>index</code> contains many duplicate values so you are therefore<br>
attempting to write to the same location in <code>data_store</code> multiple times in a<br>
single assignment operation.  Doing so is not supported by pytorch and is<br>
not well defined.</p>
<p>Why you get different results from nearly identical assignment statements<br>
is a bit of a mystery – pytorch is presumably taking advantage of the<br>
freedom given it <em>not</em> to attempt to make the multiple write well defined to<br>
perform some optimization (especially on the gpu).</p>
<p>Best.</p>
<p>K. Frank</p>