Combination of two independent models

Hello,

I want to combine two independent models as follows:

I have two independent models, which are stored in different folders - Model A and Model B. I want to attach selected blocks of Model A to Model B, so that they work as a single model. Both models have been pre-trained before and I have files with weights for both models.
Please find a visualization of this problem in the image below:

My idea was to create a Model A object in the Model B constructor, and then extract the individual Model A blocks so that they could be used in the Model B forward function. Due to the complexity of the models, it was not so easy and I was not able to connect the models in this way.

Another solution that comes to mind is to modify the source code of Model A so as to keep only the necessary blocks of the model and then use only their outputs, but I’m not sure if that would work.

What is the most correct and the most PyTorch way to connect models in such a way?
How would you combine the models if you were me?

Thanks!