ComfyUI Node

Pre CFG subtract mean

Subtract the prediction's mean

By Extraltodeus·Created 2 years ago·Updated about a year ago· 66
Pre CFG subtract mean
  • model
  • MODEL
start_at_sigma15.00
end_at_sigma0.00
enabledtrue

Every now and then your SDXL render comes out with a color cast - everything a bit green, or washed, or too warm - and no amount of negative-prompting fixes it because it's not a prompt problem. It's a drift problem: the noise prediction itself has a nonzero mean, and that mean gets baked into the latent and shows up in the image as a color shift. "Pre CFG subtract mean" is a two-minute cure: it subtracts the mean from your cond/uncond predictions right before the CFG merge, so the drift never reaches the image.

The mechanism is almost insultingly simple. The node runs in the pre-CFG hook (after the model predicts, before CFG combines), and for each prediction it does prediction - prediction.mean(). That recenters the noise prediction around zero, which is where it's supposed to be. The README's description is exactly right: "subtract prediction mean: gives more balanced colors." A notable detail from the code: the per-channel variant is commented out with the note "It's just not good" - so the author found subtracting the overall mean works better than per-channel. Trust him, and don't go hunting for a per-channel toggle.

The inputs are minimal:

  • model - patched MODEL in/out, wired between your loader and KSampler.
  • start_at_sigma / end_at_sigma - sigma window, defaults 15 down to 0 (the whole SDXL range). Color drift mostly accumulates late in sampling, so if you want it on only the final steps, set start_at_sigma lower.
  • enabled - easy on/off for A/B.

That's it. No scale slider, nothing to tune - which makes this the most "set and forget" node in the whole pre-CFG pack. It's also cheap: a mean subtraction per step, no extra forward pass, no measurable slowdown.

It's related to (but different from) the pack's "Subtract noise mean" node, which does the same trick to a latent (like your initial noise) before sampling. Pre-CFG subtract mean works on the predictions every step instead. If you're seeing casts that appear over the whole generation, this is the one you want.

Where people get burned: none of this works if there's no prediction to fix. On guidance-distilled models running at CFG 1 (where ComfyUI skips the uncond pass), or in a workflow that's already eliminating the negative, this node is a no-op - there's no uncond mean to subtract, and the author's testing was all SDXL. And since it removes the mean of both predictions, don't expect it to fix a cast that comes from your LoRA or your VAE; those live somewhere else entirely.

Installation is the whole-pack one-liner:

cd ComfyUI/custom_nodes && git clone https://github.com/Extraltodeus/pre_cfg_comfy_nodes_for_ComfyUI

or ComfyUI Manager → search pre_cfg_comfy_nodes_for_ComfyUI → restart. No requirements, no model files - the pack is one Python file on ComfyUI's existing hooks. For a "why is everything green" headache, this is the first pre-CFG node I'd grab.

Categorymodel_patches/Pre CFG

Inputs (4)

NameTypeDefaultDescription
modelMODEL
start_at_sigmaFLOAT15.000–1000
end_at_sigmaFLOAT0.000–1000
enabledBOOLEANtrue

Outputs (1)

NameTypeDescription
MODELMODEL