Skip to content

The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.

Add LatentOperationBlend and start/end percent to LatentApplyOperationCFG (#16923)

comfyanonymouse450673ef21a
diff --git a/comfy_extras/nodes_latent.py b/comfy_extras/nodes_latent.py
index 1f93e34d6..e874598ba 100644
--- a/comfy_extras/nodes_latent.py
+++ b/comfy_extras/nodes_latent.py
@@ -349,6 +349,8 @@ class LatentApplyOperationCFG(io.ComfyNode):
inputs=[
io.Model.Input("model"),
io.LatentOperation.Input("operation"),
+ io.Float.Input("start_percent", default=0.0, min=0.0, max=1.0, step=0.001, optional=True, tooltip="Start applying the operation at this fraction of the model's denoising schedule. 0 is the beginning."),
+ io.Float.Input("end_percent", default=1.0, min=0.0, max=1.0, step=0.001, optional=True, tooltip="Stop applying the operation at this fraction of the model's denoising schedule. 1 is the end."),
],
outputs=[
io.Model.Output(),
@@ -356,11 +358,17 @@ class LatentApplyOperationCFG(io.ComfyNode):
)
@classmethod
- def execute(cls, model, operation) -> io.NodeOutput:
+ def execute(cls, model, operation, start_percent=0.0, end_percent=1.0) -> io.NodeOutput:
m = model.clone()
+ model_sampling = m.get_model_object("model_sampling")
+ sigma_start = model_sampling.percent_to_sigma(start_percent)
+ sigma_end = model_sampling.percent_to_sigma(end_percent)
def pre_cfg_function(args):
conds_out = args["conds_out"]
+ sigma = args["sigma"][0].item()
+ if sigma > sigma_start or sigma < sigma_end:
+ return conds_out
if len(conds_out) == 2:
conds_out[0] = operation(latent=(conds_out[0] - conds_out[1])) + conds_out[1]
else:
@@ -370,6 +378,39 @@ class LatentApplyOperationCFG(io.ComfyNode):
m.set_model_sampler_pre_cfg_function(pre_cfg_function)
return io.NodeOutput(m)
+class LatentOperationBlend(io.ComfyNode):
+ @classmethod
+ def define_schema(cls):
+ return io.Schema(
+ node_id="LatentOperationBlend",
+ display_name="Latent Operation Blend",
+ category="model/latent/advanced/operations",
+ description="Blend the entire latent toward the reference latent, resizing the reference with nearest-neighbor interpolation when needed.",
+ is_experimental=True,
+ inputs=[
+ io.Latent.Input("reference"),
+ io.Float.Input("strength", default=1.0, min=0.0, max=1.0, step=0.0001, tooltip="0 leaves the latent unchanged; 1 matches the resized reference latent."),
+ ],
+ outputs=[
+ io.LatentOperation.Output(),
+ ],
+ )
+
+ @classmethod
+ def execute(cls, reference, strength) -> io.NodeOutput:
+ samples = reference["samples"]
+
+ def match_reference(latent, **kwargs):
+ if strength == 0:
+ return latent
+ target = samples.to(latent)
+ if target.shape[2:] != latent.shape[2:]:
+ target = torch.nn.functional.interpolate(target, size=latent.shape[2:], mode="nearest")
+ target = comfy.utils.repeat_to_batch_size(target, latent.shape[0])
+ return torch.lerp(latent, target, strength)
+
+ return io.NodeOutput(match_reference)
+
class LatentOperationTonemapReinhard(io.ComfyNode):
@classmethod
def define_schema(cls):
@@ -496,6 +537,7 @@ class LatentExtension(ComfyExtension):
LatentBatchSeedBehavior,
LatentApplyOperation,
LatentApplyOperationCFG,
+ LatentOperationBlend,
LatentOperationTonemapReinhard,
LatentOperationSharpen,
ReplaceVideoLatentFrames