fusion-bench 0.2.22__py3-none-any.whl → 0.2.23__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. fusion_bench/__init__.py +4 -0
  2. fusion_bench/compat/method/__init__.py +5 -2
  3. fusion_bench/compat/method/base_algorithm.py +3 -2
  4. fusion_bench/compat/modelpool/base_pool.py +3 -3
  5. fusion_bench/compat/taskpool/clip_image_classification.py +1 -1
  6. fusion_bench/dataset/gpt2_glue.py +1 -1
  7. fusion_bench/method/__init__.py +4 -2
  8. fusion_bench/method/analysis/task_vector_cos_similarity.py +95 -12
  9. fusion_bench/method/analysis/task_vector_violin_plot.py +160 -52
  10. fusion_bench/method/bitdelta/bitdelta.py +7 -23
  11. fusion_bench/method/expert_sparsity/mixtral/dynamic_skipping.py +2 -0
  12. fusion_bench/method/expert_sparsity/mixtral/layer_wise_pruning.py +2 -0
  13. fusion_bench/method/expert_sparsity/mixtral/progressive_pruning.py +2 -0
  14. fusion_bench/method/model_stock/__init__.py +1 -0
  15. fusion_bench/method/model_stock/model_stock.py +309 -0
  16. fusion_bench/method/regmean/clip_regmean.py +3 -6
  17. fusion_bench/method/regmean/regmean.py +27 -56
  18. fusion_bench/method/regmean/utils.py +56 -0
  19. fusion_bench/method/regmean_plusplus/regmean_plusplus.py +21 -60
  20. fusion_bench/method/slerp/__init__.py +1 -1
  21. fusion_bench/method/slerp/slerp.py +110 -14
  22. fusion_bench/method/we_moe/flan_t5_we_moe.py +9 -20
  23. fusion_bench/mixins/clip_classification.py +26 -6
  24. fusion_bench/mixins/serialization.py +25 -15
  25. fusion_bench/modelpool/base_pool.py +1 -1
  26. fusion_bench/modelpool/causal_lm/causal_lm.py +262 -43
  27. fusion_bench/modelpool/seq2seq_lm/modelpool.py +146 -0
  28. fusion_bench/models/hf_utils.py +9 -4
  29. fusion_bench/models/linearized/vision_model.py +6 -6
  30. fusion_bench/models/modeling_smile_mistral/__init__.py +1 -0
  31. fusion_bench/models/we_moe.py +8 -8
  32. fusion_bench/taskpool/base_pool.py +99 -17
  33. fusion_bench/taskpool/clip_vision/taskpool.py +1 -1
  34. fusion_bench/taskpool/dummy.py +101 -13
  35. fusion_bench/taskpool/lm_eval_harness/taskpool.py +80 -0
  36. fusion_bench/taskpool/nyuv2_taskpool.py +28 -0
  37. fusion_bench/utils/__init__.py +1 -0
  38. fusion_bench/utils/data.py +6 -4
  39. fusion_bench/utils/devices.py +7 -4
  40. fusion_bench/utils/dtype.py +3 -2
  41. fusion_bench/utils/lazy_state_dict.py +82 -19
  42. fusion_bench/utils/packages.py +3 -3
  43. fusion_bench/utils/parameters.py +0 -2
  44. fusion_bench/utils/timer.py +92 -10
  45. {fusion_bench-0.2.22.dist-info → fusion_bench-0.2.23.dist-info}/METADATA +1 -1
  46. {fusion_bench-0.2.22.dist-info → fusion_bench-0.2.23.dist-info}/RECORD +53 -47
  47. fusion_bench_config/_get_started/llm_slerp.yaml +12 -0
  48. fusion_bench_config/method/model_stock/model_stock.yaml +12 -0
  49. fusion_bench_config/method/slerp/slerp_lm.yaml +4 -0
  50. {fusion_bench-0.2.22.dist-info → fusion_bench-0.2.23.dist-info}/WHEEL +0 -0
  51. {fusion_bench-0.2.22.dist-info → fusion_bench-0.2.23.dist-info}/entry_points.txt +0 -0
  52. {fusion_bench-0.2.22.dist-info → fusion_bench-0.2.23.dist-info}/licenses/LICENSE +0 -0
  53. {fusion_bench-0.2.22.dist-info → fusion_bench-0.2.23.dist-info}/top_level.txt +0 -0
@@ -33,6 +33,44 @@ log = logging.getLogger(__name__)
33
33
 
34
34
  @auto_register_config
35
35
  class CausalLMPool(BaseModelPool):
36
+ """A model pool for managing and loading causal language models.
37
+
38
+ This class provides a unified interface for loading and managing multiple
39
+ causal language models, typically used in model fusion and ensemble scenarios.
40
+ It supports both eager and lazy loading strategies, and handles model
41
+ configuration through YAML configs or direct instantiation.
42
+
43
+ The pool can manage models from Hugging Face Hub, local paths, or custom
44
+ configurations. It also provides tokenizer management and model saving
45
+ capabilities with optional Hugging Face Hub integration.
46
+
47
+ Args:
48
+ models: Dictionary or configuration specifying the models to be managed.
49
+ Can contain model names mapped to paths or detailed configurations.
50
+ tokenizer: Tokenizer configuration, either a string path/name or
51
+ a DictConfig with detailed tokenizer settings.
52
+ model_kwargs: Additional keyword arguments passed to model loading.
53
+ Common options include torch_dtype, device_map, etc.
54
+ enable_lazy_loading: Whether to use lazy loading for models. When True,
55
+ models are loaded as LazyStateDict objects instead of actual models,
56
+ which can save memory for large model collections.
57
+ **kwargs: Additional arguments passed to the parent BaseModelPool.
58
+
59
+ Example:
60
+ ```python
61
+ >>> pool = CausalLMPool(
62
+ ... models={
63
+ ... "model_a": "microsoft/DialoGPT-medium",
64
+ ... "model_b": "/path/to/local/model"
65
+ ... },
66
+ ... tokenizer="microsoft/DialoGPT-medium",
67
+ ... model_kwargs={"torch_dtype": "bfloat16"}
68
+ ... )
69
+ >>> model = pool.load_model("model_a")
70
+ >>> tokenizer = pool.load_tokenizer()
71
+ ```
72
+ """
73
+
36
74
  def __init__(
37
75
  self,
38
76
  models,
@@ -47,6 +85,20 @@ class CausalLMPool(BaseModelPool):
47
85
  self.model_kwargs = DictConfig({})
48
86
 
49
87
  def get_model_path(self, model_name: str):
88
+ """Extract the model path from the model configuration.
89
+
90
+ Args:
91
+ model_name: The name of the model as defined in the models configuration.
92
+
93
+ Returns:
94
+ str: The path or identifier for the model. For string configurations,
95
+ returns the string directly. For dict configurations, extracts
96
+ the 'pretrained_model_name_or_path' field.
97
+
98
+ Raises:
99
+ RuntimeError: If the model configuration is invalid or the model
100
+ name is not found in the configuration.
101
+ """
50
102
  model_name_or_config = self._models[model_name]
51
103
  if isinstance(model_name_or_config, str):
52
104
  return model_name_or_config
@@ -56,6 +108,16 @@ class CausalLMPool(BaseModelPool):
56
108
  raise RuntimeError("Invalid model configuration")
57
109
 
58
110
  def get_model_kwargs(self):
111
+ """Get processed model keyword arguments for model loading.
112
+
113
+ Converts the stored `model_kwargs` from DictConfig to a regular dictionary
114
+ and processes special arguments like torch_dtype for proper model loading.
115
+
116
+ Returns:
117
+ dict: Processed keyword arguments ready to be passed to model
118
+ loading functions. The torch_dtype field, if present, is
119
+ converted from string to the appropriate torch dtype object.
120
+ """
59
121
  model_kwargs = (
60
122
  OmegaConf.to_container(self.model_kwargs, resolve=True)
61
123
  if isinstance(self.model_kwargs, DictConfig)
@@ -71,31 +133,52 @@ class CausalLMPool(BaseModelPool):
71
133
  model_name_or_config: str | DictConfig,
72
134
  *args,
73
135
  **kwargs,
74
- ) -> PreTrainedModel:
75
- """
76
- Example of YAML config:
136
+ ) -> Union[PreTrainedModel, LazyStateDict]:
137
+ """Load a causal language model from the model pool.
77
138
 
78
- ```yaml
79
- models:
80
- _pretrained_: path_to_pretrained_model # if a plain string, it will be passed to AutoModelForCausalLM.from_pretrained
81
- model_a: path_to_model_a
82
- model_b: path_to_model_b
83
- ```
139
+ This method supports multiple loading strategies:
140
+ 1. Loading by model name from the configured model pool
141
+ 2. Loading from a direct configuration dictionary
142
+ 3. Lazy loading using LazyStateDict for memory efficiency
84
143
 
85
- or equivalently,
86
-
87
- ```yaml
88
- models:
89
- _pretrained_:
90
- _target_: transformers.AutoModelForCausalLM # any callable that returns a model
91
- pretrained_model_name_or_path: path_to_pretrained_model
92
- model_a:
93
- _target_: transformers.AutoModelForCausalLM
94
- pretrained_model_name_or_path: path_to_model_a
95
- model_b:
96
- _target_: transformers.AutoModelForCausalLM
97
- pretrained_model_name_or_path: path_to_model_b
98
- ```
144
+ The method automatically handles different model configuration formats
145
+ and applies the appropriate loading strategy based on the enable_lazy_loading flag.
146
+
147
+ Args:
148
+ model_name_or_config: Either a string model name that exists in the
149
+ model pool configuration, or a DictConfig/dict containing the
150
+ model configuration directly.
151
+ *args: Additional positional arguments passed to the model constructor.
152
+ **kwargs: Additional keyword arguments passed to the model constructor.
153
+ These will be merged with the pool's model_kwargs.
154
+
155
+ Returns:
156
+ Union[PreTrainedModel, LazyStateDict]: The loaded model. Returns a
157
+ PreTrainedModel for normal loading or a LazyStateDict for lazy loading.
158
+
159
+ Raises:
160
+ RuntimeError: If the model configuration is invalid.
161
+ KeyError: If the model name is not found in the model pool.
162
+
163
+ Example YAML configurations:
164
+ Simple string configuration:
165
+ ```yaml
166
+ models:
167
+ _pretrained_: path_to_pretrained_model
168
+ model_a: path_to_model_a
169
+ model_b: path_to_model_b
170
+ ```
171
+
172
+ Detailed configuration:
173
+ ```yaml
174
+ models:
175
+ _pretrained_:
176
+ _target_: transformers.AutoModelForCausalLM
177
+ pretrained_model_name_or_path: path_to_pretrained_model
178
+ model_a:
179
+ _target_: transformers.AutoModelForCausalLM
180
+ pretrained_model_name_or_path: path_to_model_a
181
+ ```
99
182
  """
100
183
  model_kwargs = self.get_model_kwargs()
101
184
  model_kwargs.update(kwargs)
@@ -139,23 +222,36 @@ class CausalLMPool(BaseModelPool):
139
222
  return model
140
223
 
141
224
  def load_tokenizer(self, *args, **kwargs) -> PreTrainedTokenizer:
142
- """
143
- Example of YAML config:
144
-
145
- ```yaml
146
- tokenizer: google/gemma-2-2b-it # if a plain string, it will be passed to AutoTokenizer.from_pretrained
147
- ```
225
+ """Load the tokenizer associated with this model pool.
148
226
 
149
- or equivalently,
227
+ Loads a tokenizer based on the tokenizer configuration provided during
228
+ pool initialization. Supports both simple string paths and detailed
229
+ configuration dictionaries.
150
230
 
151
- ```yaml
152
- tokenizer:
153
- _target_: transformers.AutoTokenizer # any callable that returns a tokenizer
154
- pretrained_model_name_or_path: google/gemma-2-2b-it
155
- ```
231
+ Args:
232
+ *args: Additional positional arguments passed to the tokenizer constructor.
233
+ **kwargs: Additional keyword arguments passed to the tokenizer constructor.
156
234
 
157
235
  Returns:
158
- PreTrainedTokenizer: The tokenizer.
236
+ PreTrainedTokenizer: The loaded tokenizer instance.
237
+
238
+ Raises:
239
+ AssertionError: If no tokenizer is defined in the configuration.
240
+
241
+ Example YAML configurations:
242
+ Simple string configuration:
243
+ ```yaml
244
+ tokenizer: google/gemma-2-2b-it
245
+ ```
246
+
247
+ Detailed configuration:
248
+ ```yaml
249
+ tokenizer:
250
+ _target_: transformers.AutoTokenizer
251
+ pretrained_model_name_or_path: google/gemma-2-2b-it
252
+ use_fast: true
253
+ padding_side: left
254
+ ```
159
255
  """
160
256
  assert self.tokenizer is not None, "Tokenizer is not defined in the config"
161
257
  log.info("Loading tokenizer.", stacklevel=2)
@@ -177,15 +273,49 @@ class CausalLMPool(BaseModelPool):
177
273
  tokenizer: Optional[PreTrainedTokenizer] = None,
178
274
  **kwargs,
179
275
  ):
180
- """
181
- Save the model to the specified path.
276
+ """Save a model to the specified path with optional tokenizer and Hub upload.
277
+
278
+ This method provides comprehensive model saving capabilities including
279
+ optional tokenizer saving, dtype conversion, and Hugging Face Hub upload.
280
+ The model is saved in the standard Hugging Face format.
182
281
 
183
282
  Args:
184
- model (PreTrainedModel): The model to be saved.
185
- path (str): The path where the model will be saved.
186
- push_to_hub (bool, optional): Whether to push the model to the Hugging Face Hub. Defaults to False.
187
- save_tokenizer (bool, optional): Whether to save the tokenizer along with the model. Defaults to False.
188
- **kwargs: Additional keyword arguments passed to the `save_pretrained` method.
283
+ model: The PreTrainedModel instance to be saved.
284
+ path: The local path where the model will be saved. Supports tilde
285
+ expansion for home directory paths.
286
+ push_to_hub: Whether to push the saved model to the Hugging Face Hub.
287
+ Requires proper authentication and repository permissions.
288
+ model_dtype: Optional string specifying the target dtype for the model
289
+ before saving (e.g., "float16", "bfloat16"). The model will be
290
+ converted to this dtype before saving.
291
+ save_tokenizer: Whether to save the tokenizer alongside the model.
292
+ If True, the tokenizer will be loaded using the pool's tokenizer
293
+ configuration and saved to the same path.
294
+ tokenizer_kwargs: Additional keyword arguments for tokenizer loading
295
+ when save_tokenizer is True.
296
+ tokenizer: Optional pre-loaded tokenizer instance. If provided, this
297
+ tokenizer will be saved regardless of the save_tokenizer flag.
298
+ **kwargs: Additional keyword arguments passed to the model's
299
+ save_pretrained method.
300
+
301
+ Side Effects:
302
+ - Creates model files in the specified directory
303
+ - Optionally creates tokenizer files in the same directory
304
+ - May convert the model to a different dtype
305
+ - May upload files to Hugging Face Hub
306
+
307
+ Example:
308
+ ```python
309
+ >>> pool = CausalLMPool(models=..., tokenizer=...)
310
+ >>> model = pool.load_model("my_model")
311
+ >>> pool.save_model(
312
+ ... model,
313
+ ... "/path/to/save",
314
+ ... save_tokenizer=True,
315
+ ... model_dtype="float16",
316
+ ... push_to_hub=True
317
+ ... )
318
+ ```
189
319
  """
190
320
  path = os.path.expanduser(path)
191
321
  # NOTE: if tokenizer is provided, it will be saved regardless of `save_tokenizer`
@@ -209,9 +339,55 @@ class CausalLMPool(BaseModelPool):
209
339
 
210
340
 
211
341
  class CausalLMBackbonePool(CausalLMPool):
342
+ """A specialized model pool that loads only the transformer backbone layers.
343
+
344
+ This class extends CausalLMPool to provide access to just the transformer
345
+ layers (backbone) of causal language models, excluding the language modeling
346
+ head and embeddings. This is useful for model fusion scenarios where only
347
+ the core transformer layers are needed.
348
+
349
+ The class automatically extracts the `model.layers` component from loaded
350
+ AutoModelForCausalLM instances, providing direct access to the transformer
351
+ blocks. Lazy loading is not supported for this pool type.
352
+
353
+ Note:
354
+ This pool automatically disables lazy loading as it needs to access
355
+ the internal structure of the model to extract the backbone layers.
356
+
357
+ Example:
358
+ ```python
359
+ >>> backbone_pool = CausalLMBackbonePool(
360
+ ... models={"model_a": "microsoft/DialoGPT-medium"},
361
+ ... tokenizer="microsoft/DialoGPT-medium"
362
+ ... )
363
+ >>> layers = backbone_pool.load_model("model_a") # Returns nn.ModuleList of transformer layers
364
+ ```
365
+ """
366
+
212
367
  def load_model(
213
368
  self, model_name_or_config: str | DictConfig, *args, **kwargs
214
369
  ) -> Module:
370
+ """Load only the transformer backbone layers from a causal language model.
371
+
372
+ This method loads a complete causal language model and then extracts
373
+ only the transformer layers (backbone), discarding the embedding layers
374
+ and language modeling head. This is useful for model fusion scenarios
375
+ where only the core transformer computation is needed.
376
+
377
+ Args:
378
+ model_name_or_config: Either a string model name from the pool
379
+ configuration or a DictConfig with model loading parameters.
380
+ *args: Additional positional arguments passed to the parent load_model method.
381
+ **kwargs: Additional keyword arguments passed to the parent load_model method.
382
+
383
+ Returns:
384
+ Module: The transformer layers (typically a nn.ModuleList) containing
385
+ the core transformer blocks without embeddings or output heads.
386
+
387
+ Note:
388
+ Lazy loading is automatically disabled for this method as it needs
389
+ to access the internal model structure to extract the layers.
390
+ """
215
391
  if self.enable_lazy_loading:
216
392
  log.warning(
217
393
  "CausalLMBackbonePool does not support lazy loading. "
@@ -231,6 +407,49 @@ def load_peft_causal_lm(
231
407
  is_trainable: bool = True,
232
408
  merge_and_unload: bool = False,
233
409
  ):
410
+ """Load a causal language model with PEFT (Parameter-Efficient Fine-Tuning) adapters.
411
+
412
+ This function loads a base causal language model and applies PEFT adapters
413
+ (such as LoRA, AdaLoRA, or other parameter-efficient fine-tuning methods)
414
+ to create a fine-tuned model. It supports both keeping the adapters separate
415
+ or merging them into the base model.
416
+
417
+ Args:
418
+ base_model_path: Path or identifier for the base causal language model.
419
+ Can be a Hugging Face model name or local path.
420
+ peft_model_path: Path to the PEFT adapter configuration and weights.
421
+ This should contain the adapter_config.json and adapter weights.
422
+ torch_dtype: The torch data type to use for the model. Common options
423
+ include "float16", "bfloat16", "float32". Defaults to "bfloat16".
424
+ is_trainable: Whether the loaded PEFT model should be trainable.
425
+ Set to False for inference-only usage to save memory.
426
+ merge_and_unload: Whether to merge the PEFT adapters into the base model
427
+ and unload the adapter weights. When True, returns a standard
428
+ PreTrainedModel instead of a PeftModel.
429
+
430
+ Returns:
431
+ Union[PeftModel, PreTrainedModel]: The loaded model with PEFT adapters.
432
+ Returns a PeftModel if merge_and_unload is False, or a PreTrainedModel
433
+ if the adapters are merged and unloaded.
434
+
435
+ Example:
436
+ ```python
437
+ >>> # Load model with adapters for training
438
+ >>> model = load_peft_causal_lm(
439
+ ... "microsoft/DialoGPT-medium",
440
+ ... "/path/to/lora/adapters",
441
+ ... is_trainable=True
442
+ ... )
443
+
444
+ >>> # Load and merge adapters for inference
445
+ >>> merged_model = load_peft_causal_lm(
446
+ ... "microsoft/DialoGPT-medium",
447
+ ... "/path/to/lora/adapters",
448
+ ... merge_and_unload=True,
449
+ ... is_trainable=False
450
+ ... )
451
+ ```
452
+ """
234
453
  base_model = AutoModelForCausalLM.from_pretrained(
235
454
  base_model_path, torch_dtype=torch_dtype
236
455
  )
@@ -18,6 +18,48 @@ def load_lora_model(
18
18
  is_trainable: bool = True,
19
19
  merge_and_unload: bool = True,
20
20
  ):
21
+ """Load a sequence-to-sequence model with LoRA (Low-Rank Adaptation) fine-tuning.
22
+
23
+ This function loads a base sequence-to-sequence language model and applies
24
+ LoRA adapters for parameter-efficient fine-tuning. LoRA allows for efficient
25
+ adaptation of large models by adding trainable low-rank matrices to the
26
+ existing weights without modifying the original parameters.
27
+
28
+ Args:
29
+ base_model_path: Path or identifier for the base sequence-to-sequence model.
30
+ Can be a Hugging Face model name (e.g., "t5-base") or local path.
31
+ peft_model_path: Path to the directory containing LoRA adapter weights
32
+ and configuration. Should include adapter_config.json and adapter weights.
33
+ is_trainable: Whether the loaded model should be trainable. Set to False
34
+ for inference-only usage to save memory and computation.
35
+ merge_and_unload: Whether to merge the LoRA weights into the base model
36
+ and unload the adapter. When True, returns a standard model instead
37
+ of a PeftModel, which can be more efficient for inference.
38
+
39
+ Returns:
40
+ Union[PeftModel, AutoModelForSeq2SeqLM]: The loaded model with LoRA
41
+ adapters. Returns a PeftModel if merge_and_unload is False, or
42
+ a standard AutoModelForSeq2SeqLM if adapters are merged.
43
+
44
+ Example:
45
+ ```python
46
+ >>> # Load model with separate adapters for training
47
+ >>> model = load_lora_model(
48
+ ... "t5-base",
49
+ ... "/path/to/lora/adapters",
50
+ ... is_trainable=True,
51
+ ... merge_and_unload=False
52
+ ... )
53
+
54
+ >>> # Load and merge adapters for efficient inference
55
+ >>> merged_model = load_lora_model(
56
+ ... "t5-base",
57
+ ... "/path/to/lora/adapters",
58
+ ... is_trainable=False,
59
+ ... merge_and_unload=True
60
+ ... )
61
+ ```
62
+ """
21
63
  base_model = AutoModelForSeq2SeqLM.from_pretrained(base_model_path)
22
64
  model = PeftModel.from_pretrained(
23
65
  base_model,
@@ -30,6 +72,46 @@ def load_lora_model(
30
72
 
31
73
 
32
74
  class Seq2SeqLMPool(BaseModelPool):
75
+ """A model pool specialized for sequence-to-sequence language models.
76
+
77
+ This model pool provides management and loading capabilities for sequence-to-sequence
78
+ (seq2seq) language models such as T5, BART, and mT5. It extends the base model pool
79
+ functionality with seq2seq-specific features including tokenizer management and
80
+ model configuration handling.
81
+
82
+ Seq2seq models are particularly useful for tasks that require generating output
83
+ sequences from input sequences, such as translation, summarization, question
84
+ answering, and text generation. This pool streamlines the process of loading
85
+ and configuring multiple seq2seq models for fusion and ensemble scenarios.
86
+
87
+ Key Features:
88
+ - Specialized loading for AutoModelForSeq2SeqLM models
89
+ - Integrated tokenizer management
90
+ - Support for model-specific keyword arguments
91
+ - Automatic dtype parsing and configuration
92
+ - Compatible with PEFT (Parameter-Efficient Fine-Tuning) adapters
93
+
94
+ Attributes:
95
+ _tokenizer: Configuration for the tokenizer associated with the models
96
+ _model_kwargs: Default keyword arguments applied to all model loading operations
97
+
98
+ Example:
99
+ ```python
100
+ pool = Seq2SeqLMPool(
101
+ models={
102
+ "t5_base": "t5-base",
103
+ "t5_large": "t5-large",
104
+ "custom_model": "/path/to/local/model"
105
+ },
106
+ tokenizer={"_target_": "transformers.T5Tokenizer",
107
+ "pretrained_model_name_or_path": "t5-base"},
108
+ model_kwargs={"torch_dtype": "float16", "device_map": "auto"}
109
+ )
110
+ model = pool.load_model("t5_base")
111
+ tokenizer = pool.load_tokenizer()
112
+ ```
113
+ """
114
+
33
115
  _config_mapping = BaseModelPool._config_mapping | {
34
116
  "_tokenizer": "tokenizer",
35
117
  "_model_kwargs": "model_kwargs",
@@ -43,6 +125,35 @@ class Seq2SeqLMPool(BaseModelPool):
43
125
  model_kwargs: Optional[DictConfig] = None,
44
126
  **kwargs,
45
127
  ):
128
+ """Initialize the sequence-to-sequence language model pool.
129
+
130
+ Sets up the model pool with configurations for models, tokenizer, and
131
+ default model loading parameters. Automatically processes model kwargs
132
+ to handle special configurations like torch_dtype parsing.
133
+
134
+ Args:
135
+ models: Configuration dictionary specifying the seq2seq models to manage.
136
+ Keys are model names, values can be model paths/names or detailed configs.
137
+ tokenizer: Configuration for the tokenizer to use with the models.
138
+ Can be a simple path/name or detailed configuration with _target_.
139
+ model_kwargs: Default keyword arguments applied to all model loading
140
+ operations. Common options include torch_dtype, device_map, etc.
141
+ The torch_dtype field is automatically parsed from string to dtype.
142
+ **kwargs: Additional arguments passed to the parent BaseModelPool.
143
+
144
+ Example:
145
+ ```python
146
+ pool = Seq2SeqLMPool(
147
+ models={
148
+ "base": "t5-base",
149
+ "large": {"_target_": "transformers.AutoModelForSeq2SeqLM",
150
+ "pretrained_model_name_or_path": "t5-large"}
151
+ },
152
+ tokenizer="t5-base",
153
+ model_kwargs={"torch_dtype": "bfloat16"}
154
+ )
155
+ ```
156
+ """
46
157
  super().__init__(models, **kwargs)
47
158
  self._tokenizer = tokenizer
48
159
  self._model_kwargs = model_kwargs
@@ -55,11 +166,46 @@ class Seq2SeqLMPool(BaseModelPool):
55
166
  )
56
167
 
57
168
  def load_model(self, model_name_or_config: str | DictConfig, *args, **kwargs):
169
+ """Load a sequence-to-sequence language model from the pool.
170
+
171
+ Loads a seq2seq model using the parent class loading mechanism while
172
+ automatically applying the pool's default model kwargs. The method
173
+ merges the pool's model_kwargs with any additional kwargs provided,
174
+ giving priority to the explicitly provided kwargs.
175
+
176
+ Args:
177
+ model_name_or_config: Either a string model name from the pool
178
+ configuration or a DictConfig containing model loading parameters.
179
+ *args: Additional positional arguments passed to the parent load_model method.
180
+ **kwargs: Additional keyword arguments that override the pool's default
181
+ model_kwargs. Common options include device, torch_dtype, etc.
182
+
183
+ Returns:
184
+ AutoModelForSeq2SeqLM: The loaded sequence-to-sequence language model.
185
+ """
58
186
  model_kwargs = deepcopy(self._model_kwargs)
59
187
  model_kwargs.update(kwargs)
60
188
  return super().load_model(model_name_or_config, *args, **model_kwargs)
61
189
 
62
190
  def load_tokenizer(self, *args, **kwargs):
191
+ """Load the tokenizer associated with the sequence-to-sequence models.
192
+
193
+ Loads a tokenizer based on the tokenizer configuration provided during
194
+ pool initialization. The tokenizer should be compatible with the seq2seq
195
+ models in the pool and is typically used for preprocessing input text
196
+ and postprocessing generated output.
197
+
198
+ Args:
199
+ *args: Additional positional arguments passed to the tokenizer constructor.
200
+ **kwargs: Additional keyword arguments passed to the tokenizer constructor.
201
+
202
+ Returns:
203
+ PreTrainedTokenizer: The loaded tokenizer instance compatible with
204
+ the seq2seq models in this pool.
205
+
206
+ Raises:
207
+ AssertionError: If no tokenizer configuration is provided.
208
+ """
63
209
  assert self._tokenizer is not None, "Tokenizer is not defined in the config"
64
210
  tokenizer = isinstance(self._tokenizer, *args, **kwargs)
65
211
  return tokenizer
@@ -42,12 +42,14 @@ def load_model_card_template(basename: str) -> str:
42
42
  FileNotFoundError: If the template file is not found in any of the search locations.
43
43
  """
44
44
  if os.path.exists(basename):
45
- return open(basename).read()
45
+ with open(basename, "r") as f:
46
+ return f.read()
46
47
 
47
48
  for template_dir in MODEL_CARD_TEMPLATE_DIRS:
48
49
  template_path = os.path.join(template_dir, basename)
49
50
  if os.path.exists(template_path):
50
- return open(template_path).read()
51
+ with open(template_path, "r") as f:
52
+ return f.read()
51
53
 
52
54
  raise FileNotFoundError(f"Model card template '{basename}' not found.")
53
55
 
@@ -141,6 +143,9 @@ def save_pretrained_with_remote_code(
141
143
 
142
144
  def create_default_model_card(
143
145
  models: list[str],
146
+ *,
147
+ title: str = "Deep Model Fusion",
148
+ tags: list[str] = ["fusion-bench", "merge"],
144
149
  description=None,
145
150
  algorithm_config: DictConfig = None,
146
151
  modelpool_config: DictConfig = None,
@@ -151,8 +156,8 @@ def create_default_model_card(
151
156
  card = template.render(
152
157
  models=models,
153
158
  library_name="transformers",
154
- tags=["fusion-bench", "merge"],
155
- title="Deep Model Fusion",
159
+ title=title,
160
+ tags=tags,
156
161
  description=description,
157
162
  algorithm_config_str=try_to_yaml(algorithm_config),
158
163
  modelpool_config_str=try_to_yaml(modelpool_config),
@@ -45,21 +45,21 @@ def linearize_lora_model_(model):
45
45
 
46
46
 
47
47
  def load_fft_vision_model_hf(
48
- model_name: str, return_vison_model=True
48
+ model_name: str, return_vision_model=True
49
49
  ) -> Union[CLIPVisionTransformer, CLIPVisionModel]:
50
50
  """
51
51
  Load a CLIP vision model from Hugging Face.
52
52
 
53
53
  Args:
54
54
  model_name (str): The name of the CLIP vision model to load from Hugging Face.
55
- return_vison_model (bool, optional): If False, the full CLIPVisionModel is returned. If True, only the vision model (`CLIPVisionTransformer`) is returned. Defaults to True.
55
+ return_vision_model (bool, optional): If False, the full CLIPVisionModel is returned. If True, only the vision model (`CLIPVisionTransformer`) is returned. Defaults to True.
56
56
 
57
57
  Returns:
58
58
  Union[CLIPVisionTransformer, CLIPVisionModel]: The vision model.
59
59
  """
60
60
  model = CLIPVisionModel.from_pretrained(model_name)
61
61
 
62
- if return_vison_model:
62
+ if return_vision_model:
63
63
  return CLIPVisionModel.from_pretrained(model_name).vision_model
64
64
  else:
65
65
  return model
@@ -69,7 +69,7 @@ def load_lora_vision_model_hf(
69
69
  base_model_name: str,
70
70
  peft_name: str,
71
71
  merge_and_unload: bool = False,
72
- return_vison_model=True,
72
+ return_vision_model=True,
73
73
  ) -> PeftModel:
74
74
  """
75
75
  Load a LoRA (Low-Rank Adaptation) vision model from Hugging Face.
@@ -80,7 +80,7 @@ def load_lora_vision_model_hf(
80
80
  base_model_name (str): The name of the base vision model to load from Hugging Face.
81
81
  peft_name (str): The name of the LoRA adaptation to apply to the base model.
82
82
  merge_and_unload (bool, optional): If True, the LoRA adaptation is merged into the base model and the LoRA layers are removed. Defaults to False.
83
- return_vison_model (bool, optional): If False, the full CLIPVisionModel is returned. If True, only the vision model (`CLIPVisionTransformer`) is returned. Defaults to True.
83
+ return_vision_model (bool, optional): If False, the full CLIPVisionModel is returned. If True, only the vision model (`CLIPVisionTransformer`) is returned. Defaults to True.
84
84
 
85
85
  Returns:
86
86
  PeftModel: The adapted vision model, optionally merged and unloaded.
@@ -97,7 +97,7 @@ def load_lora_vision_model_hf(
97
97
  vision_model = peft_model
98
98
 
99
99
  # Return the vision model
100
- if return_vison_model:
100
+ if return_vision_model:
101
101
  return vision_model
102
102
  else:
103
103
  model.vision_model = vision_model
@@ -1,6 +1,7 @@
1
1
  from . import register
2
2
  from .configuration_smile_mistral import SmileMistralConfig
3
3
  from .modeling_smile_mistral import (
4
+ SmileMistralDecoderLayer,
4
5
  SmileMistralForCausalLM,
5
6
  SmileMistralModel,
6
7
  )