Extensions/Negative Rejection Steering
ComfyUI Extension

Negative Rejection Steering

NRS seeks to replace the 'naive' linear interpolation of Classifier Free Guidance with a more nuanced and composable steering of the generation process with better…

By Reithan·Created about a year ago·Updated 3 months ago· 16
Reithan/negative_rejection_steering
Nodes1
On cloudLocal install
Categoryadvanced/model
Stars16
Updated3 months ago
Readme

CodeQL ComfyUI Registry

Negative Rejection Steering

NRS seeks to replace the 'naive' linear interpolation of Classifier Free Guidance with a more nuanced and composable steering of the generation process with better mathematical basis.

TL;DR:

  1. CFG is a bad 'knob'
  2. NRS replaces CFG with 3 new knobs.
  3. NRS lets you to create cooler outputs than CFG.

Contributing: See CONTRIBUTING.md for development setup and guidelines.

[!TIP] Skip to the Beginner How-To if you want to just get started.

Math Demonstration

<details> <summary>Expand for explanation of algorithm</summary> <img align="right" src="Examples/NRS_graph.png" alt="Graph of NRS vs CFG" style="width: 40%; float: right;">

NRS is Applied in Three Steps:

  1. V-Space: Optional pre-NRS step If the model is not using v-prediction, we transform the EPS cond and uncond into v-prediction space before continuing, then revert to eps-space before return.
  2. Skewing: The conditioned output tensor is skewed away from the direction of the rejection of the unconditioned tensor on the conditioned tensor. This lengthens the tensor in a direction perpendicular to its direction without affecting the positive guidance. The tensor is displaced by the rejection multiplied by the Skew parameter.[^1]
  3. Stretching: The skewed tensor is stretched towards the direction of the original conditioned tensor based on its difference from the projection of uncond on cond. The stretch is multiplied by the Stretch parameter.[^1]
  4. Squashing: The skewed and stretched tensor is rescaled towards the original length of the conditioned tensor. 100% squashing outputs the original length of the conditioned tensor simply 'steered' towards the skewed & squashed version's direction.[^1] [^1]: All operations are done per feature across the step's batch, width, and height.

Interactive Graph on Math3D.org

</details>

Examples of NRS Effects

Skew Skew Example Stretch Stretch Example Squash Squash Example

<details> <summary><small>Generation details for reproduction</small></summary>

| Prompt | | | ---------- | --- | | Tool | Stable Diffusion WebUI reForge | | Sampler | DPM++ 2M | | Scheduler | Align Your Steps | | Steps | 25 | | Dimensions | 912 x 624 | | Seed | 1334103348 | | Model | Lobotomized Mix v1.5 | | Embeddings | Lazy Embeddings for ALL illustrious NoobAI..., Smooth Embeddings | | Positive | lazypos, [Smooth_Quality|SmoothNoob_Quality], BREAK<br>very awa, masterpiece, best quality, year 2024, newest, highres, absurdres,<br>1girl, samurai archer, cyberpunk cityscape, rain-soaked rooftop, neon reflection puddles, volumetric mist,<br>photorealistic, digital art,<br>dramatic rim lighting, shallow depth of field, low angle viewpoint | | Negative | lazyloli, lazynsfw, BREAK<br>lazyhand, SmoothNegative_Hands-neg, BREAK<br>[Smooth_Negative-neg|SmoothNoob_Negative-neg], BREAK<br>lowres, worst quality, worst aesthetic, bad quality, jpeg artifacts, scan artifacts,<br>blurry, deformed anatomy, bad hands, extra fingers, missing fingers, mutated hands,<br>watermark, logo, text, nsfw |

</details>

Explanation of Effects

Skew

Skew changes the 'direction' of your generation, altering the image generation to 'steer' away from negative prompt elements as they conflict with your positive prompt. Increasing Skew will change scene composition, geometry, and scene elements to ensure that the final image aligns with the intention of your prompt pair.

Stretch

Stretch changes the intensity of generated elements that align more with your positive prompt than the negative. This 'hits the gas' on any elements that are more strongly aligned with your positive prompt than your negative, and 'hit the brakes' on the opposite.

Squash

Squash is the speed limit. At 0.0 Squash, each diffusion step receives the full intensity you set from Skew and Stretch, while 1.0 Squash ensures each step has only the original step size output by the model. This setting will only remove intensity unless you have a non-zero Skew value. Squash will 'soften' the effects of Skew and Stretch as it's raised, but the 'removed' Skew and Stretch intensity is replaced by enhanced micro-detailing and 'burn'. Squash should generally be left low and used as a 'finishing' step after dialing in a decent Skew and Stretch value.

Beginner How-To

  1. Set Skew to 1/2 of your normal CFG Scale setting and Stretch to your full normal CFG Scale. Set Squash to 0.0.<br> Alternatively, try starting with the default of 2/5/0.75, or at 1/1/1 to get a baseline.
  2. Test some outputs. Results should be similar in quality to CFG.
  3. Adjust Skew to change the intensity of your outputs adherence to your positive and negative prompts. This primarily effects composition of the output.
  4. Adjust Stretch to intensify your positive prompt's aspects and colors where they differ from the negative prompt. This primarily effects color and texture.
  5. Adjust Squash to soften Skew and Stretch's effects. The intensity removed from Skew and Stretch will generally become additional micro-detailing and elements.

[!TIP] You can experiment with negative values for each setting as well. This can be useful to understand how the model interpreting your negative prompt.

[!WARNING] Don't set NRS values to negatives if there are things in your negative prompt you actually don't want to see.

Setup & Installation

ComfyUI

<details> <summary>ComfyUI Setup Instructions</summary>

Installation

Install via ComfyUI Manager or manually clone this repository into your ComfyUI/custom_nodes/ directory.

Usage

  1. Important: Ignore the CFG setting on your KSampler node - NRS replaces CFG entirely
  2. Connect your model through the Negative Rejection Steering node before sampling
  3. Configure NRS parameters (Skew/Stretch/Squash) instead of using CFG

Basic Workflow

Model → NRS Node → KSampler

Pro tip: To verify NRS is working correctly, set CFG to an extremely high value (like 30). If your output looks normal, NRS is functioning properly. If the output appears "turbo fried," check your node connections.

Sampler Compatibility: NRS now supports advanced samplers including WanKSamplerAdvanced, RES4LYF samplers, and FLOW models (WAN21, Flux) with enhanced prediction type detection.

ComfyUI Workflow Example

</details>

Automatic1111 / Forge / reForge

<details> <summary>WebUI Setup Instructions</summary>

Installation

  1. Install the extension through the Extensions tab in your WebUI
  2. Enable the extension and restart your WebUI

Usage

Once installed and enabled, the NRS settings panel will appear in your generation interface. When NRS is active:

  • CFG Scale is ignored - the WebUI may still show the CFG setting, but it has no effect
  • Use the NRS parameters (Skew/Stretch/Squash) to control generation instead
  • Follow the same parameter guidelines from the Beginner How-To section
</details>

StabilityMatrix Integration

NRS is available as a natively supported module in StabilityMatrix, providing an easy installation and management option for users of that platform.

Submitted User Examples

| User | CFG | NRS | | --- | --- | --- | | Mohnjiles from StabilityMatrix | CFG Example | NRS Example |