how to merge many models to one model with shared backbone just use some code ,not a create a new model

I train some models with different datas ,these models’ some parameters are shared ,
when i inference the models ,i need merge the models to one model ,i know the shared op ,so ,i want to merge these models
shared op to one op with seperate head only when inference not train.
i don’t want to write a new model ,could i write a function to merge the op with same dict name ,and create a new inference model
automatic .
or maybe any way else is ok
thank you !