diff options
author | hako-mikan <122196982+hako-mikan@users.noreply.github.com> | 2024-02-09 14:17:40 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-09 14:17:40 +0000 |
commit | 0bc7867ccd4ac24f5f270cb767c4642d0a0c001c (patch) | |
tree | 2ad13a0cf77bc189a8c9097bd507f9674f993da6 /extensions-builtin/Lora/lyco_helpers.py | |
parent | 816096e642187a18b11e2729c42c0b5f677f047d (diff) | |
parent | cf2772fab0af5573da775e7437e6acdca424f26e (diff) | |
download | stable-diffusion-webui-gfx803-0bc7867ccd4ac24f5f270cb767c4642d0a0c001c.tar.gz stable-diffusion-webui-gfx803-0bc7867ccd4ac24f5f270cb767c4642d0a0c001c.tar.bz2 stable-diffusion-webui-gfx803-0bc7867ccd4ac24f5f270cb767c4642d0a0c001c.zip |
Merge branch 'AUTOMATIC1111:master' into master
Diffstat (limited to 'extensions-builtin/Lora/lyco_helpers.py')
-rw-r--r-- | extensions-builtin/Lora/lyco_helpers.py | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/extensions-builtin/Lora/lyco_helpers.py b/extensions-builtin/Lora/lyco_helpers.py index 279b34bc..1679a0ce 100644 --- a/extensions-builtin/Lora/lyco_helpers.py +++ b/extensions-builtin/Lora/lyco_helpers.py @@ -19,3 +19,50 @@ def rebuild_cp_decomposition(up, down, mid): up = up.reshape(up.size(0), -1)
down = down.reshape(down.size(0), -1)
return torch.einsum('n m k l, i n, m j -> i j k l', mid, up, down)
+
+
+# copied from https://github.com/KohakuBlueleaf/LyCORIS/blob/dev/lycoris/modules/lokr.py
+def factorization(dimension: int, factor:int=-1) -> tuple[int, int]:
+ '''
+ return a tuple of two value of input dimension decomposed by the number closest to factor
+ second value is higher or equal than first value.
+
+ In LoRA with Kroneckor Product, first value is a value for weight scale.
+ secon value is a value for weight.
+
+ Becuase of non-commutative property, A⊗B ≠ B⊗A. Meaning of two matrices is slightly different.
+
+ examples)
+ factor
+ -1 2 4 8 16 ...
+ 127 -> 1, 127 127 -> 1, 127 127 -> 1, 127 127 -> 1, 127 127 -> 1, 127
+ 128 -> 8, 16 128 -> 2, 64 128 -> 4, 32 128 -> 8, 16 128 -> 8, 16
+ 250 -> 10, 25 250 -> 2, 125 250 -> 2, 125 250 -> 5, 50 250 -> 10, 25
+ 360 -> 8, 45 360 -> 2, 180 360 -> 4, 90 360 -> 8, 45 360 -> 12, 30
+ 512 -> 16, 32 512 -> 2, 256 512 -> 4, 128 512 -> 8, 64 512 -> 16, 32
+ 1024 -> 32, 32 1024 -> 2, 512 1024 -> 4, 256 1024 -> 8, 128 1024 -> 16, 64
+ '''
+
+ if factor > 0 and (dimension % factor) == 0:
+ m = factor
+ n = dimension // factor
+ if m > n:
+ n, m = m, n
+ return m, n
+ if factor < 0:
+ factor = dimension
+ m, n = 1, dimension
+ length = m + n
+ while m<n:
+ new_m = m + 1
+ while dimension%new_m != 0:
+ new_m += 1
+ new_n = dimension // new_m
+ if new_m + new_n > length or new_m>factor:
+ break
+ else:
+ m, n = new_m, new_n
+ if m > n:
+ n, m = m, n
+ return m, n
+
|