Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
S
Stable Diffusion Webui
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
novelai-storage
Stable Diffusion Webui
Commits
900419e8
Commit
900419e8
authored
Feb 26, 2024
by
AUTOMATIC1111
Committed by
GitHub
Feb 26, 2024
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #14973 from AUTOMATIC1111/Fix-new-oft-boft
Fix the OFT/BOFT bugs when using new LyCORIS implementation
parents
18819723
c4afdb78
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
26 deletions
+24
-26
extensions-builtin/Lora/network_oft.py
extensions-builtin/Lora/network_oft.py
+24
-26
No files found.
extensions-builtin/Lora/network_oft.py
View file @
900419e8
import
torch
import
torch
import
network
import
network
from
lyco_helpers
import
factorization
from
einops
import
rearrange
from
einops
import
rearrange
...
@@ -22,24 +21,24 @@ class NetworkModuleOFT(network.NetworkModule):
...
@@ -22,24 +21,24 @@ class NetworkModuleOFT(network.NetworkModule):
self
.
org_module
:
list
[
torch
.
Module
]
=
[
self
.
sd_module
]
self
.
org_module
:
list
[
torch
.
Module
]
=
[
self
.
sd_module
]
self
.
scale
=
1.0
self
.
scale
=
1.0
self
.
is_
kohya
=
False
self
.
is_
R
=
False
self
.
is_boft
=
False
self
.
is_boft
=
False
# kohya-ss
# kohya-ss
/New LyCORIS OFT/BOFT
if
"oft_blocks"
in
weights
.
w
.
keys
():
if
"oft_blocks"
in
weights
.
w
.
keys
():
self
.
is_kohya
=
True
self
.
oft_blocks
=
weights
.
w
[
"oft_blocks"
]
# (num_blocks, block_size, block_size)
self
.
oft_blocks
=
weights
.
w
[
"oft_blocks"
]
# (num_blocks, block_size, block_size)
self
.
alpha
=
weights
.
w
[
"alpha"
]
# alpha is constraint
self
.
alpha
=
weights
.
w
.
get
(
"alpha"
,
None
)
# alpha is constraint
self
.
dim
=
self
.
oft_blocks
.
shape
[
0
]
# lora dim
self
.
dim
=
self
.
oft_blocks
.
shape
[
0
]
# lora dim
# LyCORIS OFT
#
Old
LyCORIS OFT
elif
"oft_diag"
in
weights
.
w
.
keys
():
elif
"oft_diag"
in
weights
.
w
.
keys
():
self
.
is_R
=
True
self
.
oft_blocks
=
weights
.
w
[
"oft_diag"
]
self
.
oft_blocks
=
weights
.
w
[
"oft_diag"
]
# self.alpha is unused
# self.alpha is unused
self
.
dim
=
self
.
oft_blocks
.
shape
[
1
]
# (num_blocks, block_size, block_size)
self
.
dim
=
self
.
oft_blocks
.
shape
[
1
]
# (num_blocks, block_size, block_size)
# LyCORIS BOFT
# LyCORIS BOFT
if
weights
.
w
[
"oft_diag"
]
.
dim
()
==
4
:
if
self
.
oft_blocks
.
dim
()
==
4
:
self
.
is_boft
=
True
self
.
is_boft
=
True
self
.
rescale
=
weights
.
w
.
get
(
'rescale'
,
None
)
self
.
rescale
=
weights
.
w
.
get
(
'rescale'
,
None
)
if
self
.
rescale
is
not
None
:
if
self
.
rescale
is
not
None
:
self
.
rescale
=
self
.
rescale
.
reshape
(
-
1
,
*
[
1
]
*
(
self
.
org_module
[
0
]
.
weight
.
dim
()
-
1
))
self
.
rescale
=
self
.
rescale
.
reshape
(
-
1
,
*
[
1
]
*
(
self
.
org_module
[
0
]
.
weight
.
dim
()
-
1
))
...
@@ -55,30 +54,29 @@ class NetworkModuleOFT(network.NetworkModule):
...
@@ -55,30 +54,29 @@ class NetworkModuleOFT(network.NetworkModule):
elif
is_other_linear
:
elif
is_other_linear
:
self
.
out_dim
=
self
.
sd_module
.
embed_dim
self
.
out_dim
=
self
.
sd_module
.
embed_dim
if
self
.
is_kohya
:
self
.
num_blocks
=
self
.
dim
self
.
constraint
=
self
.
alpha
*
self
.
out_dim
self
.
block_size
=
self
.
out_dim
//
self
.
dim
self
.
num_blocks
=
self
.
dim
self
.
constraint
=
(
0
if
self
.
alpha
is
None
else
self
.
alpha
)
*
self
.
out_dim
self
.
block_size
=
self
.
out_dim
//
self
.
dim
if
self
.
is_R
:
elif
self
.
is_boft
:
self
.
constraint
=
None
self
.
constraint
=
None
self
.
boft_m
=
weights
.
w
[
"oft_diag"
]
.
shape
[
0
]
self
.
block_size
=
self
.
dim
self
.
block_num
=
weights
.
w
[
"oft_diag"
]
.
shape
[
1
]
self
.
num_blocks
=
self
.
out_dim
//
self
.
dim
self
.
block_size
=
weights
.
w
[
"oft_diag"
]
.
shape
[
2
]
elif
self
.
is_boft
:
self
.
boft_m
=
self
.
oft_blocks
.
shape
[
0
]
self
.
num_blocks
=
self
.
oft_blocks
.
shape
[
1
]
self
.
block_size
=
self
.
oft_blocks
.
shape
[
2
]
self
.
boft_b
=
self
.
block_size
self
.
boft_b
=
self
.
block_size
#self.block_size, self.block_num = butterfly_factor(self.out_dim, self.dim)
else
:
self
.
constraint
=
None
self
.
block_size
,
self
.
num_blocks
=
factorization
(
self
.
out_dim
,
self
.
dim
)
def
calc_updown
(
self
,
orig_weight
):
def
calc_updown
(
self
,
orig_weight
):
oft_blocks
=
self
.
oft_blocks
.
to
(
orig_weight
.
device
)
oft_blocks
=
self
.
oft_blocks
.
to
(
orig_weight
.
device
)
eye
=
torch
.
eye
(
self
.
block_size
,
device
=
oft_blocks
.
device
)
eye
=
torch
.
eye
(
self
.
block_size
,
device
=
oft_blocks
.
device
)
if
self
.
is_kohya
:
if
not
self
.
is_R
:
block_Q
=
oft_blocks
-
oft_blocks
.
transpose
(
1
,
2
)
# ensure skew-symmetric orthogonal matrix
block_Q
=
oft_blocks
-
oft_blocks
.
transpose
(
-
1
,
-
2
)
# ensure skew-symmetric orthogonal matrix
norm_Q
=
torch
.
norm
(
block_Q
.
flatten
())
if
self
.
constraint
!=
0
:
new_norm_Q
=
torch
.
clamp
(
norm_Q
,
max
=
self
.
constraint
.
to
(
oft_blocks
.
device
))
norm_Q
=
torch
.
norm
(
block_Q
.
flatten
())
block_Q
=
block_Q
*
((
new_norm_Q
+
1e-8
)
/
(
norm_Q
+
1e-8
))
new_norm_Q
=
torch
.
clamp
(
norm_Q
,
max
=
self
.
constraint
.
to
(
oft_blocks
.
device
))
block_Q
=
block_Q
*
((
new_norm_Q
+
1e-8
)
/
(
norm_Q
+
1e-8
))
oft_blocks
=
torch
.
matmul
(
eye
+
block_Q
,
(
eye
-
block_Q
)
.
float
()
.
inverse
())
oft_blocks
=
torch
.
matmul
(
eye
+
block_Q
,
(
eye
-
block_Q
)
.
float
()
.
inverse
())
R
=
oft_blocks
.
to
(
orig_weight
.
device
)
R
=
oft_blocks
.
to
(
orig_weight
.
device
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment