Nodes/ComfyUI-latent-ops/LatentOperationApplyCFG
ComfyUI Node

LatentOperationApplyCFG

Re-apply guidance to an already-sampled latent

By hnmr293·Created about a year ago·Updated about a year ago· 2
LatentOperationApplyCFG
    • op
    scale1.0000

    CFG is normally a sampler's job - you set the scale, and the sampler computes both the conditioned and unconditioned predictions and blends them each step. LatentOperationApplyCFG takes the other route: it does that blend yourself, on a latent that's already been produced, by hand. If you've ever wanted to turn the CFG dial after sampling - or mix a conditional and unconditional latent that a custom sampler handed you - this is the node.

    It's from hnmr293's ComfyUI-latent-ops pack, and yes, it follows the pack's deferred-op pattern: it outputs a recipe, not a result.

    How it works

    The author's own tooltip gives the formula, which is worth memorizing because it's exactly the classifier-free guidance equation:

    result = (1-scale) * uncond + scale * cond
    

    When the op is applied to a latent, it first splits that latent into two halves along the batch dimension (torch.chunk(2, dim=0)): the first half is treated as the unconditional prediction, the second as the conditional one. Then it computes uncond + scale * (cond - uncond) - identical to ComfyUI's own uncond_pred + (cond_pred - uncond_pred) * cond_scale that runs inside every sampler. So this node isn't a hack; it's the CFG math, exposed for post-hoc use.

    The consequence you need to know: the latent you apply this to must be a batch-stacked pair, [uncond, cond]. That's the shape custom samplers produce, not what a normal KSampler outputs. Feed it a single-latent batch and chunk(2) returns one chunk, the math degenerates to "uncond + 0", and you get the latent back unchanged - silently. Odd batches split unevenly and give you wrong mixing.

    What matters

    • scale - FLOAT, default 1.0. At 1.0 the result is exactly cond. Above 1.0 you amplify the guidance, below 1.0 you pull toward uncond, negative inverts. The 0.0001 step lets you dial with surgical precision.
    • op - the only output. Deferred operation, so same rule as every Operation node here: you need a consumer that accepts LATENT_OPERATION (this pack ships no Apply node).

    Installing it

    No extra deps, no model files. ComfyUI Manager → search "ComfyUI-latent-ops" → install, or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/hnmr293/ComfyUI-latent-ops
    

    Restart ComfyUI; it's under hnmr/latent_ops.

    Troubleshooting

    The two failure modes to watch: an op output that won't connect (missing Apply step - pack design, not a bug), and a batch that isn't a clean pair. If you're working with a regular sampler's output, you'll need the pack's LatentOperationSplitCFG or your own batching to build the [uncond, cond] stack first. And remember the 2026-era wrinkle: on guidance-distilled models that run at CFG 1, this post-hoc blend is a niche tool - the sampler already handled it. It shines on SD 1.5/SDXL-era workflows where you're doing custom two-pass sampling and want the guidance dial accessible after the fact.

    Categoryhnmr/latent_ops

    Inputs (1)

    NameTypeDefaultDescription
    scaleFLOAT1.0000-10000–10000result = (1-scale) * uncond + scale * cond

    Outputs (1)

    NameTypeDescription
    opLATENT_OPERATION