Skip to content

Commit

Permalink
fix: add butterfly_factor fn
Browse files Browse the repository at this point in the history
  • Loading branch information
v0xie committed Feb 7, 2024
1 parent a4668a1 commit 81c16c9
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions extensions-builtin/Lora/lyco_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,29 @@ def factorization(dimension: int, factor:int=-1) -> tuple[int, int]:
n, m = m, n
return m, n

# from https://github.com/KohakuBlueleaf/LyCORIS/blob/dev/lycoris/modules/boft.py
def butterfly_factor(dimension: int, factor: int = -1) -> tuple[int, int]:
"""
m = 2k
n = 2**p
m*n = dim
"""

# Find the first solution and check if it is even doable
m = n = 0
while m <= factor:
m += 2
while dimension % m != 0 and m < dimension:
m += 2
if m > factor:
break
if sum(int(i) for i in f"{dimension//m:b}") == 1:
n = dimension // m

if n == 0:
raise ValueError(
f"It is impossible to decompose {dimension} with factor {factor} under BOFT constrains."
)

#log_butterfly_factorize(dimension, factor, (dimension // n, n))
return dimension // n, n

0 comments on commit 81c16c9

Please sign in to comment.