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
76835477
Commit
76835477
authored
Oct 21, 2023
by
v0xie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: return orig weights during updown, merge weights before forward
parent
2d8c894b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
69 additions
and
21 deletions
+69
-21
extensions-builtin/Lora/network_oft.py
extensions-builtin/Lora/network_oft.py
+69
-21
No files found.
extensions-builtin/Lora/network_oft.py
View file @
76835477
import
torch
import
torch
import
network
import
network
from
modules
import
devices
class
ModuleTypeOFT
(
network
.
ModuleType
):
class
ModuleTypeOFT
(
network
.
ModuleType
):
...
@@ -29,23 +30,56 @@ class NetworkModuleOFT(network.NetworkModule):
...
@@ -29,23 +30,56 @@ class NetworkModuleOFT(network.NetworkModule):
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
.
org_weight
=
self
.
org_module
[
0
]
.
weight
.
to
(
self
.
org_module
[
0
]
.
weight
.
device
,
copy
=
True
)
#self.org_weight = self.org_module[0].weight.to(devices.cpu, copy=True)
self
.
R
=
self
.
get_weight
(
self
.
oft_blocks
)
self
.
R
=
self
.
get_weight
(
self
.
oft_blocks
)
self
.
merged_weight
=
self
.
merge_weight
()
self
.
apply_to
()
self
.
apply_to
()
self
.
merged
=
False
def
merge_weight
(
self
):
org_sd
=
self
.
org_module
[
0
]
.
state_dict
()
R
=
self
.
R
.
to
(
self
.
org_weight
.
device
,
dtype
=
self
.
org_weight
.
dtype
)
if
self
.
org_weight
.
dim
()
==
4
:
weight
=
torch
.
einsum
(
"oihw, op -> pihw"
,
self
.
org_weight
,
R
)
else
:
weight
=
torch
.
einsum
(
"oi, op -> pi"
,
self
.
org_weight
,
R
)
org_sd
[
'weight'
]
=
weight
# replace weight
#self.org_module[0].load_state_dict(org_sd)
return
weight
pass
def
replace_weight
(
self
,
new_weight
):
org_sd
=
self
.
org_module
[
0
]
.
state_dict
()
org_sd
[
'weight'
]
=
new_weight
self
.
org_module
[
0
]
.
load_state_dict
(
org_sd
)
self
.
merged
=
True
def
restore_weight
(
self
):
org_sd
=
self
.
org_module
[
0
]
.
state_dict
()
org_sd
[
'weight'
]
=
self
.
org_weight
self
.
org_module
[
0
]
.
load_state_dict
(
org_sd
)
self
.
merged
=
False
# 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?
# 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
self
.
org_module
[
0
]
.
register_forward_pre_hook
(
self
.
pre_forward_hook
)
self
.
org_module
[
0
]
.
register_forward_hook
(
self
.
forward_hook
)
self
.
org_module
[
0
]
.
register_forward_hook
(
self
.
forward_hook
)
def
get_weight
(
self
,
oft_blocks
,
multiplier
=
None
):
def
get_weight
(
self
,
oft_blocks
,
multiplier
=
None
):
self
.
constraint
=
self
.
constraint
.
to
(
oft_blocks
.
device
,
dtype
=
oft_blocks
.
dtype
)
constraint
=
self
.
constraint
.
to
(
oft_blocks
.
device
,
dtype
=
oft_blocks
.
dtype
)
block_Q
=
oft_blocks
-
oft_blocks
.
transpose
(
1
,
2
)
block_Q
=
oft_blocks
-
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
=
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
))
m_I
=
torch
.
eye
(
self
.
block_size
,
device
=
self
.
oft_blocks
.
device
)
.
unsqueeze
(
0
)
.
repeat
(
self
.
num_blocks
,
1
,
1
)
m_I
=
torch
.
eye
(
self
.
block_size
,
device
=
oft_blocks
.
device
)
.
unsqueeze
(
0
)
.
repeat
(
self
.
num_blocks
,
1
,
1
)
block_R
=
torch
.
matmul
(
m_I
+
block_Q
,
(
m_I
-
block_Q
)
.
inverse
())
block_R
=
torch
.
matmul
(
m_I
+
block_Q
,
(
m_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)
...
@@ -54,33 +88,47 @@ class NetworkModuleOFT(network.NetworkModule):
...
@@ -54,33 +88,47 @@ class NetworkModuleOFT(network.NetworkModule):
return
R
return
R
def
calc_updown
(
self
,
orig_weight
):
def
calc_updown
(
self
,
orig_weight
):
oft_blocks
=
self
.
oft_blocks
.
to
(
orig_weight
.
device
,
dtype
=
orig_weight
.
dtype
)
#
oft_blocks = self.oft_blocks.to(orig_weight.device, dtype=orig_weight.dtype)
R
=
self
.
get_weight
(
oft_blocks
)
#R = self.R.to(orig_weight.device, dtype=orig_weight.dtype
)
self
.
R
=
R
##
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
@
R
#updown = orig_weight @ R
output_shape
=
self
.
oft_blocks
.
shape
#updown = weight
updown
=
torch
.
zeros_like
(
orig_weight
,
device
=
orig_weight
.
device
,
dtype
=
orig_weight
.
dtype
)
#updown = orig_weight
output_shape
=
orig_weight
.
shape
#orig_weight = self.merged_weight.to(orig_weight.device, dtype=orig_weight.dtype)
#output_shape = self.oft_blocks.shape
return
self
.
finalize_updown
(
updown
,
orig_weight
,
output_shape
)
return
self
.
finalize_updown
(
updown
,
orig_weight
,
output_shape
)
def
pre_forward_hook
(
self
,
module
,
input
):
if
not
self
.
merged
:
self
.
replace_weight
(
self
.
merged_weight
)
def
forward_hook
(
self
,
module
,
args
,
output
):
def
forward_hook
(
self
,
module
,
args
,
output
):
if
self
.
merged
:
pass
#self.restore_weight()
#print(f'Forward hook in {self.network_key} called')
#print(f'Forward hook in {self.network_key} called')
x
=
output
R
=
self
.
R
.
to
(
x
.
device
,
dtype
=
x
.
dtype
)
if
x
.
dim
()
==
4
:
#x = output
x
=
x
.
permute
(
0
,
2
,
3
,
1
)
#R = self.R.to(x.device, dtype=x.dtype)
x
=
torch
.
matmul
(
x
,
R
)
x
=
x
.
permute
(
0
,
3
,
1
,
2
)
#if x.dim() == 4:
else
:
# x = x.permute(0, 2, 3, 1)
x
=
torch
.
matmul
(
x
,
R
)
# x = torch.matmul(x, R)
return
x
# x = x.permute(0, 3, 1, 2)
#else:
# x = torch.matmul(x, R)
#return x
# def forward(self, x, y=None):
# def forward(self, x, y=None):
# x = self.org_forward(x)
# x = self.org_forward(x)
...
...
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