When optimize same network several times, I met "RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation"

You won’t be able to update the parameters via optM.step() since it would also update the shared parameters used to calculate mid and would invalidate the stored intermediate forward activations which are needed to compute the gradients of all parameters from ld.backward().