fusion_bench.modelpool¶
Base Class¶
BaseModelPool
¶
Bases: BaseYAMLSerializableModel
, HydraConfigMixin
A class for managing and interacting with a pool of models along with their associated datasets or other specifications. For example, a model pool may contain multiple models, each with its own training, validation, and testing datasets. As for the specifications, a vision model pool may contain image preprocessor, and a language model pool may contain a tokenizer.
Attributes:
-
_models
(DictConfig
) –Configuration for all models in the pool.
-
_train_datasets
(Optional[DictConfig]
) –Configuration for training datasets.
-
_val_datasets
(Optional[DictConfig]
) –Configuration for validation datasets.
-
_test_datasets
(Optional[DictConfig]
) –Configuration for testing datasets.
-
_usage_
(Optional[str]
) –Optional usage information.
-
_version_
(Optional[str]
) –Optional version information.
Source code in fusion_bench/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 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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
|
all_model_names
property
¶
Get the names of all models in the pool, including special models.
Returns:
-
List[str]
–List[str]: A list of all model names.
has_pretrained
property
¶
Check if the model pool contains a pretrained model.
Returns:
-
bool
(bool
) –True if a pretrained model is available, False otherwise.
model_names
property
¶
Get the names of regular models, excluding special models.
Returns:
-
List[str]
–List[str]: A list of regular model names.
test_dataset_names
property
¶
Get the names of testing datasets.
Returns:
-
List[str]
–List[str]: A list of testing dataset names.
train_dataset_names
property
¶
Get the names of training datasets.
Returns:
-
List[str]
–List[str]: A list of training dataset names.
val_dataset_names
property
¶
Get the names of validation datasets.
Returns:
-
List[str]
–List[str]: A list of validation dataset names.
get_model_config(model_name, return_copy=True)
¶
Get the configuration for the specified model.
Parameters:
-
model_name
(str
) –The name of the model.
Returns:
-
DictConfig
(DictConfig
) –The configuration for the specified model.
Source code in fusion_bench/modelpool/base_pool.py
is_special_model(model_name)
staticmethod
¶
Determine if a model is special based on its name.
Parameters:
-
model_name
(str
) –The name of the model.
Returns:
-
bool
(bool
) –True if the model name indicates a special model, False otherwise.
Source code in fusion_bench/modelpool/base_pool.py
load_model(model_name_or_config, *args, **kwargs)
¶
Load a model from the pool based on the provided configuration.
Parameters:
-
model_name_or_config
(Union[str, DictConfig]
) –The model name or configuration.
Returns:
-
Module
–nn.Module: The instantiated model.
Source code in fusion_bench/modelpool/base_pool.py
load_pretrained_or_first_model(*args, **kwargs)
¶
Load the pretrained model if available, otherwise load the first available model.
Returns:
-
–
nn.Module: The loaded model.
Source code in fusion_bench/modelpool/base_pool.py
load_test_dataset(dataset_name, *args, **kwargs)
¶
Load the testing dataset for the specified model.
Parameters:
-
dataset_name
(str
) –The name of the model.
Returns:
-
Dataset
(Dataset
) –The instantiated testing dataset.
Source code in fusion_bench/modelpool/base_pool.py
load_train_dataset(dataset_name, *args, **kwargs)
¶
Load the training dataset for the specified model.
Parameters:
-
dataset_name
(str
) –The name of the model.
Returns:
-
Dataset
(Dataset
) –The instantiated training dataset.
Source code in fusion_bench/modelpool/base_pool.py
load_val_dataset(dataset_name, *args, **kwargs)
¶
Load the validation dataset for the specified model.
Parameters:
-
dataset_name
(str
) –The name of the model.
Returns:
-
Dataset
(Dataset
) –The instantiated validation dataset.
Source code in fusion_bench/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/modelpool/base_pool.py
Vision Model Pool¶
NYUv2 Tasks (ResNet)¶
NYUv2ModelPool
¶
Bases: ModelPool
Source code in fusion_bench/modelpool/nyuv2_modelpool.py
CLIP Vision Encoder¶
CLIPVisionModelPool
¶
Bases: BaseModelPool
A model pool for managing Hugging Face's CLIP Vision models.
This class extends the base ModelPool
class and overrides its methods to handle
the specifics of the CLIP Vision models provided by the Hugging Face Transformers library.
Source code in fusion_bench/modelpool/clip_vision/modelpool.py
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 |
|
load_model(model_name_or_config, *args, **kwargs)
¶
This method is used to load a CLIPVisionModel from the model pool.
Example configuration could be:
models:
cifar10: tanganke/clip-vit-base-patch32_cifar10
sun397: tanganke/clip-vit-base-patch32_sun397
stanford-cars: tanganke/clip-vit-base-patch32_stanford-cars
Parameters:
-
model_name_or_config
(Union[str, DictConfig]
) –The name of the model or the model configuration.
Returns:
-
CLIPVisionModel
(CLIPVisionModel
) –The loaded CLIPVisionModel.
Source code in fusion_bench/modelpool/clip_vision/modelpool.py
save_model(model, path)
¶
Save a CLIP Vision model to the given path.
Parameters:
-
model
(CLIPVisionModel
) –The model to save.
-
path
(str
) –The path to save the model to.
Source code in fusion_bench/modelpool/clip_vision/modelpool.py
OpenCLIP Vision Encoder¶
OpenCLIPVisionModelPool
¶
Bases: BaseModelPool
A model pool for managing OpenCLIP Vision models (models from task vector paper).
Source code in fusion_bench/modelpool/openclip_vision/modelpool.py
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 253 254 255 |
|
load_classification_head(model_name_or_config, *args, **kwargs)
¶
The model config can be:
- A string, which is the path to the model checkpoint in pickle format. Load directly using
torch.load
. - Default, load the model using
instantiate
from hydra.
Source code in fusion_bench/modelpool/openclip_vision/modelpool.py
load_model(model_name_or_config, *args, **kwargs)
¶
The model config can be:
- A string, which is the path to the model checkpoint in pickle format. Load directly using
torch.load
. - {"model_name": str, "pickle_path": str}, load the model from the binary file (pickle format). This will first construct the model using
ImageEncoder(model_name)
, and then load the state dict from model located in the pickle file. - {"model_name": str, "state_dict_path": str}, load the model from the state dict file. This will first construct the model using
ImageEncoder(model_name)
, and then load the state dict from the file. - Default, load the model using
instantiate
from hydra.
Source code in fusion_bench/modelpool/openclip_vision/modelpool.py
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 |
|
NLP Model Pool¶
GPT-2¶
HuggingFaceGPT2ClassificationPool = GPT2ForSequenceClassificationPool
module-attribute
¶
GPT2ForSequenceClassificationPool
¶
Bases: BaseModelPool
Source code in fusion_bench/modelpool/huggingface_gpt2_classification.py
Seq2Seq Language Models (Flan-T5)¶
Seq2SeqLMPool
¶
Bases: BaseModelPool
Source code in fusion_bench/modelpool/seq2seq_lm/modelpool.py
SequenceClassificationModelPool
¶
Bases: BaseModelPool
Source code in fusion_bench/modelpool/seq_classification_lm/seq_classification_lm.py
save_model(model, path, push_to_hub=False, model_dtype=None, save_tokenizer=False, tokenizer_kwargs=None, **kwargs)
¶
Save the model to the specified path.
Parameters:
-
model
(PreTrainedModel
) –The model to be saved.
-
path
(str
) –The path where the model will be saved.
-
push_to_hub
(bool
, default:False
) –Whether to push the model to the Hugging Face Hub. Defaults to False.
-
save_tokenizer
(bool
, default:False
) –Whether to save the tokenizer along with the model. Defaults to False.
-
**kwargs
–Additional keyword arguments passed to the
save_pretrained
method.
Source code in fusion_bench/modelpool/seq_classification_lm/seq_classification_lm.py
PeftModelForSeq2SeqLMPool
¶
Bases: ModelPool
Source code in fusion_bench/modelpool/PeftModelForSeq2SeqLM.py
load_model(model_config)
¶
Load a model based on the provided configuration.
The configuration options of model_config
are:
- name: The name of the model. If it is "pretrained", a pretrained Seq2Seq language model is returned.
- path: The path where the model is stored.
- is_trainable: A boolean indicating whether the model parameters should be trainable. Default is
True
. - merge_and_unload: A boolean indicating whether to merge and unload the PEFT model after loading. Default is
True
.
Parameters:
-
model_config
(str | DictConfig
) –The configuration for the model. This can be either a string (name of the model) or a DictConfig object containing the model configuration.
Returns:
-
model
–The loaded model. If the model name is "pretrained", it returns a pretrained Seq2Seq language model. Otherwise, it returns a PEFT model.
Source code in fusion_bench/modelpool/PeftModelForSeq2SeqLM.py
Causal Language Models (Llama, Mistral, Qwen...)¶
CausalLMPool
¶
Bases: BaseModelPool
Source code in fusion_bench/modelpool/causal_lm/causal_lm.py
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 |
|
load_model(model_name_or_config, *args, **kwargs)
¶
Example of YAML config:
models:
_pretrained_: path_to_pretrained_model # if a plain string, it will be passed to AutoModelForCausalLM.from_pretrained
model_a: path_to_model_a
model_b: path_to_model_b
or equivalently,
models:
_pretrained_:
_target_: transformers.AutoModelForCausalLM # any callable that returns a model
pretrained_model_name_or_path: path_to_pretrained_model
model_a:
_target_: transformers.AutoModelForCausalLM
pretrained_model_name_or_path: path_to_model_a
model_b:
_target_: transformers.AutoModelForCausalLM
pretrained_model_name_or_path: path_to_model_b
Source code in fusion_bench/modelpool/causal_lm/causal_lm.py
load_tokenizer(*args, **kwargs)
¶
Example of YAML config:
tokenizer: google/gemma-2-2b-it # if a plain string, it will be passed to AutoTokenizer.from_pretrained
or equivalently,
tokenizer:
_target_: transformers.AutoTokenizer # any callable that returns a tokenizer
pretrained_model_name_or_path: google/gemma-2-2b-it
Returns:
-
PreTrainedTokenizer
(PreTrainedTokenizer
) –The tokenizer.
Source code in fusion_bench/modelpool/causal_lm/causal_lm.py
save_model(model, path, push_to_hub=False, model_dtype=None, save_tokenizer=False, tokenizer_kwargs=None, tokenizer=None, **kwargs)
¶
Save the model to the specified path.
Parameters:
-
model
(PreTrainedModel
) –The model to be saved.
-
path
(str
) –The path where the model will be saved.
-
push_to_hub
(bool
, default:False
) –Whether to push the model to the Hugging Face Hub. Defaults to False.
-
save_tokenizer
(bool
, default:False
) –Whether to save the tokenizer along with the model. Defaults to False.
-
**kwargs
–Additional keyword arguments passed to the
save_pretrained
method.
Source code in fusion_bench/modelpool/causal_lm/causal_lm.py
CausalLMBackbonePool
¶
Bases: CausalLMPool
Source code in fusion_bench/modelpool/causal_lm/causal_lm.py
Others¶
Transformers AutoModel¶
AutoModelPool
¶
Bases: ModelPool