r""" Converts a state dict to the PEFT format The state dict can be from previous diffusers format (`OLD_DIFFUSERS`), or new diffusers format (`DIFFUSERS`). The method only supports the conversion from diffusers old/new to PEFT for now. Args: state_dict (`dict[str, torch.Tensor]
(state_dict, original_type=None, **kwargs)
| 208 | |
| 209 | |
| 210 | def convert_state_dict_to_peft(state_dict, original_type=None, **kwargs): |
| 211 | r""" |
| 212 | Converts a state dict to the PEFT format The state dict can be from previous diffusers format (`OLD_DIFFUSERS`), or |
| 213 | new diffusers format (`DIFFUSERS`). The method only supports the conversion from diffusers old/new to PEFT for now. |
| 214 | |
| 215 | Args: |
| 216 | state_dict (`dict[str, torch.Tensor]`): |
| 217 | The state dict to convert. |
| 218 | original_type (`StateDictType`, *optional*): |
| 219 | The original type of the state dict, if not provided, the method will try to infer it automatically. |
| 220 | """ |
| 221 | if original_type is None: |
| 222 | # Old diffusers to PEFT |
| 223 | if any("to_out_lora" in k for k in state_dict.keys()): |
| 224 | original_type = StateDictType.DIFFUSERS_OLD |
| 225 | elif any("lora_linear_layer" in k for k in state_dict.keys()): |
| 226 | original_type = StateDictType.DIFFUSERS |
| 227 | else: |
| 228 | raise ValueError("Could not automatically infer state dict type") |
| 229 | |
| 230 | if original_type not in PEFT_STATE_DICT_MAPPINGS.keys(): |
| 231 | raise ValueError(f"Original type {original_type} is not supported") |
| 232 | |
| 233 | mapping = PEFT_STATE_DICT_MAPPINGS[original_type] |
| 234 | return convert_state_dict(state_dict, mapping) |
| 235 | |
| 236 | |
| 237 | def convert_state_dict_to_diffusers(state_dict, original_type=None, **kwargs): |
no test coverage detected
searching dependent graphs…