Nodes/ComfyUI-latent-ops/LatentOperationSplitCFG
ComfyUI Node

LatentOperationSplitCFG

Pry the cond and uncond apart when one latent is secretly two

By hnmr293·Created about a year ago·Updated about a year ago· 2
LatentOperationSplitCFG
  • latent
  • uncond
  • cond

Classifier-free guidance has a dirty little secret: it runs your model twice per step - once with the prompt, once without - and then extrapolates away from the unconditional result. In ComfyUI that usually stays hidden inside the sampler, but some workflows deliberately stack both predictions into a single latent, batch dimension of 2, and process them together. That's the situation LatentOperationSplitCFG is for: it takes that stacked latent and tears it back apart into the two halves the sampler was juggling.

It's one of the more self-contained nodes in hnmr293/ComfyUI-latent-ops, the latent-math toolbox from the dev behind sd-webui-cutoff and the LLuL latent upscaler. And unlike most of that pack, this one actually does its whole job on its own - no helper node required.

What it does

Feed it a LATENT and it does one operation, straight from the source: latent["samples"].chunk(2, dim=0). The batch dimension gets split into two equal halves, and you get back two fresh latents:

  • uncond - the first half
  • cond - the second half

That's the whole mechanism. ComfyUI's convention for "one latent that's actually two" is to stack uncond on top of cond along the batch axis, and this node assumes exactly that order. If your latent came from a sampler run at a CFG above 1, the halves are already in there; this node just labels them so you can do different things to each one.

Why you'd bother

The companion node in the same pack, LatentOperationApplyCFG, is the other half of the trick. Its source is one line: uncond + scale * (cond - uncond) - which is literally the textbook CFG formula (the same one the KB's CFG panel quotes: uncond_pred + (cond_pred - uncond_pred) * scale). Split the latent, apply a different operation to the cond half than the uncond half, then recombine or reapply CFG at your own scale. That's a real workflow shape: per-branch latent surgery that the built-in samplers won't let you do.

It also matters for the CFG-of-the-future gotcha: on guidance-distilled models that run at CFG 1, there is no unconditional pass - so a stacked latent never exists and this node has nothing to split. If you're on a Turbo or a distilled checkpoint, that's not a bug in this node, that's the model being CFG-free.

The honest caveat

ComfyUI core already ships SplitLatentBatch, which splits a latent batch into two halves with zero installs. So if you just need halves, use the core node - it's the same idea and you already have it. LatentOperationSplitCFG earns its name in two ways: it hardcodes the CFG semantics (uncond first, cond second, in case you forget the order), and it pairs visually with LatentOperationApplyCFG in the same menu so a CFG-shaping subgraph reads clearly. It's convenience plus documentation, not new magic.

One practical note: torch.chunk(2, dim=0) doesn't error on an odd batch size, it just gives you an uneven first chunk. If your latent has 3 images in the batch, your "uncond" will be a lie. Keep batches even.

Install

Standard pack install - via ComfyUI Manager (search "ComfyUI-latent-ops") or:

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

then restart ComfyUI. No requirements.txt, no model files, no dependencies beyond PyTorch. Everything shows up under hnmr/latent_ops.

Common issues

  • "My outputs are swapped." The node assumes uncond-first stacking. If your pipeline stacks cond-first, the halves come out backwards - swap the wires.
  • Odd batch size yields an uneven split. Check your batch.
  • Nothing to split. At CFG 1 (guidance-distilled models), there's no unconditional pass and no stacked latent. Use it on SD 1.5/SDXL-style CFG sampling, where the double pass still happens.
Categoryhnmr/latent_ops

Inputs (1)

NameTypeDefaultDescription
latentLATENT

Outputs (2)

NameTypeDescription
uncondLATENT
condLATENT