Hi,
I think the problem lies in the optimization step. As you can see here :
SGD step  4 the step function is operating on p.data instead p (parameter). So the computational graph is not keeping track of these operations ( Equation 1 of the paper for instance).
You may also need to set  create_graph to True in your backward function in order to compute the second order derivatives of loss1 since you will need it at some point to backpropagate the gradients of loss2.