Nodes/CFG Megapack/EP-CFG: Energy-Preserving CFG (Zhang et al. 2024)
ComfyUI Node

EP-CFG: Energy-Preserving CFG (Zhang et al. 2024)

Keep the energy, lose the burn

By AbstractEyes·Created 5 days ago·Updated 5 days ago· 3
EP-CFG: Energy-Preserving CFG (Zhang et al. 2024)
  • model
  • MODEL
◄scale-1.0►
◄robusttrue►
◄lo45►
◄hi55►
◄spaceauto (the method's own)►

Every high-CFG fix in this pack is a variation on one question: after guidance has pushed the prediction somewhere new, how much of it do you keep? EP-CFG - Energy-Preserving CFG, Zhang, Luan, Bi & Zhang, arXiv 2024 - answers with a single global number. Compute the sum of squares of the guided prediction and the sum of squares of the conditional one, then scale the guided result so the two match. Same picture, same energy, no runaway.

It's the bluntest tool in the correction drawer and, precisely for that reason, the easiest one to reason about. Nothing local moves; the whole latent is multiplied by one factor.

What makes it interesting

The paper's twist is the word "robust" in the robust form. If you measure energy over the entire latent, a handful of extreme values dominate the sum of squares and your scale factor ends up being decided by outliers - exactly the pixels you were worried about. So instead the robust form only counts squared values between two percentiles. By default lo = 45 and hi = 55, which sounds alarming until you read it right: it's not "the middle 10% of pixels' energy", it's the energy computed over the squared values that fall between the 45th and 55th percentiles of magnitude. The extremes are excluded from the measurement, so the correction is set by what's typical rather than by what's worst.

That is the version you want at high scale. Turn robust off and you're back to a plain global energy match.

Inputs

  • model, and scale (default -1 = use the KSampler's cfg).
  • robust (default on) - measure the energy between the two percentiles only. The paper uses it.
  • lo / hi - the percentile band, 45 and 55 out of the box. Nudging them wide (say 30/70) makes the measurement less twitchy; narrowing it makes the node more sensitive to the bulk of the image. These are real knobs, but the defaults are the paper's, and unlike most of the pack they don't need retuning per model - the correction is scale-free by construction.
  • space - auto (noise prediction for this method). Nonlinear in practice, so don't change it casually.

One MODEL output, wired between the loader and the sampler.

Install

# ComfyUI Manager: search "CFG Megapack" -> Install -> restart
# or:
comfy node install comfy-cfg-megapack
# or by hand:
cd ComfyUI/custom_nodes && git clone https://github.com/AbstractEyes/comfy-cfg-megapack

No dependencies to install, no checkpoints to fetch - the pack uses only torch and the standard library, and it needs ComfyUI ≥ 0.38.

Where it fits in the pack

EP-CFG is one of six methods inside the CFG Correct: Magnitude stage node, which is the pack's "pick a correction from a dropdown" node. This paper node exists so the settings start at the paper's values and so you can chain several corrections and see which one earns its place. A correction is the one stage that stacks: chain EP-CFG and then, say, Guidance Rescale and they apply in order, while two mix nodes of the same stage would just fight and the later one would win.

Two practical notes:

Stacking corrections compounds. A norm match after an energy match is applying two different ideas of "how big should this be", to the same tensor. If your output goes flat and grey, that's why. Start with one.

Sanity-check with the plan readout. The pack's CFG Plan Readout node prints the whole guidance plan on a model - every stage, in the order it will run. If a chain of corrections isn't behaving, put the readout at the end of the model wire and see what's actually installed rather than guessing. The CFG Measure: Per-Step Probe node writes per-step numbers (scale, sizes, how far guidance pushed, the std ratio that shows saturation) to output/cfg_probe/, which is the honest way to tell whether a correction is doing anything at all.

And the usual pack-wide caveat: ComfyUI has exactly one CFG-function slot per model, so a RescaleCFG or Mahiro node from another pack chained after this one will replace it, silently.

CategoryCFG Megapack/papers/combining the two predictions

Inputs (6)

NameTypeDefaultDescription
modelMODEL—
scaleFLOAT-1.0-1–100The guidance scale w for this rule. -1 uses the sampler's cfg value.
robustBOOLEANtrueMeasure energy between two percentiles only (paper).
loFLOAT450–100Lower percentile of the robust energy.
hiFLOAT550–100Upper percentile of the robust energy.
spaceCOMBOauto (the method's own)Where the rule is computed. Linear rules give the same image in any space; nonlinear ones do not. 'auto' uses the space the method was published in (noise for most, denoised for APG and the angle rule, velocity for flow models).

Outputs (1)

NameTypeDescription
MODELMODEL—