I think this should work (I don’t tested):
  import random
  def forward(self, x1, x2):
        path = random.choice([True, False])
        if path:
            x1 = self.encoder1(x1)
            x2_hat = self.decoder2(x1)
            return x2_hat
        else:
            x2 = self.encoder2(x2)
            x1_hat = self.decoder1(x2) 
            return x1_hat