diff --git a/invokeai/backend/peft/sdxl_format_utils.py b/invokeai/backend/peft/sdxl_format_utils.py index 1e3a99db0e..af5adee8db 100644 --- a/invokeai/backend/peft/sdxl_format_utils.py +++ b/invokeai/backend/peft/sdxl_format_utils.py @@ -1,14 +1,15 @@ import bisect -from typing import Dict, List, Tuple import torch -# code from -# https://github.com/bmaltais/kohya_ss/blob/2accb1305979ba62f5077a23aabac23b4c37e935/networks/lora_diffusers.py#L15C1-L97C32 -def make_sdxl_unet_conversion_map() -> List[Tuple[str, str]]: - """Create a dict mapping state_dict keys from Stability AI SDXL format to diffusers SDXL format.""" - unet_conversion_map_layer = [] +def make_sdxl_unet_conversion_map() -> list[tuple[str, str]]: + """Create a dict mapping state_dict keys from Stability AI SDXL format to diffusers SDXL format. + + Ported from: + https://github.com/bmaltais/kohya_ss/blob/2accb1305979ba62f5077a23aabac23b4c37e935/networks/lora_diffusers.py#L15C1-L97C32 + """ + unet_conversion_map_layer: list[tuple[str, str]] = [] for i in range(3): # num_blocks is 3 in sdxl # loop over downblocks/upblocks @@ -66,7 +67,7 @@ def make_sdxl_unet_conversion_map() -> List[Tuple[str, str]]: ("skip_connection.", "conv_shortcut."), ] - unet_conversion_map = [] + unet_conversion_map: list[tuple[str, str]] = [] for sd, hf in unet_conversion_map_layer: if "resnets" in hf: for sd_res, hf_res in unet_conversion_map_resnet: @@ -96,8 +97,7 @@ SDXL_UNET_STABILITY_TO_DIFFUSERS_MAP = { } -@classmethod -def convert_sdxl_keys_to_diffusers_format(cls, state_dict: Dict[str, torch.Tensor]) -> Dict[str, torch.Tensor]: +def convert_sdxl_keys_to_diffusers_format(state_dict: dict[str, torch.Tensor]) -> dict[str, torch.Tensor]: """Convert the keys of an SDXL LoRA state_dict to diffusers format. The input state_dict can be in either Stability AI format or diffusers format. If the state_dict is already in @@ -124,7 +124,7 @@ def convert_sdxl_keys_to_diffusers_format(cls, state_dict: Dict[str, torch.Tenso stability_unet_keys = list(SDXL_UNET_STABILITY_TO_DIFFUSERS_MAP) stability_unet_keys.sort() - new_state_dict = {} + new_state_dict: dict[str, torch.Tensor] = {} for full_key, value in state_dict.items(): if full_key.startswith("lora_unet_"): search_key = full_key.replace("lora_unet_", "")