# One of the variables needed for gradient computation has been modified by an inplace operation: \[torch.cuda.FloatTensor \[128, 128\]\], which is output 0 of TBackward, is at version 2; expected version 1 instead

**URL:** <https://discuss.pytorch.org/t/one-of-the-variables-needed-for-gradient-computation-has-been-modified-by-an-inplace-operation-torch-cuda-floattensor-128-128-which-is-output-0-of-tbackward-is-at-version-2-expected-version-1-instead/108178>\
**Category:** Uncategorized\
**Created:** [January 7, 2021, 1:52pm UTC](https://discuss.pytorch.org/t/one-of-the-variables-needed-for-gradient-computation-has-been-modified-by-an-inplace-operation-torch-cuda-floattensor-128-128-which-is-output-0-of-tbackward-is-at-version-2-expected-version-1-instead/108178 "2021-01-07T13:52:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![coballe](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/c/53a042/32.png) [@coballe](https://discuss.pytorch.org/u/coballe)\
**Post date:** [January 7, 2021, 1:52pm UTC](https://discuss.pytorch.org/t/one-of-the-variables-needed-for-gradient-computation-has-been-modified-by-an-inplace-operation-torch-cuda-floattensor-128-128-which-is-output-0-of-tbackward-is-at-version-2-expected-version-1-instead/108178/1 "2021-01-07T13:52:32Z")

</div>

I am training a network to predict trajectories of particles. The main training loop is:

```
    for t,time_slice in enumerate(TrajectoryBatch(traj_batch,time_steps = time_steps)):

        time_slice.to(device)
        
        if t == 0:
            enc_pred = 0
        else:
            enc_pred = rollout
        
        # forward pass
        enc,recon,rollout,pred = mod(time_slice)
    
        # compute loss
        dx,dg = torch.cdist(time_slice.x,time_slice.x),torch.cdist(enc,enc) # distance matrices for metric loss

        l1 = (1/time_steps)*ae_loss(time_slice.x,recon)
        l2 = (1/time_steps)*pred_loss(time_slice.y,pred)
        l3 = (1/time_steps)*met_loss(dx,dg)
        
        if t == 0:
            l4 = (1/time_steps)*lin_pred_loss(enc,enc)
        
        else:
            l4 = (1/time_steps)*lin_pred_loss(enc,enc_pred)
        
        time_slice_loss = ae_reg*l1+pred_reg*l2+met_reg*l3+lin_pred_reg*l4
        
        
        # backprop
        time_slice_loss.backward(retain_graph = True)
        optimizer.step()
        optimizer.zero_grad()
        
        ep_loss += float(time_slice_loss)
        pred_acc_train += float(l2)

```

The code runs for t == 0, but then gives the error message:

> * * *
> 
> RuntimeError Traceback (most recent call last)  
> in   
> 43 # backprop  
> 44  
> —\> 45 time\_slice\_loss.backward(retain\_graph = True)  
> 46 optimizer.step()  
> 47 optimizer.zero\_grad()
> 
> ~/anaconda3/envs/tf\_gpu/lib/python3.8/site-packages/torch/tensor.py in backward(self, gradient, retain\_graph, create\_graph)  
> 219 retain\_graph=retain\_graph,  
> 220 create\_graph=create\_graph)  
> → 221 torch.autograd.backward(self, gradient, retain\_graph, create\_graph)  
> 222  
> 223 def register\_hook(self, hook):
> 
> ~/anaconda3/envs/tf\_gpu/lib/python3.8/site-packages/torch/autograd/ **init**.py in backward(tensors, grad\_tensors, retain\_graph, create\_graph, grad\_variables)  
> 128 retain\_graph = create\_graph  
> 129  
> → 130 Variable._execution\_engine.run\_backward(  
> 131 tensors, grad\_tensors_, retain\_graph, create\_graph,  
> 132 allow\_unreachable=True) # allow\_unreachable flag  
> RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [128, 128]], which is output 0 of TBackward, is at version 1; expected version 0 instead. Hint: the backtrace further above shows the operation that failed to compute its gradient. The variable in question was changed in there or anywhere later. Good luck!

The issue is related to the loss component, l4

```auto
l4 = (1/time_steps)*lin_pred_loss(enc,enc_pred)

```

because the code runs when l4 is not included in the total loss.

The variable enc\_pred is an output of the previous forward pass. I’ve read through other posts pertaining to this error message, but I couldn’t find anything that resolved the issue. I’ve tried substituting:

```auto
enc_pred = rollout.clone()

```

and,

```auto
time_slice_loss = (ae_reg*l1+pred_reg*l2+met_reg*l3+lin_pred_reg*l4).clone()

```

with no success. Any help is greatly appreciated.

UPDATE:

The code runs with

```auto
enc_pred = rollout.detach()

```

although I’ll have to think about what exactly is going on.

---

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [January 7, 2021, 2:46pm UTC](https://discuss.pytorch.org/t/one-of-the-variables-needed-for-gradient-computation-has-been-modified-by-an-inplace-operation-torch-cuda-floattensor-128-128-which-is-output-0-of-tbackward-is-at-version-2-expected-version-1-instead/108178/2 "2021-01-07T14:46:51Z")

</div>

> [@coballe](#):
>
> The code runs with
> 
> ```auto
> enc_pred = rollout.detach()
> 
> ```

You most likely don’t want that as this is the same as not including this part into the loss function!

The error you see does come from an inplace op that is causing issue.  
From your code sample, a possible reason is that `optimizer.step()` modifies the weights inplace. But the original values are needed for the gradient computation, so if you try to backward again, it will fail with this error.

---

<div class="post-metadata">

**Author:** ![coballe](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/c/53a042/32.png) [@coballe](https://discuss.pytorch.org/u/coballe)\
**Post date:** [January 7, 2021, 2:54pm UTC](https://discuss.pytorch.org/t/one-of-the-variables-needed-for-gradient-computation-has-been-modified-by-an-inplace-operation-torch-cuda-floattensor-128-128-which-is-output-0-of-tbackward-is-at-version-2-expected-version-1-instead/108178/3 "2021-01-07T14:54:35Z")

</div>

Does

```auto
enc_pred = rollout.detach()

```

result in enc\_pred being treated as a constant in

```auto
l4 = (1/time_steps)*lin_pred_loss(enc,enc_pred)

```

or does it cause l4 to be disregarded altogether?

---

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [January 7, 2021, 2:59pm UTC](https://discuss.pytorch.org/t/one-of-the-variables-needed-for-gradient-computation-has-been-modified-by-an-inplace-operation-torch-cuda-floattensor-128-128-which-is-output-0-of-tbackward-is-at-version-2-expected-version-1-instead/108178/4 "2021-01-07T14:59:53Z")

</div>

It only causes it to be considered as a constant. So not gradient will “flow back” through `enc_pred`.

---

<div class="post-metadata">

**Author:** ![coballe](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/c/53a042/32.png) [@coballe](https://discuss.pytorch.org/u/coballe)\
**Post date:** [January 7, 2021, 3:23pm UTC](https://discuss.pytorch.org/t/one-of-the-variables-needed-for-gradient-computation-has-been-modified-by-an-inplace-operation-torch-cuda-floattensor-128-128-which-is-output-0-of-tbackward-is-at-version-2-expected-version-1-instead/108178/5 "2021-01-07T15:23:34Z")

</div>

I see. In the model, enc flows through the first part of the network that enc\_pred does. Could this be an issue?

For example, does autograd update the network by flowing through enc, then throw an error when flowing through enc\_pred since weights have been modified in place?

---

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [January 7, 2021, 5:57pm UTC](https://discuss.pytorch.org/t/one-of-the-variables-needed-for-gradient-computation-has-been-modified-by-an-inplace-operation-torch-cuda-floattensor-128-128-which-is-output-0-of-tbackward-is-at-version-2-expected-version-1-instead/108178/6 "2021-01-07T17:57:13Z")

</div>

I don’t know what your model is 😃 But that sounds like something that is plausible.  
You can easily avoid this by re-doing a forward pass after you update the weights.
