Save the LoRA parameters corresponding to the underlying model. Arguments: save_directory (`str` or `os.PathLike`): Directory to save LoRA parameters to. Will be created if it doesn't exist. adapter_name: (`str`, defaults to "default"): The n
(
self,
save_directory,
adapter_name: str = "default",
upcast_before_saving: bool = False,
safe_serialization: bool = True,
weight_name: str | None = None,
)
| 382 | ) |
| 383 | |
| 384 | def save_lora_adapter( |
| 385 | self, |
| 386 | save_directory, |
| 387 | adapter_name: str = "default", |
| 388 | upcast_before_saving: bool = False, |
| 389 | safe_serialization: bool = True, |
| 390 | weight_name: str | None = None, |
| 391 | ): |
| 392 | """ |
| 393 | Save the LoRA parameters corresponding to the underlying model. |
| 394 | |
| 395 | Arguments: |
| 396 | save_directory (`str` or `os.PathLike`): |
| 397 | Directory to save LoRA parameters to. Will be created if it doesn't exist. |
| 398 | adapter_name: (`str`, defaults to "default"): The name of the adapter to serialize. Useful when the |
| 399 | underlying model has multiple adapters loaded. |
| 400 | upcast_before_saving (`bool`, defaults to `False`): |
| 401 | Whether to cast the underlying model to `torch.float32` before serialization. |
| 402 | safe_serialization (`bool`, *optional*, defaults to `True`): |
| 403 | Whether to save the model using `safetensors` or the traditional PyTorch way with `pickle`. |
| 404 | weight_name: (`str`, *optional*, defaults to `None`): Name of the file to serialize the state dict with. |
| 405 | """ |
| 406 | from peft.utils import get_peft_model_state_dict |
| 407 | |
| 408 | from .lora_base import LORA_ADAPTER_METADATA_KEY, LORA_WEIGHT_NAME, LORA_WEIGHT_NAME_SAFE |
| 409 | |
| 410 | if adapter_name is None: |
| 411 | adapter_name = get_adapter_name(self) |
| 412 | |
| 413 | if adapter_name not in getattr(self, "peft_config", {}): |
| 414 | raise ValueError(f"Adapter name {adapter_name} not found in the model.") |
| 415 | |
| 416 | lora_adapter_metadata = self.peft_config[adapter_name].to_dict() |
| 417 | |
| 418 | lora_layers_to_save = get_peft_model_state_dict( |
| 419 | self.to(dtype=torch.float32 if upcast_before_saving else None), adapter_name=adapter_name |
| 420 | ) |
| 421 | if os.path.isfile(save_directory): |
| 422 | raise ValueError(f"Provided path ({save_directory}) should be a directory, not a file") |
| 423 | |
| 424 | if safe_serialization: |
| 425 | |
| 426 | def save_function(weights, filename): |
| 427 | # Inject framework format. |
| 428 | metadata = {"format": "pt"} |
| 429 | if lora_adapter_metadata is not None: |
| 430 | for key, value in lora_adapter_metadata.items(): |
| 431 | if isinstance(value, set): |
| 432 | lora_adapter_metadata[key] = list(value) |
| 433 | metadata[LORA_ADAPTER_METADATA_KEY] = json.dumps(lora_adapter_metadata, indent=2, sort_keys=True) |
| 434 | |
| 435 | return safetensors.torch.save_file(weights, filename, metadata=metadata) |
| 436 | |
| 437 | else: |
| 438 | save_function = torch.save |
| 439 | |
| 440 | os.makedirs(save_directory, exist_ok=True) |
| 441 |