lycoris-lora 3.1.1__tar.gz → 3.2.0.dev1__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 (46) hide show
  1. {lycoris_lora-3.1.1/lycoris_lora.egg-info → lycoris_lora-3.2.0.dev1}/PKG-INFO +1 -1
  2. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/lycoris/config.py +14 -4
  3. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/lycoris/kohya.py +131 -56
  4. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/lycoris/modules/base.py +4 -0
  5. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1/lycoris_lora.egg-info}/PKG-INFO +1 -1
  6. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/setup.py +1 -1
  7. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/LICENSE.md +0 -0
  8. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/README.md +0 -0
  9. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/lycoris/__init__.py +0 -0
  10. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/lycoris/functional/__init__.py +0 -0
  11. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/lycoris/functional/boft.py +0 -0
  12. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/lycoris/functional/diag_oft.py +0 -0
  13. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/lycoris/functional/general.py +0 -0
  14. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/lycoris/functional/locon.py +0 -0
  15. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/lycoris/functional/loha.py +0 -0
  16. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/lycoris/functional/lokr.py +0 -0
  17. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/lycoris/logging.py +0 -0
  18. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/lycoris/modules/__init__.py +0 -0
  19. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/lycoris/modules/boft.py +0 -0
  20. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/lycoris/modules/diag_oft.py +0 -0
  21. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/lycoris/modules/dylora.py +0 -0
  22. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/lycoris/modules/full.py +0 -0
  23. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/lycoris/modules/glora.py +0 -0
  24. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/lycoris/modules/ia3.py +0 -0
  25. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/lycoris/modules/locon.py +0 -0
  26. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/lycoris/modules/loha.py +0 -0
  27. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/lycoris/modules/lokr.py +0 -0
  28. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/lycoris/modules/norms.py +0 -0
  29. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/lycoris/utils/__init__.py +0 -0
  30. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/lycoris/utils/general.py +0 -0
  31. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/lycoris/utils/logger.py +0 -0
  32. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/lycoris/utils/preset.py +0 -0
  33. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/lycoris/utils/quant.py +0 -0
  34. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/lycoris/utils/xformers_utils.py +0 -0
  35. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/lycoris/wrapper.py +0 -0
  36. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/lycoris_lora.egg-info/SOURCES.txt +0 -0
  37. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/lycoris_lora.egg-info/dependency_links.txt +0 -0
  38. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/lycoris_lora.egg-info/not-zip-safe +0 -0
  39. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/lycoris_lora.egg-info/requires.txt +0 -0
  40. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/lycoris_lora.egg-info/top_level.txt +0 -0
  41. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/setup.cfg +0 -0
  42. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/test/__init__.py +0 -0
  43. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/test/functional.py +0 -0
  44. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/test/kohya.py +0 -0
  45. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/test/module.py +0 -0
  46. {lycoris_lora-3.1.1 → lycoris_lora-3.2.0.dev1}/test/wrapper.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: lycoris_lora
3
- Version: 3.1.1
3
+ Version: 3.2.0.dev1
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
@@ -6,10 +6,12 @@ PRESET = {
6
6
  "ResnetBlock2D",
7
7
  "Downsample2D",
8
8
  "Upsample2D",
9
- "HunYuanDiTBlock",
10
- "DoubleStreamBlock",
11
- "SingleStreamBlock",
12
- "SingleDiTBlock",
9
+ "HunYuanDiTBlock", #HunYuanDiT
10
+ "DoubleStreamBlock", #Flux
11
+ "SingleStreamBlock", #Flux
12
+ "SingleDiTBlock", #SD3.5
13
+ "MMDoubleStreamBlock", #HunYuanVideo
14
+ "MMSingleStreamBlock", #HunYuanVideo
13
15
  ],
14
16
  "unet_target_name": [
15
17
  "conv_in",
@@ -35,6 +37,8 @@ PRESET = {
35
37
  "DoubleStreamBlock",
36
38
  "SingleStreamBlock",
37
39
  "SingleDiTBlock",
40
+ "MMDoubleStreamBlock", #HunYuanVideo
41
+ "MMSingleStreamBlock", #HunYuanVideo
38
42
  ],
39
43
  "unet_target_name": [
40
44
  "time_embedding.linear_1",
@@ -57,6 +61,8 @@ PRESET = {
57
61
  "DoubleStreamBlock",
58
62
  "SingleStreamBlock",
59
63
  "SingleDiTBlock",
64
+ "MMDoubleStreamBlock", #HunYuanVideo
65
+ "MMSingleStreamBlock", #HunYuanVideo
60
66
  ],
61
67
  "unet_target_name": [],
62
68
  "text_encoder_target_module": [
@@ -94,6 +100,8 @@ PRESET = {
94
100
  "DoubleStreamBlock",
95
101
  "SingleStreamBlock",
96
102
  "SingleDiTBlock",
103
+ "MMDoubleStreamBlock", #HunYuanVideo
104
+ "MMSingleStreamBlock", #HunYuanVideo
97
105
  ],
98
106
  "unet_target_name": [
99
107
  "conv_in",
@@ -112,6 +120,8 @@ PRESET = {
112
120
  "DoubleStreamBlock",
113
121
  "SingleStreamBlock",
114
122
  "SingleDiTBlock",
123
+ "MMDoubleStreamBlock", #HunYuanVideo
124
+ "MMSingleStreamBlock", #HunYuanVideo
115
125
  ],
116
126
  "unet_target_name": [],
117
127
  "text_encoder_target_module": [],
@@ -63,6 +63,10 @@ def create_network(
63
63
  rs_lora = str_bool(kwargs.get("rs_lora", False))
64
64
  unbalanced_factorization = str_bool(kwargs.get("unbalanced_factorization", False))
65
65
  train_t5xxl = str_bool(kwargs.get("train_t5xxl", False))
66
+ #lora_plus
67
+ loraplus_lr_ratio = float(kwargs.get("loraplus_lr_ratio", None)) if kwargs.get("loraplus_lr_ratio", None) is not None else None
68
+ loraplus_unet_lr_ratio = float(kwargs.get("loraplus_unet_lr_ratio", None)) if kwargs.get("loraplus_unet_lr_ratio", None) is not None else None
69
+ loraplus_text_encoder_lr_ratio = float(kwargs.get("loraplus_text_encoder_lr_ratio", None)) if kwargs.get("loraplus_text_encoder_lr_ratio", None) is not None else None
66
70
 
67
71
  if unbalanced_factorization:
68
72
  logger.info("Unbalanced factorization for LoKr is enabled")
@@ -117,6 +121,8 @@ def create_network(
117
121
  unbalanced_factorization=unbalanced_factorization,
118
122
  train_t5xxl=train_t5xxl,
119
123
  )
124
+ if loraplus_lr_ratio is not None or loraplus_unet_lr_ratio is not None or loraplus_text_encoder_lr_ratio is not None:
125
+ network.set_loraplus_lr_ratio(loraplus_lr_ratio, loraplus_unet_lr_ratio, loraplus_text_encoder_lr_ratio)
120
126
 
121
127
  return network
122
128
 
@@ -157,22 +163,23 @@ def create_network_from_weights(
157
163
  if lora_name in unet_loras:
158
164
  unet_loras[lora_name] = modules
159
165
 
160
- if isinstance(text_encoder, list):
161
- text_encoders = text_encoder
162
- use_index = True
163
- else:
164
- text_encoders = [text_encoder]
165
- use_index = False
166
-
167
- for idx, te in enumerate(text_encoders):
168
- if use_index:
169
- prefix = f"{LycorisNetworkKohya.LORA_PREFIX_TEXT_ENCODER}{idx+1}"
166
+ if text_encoder:
167
+ if isinstance(text_encoder, list):
168
+ text_encoders = text_encoder
169
+ use_index = True
170
170
  else:
171
- prefix = LycorisNetworkKohya.LORA_PREFIX_TEXT_ENCODER
172
- for name, modules in te.named_modules():
173
- lora_name = f"{prefix}_{name}".replace(".", "_")
174
- if lora_name in te_loras:
175
- te_loras[lora_name] = modules
171
+ text_encoders = [text_encoder]
172
+ use_index = False
173
+
174
+ for idx, te in enumerate(text_encoders):
175
+ if use_index:
176
+ prefix = f"{LycorisNetworkKohya.LORA_PREFIX_TEXT_ENCODER}{idx+1}"
177
+ else:
178
+ prefix = LycorisNetworkKohya.LORA_PREFIX_TEXT_ENCODER
179
+ for name, modules in te.named_modules():
180
+ lora_name = f"{prefix}_{name}".replace(".", "_")
181
+ if lora_name in te_loras:
182
+ te_loras[lora_name] = modules
176
183
 
177
184
  original_level = logger.level
178
185
  logger.setLevel(logging.ERROR)
@@ -192,14 +199,16 @@ def create_network_from_weights(
192
199
  logger.info(f"{len(network.unet_loras)} Modules Loaded")
193
200
 
194
201
  logger.info("Loading TE Modules from state dict...")
195
- for lora_name, orig_modules in te_loras.items():
196
- if orig_modules is None:
197
- continue
198
- lyco_type, params = get_module(weights_sd, lora_name)
199
- module = make_module(lyco_type, params, lora_name, orig_modules)
200
- if module is not None:
201
- network.text_encoder_loras.append(module)
202
- logger.info(f"{len(network.text_encoder_loras)} Modules Loaded")
202
+
203
+ if text_encoder:
204
+ for lora_name, orig_modules in te_loras.items():
205
+ if orig_modules is None:
206
+ continue
207
+ lyco_type, params = get_module(weights_sd, lora_name)
208
+ module = make_module(lyco_type, params, lora_name, orig_modules)
209
+ if module is not None:
210
+ network.text_encoder_loras.append(module)
211
+ logger.info(f"{len(network.text_encoder_loras)} Modules Loaded")
203
212
 
204
213
  for lora in network.unet_loras + network.text_encoder_loras:
205
214
  lora.multiplier = multiplier
@@ -223,6 +232,8 @@ class LycorisNetworkKohya(LycorisNetwork):
223
232
  "DoubleStreamBlock",
224
233
  "SingleStreamBlock",
225
234
  "SingleDiTBlock",
235
+ "MMDoubleStreamBlock", #HunYuanVideo
236
+ "MMSingleStreamBlock", #HunYuanVideo
226
237
  ]
227
238
  UNET_TARGET_REPLACE_NAME = [
228
239
  "conv_in",
@@ -290,6 +301,11 @@ class LycorisNetworkKohya(LycorisNetwork):
290
301
  self.multiplier = multiplier
291
302
  self.lora_dim = lora_dim
292
303
  self.train_t5xxl = train_t5xxl
304
+
305
+ # 初始化LoRA+相关属性
306
+ self.loraplus_lr_ratio = None
307
+ self.loraplus_unet_lr_ratio = None
308
+ self.loraplus_text_encoder_lr_ratio = None
293
309
 
294
310
  if not self.ENABLE_CONV:
295
311
  conv_lora_dim = 0
@@ -460,27 +476,28 @@ class LycorisNetworkKohya(LycorisNetwork):
460
476
  ]
461
477
  LycorisNetworkKohya.UNET_TARGET_REPLACE_NAME = []
462
478
 
463
- if isinstance(text_encoder, list):
464
- text_encoders = text_encoder
465
- use_index = True
466
- else:
467
- text_encoders = [text_encoder]
468
- use_index = False
469
-
470
479
  self.text_encoder_loras = []
471
- for i, te in enumerate(text_encoders):
472
- self.text_encoder_loras.extend(
473
- create_modules(
474
- LycorisNetworkKohya.LORA_PREFIX_TEXT_ENCODER
475
- + (f"{i+1}" if use_index else ""),
476
- te,
477
- LycorisNetworkKohya.TEXT_ENCODER_TARGET_REPLACE_MODULE,
478
- LycorisNetworkKohya.TEXT_ENCODER_TARGET_REPLACE_NAME,
480
+ if text_encoder:
481
+ if isinstance(text_encoder, list):
482
+ text_encoders = text_encoder
483
+ use_index = True
484
+ else:
485
+ text_encoders = [text_encoder]
486
+ use_index = False
487
+
488
+ for i, te in enumerate(text_encoders):
489
+ self.text_encoder_loras.extend(
490
+ create_modules(
491
+ LycorisNetworkKohya.LORA_PREFIX_TEXT_ENCODER
492
+ + (f"{i+1}" if use_index else ""),
493
+ te,
494
+ LycorisNetworkKohya.TEXT_ENCODER_TARGET_REPLACE_MODULE,
495
+ LycorisNetworkKohya.TEXT_ENCODER_TARGET_REPLACE_NAME,
496
+ )
479
497
  )
498
+ logger.info(
499
+ f"create LyCORIS for Text Encoder: {len(self.text_encoder_loras)} modules."
480
500
  )
481
- logger.info(
482
- f"create LyCORIS for Text Encoder: {len(self.text_encoder_loras)} modules."
483
- )
484
501
 
485
502
  self.unet_loras = create_modules(
486
503
  LycorisNetworkKohya.LORA_PREFIX_UNET,
@@ -604,29 +621,87 @@ class LycorisNetworkKohya(LycorisNetwork):
604
621
 
605
622
  return key_scaled, sum(norms) / len(norms), max(norms)
606
623
 
607
- def prepare_optimizer_params(self, text_encoder_lr, unet_lr, learning_rate):
608
- def enumerate_params(loras):
609
- params = []
610
- for lora in loras:
611
- params.extend(lora.parameters())
612
- return params
624
+ def set_loraplus_lr_ratio(self, loraplus_lr_ratio, loraplus_unet_lr_ratio, loraplus_text_encoder_lr_ratio):
625
+ self.loraplus_lr_ratio = loraplus_lr_ratio
626
+ self.loraplus_unet_lr_ratio = loraplus_unet_lr_ratio
627
+ self.loraplus_text_encoder_lr_ratio = loraplus_text_encoder_lr_ratio
613
628
 
629
+ logger.info(f"LoRA+ UNet LR Ratio: {self.loraplus_unet_lr_ratio or self.loraplus_lr_ratio}")
630
+ logger.info(f"LoRA+ Text Encoder LR Ratio: {self.loraplus_text_encoder_lr_ratio or self.loraplus_lr_ratio}")
631
+
632
+ def prepare_optimizer_params(self, text_encoder_lr=None, unet_lr: float = 1e-4, learning_rate=None):
614
633
  self.requires_grad_(True)
634
+
615
635
  all_params = []
636
+ lr_descriptions = []
637
+
638
+ def assemble_params(loras, lr, ratio):
639
+ param_groups = {"lora": {}, "plus": {}}
640
+ for lora in loras:
641
+ for name, param in lora.named_parameters():
642
+ if ratio is not None and "lora_up" in name:
643
+ param_groups["plus"][f"{lora.lora_name}.{name}"] = param
644
+ else:
645
+ param_groups["lora"][f"{lora.lora_name}.{name}"] = param
646
+
647
+ params = []
648
+ descriptions = []
649
+ for key in param_groups.keys():
650
+ param_data = {"params": param_groups[key].values()}
651
+
652
+ if len(param_data["params"]) == 0:
653
+ continue
654
+
655
+ if lr is not None:
656
+ if key == "plus":
657
+ param_data["lr"] = lr * ratio
658
+ else:
659
+ param_data["lr"] = lr
660
+
661
+ if param_data.get("lr", None) == 0 or param_data.get("lr", None) is None:
662
+ logger.info("NO LR skipping!")
663
+ continue
664
+
665
+ params.append(param_data)
666
+ descriptions.append("plus" if key == "plus" else "")
667
+
668
+ return params, descriptions
616
669
 
617
670
  if self.text_encoder_loras:
618
- param_data = {"params": enumerate_params(self.text_encoder_loras)}
619
- if text_encoder_lr is not None:
620
- param_data["lr"] = text_encoder_lr
621
- all_params.append(param_data)
671
+ params, descriptions = assemble_params(
672
+ self.text_encoder_loras,
673
+ text_encoder_lr if text_encoder_lr is not None else default_lr,
674
+ self.loraplus_text_encoder_lr_ratio or self.loraplus_lr_ratio,
675
+ )
676
+ all_params.extend(params)
677
+ lr_descriptions.extend(["textencoder" + (" " + d if d else "") for d in descriptions])
622
678
 
623
679
  if self.unet_loras:
624
- param_data = {"params": enumerate_params(self.unet_loras)}
625
- if unet_lr is not None:
626
- param_data["lr"] = unet_lr
627
- all_params.append(param_data)
680
+ params, descriptions = assemble_params(
681
+ self.unet_loras,
682
+ unet_lr if unet_lr is not None else default_lr,
683
+ self.loraplus_unet_lr_ratio or self.loraplus_lr_ratio,
684
+ )
685
+ all_params.extend(params)
686
+ lr_descriptions.extend(["unet" + (" " + d if d else "") for d in descriptions])
687
+
688
+ return all_params, lr_descriptions
689
+
690
+ def enable_gradient_checkpointing(self):
691
+ # not supported
692
+ pass
693
+
694
+ def prepare_grad_etc(self, unet):
695
+ self.requires_grad_(True)
696
+
697
+ def on_epoch_start(self, unet):
698
+ self.train()
699
+
700
+ def on_step_start(self):
701
+ pass
628
702
 
629
- return all_params
703
+ def get_trainable_params(self):
704
+ return self.parameters()
630
705
 
631
706
  def save_weights(self, file, dtype, metadata):
632
707
  if metadata is not None and len(metadata) == 0:
@@ -275,6 +275,9 @@ class LycorisBaseModule(ModuleCustomSD):
275
275
  def merge_to(self, multiplier=1.0):
276
276
  if self.not_supported:
277
277
  return
278
+ self_device = next(self.parameters()).device
279
+ self_dtype = next(self.parameters()).dtype
280
+ self.to(self.org_weight)
278
281
  weight, bias = self.get_merged_weight(
279
282
  multiplier, self.org_weight.shape, self.org_weight.device
280
283
  )
@@ -285,6 +288,7 @@ class LycorisBaseModule(ModuleCustomSD):
285
288
  self.org_module[0].bias.data.copy_(bias)
286
289
  else:
287
290
  self.org_module[0].bias = nn.Parameter(bias)
291
+ self.to(self_device, self_dtype)
288
292
 
289
293
  def get_diff_weight(self, multiplier=1.0, shape=None, device=None):
290
294
  raise NotImplementedError
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: lycoris_lora
3
- Version: 3.1.1
3
+ Version: 3.2.0.dev1
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="3.1.1",
7
+ version="3.2.0.dev1",
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",