Hi,
The thing is that during .backward() call, to reduce the peak memory usage, pytorch frees all the memory buffers kept by all the functions in the graph.
This means that you cannot call .backward() twice on the same graph otherwise you will get the error you see above. Note that for graphs that do not have any memory buffer, this will work properly as no missing buffers will be encountered during the backward pass.
I think each of the different scenario you show will actually change which functions keep buffers or not.
What you need to set is gradient_penalty.backward(retain_graph=True) and loss_D_real.backward(retain_graph=True).