Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes in SDLoraManager #212

Merged
merged 3 commits into from
Jan 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 11 additions & 17 deletions src/refiners/foundationals/latent_diffusion/lora.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def add_loras(
loras = {key: loras[key] for key in sorted(loras.keys(), key=SDLoraManager.sort_keys)}

# if no key contains "unet" or "text", assume all keys are for the unet
if not "unet" in loras and not "text" in loras:
loras = {f"unet_{key}": loras[key] for key in loras.keys()}
if all("unet" not in key and "text" not in key for key in loras.keys()):
loras = {f"unet_{key}": value for key, value in loras.items()}

self.add_loras_to_unet(loras)
self.add_loras_to_text_encoder(loras)
Expand All @@ -67,11 +67,8 @@ def add_loras_to_text_encoder(self, loras: dict[str, Lora], /) -> None:

def add_loras_to_unet(self, loras: dict[str, Lora], /) -> None:
unet_loras = {key: loras[key] for key in loras.keys() if "unet" in key}
exclude: list[str] = []
exclude = [
self.unet_exclusions[exclusion]
for exclusion in self.unet_exclusions
if all([exclusion not in key for key in unet_loras.keys()])
block for s, block in self.unet_exclusions.items() if all([s not in key for key in unet_loras.keys()])
]
SDLoraManager.auto_attach(unet_loras, self.unet, exclude=exclude)

Expand Down Expand Up @@ -121,8 +118,8 @@ def unet_exclusions(self) -> dict[str, str]:
return {
"time": "TimestepEncoder",
"res": "ResidualBlock",
"downsample": "DownsampleBlock",
"upsample": "UpsampleBlock",
"downsample": "Downsample",
"upsample": "Upsample",
}

@property
Expand All @@ -141,15 +138,12 @@ def pad(input: str, /, padding_length: int = 2) -> str:

@staticmethod
def sort_keys(key: str, /) -> tuple[str, int]:
# out0 happens sometimes as an alias for out ; this dict might not be exhaustive
key_char_order = {"q": 1, "k": 2, "v": 3, "out": 4, "out0": 4}

for i, s in enumerate(key.split("_")):
if s in key_char_order:
prefix = SDLoraManager.pad("_".join(key.split("_")[:i]))
return (prefix, key_char_order[s])

return (SDLoraManager.pad(key), 5)
# this dict might not be exhaustive
suffix_scores = {"q": 1, "k": 2, "v": 3, "in": 3, "out": 4, "out0": 4, "out_0": 4}
patterns = ["_{}", "_{}_lora"]
key_char_order = {f.format(k): v for k, v in suffix_scores.items() for f in patterns}
(sfx, score) = next(((k, v) for k, v in key_char_order.items() if key.endswith(k)), ("", 5))
return (SDLoraManager.pad(key.removesuffix(sfx)), score)

@staticmethod
def auto_attach(
Expand Down