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
574363a8
"driver/driver.cpp" did not exist on "1b323316a8448499ad835d495ffbe197ef97761a"
Commit
574363a8
authored
Dec 16, 2023
by
Hari
Browse files
Implement Perp-Neg
parent
a5056cfb
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
61 additions
and
1 deletion
+61
-1
comfy/samplers.py
comfy/samplers.py
+2
-1
comfy_extras/nodes_perpneg.py
comfy_extras/nodes_perpneg.py
+58
-0
nodes.py
nodes.py
+1
-0
No files found.
comfy/samplers.py
View file @
574363a8
...
...
@@ -251,7 +251,8 @@ def sampling_function(model, x, timestep, uncond, cond, cond_scale, model_option
cond_pred
,
uncond_pred
=
calc_cond_uncond_batch
(
model
,
cond
,
uncond_
,
x
,
timestep
,
model_options
)
if
"sampler_cfg_function"
in
model_options
:
args
=
{
"cond"
:
x
-
cond_pred
,
"uncond"
:
x
-
uncond_pred
,
"cond_scale"
:
cond_scale
,
"timestep"
:
timestep
,
"input"
:
x
,
"sigma"
:
timestep
}
args
=
{
"cond"
:
x
-
cond_pred
,
"uncond"
:
x
-
uncond_pred
,
"cond_scale"
:
cond_scale
,
"timestep"
:
timestep
,
"input"
:
x
,
"sigma"
:
timestep
,
"cond_denoised"
:
cond_pred
,
"uncond_denoised"
:
uncond_pred
,
"model"
:
model
,
"model_options"
:
model_options
}
cfg_result
=
x
-
model_options
[
"sampler_cfg_function"
](
args
)
else
:
cfg_result
=
uncond_pred
+
(
cond_pred
-
uncond_pred
)
*
cond_scale
...
...
comfy_extras/nodes_perpneg.py
0 → 100644
View file @
574363a8
import
torch
import
comfy.model_management
import
comfy.sample
import
comfy.samplers
import
comfy.utils
class
PerpNeg
:
@
classmethod
def
INPUT_TYPES
(
s
):
return
{
"required"
:
{
"model"
:
(
"MODEL"
,
),
"clip"
:
(
"CLIP"
,
),
"neg_scale"
:
(
"FLOAT"
,
{
"default"
:
1.0
,
"min"
:
0.0
,
"max"
:
100.0
}),
}}
RETURN_TYPES
=
(
"MODEL"
,)
FUNCTION
=
"patch"
CATEGORY
=
"_for_testing"
def
patch
(
self
,
model
,
clip
,
neg_scale
):
m
=
model
.
clone
()
tokens
=
clip
.
tokenize
(
""
)
nocond
,
nocond_pooled
=
clip
.
encode_from_tokens
(
tokens
,
return_pooled
=
True
)
nocond
=
[[
nocond
,
{
"pooled_output"
:
nocond_pooled
}]]
nocond
=
comfy
.
sample
.
convert_cond
(
nocond
)
def
cfg_function
(
args
):
model
=
args
[
"model"
]
noise_pred_pos
=
args
[
"cond_denoised"
]
noise_pred_neg
=
args
[
"uncond_denoised"
]
cond_scale
=
args
[
"cond_scale"
]
x
=
args
[
"input"
]
sigma
=
args
[
"sigma"
]
model_options
=
args
[
"model_options"
]
(
noise_pred_nocond
,
_
)
=
comfy
.
samplers
.
calc_cond_uncond_batch
(
model
,
nocond
,
None
,
x
,
sigma
,
model_options
)
pos
=
noise_pred_pos
-
noise_pred_nocond
neg
=
noise_pred_neg
-
noise_pred_nocond
perp
=
((
torch
.
mul
(
pos
,
neg
).
sum
())
/
(
torch
.
norm
(
neg
)
**
2
))
*
neg
perp_neg
=
perp
*
neg_scale
cfg_result
=
noise_pred_nocond
+
cond_scale
*
(
pos
-
perp_neg
)
cfg_result
=
x
-
cfg_result
return
cfg_result
m
.
set_model_sampler_cfg_function
(
cfg_function
)
return
(
m
,
)
NODE_CLASS_MAPPINGS
=
{
"PerpNeg"
:
PerpNeg
,
}
NODE_DISPLAY_NAME_MAPPINGS
=
{
"PerpNeg"
:
"Perp-Neg"
,
}
nodes.py
View file @
574363a8
...
...
@@ -1868,6 +1868,7 @@ def init_custom_nodes():
"nodes_images.py"
,
"nodes_video_model.py"
,
"nodes_sag.py"
,
"nodes_perpneg.py"
,
]
for
node_file
in
extras_files
:
...
...
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