return_modelpool (optional): A boolean indicating whether to return the entire model pool or just the first model. Defaults to True.
If return_modelpool is True, the run method returns a new ModelPool with the recombined models. If False, it returns the first model from the new model pool.
Configuration template for the model recombination algorithm:
config/method/model_recombination.yaml
name:model_recombination# if `return_model_pool` is not null, the argument `return_modelpool` passed to the `run` method will be ignored.return_modelpool:null
Construct a model recombination using our CLI tool fusion_bench:
classModelRecombinationAlgorithm(BaseAlgorithm):""" Model recombination recombinates the layers of the given models, to create a new set of models. """_config_mapping=BaseAlgorithm._config_mapping|{"return_modelpool":"return_modelpool",}def__init__(self,return_modelpool:bool,**kwargs):self.return_modelpool=return_modelpoolsuper().__init__(**kwargs)@torch.no_grad()defrun(self,modelpool:BaseModelPool,return_modelpool:bool=True,)->Union[nn.Module,BaseModelPool]:""" Executes the model recombination algorithm on a given model pool. This method loads models from the model pool, determines their type, and applies the appropriate recombination method. It then creates a new model pool with the recombined models. Depending on the `return_modelpool` flag, it either returns the entire new model pool or just the first model from it. - If the models in the model pool are of type `nn.ModuleList`, the recombination method `recombine_modellist` is used. Where each module in the list is shuffled across the models. - If the models are of type `nn.ModuleDict`, the recombination method `recombine_modeldict` is used. Where each module in the dictionary is shuffled across the models. - If the models are of type `nn.Module`, the recombination method `recombine_state_dict` is used. Where the state dictionaries of the models are shuffled across the models. Args: modelpool (BaseModelPool): The pool of models to recombine. return_modelpool (bool, optional): Flag indicating whether to return the entire model pool or just the first model. Defaults to True. If this algorithm is initialized with config, the value of `return_modelpool` in the config will be used and this argument passed to the method will be ignored. Returns: Union[nn.Module, BaseModelPool]: The recombined model pool or the first model from the recombined pool, depending on the `return_modelpool` flag. Raises: ValueError: If the models in the model pool are of an unsupported type. """# If the config has a return_modelpool flag, use that, otherwise use the argumentifself.config.get("return_modelpool",None)isnotNone:return_modelpool=self.config.return_modelpool# check the modelpool typeifnotisinstance(modelpool,BaseModelPool):modelpool=BaseModelPool(modelpool)log.info(f"Running model recombination algorithm with {len(modelpool)} models")# TODO: optimize the `recombine_*` functions, if `return_modelpool` is False, we don't need to create the new modelpool, just the first modelmodels=[modelpool.load_model(m)forminmodelpool.model_names]ifisinstance(models[0],nn.ModuleList):new_models=recombine_modellist(models)elifisinstance(models[0],nn.ModuleDict):new_models=recombine_modeldict(models)elifisinstance(models[0],nn.Module):new_models=recombine_state_dict(models)else:raiseValueError(f"Unsupported model type {type(models[0])}")new_modelpool=BaseModelPool({n:mforn,minzip(modelpool.model_names,new_models)})ifreturn_modelpool:returnnew_modelpoolelse:returnnew_modelpool.load_model(new_modelpool.model_names[0])
Executes the model recombination algorithm on a given model pool.
This method loads models from the model pool, determines their type, and applies the appropriate recombination method.
It then creates a new model pool with the recombined models. Depending on the return_modelpool flag, it either returns
the entire new model pool or just the first model from it.
If the models in the model pool are of type nn.ModuleList, the recombination method recombine_modellist is used. Where each module in the list is shuffled across the models.
If the models are of type nn.ModuleDict, the recombination method recombine_modeldict is used. Where each module in the dictionary is shuffled across the models.
If the models are of type nn.Module, the recombination method recombine_state_dict is used. Where the state dictionaries of the models are shuffled across the models.
Flag indicating whether to return the entire model pool or just the first model. Defaults to True. If this algorithm is initialized with config, the value of return_modelpool in the config will be used and this argument passed to the method will be ignored.
@torch.no_grad()defrun(self,modelpool:BaseModelPool,return_modelpool:bool=True,)->Union[nn.Module,BaseModelPool]:""" Executes the model recombination algorithm on a given model pool. This method loads models from the model pool, determines their type, and applies the appropriate recombination method. It then creates a new model pool with the recombined models. Depending on the `return_modelpool` flag, it either returns the entire new model pool or just the first model from it. - If the models in the model pool are of type `nn.ModuleList`, the recombination method `recombine_modellist` is used. Where each module in the list is shuffled across the models. - If the models are of type `nn.ModuleDict`, the recombination method `recombine_modeldict` is used. Where each module in the dictionary is shuffled across the models. - If the models are of type `nn.Module`, the recombination method `recombine_state_dict` is used. Where the state dictionaries of the models are shuffled across the models. Args: modelpool (BaseModelPool): The pool of models to recombine. return_modelpool (bool, optional): Flag indicating whether to return the entire model pool or just the first model. Defaults to True. If this algorithm is initialized with config, the value of `return_modelpool` in the config will be used and this argument passed to the method will be ignored. Returns: Union[nn.Module, BaseModelPool]: The recombined model pool or the first model from the recombined pool, depending on the `return_modelpool` flag. Raises: ValueError: If the models in the model pool are of an unsupported type. """# If the config has a return_modelpool flag, use that, otherwise use the argumentifself.config.get("return_modelpool",None)isnotNone:return_modelpool=self.config.return_modelpool# check the modelpool typeifnotisinstance(modelpool,BaseModelPool):modelpool=BaseModelPool(modelpool)log.info(f"Running model recombination algorithm with {len(modelpool)} models")# TODO: optimize the `recombine_*` functions, if `return_modelpool` is False, we don't need to create the new modelpool, just the first modelmodels=[modelpool.load_model(m)forminmodelpool.model_names]ifisinstance(models[0],nn.ModuleList):new_models=recombine_modellist(models)elifisinstance(models[0],nn.ModuleDict):new_models=recombine_modeldict(models)elifisinstance(models[0],nn.Module):new_models=recombine_state_dict(models)else:raiseValueError(f"Unsupported model type {type(models[0])}")new_modelpool=BaseModelPool({n:mforn,minzip(modelpool.model_names,new_models)})ifreturn_modelpool:returnnew_modelpoolelse:returnnew_modelpool.load_model(new_modelpool.model_names[0])