lycoris-lora 2.1.0.dev4__tar.gz → 2.1.0.dev5__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/PKG-INFO +1 -1
  2. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris/kohya/__init__.py +3 -1
  3. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris/modules/__init__.py +2 -1
  4. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris/modules/base.py +21 -0
  5. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris/modules/full.py +19 -2
  6. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris/modules/loha.py +4 -2
  7. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris/utils/__init__.py +29 -52
  8. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris_lora.egg-info/PKG-INFO +1 -1
  9. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/setup.py +1 -1
  10. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/LICENSE.md +0 -0
  11. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/README.md +0 -0
  12. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris/__init__.py +0 -0
  13. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris/config.py +0 -0
  14. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris/hcp/__init__.py +0 -0
  15. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris/hcp/base.py +0 -0
  16. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris/hcp/diag_oft.py +0 -0
  17. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris/hcp/loha.py +0 -0
  18. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris/hcp/lokr.py +0 -0
  19. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris/kohya/model_utils.py +0 -0
  20. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris/kohya/original_unet.py +0 -0
  21. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris/kohya/sdxl_model_util.py +0 -0
  22. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris/kohya/sdxl_original_unet.py +0 -0
  23. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris/kohya/utils.py +0 -0
  24. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris/modules/attention.py +0 -0
  25. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris/modules/diag_oft.py +0 -0
  26. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris/modules/dylora.py +0 -0
  27. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris/modules/glokr.py +0 -0
  28. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris/modules/glora.py +0 -0
  29. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris/modules/hypernet/__init__.py +0 -0
  30. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris/modules/hypernet/generater.py +0 -0
  31. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris/modules/hypernet/text_encoder.py +0 -0
  32. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris/modules/ia3.py +0 -0
  33. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris/modules/locon.py +0 -0
  34. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris/modules/lokr.py +0 -0
  35. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris/modules/norms.py +0 -0
  36. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris/utils/logger.py +0 -0
  37. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris/utils/preset.py +0 -0
  38. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris/utils/xformers_utils.py +0 -0
  39. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris/wrapper.py +0 -0
  40. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris_lora.egg-info/SOURCES.txt +0 -0
  41. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris_lora.egg-info/dependency_links.txt +0 -0
  42. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris_lora.egg-info/not-zip-safe +0 -0
  43. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris_lora.egg-info/requires.txt +0 -0
  44. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/lycoris_lora.egg-info/top_level.txt +0 -0
  45. {lycoris_lora-2.1.0.dev4 → lycoris_lora-2.1.0.dev5}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: lycoris_lora
3
- Version: 2.1.0.dev4
3
+ Version: 2.1.0.dev5
4
4
  Summary: Lora beYond Conventional methods, Other Rank adaptation Implementations for Stable diffusion
5
5
  Home-page: https://github.com/KohakuBlueleaf/LyCORIS
6
6
  Author: Shih-Ying Yeh(KohakuBlueLeaf), Yu-Guan Hsieh, Zhidong Gao
@@ -586,7 +586,9 @@ class LycorisNetworkKohya(LycorisNetwork):
586
586
  else:
587
587
  self.unet_loras = []
588
588
 
589
- for lora in self.text_encoder_loras + self.unet_loras:
589
+ self.loras = self.text_encoder_loras + self.unet_loras
590
+
591
+ for lora in self.loras:
590
592
  lora.apply_to()
591
593
  self.add_module(lora.lora_name, lora)
592
594
 
@@ -99,7 +99,8 @@ def make_module(lyco_type, params, lora_name, orig_module):
99
99
  1,
100
100
  )
101
101
  module.w_norm.copy_(w_norm)
102
- module.b_norm.copy_(b_norm)
102
+ if b_norm is not None:
103
+ module.b_norm.copy_(b_norm)
103
104
  elif lyco_type == "full":
104
105
  diff, diff_b = params
105
106
  module = FullModule(
@@ -1,10 +1,31 @@
1
1
  from collections import OrderedDict
2
+ from typing import Any, Mapping
2
3
  import torch
3
4
  import torch.nn as nn
4
5
  import torch.nn.functional as F
5
6
 
6
7
 
7
8
  class ModuleCustomSD(nn.Module):
9
+ def __init__(self):
10
+ super().__init__()
11
+ self._register_load_state_dict_pre_hook(self.load_weight_prehook)
12
+ self.register_load_state_dict_post_hook(self.load_weight_hook)
13
+
14
+ def load_weight_prehook(
15
+ self,
16
+ state_dict,
17
+ prefix,
18
+ local_metadata,
19
+ strict,
20
+ missing_keys,
21
+ unexpected_keys,
22
+ error_msgs,
23
+ ):
24
+ pass
25
+
26
+ def load_weight_hook(self, module, incompatible_keys):
27
+ pass
28
+
8
29
  def custom_state_dict(self):
9
30
  return None
10
31
 
@@ -1,6 +1,7 @@
1
1
  import torch
2
2
  import torch.nn as nn
3
3
  import torch.nn.functional as F
4
+ from torch.nn.modules import Module
4
5
 
5
6
  from .base import ModuleCustomSD
6
7
 
@@ -54,11 +55,11 @@ class FullModule(ModuleCustomSD):
54
55
  def apply_to(self, **kwargs):
55
56
  self.org_forward = self.org_module[0].forward
56
57
  self.org_module[0].forward = self.forward
57
- self.weight.data.copy_(self.org_module[0].weight.data)
58
+ self.weight.data.add_(self.org_module[0].weight.data)
58
59
  self.org_weight = [self.org_module[0].weight.data.cpu().clone()]
59
60
  delattr(self.org_module[0], "weight")
60
61
  if self.org_module[0].bias is not None:
61
- self.bias.data.copy_(self.org_module[0].bias.data)
62
+ self.bias.data.add_(self.org_module[0].bias.data)
62
63
  self.org_bias = [self.org_module[0].bias.data.cpu().clone()]
63
64
  delattr(self.org_module[0], "bias")
64
65
  else:
@@ -76,6 +77,22 @@ class FullModule(ModuleCustomSD):
76
77
  sd["diff_b"] = self.bias.data.cpu() - self.org_bias[0]
77
78
  return sd
78
79
 
80
+ def load_weight_prehook(
81
+ self,
82
+ state_dict,
83
+ prefix,
84
+ local_metadata,
85
+ strict,
86
+ missing_keys,
87
+ unexpected_keys,
88
+ error_msgs,
89
+ ):
90
+ diff_weight = state_dict["diff"]
91
+ self.weight.data.add_(diff_weight)
92
+ if "diff_b" in state_dict:
93
+ diff_bias = state_dict["diff_b"]
94
+ self.bias.data.add_(diff_bias)
95
+
79
96
  def make_weight(self, scale=1, device=None, original=False):
80
97
  if original:
81
98
  weight = self.org_weight[0].to(device, dtype=self.weight.dtype)
@@ -191,9 +191,11 @@ class LohaModule(ModuleCustomSD):
191
191
  self.org_module = [org_module] # remove in applying
192
192
  self.org_forward = self.org_module[0].forward
193
193
  self.grad_ckpt = False
194
- self.register_load_state_dict_post_hook(self.load_weight_hook)
195
194
 
196
- def load_weight_hook(self, *args, **kwargs):
195
+ def load_weight_hook(self, module: nn.Module, incompatible_keys):
196
+ unexpected_keys = incompatible_keys.unexpected_keys
197
+ if "scalar" in unexpected_keys:
198
+ del unexpected_keys[unexpected_keys.index("scalar")]
197
199
  self.scalar = nn.Parameter(torch.ones_like(self.scalar))
198
200
 
199
201
  def apply_to(self):
@@ -147,6 +147,7 @@ def extract_diff(
147
147
  "GroupNorm32",
148
148
  ]
149
149
  TEXT_ENCODER_TARGET_REPLACE_MODULE = [
150
+ "Embedding",
150
151
  "Linear",
151
152
  "Conv2d",
152
153
  "LayerNorm",
@@ -312,7 +313,11 @@ def extract_diff(
312
313
  )
313
314
  del te1, te2
314
315
 
315
- print(len(all_loras))
316
+ all_lora_name = set()
317
+ for k in all_loras:
318
+ lora_name, weight = k.rsplit(".", 1)
319
+ all_lora_name.add(lora_name)
320
+ print(len(all_lora_name))
316
321
  return all_loras
317
322
 
318
323
 
@@ -420,7 +425,7 @@ def get_module(lyco_state_dict: Dict, lora_name):
420
425
  return "full", (diff, diff_b)
421
426
  elif f"{lora_name}.w_norm" in lyco_state_dict:
422
427
  w_norm = lyco_state_dict[f"{lora_name}.w_norm"]
423
- b_norm = lyco_state_dict.get(f"{lora_name}.b_norm")
428
+ b_norm = lyco_state_dict.get(f"{lora_name}.b_norm", None)
424
429
  return "norm", (w_norm, b_norm)
425
430
  else:
426
431
  return "None", ()
@@ -517,19 +522,20 @@ def rebuild_weight(module_type, params, orig_weight, orig_bias, scale=1):
517
522
  @torch.no_grad()
518
523
  def merge(tes, unet, lyco_state_dict, scale: float = 1.0, device="cpu"):
519
524
  UNET_TARGET_REPLACE_MODULE = [
520
- "Transformer2DModel",
521
- "Attention",
522
- "ResnetBlock2D",
523
- "Downsample2D",
524
- "Upsample2D",
525
+ "Linear",
526
+ "Conv2d",
527
+ "LayerNorm",
528
+ "GroupNorm",
529
+ "GroupNorm32",
525
530
  ]
526
- UNET_TARGET_REPLACE_NAME = [
527
- "conv_in",
528
- "conv_out",
529
- "time_embedding.linear_1",
530
- "time_embedding.linear_2",
531
+ TEXT_ENCODER_TARGET_REPLACE_MODULE = [
532
+ "Embedding",
533
+ "Linear",
534
+ "Conv2d",
535
+ "LayerNorm",
536
+ "GroupNorm",
537
+ "GroupNorm32",
531
538
  ]
532
- TEXT_ENCODER_TARGET_REPLACE_MODULE = ["CLIPAttention", "CLIPMLP"]
533
539
  LORA_PREFIX_UNET = "lora_unet"
534
540
  LORA_PREFIX_TEXT_ENCODER = "lora_te"
535
541
  merged = 0
@@ -539,55 +545,28 @@ def merge(tes, unet, lyco_state_dict, scale: float = 1.0, device="cpu"):
539
545
  root_module: torch.nn.Module,
540
546
  lyco_state_dict: Dict[str, torch.Tensor],
541
547
  target_replace_modules,
542
- target_replace_names=[],
543
548
  ):
544
549
  nonlocal merged
545
- for name, module in tqdm(
550
+ for child_name, child_module in tqdm(
546
551
  list(root_module.named_modules()), desc=f"Merging {prefix}"
547
552
  ):
548
- if module.__class__.__name__ in target_replace_modules:
549
- for child_name, child_module in module.named_modules():
550
- if child_module.__class__.__name__ not in {
551
- "Linear",
552
- "Conv2d",
553
- "GroupNorm",
554
- "GroupNorm32",
555
- "LayerNorm",
556
- }:
557
- continue
558
- lora_name = prefix + "." + name + "." + child_name
559
- lora_name = lora_name.replace(".", "_")
560
-
561
- result, result_b = rebuild_weight(
562
- *get_module(lyco_state_dict, lora_name),
563
- getattr(child_module, "weight"),
564
- getattr(child_module, "bias", None),
565
- scale,
566
- )
567
- if result is not None:
568
- key_dict.pop(lora_name)
569
- merged += 1
570
- child_module.requires_grad_(False)
571
- child_module.weight.copy_(result)
572
- if result_b is not None:
573
- child_module.bias.copy_(result_b)
574
- elif name in target_replace_names:
575
- lora_name = prefix + "." + name
553
+ if child_module.__class__.__name__ in target_replace_modules:
554
+ lora_name = prefix + "." + child_name
576
555
  lora_name = lora_name.replace(".", "_")
577
556
 
578
557
  result, result_b = rebuild_weight(
579
558
  *get_module(lyco_state_dict, lora_name),
580
- getattr(module, "weight"),
581
- getattr(module, "bias", None),
559
+ getattr(child_module, "weight"),
560
+ getattr(child_module, "bias", None),
582
561
  scale,
583
562
  )
584
563
  if result is not None:
585
564
  key_dict.pop(lora_name)
586
565
  merged += 1
587
- module.requires_grad_(False)
588
- module.weight.copy_(result)
566
+ child_module.requires_grad_(False)
567
+ child_module.weight.copy_(result)
589
568
  if result_b is not None:
590
- module.bias.copy_(result_b)
569
+ child_module.bias.copy_(result_b)
591
570
 
592
571
  key_dict = {}
593
572
  for k, v in tqdm(list(lyco_state_dict.items()), desc="Converting Dtype and Device"):
@@ -596,10 +575,10 @@ def merge(tes, unet, lyco_state_dict, scale: float = 1.0, device="cpu"):
596
575
  if convert_key != module and len(tes) > 1:
597
576
  # kohya's format for sdxl is as same as SGM, not diffusers
598
577
  del lyco_state_dict[k]
599
- key_dict[convert_key] = k
578
+ key_dict[convert_key] = key_dict.get(convert_key, []) + [k]
600
579
  k = f"{convert_key}.{weight_key}"
601
580
  else:
602
- key_dict[module] = k
581
+ key_dict[module] = key_dict.get(module, []) + [k]
603
582
  if device == "cpu":
604
583
  lyco_state_dict[k] = v.float().cpu()
605
584
  else:
@@ -617,14 +596,12 @@ def merge(tes, unet, lyco_state_dict, scale: float = 1.0, device="cpu"):
617
596
  te.to(device),
618
597
  lyco_state_dict,
619
598
  TEXT_ENCODER_TARGET_REPLACE_MODULE,
620
- UNET_TARGET_REPLACE_NAME,
621
599
  )
622
600
  merge_state_dict(
623
601
  LORA_PREFIX_UNET,
624
602
  unet.to(device),
625
603
  lyco_state_dict,
626
604
  UNET_TARGET_REPLACE_MODULE,
627
- UNET_TARGET_REPLACE_NAME,
628
605
  )
629
606
  print(f"Unused state dict key: {key_dict}")
630
607
  print(f"{merged} Modules been merged")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: lycoris-lora
3
- Version: 2.1.0.dev4
3
+ Version: 2.1.0.dev5
4
4
  Summary: Lora beYond Conventional methods, Other Rank adaptation Implementations for Stable diffusion
5
5
  Home-page: https://github.com/KohakuBlueleaf/LyCORIS
6
6
  Author: Shih-Ying Yeh(KohakuBlueLeaf), Yu-Guan Hsieh, Zhidong Gao
@@ -4,7 +4,7 @@ from setuptools import setup, find_packages
4
4
  setup(
5
5
  name="lycoris_lora",
6
6
  packages=find_packages(),
7
- version="2.1.0.dev4",
7
+ version="2.1.0.dev5",
8
8
  url="https://github.com/KohakuBlueleaf/LyCORIS",
9
9
  description="Lora beYond Conventional methods, Other Rank adaptation Implementations for Stable diffusion",
10
10
  author="Shih-Ying Yeh(KohakuBlueLeaf), Yu-Guan Hsieh, Zhidong Gao",