For the inplace above, the problem is actuall with x[i] = x[i] * lmb. Since you need gradients for lmb as they are your parameters, you need the value x[i] to compute those. But you change x[i] inplace on the same line. So the value of x[i] needed to compute the gradients of lmb is overwritten hence the error you see. To remove the inplace error, you will need to at least make sure you don’t override this value. And potentially the same thing at other places.