What is loss2.step()? Do you mean stepping with an optimizer?
To prevent gradient computation for model1’s parameters, you can detach model1_output from your computation graph:
loss2 = loss(model2_output, model1_output.detach())