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
e2c585f3
Unverified
Commit
e2c585f3
authored
Jun 01, 2024
by
Peter Crabtree
Committed by
GitHub
Jun 01, 2024
Browse files
Fix to allow use of PerpNegGuider with cfg_function_post hooks (like PAG) (#3618)
parent
04b30822
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
3 deletions
+30
-3
.gitignore
.gitignore
+2
-1
comfy_extras/nodes_perpneg.py
comfy_extras/nodes_perpneg.py
+28
-2
No files found.
.gitignore
View file @
e2c585f3
...
@@ -16,3 +16,4 @@ venv/
...
@@ -16,3 +16,4 @@ venv/
!/web/extensions/core/
!/web/extensions/core/
/tests-ui/data/object_info.json
/tests-ui/data/object_info.json
/user/
/user/
comfyui*.log
comfy_extras/nodes_perpneg.py
View file @
e2c585f3
...
@@ -61,12 +61,38 @@ class Guider_PerpNeg(comfy.samplers.CFGGuider):
...
@@ -61,12 +61,38 @@ class Guider_PerpNeg(comfy.samplers.CFGGuider):
self
.
neg_scale
=
neg_scale
self
.
neg_scale
=
neg_scale
def
predict_noise
(
self
,
x
,
timestep
,
model_options
=
{},
seed
=
None
):
def
predict_noise
(
self
,
x
,
timestep
,
model_options
=
{},
seed
=
None
):
# in CFGGuider.predict_noise, we call sampling_function(), which uses cfg_function() to compute pos & neg
# but we'd rather do a single batch of sampling pos, neg, and empty, so we call calc_cond_batch([pos,neg,empty]) directly
positive_cond
=
self
.
conds
.
get
(
"positive"
,
None
)
positive_cond
=
self
.
conds
.
get
(
"positive"
,
None
)
negative_cond
=
self
.
conds
.
get
(
"negative"
,
None
)
negative_cond
=
self
.
conds
.
get
(
"negative"
,
None
)
empty_cond
=
self
.
conds
.
get
(
"empty_negative_prompt"
,
None
)
empty_cond
=
self
.
conds
.
get
(
"empty_negative_prompt"
,
None
)
out
=
comfy
.
samplers
.
calc_cond_batch
(
self
.
inner_model
,
[
negative_cond
,
positive_cond
,
empty_cond
],
x
,
timestep
,
model_options
)
(
noise_pred_pos
,
noise_pred_neg
,
noise_pred_empty
)
=
\
return
perp_neg
(
x
,
out
[
1
],
out
[
0
],
out
[
2
],
self
.
neg_scale
,
self
.
cfg
)
comfy
.
samplers
.
calc_cond_batch
(
self
.
inner_model
,
[
positive_cond
,
negative_cond
,
empty_cond
],
x
,
timestep
,
model_options
)
cfg_result
=
perp_neg
(
x
,
noise_pred_pos
,
noise_pred_neg
,
noise_pred_empty
,
self
.
neg_scale
,
self
.
cfg
)
# normally this would be done in cfg_function, but we skipped
# that for efficiency: we can compute the noise predictions in
# a single call to calc_cond_batch() (rather than two)
# so we replicate the hook here
for
fn
in
model_options
.
get
(
"sampler_post_cfg_function"
,
[]):
args
=
{
"denoised"
:
cfg_result
,
"cond"
:
positive_cond
,
"uncond"
:
negative_cond
,
"model"
:
self
.
inner_model
,
"uncond_denoised"
:
noise_pred_neg
,
"cond_denoised"
:
noise_pred_pos
,
"sigma"
:
timestep
,
"model_options"
:
model_options
,
"input"
:
x
,
# not in the original call in samplers.py:cfg_function, but made available for future hooks
"empty_cond"
:
empty_cond
,
"empty_cond_denoised"
:
noise_pred_empty
,}
cfg_result
=
fn
(
args
)
return
cfg_result
class
PerpNegGuider
:
class
PerpNegGuider
:
@
classmethod
@
classmethod
...
...
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