Solved by ptrblck in post #2 
              
              
                I think your approach is alright. A small improvement might be to switch the order of updating the models. 
You could first call loss2.backward(retain_graph=True) and update B as you need this gradient in A a bit later. Then call loss1.backward() and update A. 
Here is a small example: 

modelA = nn…