Unrolling a single forward-backward pass

I am trying to replicate something that the authors in the paper, Generative Modeling for Small-Data Object Detection, have proposed in the Section 3.2. Train Generator with Detection Losses which says:

I understand the theoretical aspect of the proposed solution, but I can’t figure out a way to code in PyTorch this unrolling procedure. I tried in several ways but I always get something related to the fact that the graph is disconnected.

This is a sketch of a simple implementation with VAE instead of GAN: Google Colab