fusion_bench.mixins¶
Class Definitions¶
- fusion_bench.mixins.HydraConfigMixin: A mixin class that provides configuration-based instantiation capabilities.
- fusion_bench.mixins.YAMLSerializationMixin: Provides methods for serializing and deserializing objects to and from YAML format.
- fusion_bench.mixins.SimpleProfilerMixin: Provides simple profiling capabilities for measuring execution time.
- fusion_bench.mixins.LightningFabricMixin: Integrates with Lightning Fabric for automatic distributed environment and accelerator management.
- fusion_bench.mixins.CLIPClassificationMixin: Supports CLIP-based image classification tasks.
References¶
HydraConfigMixin
¶
A mixin class that provides configuration-based instantiation capabilities.
This mixin enables classes to be instantiated directly from Hydra configuration files, supporting both direct instantiation and target-based instantiation patterns. It's particularly useful in FusionBench for creating model pools, task pools, and fusion algorithms from YAML configurations.
The mixin handles: - Configuration loading and composition - Target class validation - Nested configuration group navigation - Object instantiation with proper error handling
Example:
class MyAlgorithm(HydraConfigMixin):
def __init__(self, param1: str, param2: int = 10):
self.param1 = param1
self.param2 = param2
# Instantiate from config
algorithm = MyAlgorithm.from_config("algorithms/my_algorithm")
Note
This mixin requires Hydra to be properly initialized before use. Typically, this is handled by the main FusionBench CLI application.
Source code in fusion_bench/mixins/hydra_config.py
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 |
|
from_config(config_name, overrides=None)
classmethod
¶
Create an instance of the class from a Hydra configuration.
This method loads a Hydra configuration file and instantiates the class using the configuration parameters. It supports both direct parameter passing and target-based instantiation patterns.
Parameters:
-
config_name
(Union[str, Path]
) –The name/path of the configuration file to load. Can be a string like "algorithms/simple_average" or a Path object. The .yaml extension is optional.
-
overrides
(Optional[List[str]]
, default:None
) –Optional list of configuration overrides in the format ["key=value", "nested.key=value"]. These allow runtime modification of configuration parameters.
Returns:
-
–
An instance of the class configured according to the loaded configuration.
Raises:
-
RuntimeError
–If Hydra is not properly initialized.
-
ImportError
–If a target class specified in the config cannot be imported.
-
ValueError
–If required configuration parameters are missing.
Example
Note
The method automatically handles nested configuration groups by navigating through the configuration hierarchy based on the config_name path structure.
Source code in fusion_bench/mixins/hydra_config.py
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 |
|
YAMLSerializationMixin
¶
Source code in fusion_bench/mixins/serialization.py
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
|
config
property
¶
Returns the configuration of the model pool as a DictConfig.
This property calls the to_config
method to convert the model pool
instance into a dictionary configuration, which can be used for
serialization or other purposes.
Example:
model = SomeModelFusionAlgorithm(hyper_param_1=1, hyper_param_2=2)
config = model.config
print(config)
# DictConfig({'_target_': 'SomeModelFusionAlgorithm', 'hyper_param_1': 1, 'hyper_param_2': 2})
This is useful for serializing the object to a YAML file or for debugging.
Returns:
-
DictConfig
–The configuration of the model pool.
from_yaml(path)
classmethod
¶
Load a model pool from a YAML file.
Parameters:
-
path
(Union[str, Path]
) –The path to load the model pool from.
Returns:
-
BaseModelPool
–The loaded model pool.
Source code in fusion_bench/mixins/serialization.py
to_config()
¶
Convert the model pool to a DictConfig.
Returns:
-
Dict
–The model pool as a DictConfig.
Source code in fusion_bench/mixins/serialization.py
to_yaml(path)
¶
Save the model pool to a YAML file.
Parameters:
-
path
(Union[str, Path]
) –The path to save the model pool to.
Source code in fusion_bench/mixins/serialization.py
BaseYAMLSerializableModel
¶
Bases: YAMLSerializationMixin
A base class for YAML-serializable classes with enhanced metadata support.
This class extends YAMLSerializationMixin
to provide additional metadata
fields commonly used in FusionBench classes, including usage information
and version tracking. It serves as a foundation for all serializable
model components in the framework.
The class automatically handles serialization of usage and version metadata alongside the standard configuration parameters, making it easier to track model provenance and intended usage patterns.
Attributes:
-
_usage_
(Optional[str]
) –Description of the model's intended usage or purpose.
-
_version_
(Optional[str]
) –Version information for the model or configuration.
Example
class MyAlgorithm(BaseYAMLSerializableModel):
_config_mapping = BaseYAMLSerializableModel._config_mapping | {
"model_name": "model_name",
"num_layers": "num_layers",
}
def __init__(self, _usage_: str = None, _version_: str = None):
super().__init__(_usage_=_usage_, _version_=_version_)
# Usage with metadata
model = MyAlgorithm(
_usage_="Text classification fine-tuning",
_version_="1.0.0"
)
# Serialization includes metadata
config = model.config
# DictConfig({
# '_target_': 'MyModel',
# '_usage_': 'Text classification fine-tuning',
# '_version_': '1.0.0'
# })
Note
The underscore prefix in _usage_
and _version_
follows the convention
for metadata fields that are not core model parameters but provide
important contextual information for model management and tracking.
Source code in fusion_bench/mixins/serialization.py
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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
|
__init__(_usage_=None, _version_=None, **kwargs)
¶
Initialize a base YAML-serializable model with metadata support.
Parameters:
-
_usage_
(Optional[str]
, default:None
) –Description of the model's intended usage or purpose. This can include information about the training domain, expected input types, or specific use cases. Defaults to None.
-
_version_
(Optional[str]
, default:None
) –Version information for the model or configuration. Can be used to track model iterations, dataset versions, or compatibility information. Defaults to None.
-
**kwargs
–Additional keyword arguments passed to the parent class. Unused arguments will trigger warnings via the parent's initialization.
Example
Source code in fusion_bench/mixins/serialization.py
SimpleProfilerMixin
¶
A mixin class that provides simple profiling capabilities.
This mixin allows for easy profiling of code blocks using a context manager. It also provides methods to start and stop profiling actions, and to print a summary of the profiling results.
Examples:
class MyClass(SimpleProfilerMixin):
def do_something(self):
with self.profile("work"):
# do some work here
...
with self.profile("more work"):
# do more work here
...
# print the profiling summary
self.print_profile_summary()
Attributes:
-
_profiler
(SimpleProfiler
) –An instance of the SimpleProfiler class used for profiling.
Source code in fusion_bench/mixins/simple_profiler.py
profile(action_name)
¶
Context manager for profiling a code block
Example:
Source code in fusion_bench/mixins/simple_profiler.py
LightningFabricMixin
¶
A mixin class for integrating Lightning Fabric into a project.
This class provides methods to initialize and manage a Lightning Fabric instance for distributed computing, including setup with optional logging, device management for tensors and modules, and hyperparameter logging. It leverages the Lightning framework to facilitate distributed training and inference across multiple devices and nodes, with support for custom logging via TensorBoard.
Attributes:
- _fabric (L.Fabric): The Lightning Fabric instance used for distributed computing.
Note:
This mixin is designed to be used with classes that require distributed computing capabilities and wish to
leverage the Lightning Fabric for this purpose. It assumes the presence of a config
attribute or parameter
in the consuming class for configuration.
Source code in fusion_bench/mixins/lightning_fabric.py
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 |
|
log_dir
property
¶
Retrieves the log directory from the fabric's logger.
log(name, value, step=None)
¶
log_dict(metrics, step=None)
¶
log_hyperparams(config=None, save_dir=None, filename='config.yaml')
¶
Logs the hyperparameters and saves the configuration to a YAML file.
The YAML file is saved in the log directory by default with the name config.yaml
, or in the specified save directory save_dir
.
Parameters:
-
config
(Optional[DictConfig]
, default:None
) –The configuration to log and save. If not provided, the class's
config
attribute is used. -
save_dir
(Optional[str]
, default:None
) –The directory in which to save the configuration file. If not provided, the log directory is used.
-
filename
(str
, default:'config.yaml'
) –The name of the configuration file. Default is
config.yaml
.
Source code in fusion_bench/mixins/lightning_fabric.py
log_optimizer_lr(optimizer, step=None, name_template='train/lr_group_{0}')
¶
Logs the learning rate of the optimizer to the fabric's logger.
Source code in fusion_bench/mixins/lightning_fabric.py
setup_lightning_fabric(config)
¶
Initializes and launches the Lightning Fabric with optional logging.
This method sets up the Lightning Fabric for distributed computing based on the provided configuration. If a fabric configuration is not found, it logs a warning and exits. Optionally, if a fabric logger configuration is provided, it initializes a TensorBoardLogger with the specified settings.
Expected configuration keys: - fabric: The configuration for the Lightning Fabric. - fabric.loggers: The configuration for the TensorBoardLogger.
Source code in fusion_bench/mixins/lightning_fabric.py
to_device(obj)
¶
Moves a tensor or module to the proper device.
Parameters:
-
obj
(TensorOrModule
) –The tensor or module to move to the device.
Returns:
-
TensorOrModule
(TensorOrModule
) –the same type of object as the input, moved to the device.
Source code in fusion_bench/mixins/lightning_fabric.py
CLIPClassificationMixin
¶
Bases: LightningFabricMixin
This mixin provides methods to classify images using the CLIP model.
Attributes need to be set by the inheriting class:
_dataloader_kwargs
(Dict[str, Any]): Keyword arguments for the dataloader.modelpool
(CLIPVisionModelPool): The model pool containing the CLIP models.zeroshot_weights_cache_dir
(Optional[str]): The directory to cache the zero-shot weights.
Source code in fusion_bench/mixins/clip_classification.py
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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
|
compute_features(module, images, normalize=True)
¶
Extracts image features using CLIP's vision encoder and visual projection.
Parameters:
-
module
(Union[Module, CLIPVisionModel, CLIPVisionTransformer]
) –The CLIP vision encoder module.
-
images
(Tensor
) –Input image batch to process.
-
normalize
(bool
, default:True
) –Whether to normalize the image embeddings.
Returns:
-
Tensor
–torch.Tensor: Normalized image embeddings with dimension matching CLIP's projection space (
projection_dim
in model config).
Source code in fusion_bench/mixins/clip_classification.py
compute_logits(module, images, task, image_embeds=None)
¶
Compute the logits of the images for a given task.
Parameters:
-
module
(Union[Module, CLIPVisionModel, CLIPVisionTransformer]
) –The module to compute the logits.
-
images
(Tensor
) –The images to compute the logits.
-
task
(str
) –The task to compute the logits.
-
image_embeds
(Optional[Tensor]
, default:None
) –The precomputed image embeddings. If None, the image embeddings will be computed.
Returns:
-
Tensor
–torch.Tensor: The logits of the images.
Source code in fusion_bench/mixins/clip_classification.py
get_shuffled_test_loader_iter(task, batch_size=None, num_workers=None, **loader_kwargs)
cached
¶
Get an iterator for a shuffled test DataLoader.
This method creates a DataLoader for the test dataset of the specified task, with shuffling enabled. It allows for optional customization of batch size, number of workers, and other DataLoader keyword arguments.
Parameters:
-
task
(str
) –The task identifier for which the test dataset is to be loaded.
-
batch_size
(Optional[int]
, default:None
) –The batch size to use for the DataLoader. If None, the default batch size is used.
-
num_workers
(Optional[int]
, default:None
) –The number of worker processes to use for data loading. If None, the default number of workers is used.
-
**loader_kwargs
–Additional keyword arguments to pass to the DataLoader.
Returns:
-
Iterator
–An iterator over the shuffled test DataLoader.