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
321680cc
Commit
321680cc
authored
Oct 19, 2023
by
v0xie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: fix constraint, re-use get_weight
parent
eb01d7f0
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
24 deletions
+16
-24
extensions-builtin/Lora/network_oft.py
extensions-builtin/Lora/network_oft.py
+16
-24
No files found.
extensions-builtin/Lora/network_oft.py
View file @
321680cc
...
@@ -9,7 +9,7 @@ class ModuleTypeOFT(network.ModuleType):
...
@@ -9,7 +9,7 @@ class ModuleTypeOFT(network.ModuleType):
return
None
return
None
# adapted from https://github.com/kohya-ss/sd-scripts/blob/main/networks/oft.py
# adapted from
kohya's implementation
https://github.com/kohya-ss/sd-scripts/blob/main/networks/oft.py
class
NetworkModuleOFT
(
network
.
NetworkModule
):
class
NetworkModuleOFT
(
network
.
NetworkModule
):
def
__init__
(
self
,
net
:
network
.
Network
,
weights
:
network
.
NetworkWeights
):
def
__init__
(
self
,
net
:
network
.
Network
,
weights
:
network
.
NetworkWeights
):
...
@@ -17,7 +17,6 @@ class NetworkModuleOFT(network.NetworkModule):
...
@@ -17,7 +17,6 @@ class NetworkModuleOFT(network.NetworkModule):
self
.
oft_blocks
=
weights
.
w
[
"oft_blocks"
]
self
.
oft_blocks
=
weights
.
w
[
"oft_blocks"
]
self
.
alpha
=
weights
.
w
[
"alpha"
]
self
.
alpha
=
weights
.
w
[
"alpha"
]
self
.
dim
=
self
.
oft_blocks
.
shape
[
0
]
self
.
dim
=
self
.
oft_blocks
.
shape
[
0
]
self
.
num_blocks
=
self
.
dim
self
.
num_blocks
=
self
.
dim
...
@@ -26,64 +25,57 @@ class NetworkModuleOFT(network.NetworkModule):
...
@@ -26,64 +25,57 @@ class NetworkModuleOFT(network.NetworkModule):
elif
"Conv"
in
self
.
sd_module
.
__class__
.
__name__
:
elif
"Conv"
in
self
.
sd_module
.
__class__
.
__name__
:
self
.
out_dim
=
self
.
sd_module
.
out_channels
self
.
out_dim
=
self
.
sd_module
.
out_channels
self
.
constraint
=
self
.
alpha
self
.
constraint
=
self
.
alpha
*
self
.
out_dim
#self.constraint = self.alpha * self.out_dim
self
.
block_size
=
self
.
out_dim
//
self
.
num_blocks
self
.
block_size
=
self
.
out_dim
//
self
.
num_blocks
self
.
org_module
:
list
[
torch
.
Module
]
=
[
self
.
sd_module
]
self
.
org_module
:
list
[
torch
.
Module
]
=
[
self
.
sd_module
]
self
.
R
=
self
.
get_weight
(
self
.
oft_blocks
)
self
.
R
=
self
.
get_weight
()
self
.
apply_to
()
self
.
apply_to
()
# replace forward method of original linear rather than replacing the module
# replace forward method of original linear rather than replacing the module
# how do we revert this to unload the weights?
def
apply_to
(
self
):
def
apply_to
(
self
):
self
.
org_forward
=
self
.
org_module
[
0
]
.
forward
self
.
org_forward
=
self
.
org_module
[
0
]
.
forward
self
.
org_module
[
0
]
.
forward
=
self
.
forward
self
.
org_module
[
0
]
.
forward
=
self
.
forward
def
get_weight
(
self
,
multiplier
=
None
):
def
get_weight
(
self
,
oft_blocks
,
multiplier
=
None
):
if
not
multiplier
:
block_Q
=
oft_blocks
-
oft_blocks
.
transpose
(
1
,
2
)
multiplier
=
self
.
multiplier
()
block_Q
=
self
.
oft_blocks
-
self
.
oft_blocks
.
transpose
(
1
,
2
)
norm_Q
=
torch
.
norm
(
block_Q
.
flatten
())
norm_Q
=
torch
.
norm
(
block_Q
.
flatten
())
new_norm_Q
=
torch
.
clamp
(
norm_Q
,
max
=
self
.
constraint
)
new_norm_Q
=
torch
.
clamp
(
norm_Q
,
max
=
self
.
constraint
)
block_Q
=
block_Q
*
((
new_norm_Q
+
1e-8
)
/
(
norm_Q
+
1e-8
))
block_Q
=
block_Q
*
((
new_norm_Q
+
1e-8
)
/
(
norm_Q
+
1e-8
))
I
=
torch
.
eye
(
self
.
block_size
,
device
=
self
.
oft_blocks
.
device
)
.
unsqueeze
(
0
)
.
repeat
(
self
.
num_blocks
,
1
,
1
)
I
=
torch
.
eye
(
self
.
block_size
,
device
=
self
.
oft_blocks
.
device
)
.
unsqueeze
(
0
)
.
repeat
(
self
.
num_blocks
,
1
,
1
)
block_R
=
torch
.
matmul
(
I
+
block_Q
,
(
I
-
block_Q
)
.
inverse
())
block_R
=
torch
.
matmul
(
I
+
block_Q
,
(
I
-
block_Q
)
.
inverse
())
#block_R_weighted = multiplier * block_R + (1 - multiplier) * I
block_R_weighted
=
multiplier
*
block_R
+
(
1
-
multiplier
)
*
I
#R = torch.block_diag(*block_R_weighted)
R
=
torch
.
block_diag
(
*
block_R
_weighted
)
R
=
torch
.
block_diag
(
*
block_R
)
return
R
return
R
def
calc_updown
(
self
,
orig_weight
):
def
calc_updown
(
self
,
orig_weight
):
# this works
oft_blocks
=
self
.
oft_blocks
.
to
(
orig_weight
.
device
,
dtype
=
orig_weight
.
dtype
)
# R = self.R
self
.
R
=
self
.
get_weight
(
self
.
multiplier
())
# sending R to device causes major deepfrying i.e. just doesn't work
R
=
self
.
get_weight
(
oft_blocks
)
# R = self.R.to(orig_weight.device, dtype=orig_weight.dtype)
self
.
R
=
R
# if orig_weight.dim() == 4:
# if orig_weight.dim() == 4:
# weight = torch.einsum("oihw, op -> pihw", orig_weight, R)
# weight = torch.einsum("oihw, op -> pihw", orig_weight, R)
# else:
# else:
# weight = torch.einsum("oi, op -> pi", orig_weight, R)
# weight = torch.einsum("oi, op -> pi", orig_weight, R)
updown
=
orig_weight
@
self
.
R
updown
=
orig_weight
@
R
output_shape
=
self
.
oft_blocks
.
shape
output_shape
=
self
.
oft_blocks
.
shape
## this works
# updown = orig_weight @ R
# output_shape = [orig_weight.size(0), R.size(1)]
return
self
.
finalize_updown
(
updown
,
orig_weight
,
output_shape
)
return
self
.
finalize_updown
(
updown
,
orig_weight
,
output_shape
)
def
forward
(
self
,
x
,
y
=
None
):
def
forward
(
self
,
x
,
y
=
None
):
x
=
self
.
org_forward
(
x
)
x
=
self
.
org_forward
(
x
)
if
self
.
multiplier
()
==
0.0
:
if
self
.
multiplier
()
==
0.0
:
return
x
return
x
# calculating R here is excruciatingly slow
#R = self.get_weight().to(x.device, dtype=x.dtype)
#R = self.get_weight().to(x.device, dtype=x.dtype)
R
=
self
.
R
.
to
(
x
.
device
,
dtype
=
x
.
dtype
)
R
=
self
.
R
.
to
(
x
.
device
,
dtype
=
x
.
dtype
)
if
x
.
dim
()
==
4
:
if
x
.
dim
()
==
4
:
x
=
x
.
permute
(
0
,
2
,
3
,
1
)
x
=
x
.
permute
(
0
,
2
,
3
,
1
)
x
=
torch
.
matmul
(
x
,
R
)
x
=
torch
.
matmul
(
x
,
R
)
...
...
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