Thanks @albanD for the reply.
Follow-up questions for more clarity:
You mentioned about parameters_to_vector being differentiable. So, how can I check whether a function is differentiable or not?
Also, can you be more specific about the operations in which detach is required. For instance, if I pass the model.parameters to any other function or use it like params = list(self.model1.parameters()), will these require the use of detach()?