lycoris-lora 3.0.0.dev14__tar.gz → 3.0.0.dev15__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.0.0.dev14/lycoris_lora.egg-info → lycoris_lora-3.0.0.dev15}/PKG-INFO +1 -1
  2. lycoris_lora-3.0.0.dev15/lycoris/functional/__init__.py +9 -0
  3. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/lycoris/functional/general.py +27 -0
  4. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/lycoris/modules/base.py +2 -1
  5. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/lycoris/modules/boft.py +1 -1
  6. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/lycoris/modules/diag_oft.py +1 -1
  7. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/lycoris/modules/glora.py +1 -1
  8. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/lycoris/utils/__init__.py +21 -158
  9. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15/lycoris_lora.egg-info}/PKG-INFO +1 -1
  10. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/setup.py +1 -1
  11. lycoris_lora-3.0.0.dev14/lycoris/functional/__init__.py +0 -1
  12. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/LICENSE.md +0 -0
  13. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/README.md +0 -0
  14. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/lycoris/__init__.py +0 -0
  15. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/lycoris/config.py +0 -0
  16. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/lycoris/functional/boft.py +0 -0
  17. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/lycoris/functional/diag_oft.py +0 -0
  18. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/lycoris/functional/locon.py +0 -0
  19. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/lycoris/functional/loha.py +0 -0
  20. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/lycoris/functional/lokr.py +0 -0
  21. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/lycoris/kohya.py +0 -0
  22. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/lycoris/logging.py +0 -0
  23. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/lycoris/modules/__init__.py +0 -0
  24. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/lycoris/modules/dylora.py +0 -0
  25. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/lycoris/modules/full.py +0 -0
  26. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/lycoris/modules/ia3.py +0 -0
  27. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/lycoris/modules/locon.py +0 -0
  28. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/lycoris/modules/loha.py +0 -0
  29. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/lycoris/modules/lokr.py +0 -0
  30. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/lycoris/modules/norms.py +0 -0
  31. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/lycoris/utils/bnb.py +0 -0
  32. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/lycoris/utils/general.py +0 -0
  33. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/lycoris/utils/logger.py +0 -0
  34. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/lycoris/utils/preset.py +0 -0
  35. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/lycoris/utils/xformers_utils.py +0 -0
  36. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/lycoris/wrapper.py +0 -0
  37. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/lycoris_lora.egg-info/SOURCES.txt +0 -0
  38. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/lycoris_lora.egg-info/dependency_links.txt +0 -0
  39. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/lycoris_lora.egg-info/not-zip-safe +0 -0
  40. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/lycoris_lora.egg-info/requires.txt +0 -0
  41. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/lycoris_lora.egg-info/top_level.txt +0 -0
  42. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/setup.cfg +0 -0
  43. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/test/__init__.py +0 -0
  44. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/test/functional.py +0 -0
  45. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/test/module.py +0 -0
  46. {lycoris_lora-3.0.0.dev14 → lycoris_lora-3.0.0.dev15}/test/wrapper.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: lycoris_lora
3
- Version: 3.0.0.dev14
3
+ Version: 3.0.0.dev15
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
@@ -0,0 +1,9 @@
1
+ from .general import (
2
+ rebuild_tucker,
3
+ factorization,
4
+ power2factorization,
5
+ FUNC_LIST,
6
+ tucker_weight,
7
+ tucker_weight_from_conv,
8
+ apply_dora_scale,
9
+ )
@@ -77,3 +77,30 @@ def power2factorization(dimension: int, factor: int = -1) -> tuple[int, int]:
77
77
  n = dimension // m
78
78
 
79
79
  return dimension // n, n
80
+
81
+
82
+ def tucker_weight_from_conv(up, down, mid):
83
+ up = up.reshape(up.size(0), up.size(1))
84
+ down = down.reshape(down.size(0), down.size(1))
85
+ return torch.einsum("m n ..., i m, n j -> i j ...", mid, up, down)
86
+
87
+
88
+ def tucker_weight(wa, wb, t):
89
+ temp = torch.einsum("i j ..., j r -> i r ...", t, wb)
90
+ return torch.einsum("i j ..., i r -> r j ...", temp, wa)
91
+
92
+
93
+ def apply_dora_scale(org_weight, rebuild, dora_scale, scale):
94
+ dora_norm_dims = org_weight.dim() - 1
95
+ weight = org_weight + rebuild
96
+ weight = weight.to(dora_scale.dtype)
97
+ weight_norm = (
98
+ weight.transpose(0, 1)
99
+ .reshape(weight.shape[1], -1)
100
+ .norm(dim=1, keepdim=True)
101
+ .reshape(weight.shape[1], *[1] * dora_norm_dims)
102
+ .transpose(0, 1)
103
+ )
104
+ merged_scale1 = weight / weight_norm * dora_scale
105
+ diff_weight = merged_scale1 - org_weight
106
+ return org_weight + diff_weight * scale
@@ -269,8 +269,9 @@ class LycorisBaseModule(ModuleCustomSD):
269
269
  weight, bias = self.get_merged_weight(
270
270
  multiplier, self.org_weight.shape, self.org_weight.device
271
271
  )
272
- self.org_weight = weight
272
+ self.org_weight = weight.to(self.org_weight)
273
273
  if bias is not None:
274
+ bias = bias.to(self.org_weight)
274
275
  if self.org_module[0].bias is not None:
275
276
  self.org_module[0].bias.data.copy_(bias)
276
277
  else:
@@ -197,7 +197,7 @@ class ButterflyOFTModule(LycorisBaseModule):
197
197
  r = self.get_r()
198
198
  inp = org = self.org_forward(x)
199
199
  if self.op in {F.conv2d, F.conv1d, F.conv3d}:
200
- org_out = org_out.transpose(1, -1)
200
+ inp = inp.transpose(1, -1)
201
201
 
202
202
  for i in range(m):
203
203
  bi = r[i] # b_num, b_size, b_size
@@ -188,7 +188,7 @@ class DiagOFTModule(LycorisBaseModule):
188
188
  if diff:
189
189
  out = out - org_out
190
190
  out = oft_out.view(*shape, -1)
191
- if self.rescale is not None:
191
+ if self.rescaled:
192
192
  out = self.rescale.transpose(-1, 0) * out
193
193
  out = out + (self.rescale.transpose(-1, 0) - 1) * org_out
194
194
  if self.op in {F.conv2d, F.conv1d, F.conv3d}:
@@ -5,7 +5,7 @@ import torch.nn as nn
5
5
  import torch.nn.functional as F
6
6
 
7
7
  from .base import LycorisBaseModule
8
- from ..utils import tucker_weight_from_conv
8
+ from ..functional import tucker_weight_from_conv
9
9
  from ..utils.bnb import LinearNF4
10
10
 
11
11
 
@@ -417,138 +417,10 @@ def convert_diffusers_name_to_compvis(key):
417
417
  return key
418
418
 
419
419
 
420
- def tucker_weight_from_conv(up, down, mid):
421
- up = up.reshape(up.size(0), up.size(1))
422
- down = down.reshape(down.size(0), down.size(1))
423
- return torch.einsum("m n ..., i m, n j -> i j ...", mid, up, down)
424
-
425
-
426
- def tucker_weight(wa, wb, t):
427
- temp = torch.einsum("i j ..., j r -> i r ...", t, wb)
428
- return torch.einsum("i j ..., i r -> r j ...", temp, wa)
429
-
430
-
431
- def apply_dora_scale(org_weight, rebuild, dora_scale, scale):
432
- dora_norm_dims = org_weight.dim() - 1
433
- weight = org_weight + rebuild
434
- weight = weight.to(dora_scale.dtype)
435
- weight_norm = (
436
- weight.transpose(0, 1)
437
- .reshape(weight.shape[1], -1)
438
- .norm(dim=1, keepdim=True)
439
- .reshape(weight.shape[1], *[1] * dora_norm_dims)
440
- .transpose(0, 1)
441
- )
442
- merged_scale1 = weight / weight_norm * dora_scale
443
- diff_weight = merged_scale1 - org_weight
444
- return org_weight + diff_weight * scale
445
-
446
-
447
- @torch.no_grad()
448
- def rebuild_weight(module_type, params, orig_weight, orig_bias, scale=1):
449
- if orig_weight is None:
450
- return None, None
451
- merged = orig_weight
452
- merged_bias = orig_bias
453
- if module_type == "locon":
454
- up, down, mid, alpha, dora_scale = params
455
- if alpha is not None:
456
- scale *= alpha / up.size(1)
457
- if mid is not None:
458
- rebuild = tucker_weight_from_conv(up, down, mid)
459
- else:
460
- rebuild = up.reshape(up.size(0), -1) @ down.reshape(down.size(0), -1)
461
- rebuild = rebuild.reshape(orig_weight.shape)
462
- if dora_scale is None:
463
- merged = orig_weight + rebuild * scale
464
- else:
465
- merged = apply_dora_scale(orig_weight, rebuild, dora_scale, scale)
466
- del up, down, mid, alpha, params, rebuild
467
- elif module_type == "hada":
468
- w1a, w1b, w2a, w2b, t1, t2, alpha, dora_scale = params
469
- if alpha is not None:
470
- scale *= alpha / w1b.size(0)
471
- if t1 is not None:
472
- rebuild1 = tucker_weight(w1a, w1b, t1)
473
- else:
474
- rebuild1 = w1a @ w1b
475
- if t2 is not None:
476
- rebuild2 = tucker_weight(w2a, w2b, t2)
477
- else:
478
- rebuild2 = w2a @ w2b
479
- rebuild = (rebuild1 * rebuild2).reshape(orig_weight.shape)
480
- if dora_scale is None:
481
- merged = orig_weight + rebuild * scale
482
- else:
483
- merged = apply_dora_scale(orig_weight, rebuild, dora_scale, scale)
484
- del w1a, w1b, w2a, w2b, t1, t2, alpha, params, rebuild, rebuild1, rebuild2
485
- elif module_type == "ia3":
486
- weight, on_input = params
487
- if not on_input:
488
- weight = weight.reshape(-1, 1)
489
- merged = orig_weight + weight * orig_weight * scale
490
- del weight, on_input, params
491
- elif module_type == "kron":
492
- w1, w1a, w1b, w2, w2a, w2b, t1, t2, alpha, dora_scale = params
493
- if alpha is not None and (w1b is not None or w2b is not None):
494
- scale *= alpha / (w1b.size(0) if w1b else w2b.size(0))
495
- if w1a is not None and w1b is not None:
496
- if t1 is not None:
497
- w1 = tucker_weight(w1a, w1b, t1)
498
- else:
499
- w1 = w1a @ w1b
500
- if w2a is not None and w2b is not None:
501
- if t2 is not None:
502
- w2 = tucker_weight(w2a, w2b, t2)
503
- else:
504
- w2 = w2a @ w2b
505
- if len(w2.shape) == 4:
506
- w1 = w1.unsqueeze(2).unsqueeze(2)
507
- w2 = w2.contiguous()
508
- rebuild = torch.kron(w1, w2).reshape(orig_weight.shape)
509
- if dora_scale is None:
510
- merged = orig_weight + rebuild * scale
511
- else:
512
- merged = apply_dora_scale(orig_weight, rebuild, dora_scale, scale)
513
- del w1, w1a, w1b, w2, w2a, w2b, t1, t2, alpha, params, rebuild
514
- elif module_type == "full":
515
- rebuild = params[0].reshape(orig_weight.shape)
516
- rebuild_b = (
517
- params[1].reshape(orig_bias.shape) if orig_bias is not None else None
518
- )
519
- merged = orig_weight + rebuild * scale
520
- if rebuild_b is not None:
521
- merged_bias = orig_bias + rebuild_b * scale
522
- del params, rebuild, rebuild_b
523
- elif module_type == "norm":
524
- rebuild = params[0].reshape(orig_weight.shape)
525
- rebuild_b = params[1].reshape(orig_bias.shape)
526
- merged = orig_weight + rebuild * scale
527
- merged_bias = orig_bias + rebuild_b * scale
528
- else:
529
- return None, None
530
-
531
- return merged, merged_bias
532
-
533
-
534
420
  @torch.no_grad()
535
421
  def merge(tes, unet, lyco_state_dict, scale: float = 1.0, device="cpu"):
536
- UNET_TARGET_REPLACE_MODULE = [
537
- "Linear",
538
- "Conv2d",
539
- "LayerNorm",
540
- "GroupNorm",
541
- "GroupNorm32",
542
- ]
543
- TEXT_ENCODER_TARGET_REPLACE_MODULE = [
544
- "Embedding",
545
- "Linear",
546
- "Conv2d",
547
- "LayerNorm",
548
- "GroupNorm",
549
- "GroupNorm32",
550
- "Embedding",
551
- ]
422
+ from ..modules import make_module, get_module
423
+
552
424
  LORA_PREFIX_UNET = "lora_unet"
553
425
  LORA_PREFIX_TEXT_ENCODER = "lora_te"
554
426
  merged = 0
@@ -557,29 +429,25 @@ def merge(tes, unet, lyco_state_dict, scale: float = 1.0, device="cpu"):
557
429
  prefix,
558
430
  root_module: torch.nn.Module,
559
431
  lyco_state_dict: Dict[str, torch.Tensor],
560
- target_replace_modules,
561
432
  ):
562
433
  nonlocal merged
563
434
  for child_name, child_module in tqdm(
564
435
  list(root_module.named_modules()), desc=f"Merging {prefix}"
565
436
  ):
566
- if child_module.__class__.__name__ in target_replace_modules:
567
- lora_name = prefix + "." + child_name
568
- lora_name = lora_name.replace(".", "_")
569
-
570
- result, result_b = rebuild_weight(
571
- *get_module(lyco_state_dict, lora_name),
572
- getattr(child_module, "weight"),
573
- getattr(child_module, "bias", None),
574
- scale,
575
- )
576
- if result is not None:
577
- key_dict.pop(lora_name)
578
- merged += 1
579
- child_module.requires_grad_(False)
580
- child_module.weight.copy_(result)
581
- if result_b is not None:
582
- child_module.bias.copy_(result_b)
437
+ lora_name = prefix + "." + child_name
438
+ lora_name = lora_name.replace(".", "_")
439
+
440
+ lyco_type, params = get_module(lyco_state_dict, lora_name)
441
+ if lyco_type is None:
442
+ continue
443
+ module = make_module(lyco_type, params, lora_name, child_module)
444
+ if module is None:
445
+ continue
446
+ module.to(device)
447
+ module.merge_to(scale)
448
+ key_dict.pop(convert_diffusers_name_to_compvis(lora_name), None)
449
+ key_dict.pop(lora_name, None)
450
+ merged += 1
583
451
 
584
452
  key_dict = {}
585
453
  for k, v in tqdm(list(lyco_state_dict.items()), desc="Converting Dtype and Device"):
@@ -592,12 +460,7 @@ def merge(tes, unet, lyco_state_dict, scale: float = 1.0, device="cpu"):
592
460
  k = f"{convert_key}.{weight_key}"
593
461
  else:
594
462
  key_dict[module] = key_dict.get(module, []) + [k]
595
- if device == "cpu":
596
- lyco_state_dict[k] = v.float().cpu()
597
- else:
598
- lyco_state_dict[k] = v.to(
599
- device, dtype=tes[0].parameters().__next__().dtype
600
- )
463
+ lyco_state_dict[k] = v.float().cpu()
601
464
 
602
465
  for idx, te in enumerate(tes):
603
466
  if len(tes) > 1:
@@ -606,15 +469,15 @@ def merge(tes, unet, lyco_state_dict, scale: float = 1.0, device="cpu"):
606
469
  prefix = LORA_PREFIX_TEXT_ENCODER
607
470
  merge_state_dict(
608
471
  prefix,
609
- te.to(device),
472
+ te,
610
473
  lyco_state_dict,
611
- TEXT_ENCODER_TARGET_REPLACE_MODULE,
612
474
  )
475
+ torch.cuda.empty_cache()
613
476
  merge_state_dict(
614
477
  LORA_PREFIX_UNET,
615
- unet.to(device),
478
+ unet,
616
479
  lyco_state_dict,
617
- UNET_TARGET_REPLACE_MODULE,
618
480
  )
481
+ torch.cuda.empty_cache()
619
482
  print(f"Unused state dict key: {key_dict}")
620
483
  print(f"{merged} Modules been merged")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: lycoris_lora
3
- Version: 3.0.0.dev14
3
+ Version: 3.0.0.dev15
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.0.dev14",
7
+ version="3.0.0.dev15",
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",
@@ -1 +0,0 @@
1
- from .general import rebuild_tucker, factorization, power2factorization, FUNC_LIST