Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
ComfyUI
Commits
45beebd3
Commit
45beebd3
authored
Jun 20, 2023
by
comfyanonymous
Browse files
Add a type of model patch useful for model merging.
parent
186f9204
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
3 deletions
+17
-3
comfy/sd.py
comfy/sd.py
+17
-3
No files found.
comfy/sd.py
View file @
45beebd3
...
...
@@ -347,15 +347,23 @@ class ModelPatcher:
def
model_dtype
(
self
):
return
self
.
model
.
get_dtype
()
def
add_patches
(
self
,
patches
,
strength
=
1.0
):
def
add_patches
(
self
,
patches
,
strength
_patch
=
1.0
,
strength_model
=
1.0
):
p
=
{}
model_sd
=
self
.
model
.
state_dict
()
for
k
in
patches
:
if
k
in
model_sd
:
p
[
k
]
=
patches
[
k
]
self
.
patches
+=
[(
strength
,
p
)]
self
.
patches
+=
[(
strength
_patch
,
p
,
strength_model
)]
return
p
.
keys
()
def
model_state_dict
(
self
):
sd
=
self
.
model
.
state_dict
()
keys
=
list
(
sd
.
keys
())
for
k
in
keys
:
if
not
k
.
startswith
(
"diffusion_model."
):
sd
.
pop
(
k
)
return
sd
def
patch_model
(
self
):
model_sd
=
self
.
model
.
state_dict
()
for
p
in
self
.
patches
:
...
...
@@ -371,8 +379,14 @@ class ModelPatcher:
self
.
backup
[
key
]
=
weight
.
clone
()
alpha
=
p
[
0
]
strength_model
=
p
[
2
]
if
strength_model
!=
1.0
:
weight
*=
strength_model
if
len
(
v
)
==
4
:
#lora/locon
if
len
(
v
)
==
1
:
weight
+=
alpha
*
(
v
[
0
]).
type
(
weight
.
dtype
).
to
(
weight
.
device
)
elif
len
(
v
)
==
4
:
#lora/locon
mat1
=
v
[
0
]
mat2
=
v
[
1
]
if
v
[
2
]
is
not
None
:
...
...
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