lycoris-lora 3.0.1.dev5__tar.gz → 3.0.1.dev7__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 (47) hide show
  1. {lycoris_lora-3.0.1.dev5/lycoris_lora.egg-info → lycoris_lora-3.0.1.dev7}/PKG-INFO +1 -1
  2. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/lycoris/kohya.py +14 -4
  3. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/lycoris/modules/norms.py +2 -2
  4. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/lycoris/utils/bnb.py +3 -1
  5. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/lycoris/wrapper.py +103 -22
  6. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7/lycoris_lora.egg-info}/PKG-INFO +1 -1
  7. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/setup.py +1 -1
  8. lycoris_lora-3.0.1.dev7/test/wrapper.py +777 -0
  9. lycoris_lora-3.0.1.dev5/test/wrapper.py +0 -290
  10. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/LICENSE.md +0 -0
  11. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/README.md +0 -0
  12. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/lycoris/__init__.py +0 -0
  13. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/lycoris/config.py +0 -0
  14. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/lycoris/functional/__init__.py +0 -0
  15. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/lycoris/functional/boft.py +0 -0
  16. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/lycoris/functional/diag_oft.py +0 -0
  17. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/lycoris/functional/general.py +0 -0
  18. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/lycoris/functional/locon.py +0 -0
  19. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/lycoris/functional/loha.py +0 -0
  20. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/lycoris/functional/lokr.py +0 -0
  21. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/lycoris/logging.py +0 -0
  22. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/lycoris/modules/__init__.py +0 -0
  23. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/lycoris/modules/base.py +0 -0
  24. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/lycoris/modules/boft.py +0 -0
  25. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/lycoris/modules/diag_oft.py +0 -0
  26. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/lycoris/modules/dylora.py +0 -0
  27. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/lycoris/modules/full.py +0 -0
  28. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/lycoris/modules/glora.py +0 -0
  29. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/lycoris/modules/ia3.py +0 -0
  30. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/lycoris/modules/locon.py +0 -0
  31. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/lycoris/modules/loha.py +0 -0
  32. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/lycoris/modules/lokr.py +0 -0
  33. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/lycoris/utils/__init__.py +0 -0
  34. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/lycoris/utils/general.py +0 -0
  35. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/lycoris/utils/logger.py +0 -0
  36. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/lycoris/utils/preset.py +0 -0
  37. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/lycoris/utils/xformers_utils.py +0 -0
  38. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/lycoris_lora.egg-info/SOURCES.txt +0 -0
  39. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/lycoris_lora.egg-info/dependency_links.txt +0 -0
  40. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/lycoris_lora.egg-info/not-zip-safe +0 -0
  41. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/lycoris_lora.egg-info/requires.txt +0 -0
  42. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/lycoris_lora.egg-info/top_level.txt +0 -0
  43. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/setup.cfg +0 -0
  44. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/test/__init__.py +0 -0
  45. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/test/functional.py +0 -0
  46. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/test/kohya.py +0 -0
  47. {lycoris_lora-3.0.1.dev5 → lycoris_lora-3.0.1.dev7}/test/module.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: lycoris_lora
3
- Version: 3.0.1.dev5
3
+ Version: 3.0.1.dev7
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
@@ -1,6 +1,8 @@
1
1
  import os
2
+ import fnmatch
2
3
  import re
3
4
  import logging
5
+
4
6
  from typing import Any, List
5
7
 
6
8
  import torch
@@ -28,7 +30,7 @@ from .logging import logger
28
30
  def create_network(
29
31
  multiplier, network_dim, network_alpha, vae, text_encoder, unet, **kwargs
30
32
  ):
31
- for key, value in kwargs.items():
33
+ for key, value in list(kwargs.items()):
32
34
  if key in deprecated_arg_dict:
33
35
  logger.warning(
34
36
  f"{key} is deprecated. Please use {deprecated_arg_dict[key]} instead.",
@@ -234,6 +236,7 @@ class LycorisNetworkKohya(LycorisNetwork):
234
236
  LORA_PREFIX_TEXT_ENCODER = "lora_te"
235
237
  MODULE_ALGO_MAP = {}
236
238
  NAME_ALGO_MAP = {}
239
+ USE_FNMATCH = False
237
240
 
238
241
  @classmethod
239
242
  def apply_preset(cls, preset):
@@ -253,6 +256,8 @@ class LycorisNetworkKohya(LycorisNetwork):
253
256
  cls.MODULE_ALGO_MAP = preset["module_algo_map"]
254
257
  if "name_algo_map" in preset:
255
258
  cls.NAME_ALGO_MAP = preset["name_algo_map"]
259
+ if "use_fnmatch" in preset:
260
+ cls.USE_FNMATCH = preset["use_fnmatch"]
256
261
  return cls
257
262
 
258
263
  def __init__(
@@ -405,7 +410,7 @@ class LycorisNetworkKohya(LycorisNetwork):
405
410
  for name, module in root_module.named_modules():
406
411
  module_name = module.__class__.__name__
407
412
  if module_name in target_replace_modules and not any(
408
- re.match(t, name) for t in target_replace_names
413
+ self.match_fn(t, name) for t in target_replace_names
409
414
  ):
410
415
  if module_name in self.MODULE_ALGO_MAP:
411
416
  next_config = self.MODULE_ALGO_MAP[module_name]
@@ -419,7 +424,7 @@ class LycorisNetworkKohya(LycorisNetwork):
419
424
  )
420
425
  next_config = {}
421
426
  elif name in target_replace_names or any(
422
- re.match(t, name) for t in target_replace_names
427
+ self.match_fn(t, name) for t in target_replace_names
423
428
  ):
424
429
  conf_from_name = self.find_conf_for_name(name)
425
430
  if conf_from_name is not None:
@@ -495,6 +500,11 @@ class LycorisNetworkKohya(LycorisNetwork):
495
500
  ), f"duplicated lora name: {lora.lora_name}"
496
501
  names.add(lora.lora_name)
497
502
 
503
+ def match_fn(self, pattern: str, name: str) -> bool:
504
+ if self.USE_FNMATCH:
505
+ return fnmatch.fnmatch(name, pattern)
506
+ return re.match(pattern, name)
507
+
498
508
  def find_conf_for_name(
499
509
  self,
500
510
  name: str,
@@ -503,7 +513,7 @@ class LycorisNetworkKohya(LycorisNetwork):
503
513
  return self.NAME_ALGO_MAP[name]
504
514
 
505
515
  for key, value in self.NAME_ALGO_MAP.items():
506
- if re.match(key, name):
516
+ if self.match_fn(key, name):
507
517
  return value
508
518
 
509
519
  return None
@@ -37,14 +37,14 @@ class NormModule(LycorisBaseModule):
37
37
  if self.module_type == "unknown":
38
38
  if not hasattr(org_module, "weight") or not hasattr(org_module, "_norm"):
39
39
  warning_once(f"{type(org_module)} is not supported in Norm algo.")
40
- self.not_supported=True
40
+ self.not_supported = True
41
41
  return
42
42
  else:
43
43
  self.dim = org_module.weight.numel()
44
44
  self.not_supported = False
45
45
  elif self.module_type not in self.support_module:
46
46
  warning_once(f"{self.module_type} is not supported in Norm algo.")
47
- self.not_supported=True
47
+ self.not_supported = True
48
48
  return
49
49
 
50
50
  self.w_norm = nn.Parameter(torch.zeros(self.dim))
@@ -74,4 +74,6 @@ QuantLinears = (
74
74
 
75
75
  @cache
76
76
  def log_bypass():
77
- return logger.warning("Using bnb/quanto/optimum-quanto with LyCORIS will enable force-bypass mode.")
77
+ return logger.warning(
78
+ "Using bnb/quanto/optimum-quanto with LyCORIS will enable force-bypass mode."
79
+ )
@@ -1,5 +1,6 @@
1
1
  # General LyCORIS wrapper based on kohya-ss/sd-scripts' style
2
2
  import os
3
+ import fnmatch
3
4
  import re
4
5
  import logging
5
6
 
@@ -25,6 +26,22 @@ from .utils import str_bool
25
26
  from .logging import logger
26
27
 
27
28
 
29
+ VALID_PRESET_KEYS = [
30
+ "enable_conv",
31
+ "target_module",
32
+ "target_name",
33
+ "module_algo_map",
34
+ "name_algo_map",
35
+ "lora_prefix",
36
+ "use_fnmatch",
37
+ "unet_target_module",
38
+ "unet_target_name",
39
+ "text_encoder_target_module",
40
+ "text_encoder_target_name",
41
+ "exclude_name",
42
+ ]
43
+
44
+
28
45
  network_module_dict = {
29
46
  "lora": LoConModule,
30
47
  "locon": LoConModule,
@@ -45,7 +62,7 @@ deprecated_arg_dict = {
45
62
 
46
63
 
47
64
  def create_lycoris(module, multiplier=1.0, linear_dim=4, linear_alpha=1, **kwargs):
48
- for key, value in kwargs.items():
65
+ for key, value in list(kwargs.items()):
49
66
  if key in deprecated_arg_dict:
50
67
  logger.warning(
51
68
  f"{key} is deprecated. Please use {deprecated_arg_dict[key]} instead.",
@@ -189,9 +206,17 @@ class LycorisNetwork(torch.nn.Module):
189
206
  LORA_PREFIX = "lycoris"
190
207
  MODULE_ALGO_MAP = {}
191
208
  NAME_ALGO_MAP = {}
209
+ USE_FNMATCH = False
210
+ TARGET_EXCLUDE_NAME = []
192
211
 
193
212
  @classmethod
194
213
  def apply_preset(cls, preset):
214
+ for preset_key in preset.keys():
215
+ if preset_key not in VALID_PRESET_KEYS:
216
+ raise KeyError(
217
+ f'Unknown preset key "{preset_key}". Valid keys: {VALID_PRESET_KEYS}'
218
+ )
219
+
195
220
  if "enable_conv" in preset:
196
221
  cls.ENABLE_CONV = preset["enable_conv"]
197
222
  if "target_module" in preset:
@@ -202,6 +227,12 @@ class LycorisNetwork(torch.nn.Module):
202
227
  cls.MODULE_ALGO_MAP = preset["module_algo_map"]
203
228
  if "name_algo_map" in preset:
204
229
  cls.NAME_ALGO_MAP = preset["name_algo_map"]
230
+ if "lora_prefix" in preset:
231
+ cls.LORA_PREFIX = preset["lora_prefix"]
232
+ if "use_fnmatch" in preset:
233
+ cls.USE_FNMATCH = preset["use_fnmatch"]
234
+ if "exclude_name" in preset:
235
+ cls.TARGET_EXCLUDE_NAME = preset["exclude_name"]
205
236
  return cls
206
237
 
207
238
  def __init__(
@@ -324,27 +355,42 @@ class LycorisNetwork(torch.nn.Module):
324
355
  prefix: str,
325
356
  root_module: torch.nn.Module,
326
357
  algo,
358
+ current_lora_map: dict[str, Any],
327
359
  configs={},
328
360
  ):
329
- loras = {}
361
+ assert current_lora_map is not None, "No mapping supplied"
362
+ loras = current_lora_map
330
363
  lora_names = []
331
364
  for name, module in root_module.named_modules():
332
365
  module_name = module.__class__.__name__
333
366
  if module_name in self.MODULE_ALGO_MAP and module is not root_module:
334
367
  next_config = self.MODULE_ALGO_MAP[module_name]
335
368
  next_algo = next_config.get("algo", algo)
336
- new_loras, new_lora_names = create_modules_(
337
- f"{prefix}_{name}" if name else prefix, module, next_algo, next_config
369
+ new_loras, new_lora_names, new_lora_map = create_modules_(
370
+ f"{prefix}_{name}" if name else prefix,
371
+ module,
372
+ next_algo,
373
+ loras,
374
+ configs=next_config,
338
375
  )
376
+ loras = {**loras, **new_lora_map}
339
377
  for lora_name, lora in zip(new_lora_names, new_loras):
340
- if lora_name not in loras:
378
+ if lora_name not in loras and lora_name not in current_lora_map:
341
379
  loras[lora_name] = lora
342
380
  lora_names.append(lora_name)
343
381
  continue
382
+
344
383
  if name:
345
384
  lora_name = prefix + "." + name
346
385
  else:
347
386
  lora_name = prefix
387
+
388
+ if f"{self.LORA_PREFIX}_." in lora_name:
389
+ lora_name = lora_name.replace(
390
+ f"{self.LORA_PREFIX}_.",
391
+ f"{self.LORA_PREFIX}.",
392
+ )
393
+
348
394
  lora_name = lora_name.replace(".", "_")
349
395
  if lora_name in loras:
350
396
  continue
@@ -353,7 +399,7 @@ class LycorisNetwork(torch.nn.Module):
353
399
  if lora is not None:
354
400
  loras[lora_name] = lora
355
401
  lora_names.append(lora_name)
356
- return [loras[lora_name] for lora_name in lora_names], lora_names
402
+ return [loras[lora_name] for lora_name in lora_names], lora_names, loras
357
403
 
358
404
  # create module instances
359
405
  def create_modules(
@@ -361,28 +407,40 @@ class LycorisNetwork(torch.nn.Module):
361
407
  root_module: torch.nn.Module,
362
408
  target_replace_modules,
363
409
  target_replace_names=[],
410
+ target_exclude_names=[],
364
411
  ) -> List:
365
412
  logger.info("Create LyCORIS Module")
366
413
  loras = []
414
+ lora_map = {}
367
415
  next_config = {}
368
416
  for name, module in root_module.named_modules():
417
+ if name in target_exclude_names or any(
418
+ self.match_fn(t, name) for t in target_exclude_names
419
+ ):
420
+ continue
421
+
369
422
  module_name = module.__class__.__name__
370
423
  if module_name in target_replace_modules and not any(
371
- re.match(t, name) for t in target_replace_names
424
+ self.match_fn(t, name) for t in target_replace_names
372
425
  ):
373
426
  if module_name in self.MODULE_ALGO_MAP:
374
427
  next_config = self.MODULE_ALGO_MAP[module_name]
375
428
  algo = next_config.get("algo", network_module)
376
429
  else:
377
430
  algo = network_module
378
- loras.extend(
379
- create_modules_(f"{prefix}_{name}", module, algo, next_config)[
380
- 0
381
- ]
431
+
432
+ lora_lst, _, _lora_map = create_modules_(
433
+ f"{prefix}_{name}",
434
+ module,
435
+ algo,
436
+ lora_map,
437
+ configs=next_config,
382
438
  )
439
+ lora_map = {**lora_map, **_lora_map}
440
+ loras.extend(lora_lst)
383
441
  next_config = {}
384
442
  elif name in target_replace_names or any(
385
- re.match(t, name) for t in target_replace_names
443
+ self.match_fn(t, name) for t in target_replace_names
386
444
  ):
387
445
  conf_from_name = self.find_conf_for_name(name)
388
446
  if conf_from_name is not None:
@@ -395,23 +453,37 @@ class LycorisNetwork(torch.nn.Module):
395
453
  algo = network_module
396
454
  lora_name = prefix + "." + name
397
455
  lora_name = lora_name.replace(".", "_")
456
+
457
+ if lora_name in lora_map:
458
+ continue
459
+
398
460
  lora = create_single_module(lora_name, module, algo, **next_config)
399
461
  next_config = {}
400
462
  if lora is not None:
463
+ lora_map[lora.lora_name] = lora
401
464
  loras.append(lora)
402
465
  return loras
403
466
 
404
467
  self.loras = create_modules(
405
468
  LycorisNetwork.LORA_PREFIX,
406
469
  module,
407
- list(set([
408
- *LycorisNetwork.TARGET_REPLACE_MODULE,
409
- *LycorisNetwork.MODULE_ALGO_MAP.keys(),
410
- ])),
411
- list(set([
412
- *LycorisNetwork.TARGET_REPLACE_NAME,
413
- *LycorisNetwork.NAME_ALGO_MAP.keys(),
414
- ])),
470
+ list(
471
+ set(
472
+ [
473
+ *LycorisNetwork.TARGET_REPLACE_MODULE,
474
+ *LycorisNetwork.MODULE_ALGO_MAP.keys(),
475
+ ]
476
+ )
477
+ ),
478
+ list(
479
+ set(
480
+ [
481
+ *LycorisNetwork.TARGET_REPLACE_NAME,
482
+ *LycorisNetwork.NAME_ALGO_MAP.keys(),
483
+ ]
484
+ )
485
+ ),
486
+ target_exclude_names=LycorisNetwork.TARGET_EXCLUDE_NAME,
415
487
  )
416
488
  logger.info(f"create LyCORIS: {len(self.loras)} modules.")
417
489
 
@@ -422,7 +494,8 @@ class LycorisNetwork(torch.nn.Module):
422
494
  )
423
495
  logger.info(f"module type table: {algo_table}")
424
496
 
425
- # assertion
497
+ # Assertion to ensure we have not accidentally wrapped some layers
498
+ # multiple times.
426
499
  names = set()
427
500
  for lora in self.loras:
428
501
  assert (
@@ -430,6 +503,11 @@ class LycorisNetwork(torch.nn.Module):
430
503
  ), f"duplicated lora name: {lora.lora_name}"
431
504
  names.add(lora.lora_name)
432
505
 
506
+ def match_fn(self, pattern: str, name: str) -> bool:
507
+ if self.USE_FNMATCH:
508
+ return fnmatch.fnmatch(name, pattern)
509
+ return bool(re.match(pattern, name))
510
+
433
511
  def find_conf_for_name(
434
512
  self,
435
513
  name: str,
@@ -438,7 +516,7 @@ class LycorisNetwork(torch.nn.Module):
438
516
  return self.NAME_ALGO_MAP[name]
439
517
 
440
518
  for key, value in self.NAME_ALGO_MAP.items():
441
- if re.match(key, name):
519
+ if self.match_fn(key, name):
442
520
  return value
443
521
 
444
522
  return None
@@ -464,6 +542,9 @@ class LycorisNetwork(torch.nn.Module):
464
542
  return state
465
543
 
466
544
  def apply_to(self):
545
+ """
546
+ Register to modules to the subclass so that torch sees them.
547
+ """
467
548
  for lora in self.loras:
468
549
  lora.apply_to()
469
550
  self.add_module(lora.lora_name, lora)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: lycoris_lora
3
- Version: 3.0.1.dev5
3
+ Version: 3.0.1.dev7
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.0.1.dev5",
7
+ version="3.0.1.dev7",
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",