style-based-gan-pytorch icon indicating copy to clipboard operation
style-based-gan-pytorch copied to clipboard

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

Open sunkymepro opened this issue 3 years ago • 5 comments

Dear author, thanks for your code. But I have some trouble in the procedure of backward.

The error message is:

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [512, 1, 3, 3]] is at version 4; expected version 3 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 program reported this error on this line:

kernel, kernel_flip = ctx.saved_tensors

this line of code is inside the backward function in class BlurFunction.

I don't know why this error occurs. It seems that the value these two variables has changed and these variables are needed for gradient computation.

sunkymepro avatar Oct 11 '21 07:10 sunkymepro

I add the code 'kernel, kernel_flip = ctx.saved_tensors' before the code 'ctx.save_for_backward(kernel, kernel_flip)' in forward function of class BlurFunction. And there occurs a new error:

ValueError: not enough values to unpack (expected 2, got 0)

It seems that the variable kernel and variable kernel_flip aren't saved successfully. I don't know why this problem occurs, is there anyone who can help me?

sunkymepro avatar Oct 11 '21 07:10 sunkymepro

Hmm I can't reproduce the error. And I don't have clue as inplace operations is not happened over these two tensors.

rosinality avatar Oct 11 '21 10:10 rosinality

Hmm I can't reproduce the error. And I don't have clue as inplace operations is not happened over these two tensors.

It may be because the version of cuda and cudnn are mismatch? I do experiment in a 3090 NVIDIA GPU and use torch 1.8.1 and cuda 11.2.

sunkymepro avatar Oct 12 '21 02:10 sunkymepro

Hmm, I use torch 1.8.1 and cuda 10.2. But I don't know cuda version could affect these kind of problems.

rosinality avatar Oct 12 '21 03:10 rosinality

I have solved ,You can see that [(https://zhuanlan.zhihu.com/p/409117481)]

superAa avatar Oct 30 '23 07:10 superAa