retain_graph doesn’t connect the losses, but keeps the intermediate activations after a backward call. This is necessary, if any other loss or output calling .backward() would need these activations from the forward pass to calculate the gradients.
The “connection” between the losses is defined by the forward pass.
E.g. if both losses were created using the same model (and thus parameters), the backward passes would calculate the gradients w.r.t. the same parameters as well.