I want to pass my input through a model then pass the output through another model and take the derivative with respect to the first model parameters. However, when I do so I get the following error,

One of the differentiated Tensors appears to not have been used in the graph. Set allow_unused=True if this is the desired behavior.

This is because when I differentiate the loss with respect to the output from the first model even after setting requires_grad = True, I get the same error, so the link is broken in the intermediate input to the second model.

How do I solve it?