In-place operations can be only used on variables that don't share storage

While running the following code

A = Variable(torch.zeros(10))
B = Variable(torch.randn(10,10))
for i in range(0,10):
A[i] = B[i,i]

A[6:].zero_()

I get the following error:

RuntimeError: in-place operations can be only used on variables that don't share storage with any other variables, but detected that there are 2 objects sharing it

I can work around the error by using A[6:] = 0. However, I am wondering the reason for prohibiting the use of in-place operations. Moreover, isn’t the operation A[6:] = 0 inplace as well?

What version of pytorch are you on?

I ran your code and it doesn’t error out. In particular, it looks like A isn’t sharing storage with B from your code snippet.

Thanks for the reply. I am using version 0.3.0. This is probably the latest version.

Okay. I’m running on the master version. I think pytorch has gotten better support for in-place operations since the 0.3 release. You can try building from source (instructions here) to use the latest features.

1 Like

Thanks :slight_smile: