How to get around the RuntimeError: in-place operations can be only used on variables that don't share storage with any other variables

With PyTorch I’m having a problem doing an operation with two Variables:

sub_patch  : [torch.FloatTensor of size 9x9x32]

pred_patch : [torch.FloatTensor of size 5x5x32]

sub_patch is a Variable made by torch.zeros
pred_patch is a Variable of which I index each of the 25 nodes with a nested for-loop, and that I multiply with its corresponding unique filter (sub_filt_patch) of size [5,5,32]. The result is added to its respective place in sub_patch.

This is a piece of my code:

for i in range(filter_sz):
    for j in range(filter_sz):

        # index correct filter from filter tensor
        sub_filt_col = (patch_col + j) * filter_sz
        sub_filt_row = (patch_row + i) * filter_sz

        sub_filt_patch = sub_filt[sub_filt_row:(sub_filt_row + filter_sz), sub_filt_col:(sub_filt_col+filter_sz), :]

        # multiply filter and pred_patch and sum onto sub patch
        sub_patch[i:(i + filter_sz), j:(j + filter_sz), :] += (sub_filt_patch * pred_patch[i,j]).sum(dim=3)

The error I get from the bottom line of the piece of code here is

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 get why it happens, since sub_patch is a Variable, and pred_patch is a Variable too, but how can I get around this error? Any help would be greatly appreciated!

Thank you!

can you build up the sub_patch as separate Variables (that are only written to once) and then build up the final sub_patch using torch.cat or torch.stack?

It seems that the problem was with the +=:

When separating this line of code into this:

sub_patch[i:(i + filter_sz), j:(j + filter_sz), :] = sub_patch[i:(i + filter_sz), j:(j + filter_sz), :] +(sub_filt_patch * pred_patch[i,j]).sum(dim=3)

Then it worked!
This must be a bug I guess?

Thanks, those were good recommendations, however, I’ve found a better solution to the problem which I’ve posted as an answer to this thread

Maybe late, but if you make the inplace operation directly on sub_patch.data rather than on sub_patch it should skip the check.

1 Like

I met same problem as yours, but I also try the code by changing += into xx = xx + . There is still the error.

1 Like

I encountered the same problem, and this method worked for me.:grinning: