A = torch.nn.Parameter(torch.randn(3, 3))
B = torch.randn(2, 4, 3)
B[0, :, :] = B[0, :, :].mm(A)
loss = B.sum()
loss.backward()

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [4, 3]] is at version 1; expected version 0 instead.
How could I solve this problem?

Thanks for your kind help. Actually, the code is the simplified version of my problem. In the real situation, I cannot assign B[0, :, :].mm(A) to z. However, following your inspiration, I find use .clone() can solve my problem

Thank you for sharing your information. Actually, I have not had such a problem before and I just found a solution by rule of thumb after reading inplace definition doc. Your approach is the conventional one.