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
571ea8cd
Commit
571ea8cd
authored
Dec 18, 2023
by
comfyanonymous
Browse files
Fix SAG not working with cfg 1.0
parent
8cf1daa1
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
8 additions
and
4 deletions
+8
-4
comfy/model_patcher.py
comfy/model_patcher.py
+6
-2
comfy/samplers.py
comfy/samplers.py
+1
-1
comfy_extras/nodes_sag.py
comfy_extras/nodes_sag.py
+1
-1
No files found.
comfy/model_patcher.py
View file @
571ea8cd
...
@@ -55,14 +55,18 @@ class ModelPatcher:
...
@@ -55,14 +55,18 @@ class ModelPatcher:
def
memory_required
(
self
,
input_shape
):
def
memory_required
(
self
,
input_shape
):
return
self
.
model
.
memory_required
(
input_shape
=
input_shape
)
return
self
.
model
.
memory_required
(
input_shape
=
input_shape
)
def
set_model_sampler_cfg_function
(
self
,
sampler_cfg_function
):
def
set_model_sampler_cfg_function
(
self
,
sampler_cfg_function
,
disable_cfg1_optimization
=
False
):
if
len
(
inspect
.
signature
(
sampler_cfg_function
).
parameters
)
==
3
:
if
len
(
inspect
.
signature
(
sampler_cfg_function
).
parameters
)
==
3
:
self
.
model_options
[
"sampler_cfg_function"
]
=
lambda
args
:
sampler_cfg_function
(
args
[
"cond"
],
args
[
"uncond"
],
args
[
"cond_scale"
])
#Old way
self
.
model_options
[
"sampler_cfg_function"
]
=
lambda
args
:
sampler_cfg_function
(
args
[
"cond"
],
args
[
"uncond"
],
args
[
"cond_scale"
])
#Old way
else
:
else
:
self
.
model_options
[
"sampler_cfg_function"
]
=
sampler_cfg_function
self
.
model_options
[
"sampler_cfg_function"
]
=
sampler_cfg_function
if
disable_cfg1_optimization
:
self
.
model_options
[
"disable_cfg1_optimization"
]
=
True
def
set_model_sampler_post_cfg_function
(
self
,
post_cfg_function
):
def
set_model_sampler_post_cfg_function
(
self
,
post_cfg_function
,
disable_cfg1_optimization
=
False
):
self
.
model_options
[
"sampler_post_cfg_function"
]
=
self
.
model_options
.
get
(
"sampler_post_cfg_function"
,
[])
+
[
post_cfg_function
]
self
.
model_options
[
"sampler_post_cfg_function"
]
=
self
.
model_options
.
get
(
"sampler_post_cfg_function"
,
[])
+
[
post_cfg_function
]
if
disable_cfg1_optimization
:
self
.
model_options
[
"disable_cfg1_optimization"
]
=
True
def
set_model_unet_function_wrapper
(
self
,
unet_wrapper_function
):
def
set_model_unet_function_wrapper
(
self
,
unet_wrapper_function
):
self
.
model_options
[
"model_function_wrapper"
]
=
unet_wrapper_function
self
.
model_options
[
"model_function_wrapper"
]
=
unet_wrapper_function
...
...
comfy/samplers.py
View file @
571ea8cd
...
@@ -244,7 +244,7 @@ def calc_cond_uncond_batch(model, cond, uncond, x_in, timestep, model_options):
...
@@ -244,7 +244,7 @@ def calc_cond_uncond_batch(model, cond, uncond, x_in, timestep, model_options):
#The main sampling function shared by all the samplers
#The main sampling function shared by all the samplers
#Returns denoised
#Returns denoised
def
sampling_function
(
model
,
x
,
timestep
,
uncond
,
cond
,
cond_scale
,
model_options
=
{},
seed
=
None
):
def
sampling_function
(
model
,
x
,
timestep
,
uncond
,
cond
,
cond_scale
,
model_options
=
{},
seed
=
None
):
if
math
.
isclose
(
cond_scale
,
1.0
):
if
math
.
isclose
(
cond_scale
,
1.0
)
and
model_options
.
get
(
"disable_cfg1_optimization"
,
False
)
==
False
:
uncond_
=
None
uncond_
=
None
else
:
else
:
uncond_
=
uncond
uncond_
=
uncond
...
...
comfy_extras/nodes_sag.py
View file @
571ea8cd
...
@@ -151,7 +151,7 @@ class SelfAttentionGuidance:
...
@@ -151,7 +151,7 @@ class SelfAttentionGuidance:
(
sag
,
_
)
=
comfy
.
samplers
.
calc_cond_uncond_batch
(
model
,
uncond
,
None
,
degraded_noised
,
sigma
,
model_options
)
(
sag
,
_
)
=
comfy
.
samplers
.
calc_cond_uncond_batch
(
model
,
uncond
,
None
,
degraded_noised
,
sigma
,
model_options
)
return
cfg_result
+
(
degraded
-
sag
)
*
sag_scale
return
cfg_result
+
(
degraded
-
sag
)
*
sag_scale
m
.
set_model_sampler_post_cfg_function
(
post_cfg_function
)
m
.
set_model_sampler_post_cfg_function
(
post_cfg_function
,
disable_cfg1_optimization
=
True
)
# from diffusers:
# from diffusers:
# unet.mid_block.attentions[0].transformer_blocks[0].attn1.patch
# unet.mid_block.attentions[0].transformer_blocks[0].attn1.patch
...
...
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