Grad lost after CopySlices of a tensor

ciacc: <p>For the following simple code, with <code>pytorch==1.9.1, python==3.9.13</code> vs <code>pytorch==1.11.0, python==3.10.4</code> , The result is totally different. In the newer version of pytorch, the grad is lost.</p>
<pre><code class="lang-auto">import torch
S = torch.zeros(1,4)
a = torch.tensor(1.,requires_grad=True)
S[0,2:4] = a
print(S)
</code></pre>
<p><code>pytorch==1.9.1, python==3.9.13</code> gives:</p>
<pre><code class="lang-auto">tensor([[0., 0., 1., 1.]], grad_fn=&lt;CopySlices&gt;)
</code></pre>
<p>but <code>pytorch==1.11.0, python==3.10.4</code> gives:</p>
<pre><code class="lang-auto">tensor([[0., 0., 1., 1.]])
</code></pre>
<p>Here you can see the grad is lost.</p>
<p>In my code base, I have a lot of implementations like this. I really want to know why this happens and then understand how I can solve this problem without massively refactoring my code base to make it work in the newer pytorch version.</p>

KFrank: <p>Hi Ciacc!</p>
<aside class="quote no-group" data-username="ciacc" data-post="1" data-topic="154358" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/c/c68b51/48.png" class="avatar"> ciacc:</div>
<blockquote>
<p>but <code>pytorch==1.11.0, python==3.10.4</code> gives:</p>
<pre><code class="lang-auto">tensor([[0., 0., 1., 1.]])
</code></pre>
</blockquote>
</aside>
<p>This appears to be a known bug / regression that has recently been fixed.<br>
It works for me in a version-1.13 nightly build:</p>
<pre><code class="lang-plaintext">&gt;&gt;&gt; import torch
&gt;&gt;&gt; torch.__version__
'1.13.0.dev20220604'
&gt;&gt;&gt; S = torch.zeros(1,4)
&gt;&gt;&gt; a = torch.tensor(1.,requires_grad=True)
&gt;&gt;&gt; S[0,2:4] = a
&gt;&gt;&gt; print(S)
tensor([[0., 0., 1., 1.]], grad_fn=&lt;CopySlices&gt;)
</code></pre>
<p>(I can reproduce your issue in 1.11.)</p>
<p>While waiting for the fix to hit an updated stable release, I would recommend<br>
downgrading back to version 1.9, or, if you’re comfortable working with an<br>
“unstable” release, upgrade to the nightly.  (Personally, I would go with the<br>
nightly, but not for important production purposes.)</p>
<p>See this github issue:</p>
<aside class="onebox githubissue" data-onebox-src="https://github.com/pytorch/pytorch/issues/77565">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/issues/77565" 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 fill-rule="evenodd" 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/77565" target="_blank" rel="noopener nofollow ugc">Assigning parameter to tensor doesn't pass gradient info</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2022-05-16" data-time="17:14:07" data-timezone="UTC">05:14PM - 16 May 22 UTC</span>
      </div>

        <div class="date">
          closed <span class="discourse-local-date" data-format="ll" data-date="2022-06-01" data-time="16:07:16" data-timezone="UTC">04:07PM - 01 Jun 22 UTC</span>
        </div>

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

    <div class="labels">
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          module: autograd
        </span>
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          triaged
        </span>
    </div>
  </div>
</div>

  <div class="github-row">
    <p class="github-body-container">## Issue description

In the example below, assigning a parameter to tensor do<span class="show-more-container"><a href="" rel="noopener" class="show-more">…</a></span><span class="excerpt hidden">esn't seem to pass gradient information, unless a list is used for indexing.

## Code example

```python
import torch
torch.__version__  # '1.11.0'
z = torch.zeros(3, 2)

z[:, 0] = torch.nn.Parameter(torch.tensor(1.0))
z

# tensor([[1., 0.],
#         [1., 0.],
#         [1., 0.]])  # &lt;------------

z[:, [0]] = torch.nn.Parameter(torch.tensor(1.0))
z

# tensor([[1., 0.],
#         [1., 0.],
#         [1., 0.]], grad_fn=&lt;CopySlices&gt;)  # &lt;------------
```

... is this behaviour expected?

cc @ezyang @albanD @zou3519 @gqchen @pearu @nikitaved @soulitzer @Lezcano @Varal7</span></p>
  </div>

  </article>

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

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

<p>Best.</p>
<p>K. Frank</p>

ciacc: <p>Hi K. Frank,</p>
<p>Thanks so much for the reply. I thought it was an intended change. Now at least I don’t have to worry about refactoring my code <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=12" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"></p>
<p>Trying out nightly sounds cool. I will give it a roll.</p>