Helper method to pack and save LoRA weights and metadata. This method centralizes the saving logic for all pipeline types.
(
cls,
save_directory: str | os.PathLike,
lora_layers: dict[str, dict[str, torch.nn.Module | torch.Tensor]],
lora_metadata: dict[str, dict | None],
is_main_process: bool = True,
weight_name: str = None,
save_function: Callable = None,
safe_serialization: bool = True,
)
| 1059 | |
| 1060 | @classmethod |
| 1061 | def _save_lora_weights( |
| 1062 | cls, |
| 1063 | save_directory: str | os.PathLike, |
| 1064 | lora_layers: dict[str, dict[str, torch.nn.Module | torch.Tensor]], |
| 1065 | lora_metadata: dict[str, dict | None], |
| 1066 | is_main_process: bool = True, |
| 1067 | weight_name: str = None, |
| 1068 | save_function: Callable = None, |
| 1069 | safe_serialization: bool = True, |
| 1070 | ): |
| 1071 | """ |
| 1072 | Helper method to pack and save LoRA weights and metadata. This method centralizes the saving logic for all |
| 1073 | pipeline types. |
| 1074 | """ |
| 1075 | state_dict = {} |
| 1076 | final_lora_adapter_metadata = {} |
| 1077 | |
| 1078 | for prefix, layers in lora_layers.items(): |
| 1079 | state_dict.update(cls.pack_weights(layers, prefix)) |
| 1080 | |
| 1081 | for prefix, metadata in lora_metadata.items(): |
| 1082 | if metadata: |
| 1083 | final_lora_adapter_metadata.update(_pack_dict_with_prefix(metadata, prefix)) |
| 1084 | |
| 1085 | cls.write_lora_layers( |
| 1086 | state_dict=state_dict, |
| 1087 | save_directory=save_directory, |
| 1088 | is_main_process=is_main_process, |
| 1089 | weight_name=weight_name, |
| 1090 | save_function=save_function, |
| 1091 | safe_serialization=safe_serialization, |
| 1092 | lora_adapter_metadata=final_lora_adapter_metadata if final_lora_adapter_metadata else None, |
| 1093 | ) |
| 1094 | |
| 1095 | @classmethod |
| 1096 | def _optionally_disable_offloading(cls, _pipeline): |
no test coverage detected