Slightly changing model causes 'modified by inplace operation' error

Previous code:
return n_vals

I added one line:
n_vals[:,1,:] = n_vals[:,1,:] * torch.tensor(2)

And now get:
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation

I don’t understand this error because the line I added is not in place.

Another potential cause of the error is because I am preloading the model’s weights from disk and changing the model (with the above line) after preloading the weights. Still, doesnt seem like it should cause this error…

The answer is that assigning the variable with slice notation is an inplace operation. The solution is to add a clone call:
n_vals[:,1,:] = n_vals[:,1,:].clone() * torch.tensor(2)