fusion_bench.compat¶
Method¶
ModelFusionAlgorithm
¶
Bases: ABC
Abstract base class for model fusion algorithms (for v0.1.x versions, deprecated).
For implementing new method, use fusion_bench.method.BaseModelFusionAlgorithm instead.
This class provides a template for implementing model fusion algorithms.
Subclasses must implement the run method to define the fusion logic.
Attributes:
-
config(DictConfig) –Configuration for the algorithm.
Source code in fusion_bench/compat/method/base_algorithm.py
__init__(algorithm_config=None)
¶
Initialize the model fusion algorithm with the given configuration.
Parameters:
-
algorithm_config(Optional[DictConfig], default:None) –Configuration for the algorithm. Defaults to an empty configuration if not provided. Get access to the configuration using
self.config.
Source code in fusion_bench/compat/method/base_algorithm.py
on_run_end()
¶
Hook method called at the end of the run. Can be overridden by subclasses to perform cleanup tasks.
on_run_start()
¶
Hook method called at the start of the run. Can be overridden by subclasses to perform initialization tasks.
run(modelpool)
abstractmethod
¶
Fuse the models in the given model pool.
This method must be implemented by subclasses to define the fusion logic.
modelpool is an object responsible for managing the models to be fused and optional datasets to be used for fusion.
Parameters:
-
modelpool(BaseModelPool) –The pool of models to fuse.
Returns:
-
Any–The fused model.
Examples:
>>> algorithm = SimpleAverageAlgorithm()
>>> modelpool = ModelPool()
>>> merged_model = algorithm.fuse(modelpool)
Source code in fusion_bench/compat/method/base_algorithm.py
AlgorithmFactory
¶
Factory class to create and manage different model fusion algorithms.
This class provides methods to create algorithms based on a given configuration, register new algorithms, and list available algorithms.
Source code in fusion_bench/compat/method/__init__.py
available_algorithms()
classmethod
¶
Get a list of available algorithms.
Returns:
-
list(List[str]) –A list of available algorithm names.
create_algorithm(method_config)
staticmethod
¶
Create an instance of a model fusion algorithm based on the provided configuration.
Parameters:
-
method_config(DictConfig) –The configuration for the algorithm. Must contain a 'name' attribute that specifies the type of the algorithm.
Returns:
-
ModelFusionAlgorithm(ModelFusionAlgorithm) –An instance of the specified algorithm.
Raises:
-
ValueError–If 'name' attribute is not found in the configuration or does not match any known algorithm names.
Source code in fusion_bench/compat/method/__init__.py
register_algorithm(name, algorithm_cls)
staticmethod
¶
Register a new algorithm with the factory.
Parameters:
-
name(str) –The name of the algorithm.
-
algorithm_cls(Type[ModelFusionAlgorithm]) –The class of the algorithm to register.
Source code in fusion_bench/compat/method/__init__.py
Model Pool¶
ModelPool
¶
Bases: ABC
This is the base class for all modelpools.
For verison v0.1.x, deprecated.
Please implemente new algorithms use fusion_bench.modelpool.BaseModelPool.
Source code in fusion_bench/compat/modelpool/base_pool.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |
has_pretrained
property
¶
Check if the pretrained model is available in the model pool.
Returns:
-
bool(bool) –True if the pretrained model is available, False otherwise.
model_names
property
¶
This property returns a list of model names from the configuration, excluding any names that start or end with an underscore.
To obtain all model names, including those starting or ending with an underscore, use the _model_names attribute.
Returns:
-
list(List[str]) –A list of model names.
__init__(modelpool_config=None)
¶
Initialize the ModelPool with the given configuration.
Parameters:
-
modelpool_config(Optional[DictConfig], default:None) –The configuration for the model pool.
Source code in fusion_bench/compat/modelpool/base_pool.py
__len__()
¶
Return the number of models in the model pool, exclude special models such as _pretrained_.
Returns:
-
int(int) –The number of models in the model pool.
get_model_config(model_name)
¶
Retrieves the configuration for a specific model from the model pool.
Parameters:
-
model_name(str) –The name of the model for which to retrieve the configuration.
Returns:
-
dict(Dict) –The configuration dictionary for the specified model.
Raises:
-
ValueError–If the specified model is not found in the model pool.
Source code in fusion_bench/compat/modelpool/base_pool.py
get_test_dataset(model_name)
¶
Get the testing dataset for the model.
Parameters:
-
model_name(str) –The name of the model for which to get the testing dataset.
Returns:
-
Any–The testing dataset for the model.
Source code in fusion_bench/compat/modelpool/base_pool.py
get_train_dataset(model_name)
¶
Get the training dataset for the model.
Parameters:
-
model_name(str) –The name of the model for which to get the training dataset.
Returns:
-
Any–The training dataset for the model.
Source code in fusion_bench/compat/modelpool/base_pool.py
load_model(model_config)
¶
The models are load lazily, so this method should be implemented to load the model from the model pool.
Load the model from the model pool.
Parameters:
-
model_config(Union[str, DictConfig]) –The configuration dictionary for the model to load.
Returns:
-
Any(Module) –The loaded model.
Source code in fusion_bench/compat/modelpool/base_pool.py
load_pretrained_or_first_model(*args, **kwargs)
¶
Load the pretrained model if available, otherwise load the first model in the list.
This method checks if a pretrained model is available. If it is, it loads the pretrained model. If not, it loads the first model from the list of model names.
Returns:
-
–
nn.Module: The loaded model.
Source code in fusion_bench/compat/modelpool/base_pool.py
models()
¶
Generator that yields models from the model pool.
Yields:
-
–
nn.Module: The next model in the model pool.
named_models()
¶
Generator that yields model names and models from the model pool.
Yields:
-
tuple–A tuple containing the model name and the model.
Source code in fusion_bench/compat/modelpool/base_pool.py
save_model(model, path)
¶
Save the state dictionary of the model to the specified path.
Parameters:
-
model(Module) –The model whose state dictionary is to be saved.
-
path(str) –The path where the state dictionary will be saved.
Source code in fusion_bench/compat/modelpool/base_pool.py
setup_taskpool(taskpool)
¶
Setup the taskpool before evaluation. Such as setting the fabric, processor, tokenizer, etc.
Parameters:
-
taskpool(Any) –The taskpool to setup.
to_modeldict()
¶
Convert the model pool to a dictionary of models.
Returns:
-
dict(Dict[str, Module]) –A dictionary of models.
to_modellist()
¶
Convert the model pool to a list of models.
Returns:
-
list(List[Module]) –A list of models.
ModelPoolFactory
¶
Factory class to create and manage different model pools.
This class provides methods to create model pools based on a given configuration, register new model pools, and list available model pools.
Source code in fusion_bench/compat/modelpool/__init__.py
available_modelpools()
classmethod
¶
Get a list of available model pools.
Returns:
-
list–A list of available model pool names.
create_modelpool(modelpool_config)
staticmethod
¶
Create an instance of a model pool based on the provided configuration.
This is for v0.1.x versions, deprecated.
For implementing new model pool, use fusion_bench.modelpool.BaseModelPool instead.
Parameters:
-
modelpool_config(DictConfig) –The configuration for the model pool.
Returns:
-
ModelPool(ModelPool) –An instance of the specified model pool.
Raises:
-
ValueError–If 'type' attribute is not found in the configuration or does not match any known model pool types.
Source code in fusion_bench/compat/modelpool/__init__.py
register_modelpool(name, modelpool_cls)
staticmethod
¶
Register a new model pool with the factory.
Parameters:
-
name(str) –The name of the model pool.
-
modelpool_cls–The class of the model pool to register.
Source code in fusion_bench/compat/modelpool/__init__.py
Task Pool¶
TaskPool
¶
A class to manage a pool of tasks for evaluation.
This is the base class for version 0.1.x, deprecated.
Use fusion_bench.taskpool.BaseTaskPool instead.
Attributes:
-
config(DictConfig) –The configuration for the task pool.
-
_all_task_names(List[str]) –A list of all task names in the task pool.
Source code in fusion_bench/compat/taskpool/base_pool.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | |
task_names
property
¶
Return a list of all task names in the task pool.
Returns:
-
–
List[str]: A list of all task names.
__init__(taskpool_config)
¶
Initialize the TaskPool with the given configuration.
Parameters:
-
taskpool_config(DictConfig) –The configuration for the task pool.
Source code in fusion_bench/compat/taskpool/base_pool.py
evaluate(model)
¶
Evaluate the model on all tasks in the task pool, and return a report.
Take image classification as an example, the report will look like:
{
"mnist": {
"accuracy": 0.8,
"loss": 0.2,
},
<task_name>: {
<metric_name>: <metric_value>,
...
},
}
Parameters:
-
model–The model to evaluate.
Returns:
-
report(dict) –A dictionary containing the results of the evaluation for each task.
Source code in fusion_bench/compat/taskpool/base_pool.py
get_task_config(task_name)
¶
Retrieve the configuration for a specific task from the task pool.
Parameters:
-
task_name(str) –The name of the task for which to retrieve the configuration.
Returns:
-
DictConfig–The configuration dictionary for the specified task.
Raises:
-
ValueError–If the specified task is not found in the task pool.
Source code in fusion_bench/compat/taskpool/base_pool.py
load_task(task_name_or_config)
¶
Load a task from the task pool.
Parameters:
-
task_name_or_config(Union[str, DictConfig]) –The name or configuration of the task to load.
Returns:
-
Any–The loaded task.
Raises:
-
NotImplementedError–If the method is not implemented in the subclass.
Source code in fusion_bench/compat/taskpool/base_pool.py
TaskPoolFactory
¶
Factory class to create and manage different task pools.
This is for v0.1.x versions, deprecated.
For implementing new task pool, use fusion_bench.taskpool.BaseTaskPool instead.
This class provides methods to create task pools based on a given configuration, register new task pools, and list available task pools.
Source code in fusion_bench/compat/taskpool/__init__.py
available_taskpools()
classmethod
¶
Get a list of available task pools.
Returns:
-
list–A list of available task pool names.
create_taskpool(taskpool_config)
staticmethod
¶
Create an instance of a task pool based on the provided configuration.
Parameters:
-
taskpool_config(DictConfig) –The configuration for the task pool. Must contain a 'type' attribute that specifies the type of the task pool.
Returns:
-
TaskPool–An instance of the specified task pool.
Raises:
-
ValueError–If 'type' attribute is not found in the configuration or does not match any known task pool types.
Source code in fusion_bench/compat/taskpool/__init__.py
register_taskpool(name, taskpool_cls)
staticmethod
¶
Register a new task pool with the factory.
Parameters:
-
name(str) –The name of the task pool.
-
taskpool_cls–The class of the task pool to register.