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
6e4fc5e1
Commit
6e4fc5e1
authored
Feb 19, 2024
by
AUTOMATIC1111
Committed by
GitHub
Feb 19, 2024
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #14871 from v0xie/boft
Support inference with LyCORIS BOFT networks
parents
9d5becb4
4eb94962
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
48 additions
and
10 deletions
+48
-10
extensions-builtin/Lora/network_oft.py
extensions-builtin/Lora/network_oft.py
+48
-10
No files found.
extensions-builtin/Lora/network_oft.py
View file @
6e4fc5e1
...
@@ -22,6 +22,8 @@ class NetworkModuleOFT(network.NetworkModule):
...
@@ -22,6 +22,8 @@ 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_boft
=
False
# kohya-ss
# kohya-ss
if
"oft_blocks"
in
weights
.
w
.
keys
():
if
"oft_blocks"
in
weights
.
w
.
keys
():
...
@@ -29,13 +31,19 @@ class NetworkModuleOFT(network.NetworkModule):
...
@@ -29,13 +31,19 @@ class NetworkModuleOFT(network.NetworkModule):
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
[
"alpha"
]
# alpha is constraint
self
.
dim
=
self
.
oft_blocks
.
shape
[
0
]
# lora dim
self
.
dim
=
self
.
oft_blocks
.
shape
[
0
]
# lora dim
# LyCORIS
# LyCORIS
OFT
elif
"oft_diag"
in
weights
.
w
.
keys
():
elif
"oft_diag"
in
weights
.
w
.
keys
():
self
.
is_kohya
=
False
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
if
weights
.
w
[
"oft_diag"
]
.
dim
()
==
4
:
self
.
is_boft
=
True
self
.
rescale
=
weights
.
w
.
get
(
'rescale'
,
None
)
if
self
.
rescale
is
not
None
:
self
.
rescale
=
self
.
rescale
.
reshape
(
-
1
,
*
[
1
]
*
(
self
.
org_module
[
0
]
.
weight
.
dim
()
-
1
))
is_linear
=
type
(
self
.
sd_module
)
in
[
torch
.
nn
.
Linear
,
torch
.
nn
.
modules
.
linear
.
NonDynamicallyQuantizableLinear
]
is_linear
=
type
(
self
.
sd_module
)
in
[
torch
.
nn
.
Linear
,
torch
.
nn
.
modules
.
linear
.
NonDynamicallyQuantizableLinear
]
is_conv
=
type
(
self
.
sd_module
)
in
[
torch
.
nn
.
Conv2d
]
is_conv
=
type
(
self
.
sd_module
)
in
[
torch
.
nn
.
Conv2d
]
is_other_linear
=
type
(
self
.
sd_module
)
in
[
torch
.
nn
.
MultiheadAttention
]
# unsupported
is_other_linear
=
type
(
self
.
sd_module
)
in
[
torch
.
nn
.
MultiheadAttention
]
# unsupported
...
@@ -51,6 +59,13 @@ class NetworkModuleOFT(network.NetworkModule):
...
@@ -51,6 +59,13 @@ class NetworkModuleOFT(network.NetworkModule):
self
.
constraint
=
self
.
alpha
*
self
.
out_dim
self
.
constraint
=
self
.
alpha
*
self
.
out_dim
self
.
num_blocks
=
self
.
dim
self
.
num_blocks
=
self
.
dim
self
.
block_size
=
self
.
out_dim
//
self
.
dim
self
.
block_size
=
self
.
out_dim
//
self
.
dim
elif
self
.
is_boft
:
self
.
constraint
=
None
self
.
boft_m
=
weights
.
w
[
"oft_diag"
]
.
shape
[
0
]
self
.
block_num
=
weights
.
w
[
"oft_diag"
]
.
shape
[
1
]
self
.
block_size
=
weights
.
w
[
"oft_diag"
]
.
shape
[
2
]
self
.
boft_b
=
self
.
block_size
#self.block_size, self.block_num = butterfly_factor(self.out_dim, self.dim)
else
:
else
:
self
.
constraint
=
None
self
.
constraint
=
None
self
.
block_size
,
self
.
num_blocks
=
factorization
(
self
.
out_dim
,
self
.
dim
)
self
.
block_size
,
self
.
num_blocks
=
factorization
(
self
.
out_dim
,
self
.
dim
)
...
@@ -68,14 +83,37 @@ class NetworkModuleOFT(network.NetworkModule):
...
@@ -68,14 +83,37 @@ class NetworkModuleOFT(network.NetworkModule):
R
=
oft_blocks
.
to
(
orig_weight
.
device
)
R
=
oft_blocks
.
to
(
orig_weight
.
device
)
# This errors out for MultiheadAttention, might need to be handled up-stream
if
not
self
.
is_boft
:
merged_weight
=
rearrange
(
orig_weight
,
'(k n) ... -> k n ...'
,
k
=
self
.
num_blocks
,
n
=
self
.
block_size
)
# This errors out for MultiheadAttention, might need to be handled up-stream
merged_weight
=
torch
.
einsum
(
merged_weight
=
rearrange
(
orig_weight
,
'(k n) ... -> k n ...'
,
k
=
self
.
num_blocks
,
n
=
self
.
block_size
)
'k n m, k n ... -> k m ...'
,
merged_weight
=
torch
.
einsum
(
R
,
'k n m, k n ... -> k m ...'
,
merged_weight
R
,
)
merged_weight
merged_weight
=
rearrange
(
merged_weight
,
'k m ... -> (k m) ...'
)
)
merged_weight
=
rearrange
(
merged_weight
,
'k m ... -> (k m) ...'
)
else
:
# TODO: determine correct value for scale
scale
=
1.0
m
=
self
.
boft_m
b
=
self
.
boft_b
r_b
=
b
//
2
inp
=
orig_weight
for
i
in
range
(
m
):
bi
=
R
[
i
]
# b_num, b_size, b_size
if
i
==
0
:
# Apply multiplier/scale and rescale into first weight
bi
=
bi
*
scale
+
(
1
-
scale
)
*
eye
inp
=
rearrange
(
inp
,
"(c g k) ... -> (c k g) ..."
,
g
=
2
,
k
=
2
**
i
*
r_b
)
inp
=
rearrange
(
inp
,
"(d b) ... -> d b ..."
,
b
=
b
)
inp
=
torch
.
einsum
(
"b i j, b j ... -> b i ..."
,
bi
,
inp
)
inp
=
rearrange
(
inp
,
"d b ... -> (d b) ..."
)
inp
=
rearrange
(
inp
,
"(c k g) ... -> (c g k) ..."
,
g
=
2
,
k
=
2
**
i
*
r_b
)
merged_weight
=
inp
# Rescale mechanism
if
self
.
rescale
is
not
None
:
merged_weight
=
self
.
rescale
.
to
(
merged_weight
)
*
merged_weight
updown
=
merged_weight
.
to
(
orig_weight
.
device
)
-
orig_weight
.
to
(
merged_weight
.
dtype
)
updown
=
merged_weight
.
to
(
orig_weight
.
device
)
-
orig_weight
.
to
(
merged_weight
.
dtype
)
output_shape
=
orig_weight
.
shape
output_shape
=
orig_weight
.
shape
...
...
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