[Solved][Pytorch1.12] RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation

I tried many ways but nothing seems to work. As a newbie, I really don’t understand how to modify the code to make it work.https://discuss.pytorch.org/t/solved-pytorch1-5-runtimeerror-one-of-the-variables-needed-for-gradient-computation-has-been-modified-by-an-inplace-operation/90256?u=peter_packhttps://discuss.pytorch.org/t/solved-pytorch1-5-runtimeerror-one-of-the-variables-needed-for-gradient-computation-has-been-modified-by-an-inplace-operation/90256?u=peter_pack

I found that it is because LightCNN and MobileFacenet change the input x_bar
It works as follows:

x_bar_ID_fea = LightCNN(x_bar.detach())
mbf_x_bar = resize_img_to_MBF_input(args, x_bar.detach())