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
86221269
Commit
86221269
authored
Aug 16, 2023
by
AUTOMATIC1111
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
RAM optimization round 2
parent
85fcb7b8
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
48 additions
and
8 deletions
+48
-8
extensions-builtin/Lora/networks.py
extensions-builtin/Lora/networks.py
+4
-1
modules/sd_disable_initialization.py
modules/sd_disable_initialization.py
+44
-7
No files found.
extensions-builtin/Lora/networks.py
View file @
86221269
...
@@ -304,7 +304,10 @@ def network_apply_weights(self: Union[torch.nn.Conv2d, torch.nn.Linear, torch.nn
...
@@ -304,7 +304,10 @@ def network_apply_weights(self: Union[torch.nn.Conv2d, torch.nn.Linear, torch.nn
wanted_names
=
tuple
((
x
.
name
,
x
.
te_multiplier
,
x
.
unet_multiplier
,
x
.
dyn_dim
)
for
x
in
loaded_networks
)
wanted_names
=
tuple
((
x
.
name
,
x
.
te_multiplier
,
x
.
unet_multiplier
,
x
.
dyn_dim
)
for
x
in
loaded_networks
)
weights_backup
=
getattr
(
self
,
"network_weights_backup"
,
None
)
weights_backup
=
getattr
(
self
,
"network_weights_backup"
,
None
)
if
weights_backup
is
None
:
if
weights_backup
is
None
and
wanted_names
!=
():
if
current_names
!=
():
raise
RuntimeError
(
"no backup weights found and current weights are not unchanged"
)
if
isinstance
(
self
,
torch
.
nn
.
MultiheadAttention
):
if
isinstance
(
self
,
torch
.
nn
.
MultiheadAttention
):
weights_backup
=
(
self
.
in_proj_weight
.
to
(
devices
.
cpu
,
copy
=
True
),
self
.
out_proj
.
weight
.
to
(
devices
.
cpu
,
copy
=
True
))
weights_backup
=
(
self
.
in_proj_weight
.
to
(
devices
.
cpu
,
copy
=
True
),
self
.
out_proj
.
weight
.
to
(
devices
.
cpu
,
copy
=
True
))
else
:
else
:
...
...
modules/sd_disable_initialization.py
View file @
86221269
...
@@ -168,22 +168,59 @@ class LoadStateDictOnMeta(ReplaceHelper):
...
@@ -168,22 +168,59 @@ class LoadStateDictOnMeta(ReplaceHelper):
device
=
self
.
device
device
=
self
.
device
def
load_from_state_dict
(
original
,
self
,
state_dict
,
prefix
,
*
args
,
**
kwargs
):
def
load_from_state_dict
(
original
,
self
,
state_dict
,
prefix
,
*
args
,
**
kwargs
):
params
=
[(
name
,
param
)
for
name
,
param
in
self
.
_parameters
.
items
()
if
param
is
not
None
and
param
.
is_meta
]
used_param_keys
=
[]
for
name
,
param
in
self
.
_parameters
.
items
():
if
param
is
None
:
continue
key
=
prefix
+
name
sd_param
=
sd
.
pop
(
key
,
None
)
if
sd_param
is
not
None
:
state_dict
[
key
]
=
sd_param
used_param_keys
.
append
(
key
)
for
name
,
param
in
params
:
if
param
.
is_meta
:
if
param
.
is_meta
:
self
.
_parameters
[
name
]
=
torch
.
nn
.
parameter
.
Parameter
(
torch
.
zeros_like
(
param
,
device
=
device
),
requires_grad
=
param
.
requires_grad
)
dtype
=
sd_param
.
dtype
if
sd_param
is
not
None
else
param
.
dtype
self
.
_parameters
[
name
]
=
torch
.
nn
.
parameter
.
Parameter
(
torch
.
zeros_like
(
param
,
device
=
device
,
dtype
=
dtype
),
requires_grad
=
param
.
requires_grad
)
for
name
in
self
.
_buffers
:
key
=
prefix
+
name
sd_param
=
sd
.
pop
(
key
,
None
)
if
sd_param
is
not
None
:
state_dict
[
key
]
=
sd_param
used_param_keys
.
append
(
key
)
original
(
self
,
state_dict
,
prefix
,
*
args
,
**
kwargs
)
original
(
self
,
state_dict
,
prefix
,
*
args
,
**
kwargs
)
for
name
,
_
in
params
:
for
key
in
used_param_keys
:
key
=
prefix
+
name
state_dict
.
pop
(
key
,
None
)
if
key
in
sd
:
del
sd
[
key
]
def
load_state_dict
(
original
,
self
,
state_dict
,
strict
=
True
):
"""torch makes a lot of copies of the dictionary with weights, so just deleting entries from state_dict does not help
because the same values are stored in multiple copies of the dict. The trick used here is to give torch a dict with
all weights on meta device, i.e. deleted, and then it doesn't matter how many copies torch makes.
In _load_from_state_dict, the correct weight will be obtained from a single dict with the right weights (sd).
The dangerous thing about this is if _load_from_state_dict is not called, (if some exotic module overloads
the function and does not call the original) the state dict will just fail to load because weights
would be on the meta device.
"""
if
state_dict
==
sd
:
state_dict
=
{
k
:
v
.
to
(
device
=
"meta"
,
dtype
=
v
.
dtype
)
for
k
,
v
in
state_dict
.
items
()}
original
(
self
,
state_dict
,
strict
=
strict
)
module_load_state_dict
=
self
.
replace
(
torch
.
nn
.
Module
,
'load_state_dict'
,
lambda
*
args
,
**
kwargs
:
load_state_dict
(
module_load_state_dict
,
*
args
,
**
kwargs
))
module_load_from_state_dict
=
self
.
replace
(
torch
.
nn
.
Module
,
'_load_from_state_dict'
,
lambda
*
args
,
**
kwargs
:
load_from_state_dict
(
module_load_from_state_dict
,
*
args
,
**
kwargs
))
linear_load_from_state_dict
=
self
.
replace
(
torch
.
nn
.
Linear
,
'_load_from_state_dict'
,
lambda
*
args
,
**
kwargs
:
load_from_state_dict
(
linear_load_from_state_dict
,
*
args
,
**
kwargs
))
linear_load_from_state_dict
=
self
.
replace
(
torch
.
nn
.
Linear
,
'_load_from_state_dict'
,
lambda
*
args
,
**
kwargs
:
load_from_state_dict
(
linear_load_from_state_dict
,
*
args
,
**
kwargs
))
conv2d_load_from_state_dict
=
self
.
replace
(
torch
.
nn
.
Conv2d
,
'_load_from_state_dict'
,
lambda
*
args
,
**
kwargs
:
load_from_state_dict
(
conv2d_load_from_state_dict
,
*
args
,
**
kwargs
))
conv2d_load_from_state_dict
=
self
.
replace
(
torch
.
nn
.
Conv2d
,
'_load_from_state_dict'
,
lambda
*
args
,
**
kwargs
:
load_from_state_dict
(
conv2d_load_from_state_dict
,
*
args
,
**
kwargs
))
mha_load_from_state_dict
=
self
.
replace
(
torch
.
nn
.
MultiheadAttention
,
'_load_from_state_dict'
,
lambda
*
args
,
**
kwargs
:
load_from_state_dict
(
mha_load_from_state_dict
,
*
args
,
**
kwargs
))
mha_load_from_state_dict
=
self
.
replace
(
torch
.
nn
.
MultiheadAttention
,
'_load_from_state_dict'
,
lambda
*
args
,
**
kwargs
:
load_from_state_dict
(
mha_load_from_state_dict
,
*
args
,
**
kwargs
))
layer_norm_load_from_state_dict
=
self
.
replace
(
torch
.
nn
.
LayerNorm
,
'_load_from_state_dict'
,
lambda
*
args
,
**
kwargs
:
load_from_state_dict
(
layer_norm_load_from_state_dict
,
*
args
,
**
kwargs
))
group_norm_load_from_state_dict
=
self
.
replace
(
torch
.
nn
.
GroupNorm
,
'_load_from_state_dict'
,
lambda
*
args
,
**
kwargs
:
load_from_state_dict
(
group_norm_load_from_state_dict
,
*
args
,
**
kwargs
))
def
__exit__
(
self
,
exc_type
,
exc_val
,
exc_tb
):
def
__exit__
(
self
,
exc_type
,
exc_val
,
exc_tb
):
self
.
restore
()
self
.
restore
()
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