Extensions/pre_cfg_comfy_nodes_for_ComfyUI
ComfyUI Extension

pre_cfg_comfy_nodes_for_ComfyUI

A set of nodes to prepare the noise predictions before the CFG function

By Extraltodeus·Created 2 years ago·Updated about a year ago· 66
Extraltodeus/pre_cfg_comfy_nodes_for_ComfyUI
Nodes31
On cloudLocal install
Categorymodel_patches/Pre CFG/channels_selectors, advanced/conditioning
Stars66
Updatedabout a year ago

Nodes (31)

Channel selector for 128 channels

A toggle wall for the widest latents

model_patches/Pre CFG/channels_selectors
Channel selector for 16 channels

Selecting channels for wider latent models

model_patches/Pre CFG/channels_selectors
Channel selector for 32 channels

The big selector, scaled

model_patches/Pre CFG/channels_selectors
Channel selector for 4 channels

Which latent channels matter? Tell Pre CFG automatic scale with four toggles

model_patches/Pre CFG/channels_selectors
Channel selector for 64 channels

The selector for wide-latent models

model_patches/Pre CFG/channels_selectors
Channel selector for 8 channels

The mid-size channel selector

model_patches/Pre CFG/channels_selectors
Conditioning set timestep from sigma

Set timestep from sigma

advanced/conditioning
Empty RGB image

A solid-color image generator, because gradient scaling needs a canvas

image
Excellent attention

A second pass over cross-attention for crisper prompts

model_patches
Gradient RGB image

The gradient generator gradient scaling wants

image
Individual channel selector

Don't scroll 128 toggles — pick one channel with three numbers

model_patches/Pre CFG/channels_selectors
Post CFG subtract mean

Washed-out colors? Subtract the mean and rebalance the palette

model_patches
Pre CFG automatic scale

The automatic CFG scale node that tunes itself

model_patches/Pre CFG
Pre CFG channel multiplier

Multiply the latent channels that are misbehaving

model_patches/Pre CFG
Pre CFG exponentiation

Saturation and contrast, the math way

model_patches/Pre CFG
Pre CFG flip flop

Swap your positive and negative predictions, just to see what happens

model_patches/Pre CFG
Pre CFG gradient scaling

Steer a generation toward an image — no ControlNet, no ML, no extra model

model_patches/Pre CFG
Pre CFG merge negative channel

Your CFG dial is global — this one turns a single latent channel down

model_patches/Pre CFG
Pre CFG multiplier

A raw multiplier on your noise predictions — handy, and the default is a trap

model_patches/Pre CFG
Pre CFG norm neg to pos

Pre CFG norm neg to pos

model_patches/Pre CFG
Pre CFG PAG

Perturbed Attention Guidance as a plain model patch — sharper details, no new sampler

model_patches/Pre CFG
Pre CFG perp-neg

Negative prompt fighting your subject? Perp-Neg stops it pulling against you

model_patches/Pre CFG
Pre CFG replace negative channel

Kill the negative on a single latent channel — when one channel is the problem

model_patches/Pre CFG
Pre CFG sharpening

Unsharp-mask your noise predictions — sharper images, less saturation

model_patches/Pre CFG
Pre CFG subtract mean

Subtract the prediction's mean

model_patches/Pre CFG
Pre CFG uncond zero

Pre CFG uncond zero

model_patches/Pre CFG
Pre CFG variable scaling

A CFG that fades across the generation — start strong, finish clean

model_patches/Pre CFG
Pre CFG zero attention

What if a pass ran with no attention at all? Zero-attention guidance, pre-CFG

model_patches/Pre CFG
Shape attention

Turn the volume up on SDXL's shape layer — or turn it all the way off

model_patches
Subtract noise mean

Subtract its mean before you sample

latent
Support empty uncond

Cut your render time in half (the support empty uncond trick)

model_patches/Pre CFG
Readme

Pre CFG nodes

A set of nodes to prepare the noise predictions before the CFG function

All can be chained and repeated within the same workflow!

They are to be highly compatible with most nodes.

The order matters and depends on your needs.

The best chaining order is therefore to be determined by your own preferences.

All are to be used like any model patching node, right after the model loader.

Nodes:

Other nodes

There are now too many nodes for me to just add a screenshot and a bunch of details but it would be a shame not to describe them:

  • Perturbed attention guidance: adaptation of PAG as a pre-CFG node.
  • Variable CFG: Make you scale vary along the generation
  • channel multipliers
  • subtract prediction mean: gives more balanced colors
  • "flip flop": swap the positive with the negative. Since the order matter, you may chain it with other nodes and go back to the correct order after. For experimental purposes.
  • Shape attention (for SDXL) can turn off the input layer 8.
  • Support empty uncond: Combined with "menu>advanced>conditioning>set timestep range" at ~65% you can now get a speed boost on any workflow.
  • Set timestep range from sigmas: same as the default node except that you're using sigmas instead of step percentage
  • The testing branch has a few more and is the current state of these nodes for me.

Pre CFG automatic scale

image

mode:

  • Automatic CFG: applies the same predictable scaling as my other nodes based on this logic
  • Strict scaling: applies a scaling which will always give the exact desired value. This tends to create artifacts and random blurs if carried through the end.

Support empty uncond:

If you use the already available node named ConditioningSetTimestepRange you can stop generating a negative prediction earlier by letting your negative conditioning go through it while setting it like this:

image

This speeds up your generation speed by two for the steps where there is no negative.

The only issue if you do this is that the CFG function will weight your positive prediction times your CFG scale against nothing and you will get a black image.

"support_empty_uncond" therefore divides your positive prediction by your CFG scale and avoids this issue.

Doing this combination is similar to the "boost" feature of my original automatic CFG node. It can also let you avoid artifacts if you want to use the strict scaling.

If you want to use this option in a chained setup using this node multiple times I recommand to use it only once and on the last.

Pre CFG perp-neg

image

Applies the already known perp-neg logic.

Code taken and adapted from ComfyAnon implementation.

The context length (added after the screenshot of the node) can be set to a higher value if you are using a tensor rt engine requiring a higher context length.

For more details you can check my node related to this "Conditioning crop or fill" where I explain a bit more about this.

Pre CFG sharpening (experimental)

image

Subtract from the current step something from the previous step. This tends to make the images sharper and less saturated.

A negative value can be set.

Pre CFG exponentiation (experimental)

image

A value lower than one will simplify the end result and enhance the saturation / contrasts.

A value higher than one will do the opposite and if pushed too far will most likely make a mess.

Gradient scaling:

Named like this because I initially wanted to test what would happen if I used, instead of a single CFG scale, a tensor shaped like the latent space with a gradual variation. And then why not try to use masks instead? And what if I could make it so each value will match as closely as possible another input image?

The result is an arithmetic scaling method which does not noticeably slow down the sampling while also scaling the intensity of the values like an "automatic cfg".

So here it is:

image

So, simply put:

  • Maximum scale: Which max CFG scale can be used to try to match the input? You can go as high as 500 and still get an output. At 1000 you should stop before the end.
  • Minimum scale: Same of course but this one I find better to let in between 3.5 and 5.
  • Strength: An overall multiplier for the effect. Generally left at 1 but if you use a plain color image and feel like your results are too smooth you may want to lower it.
  • end at sigma: You can go down to the end of the sampling if using the next described toggle but in general I prefer to stop at 0.28. Stopping before the end will give better result with super high scales. 0.28 is the default value.
  • Converging scales: make the min and max scales join your sampler scale as the sampling goes. This can weaken the pattern matching effect if you are aiming for something precise but otherwise greatly enhance the final result also allow the use of a bigger maximum scale.
  • invert mask: for convenience

Potential uses:

General light direction/composition influence (all same seed):

combined_image

Vignetting:

combined_v_images

Color influence:

combined_rgb_image

Pattern matching, here with a black and white spiral:

00347UI_00001_

A blue one with a lower scale:

00297UI_00001_

As you can notice the details a pretty well done in general. It seems that using an input latent as a guide also helps with the overall quality. Using a "freshly" encoded latent, I haven't tried to loop back a latent space resulting from sampling directly.

Text is a bit harder to enforce and may require more tweaking with the scales:

00133UI_00001_

Since it takes advantage of the "wiggling room" left by the CFG scale so to make the generation match an image, it can hardly contradict what is being generated.

Here, an example using a black and red spiral, since the base description is about black and white I could only enforce the red by using destructive scales:

combined_side_by_side_image

Side use:

  • If only using a mask for the input, will apply the selected maximum scale to the target area.
  • If nothing is connected: will use the positive prediction as guide for 74% of the sigma and the negative for the last part.

Note:

  • Given that this is a non-ml solution, unlike controlnet, it can not tell the difference in between a banana and a person. It simply tries to make the values match the input image. A giraffe is just an apple with different values at a different place.
  • It is possible to chain multiple times this node for as long as the sum of all the strength sliders is equal or below one.
  • I added two image generators. One simply using RGB sliders and a gradient generator which can also make circular patterns while outputting a mask, to make vignetting easy. You will find them in the "image" category.