Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
46540aa7bc | ||
|
|
873034b095 | ||
|
|
7d399643dd | ||
|
|
50033c2622 | ||
|
|
36eb9d592b | ||
|
|
bc50983954 | ||
|
|
0c37c6b124 | ||
|
|
8d7d9281f7 | ||
|
|
e55881afe2 | ||
|
|
0e8e508213 | ||
|
|
ff69ac386d | ||
|
|
5be3c4c4f1 | ||
|
|
81b836d2f1 | ||
|
|
a57c16a624 | ||
|
|
e6cdf03189 | ||
|
|
47e4b5073b | ||
|
|
0778fa4b03 | ||
|
|
4c8a086f6b |
Binary file not shown.
|
After Width: | Height: | Size: 2.3 MiB |
Binary file not shown.
|
After Width: | Height: | Size: 2.2 MiB |
@@ -0,0 +1 @@
|
||||
from .nodes_NRS import *
|
||||
+3
-3
@@ -1,4 +1,3 @@
|
||||
import ldm_patched.modules.model_base
|
||||
import logging
|
||||
import torch
|
||||
|
||||
@@ -6,8 +5,9 @@ class NRS:
|
||||
@classmethod
|
||||
def INPUT_TYPES(s):
|
||||
return {"required": { "model": ("MODEL",),
|
||||
"squash": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.01}),
|
||||
"stretch": ("FLOAT", {"default": 0.0, "min": -1.0, "max": 30.0, "step": 0.01}),
|
||||
"skew": ("FLOAT", {"default": 2.0, "min": -30.0, "max": 30.0, "step": 0.01}),
|
||||
"stretch": ("FLOAT", {"default": 2.0, "min": -30.0, "max": 30.0, "step": 0.01}),
|
||||
"squash": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.01}),
|
||||
}}
|
||||
RETURN_TYPES = ("MODEL",)
|
||||
FUNCTION = "patch"
|
||||
|
||||
@@ -1,27 +1,44 @@
|
||||
# Negative Rejection Steering
|
||||
NRS seeks to replace the 'naive' linear interpolation of Classifier Free Guidance with a more nuanced steering of the generation process.
|
||||
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.
|
||||
|
||||
This is accomplised in 3 steps:
|
||||
1. **Displacement**: The conditioned output tensor is displaced in the direction of the rejection of the unconditioned tensor on the conditioned tensor. This lengthens the tensor in a direction perpendicular to it's direction without affecting the positive guidance. The tensor is displaced by the rejection x the Displacement parameter.
|
||||
2. **Squashing**: The displaced tensor is rescaled towards the original length of the conditioned tensor. This means for high displacement scaling values the tensor 'turns' away from the unconditioned direction, which for very negative displacements, it turns towards the unconditioned tensor. 0 displacement outputs the original conditioned tensor.
|
||||
3. **Stretching**: The post-squash 'steered' tensor is stretched towards the direction of the original conditioned tensor. The more sharp the steering the less pronounced the stretch is, with fully aligned tensors being stretched the full stretch scale parameter. 1x stretch adds 100% length to the tensor.
|
||||
#### _**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.
|
||||
|
||||
# Alpha Release
|
||||
Implements NRS with Skew, Stretch, and Squash parameters.
|
||||
### Math Demonstration
|
||||
<details>
|
||||
<summary>Expand for explanation of algorithm</summary>
|
||||
<img align="right" src="https://github.com/user-attachments/assets/01fabaff-8499-45f6-adad-d54b2c2fb7f1" alt="Graph of NRS vs CFG" style="width: 40%; float: right;">
|
||||
|
||||
### NRS is Applied in Three Steps:
|
||||
1. **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 x the Displacement parameter.
|
||||
2. **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. 1x stretch adds 100% of this difference to the tensor's length.
|
||||
3. **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 simple 'steered' towards the skew & squash output.
|
||||
|
||||
[Interactive Graph on Math3D.org](https://www.math3d.org/aTJW4UZtCh)
|
||||
</details>
|
||||
|
||||
## Parameters
|
||||
Skew and Stretch are roughly similar to CFG, but decomposed, with `Stretch + Skew = CFG`, roughly.
|
||||
Skew and Stretch are roughly similar to CFG, but decomposed, with `Stretch + 2 * Skew = 2 * CFG`, roughly.
|
||||
Meaning, if you want to 'replicate' a simliar effect for a given CFG setting, you should set Skew equal to CFG, and Stretch to 1/2 CFG.
|
||||
Squash should initially be set to 0%, then adjusted based on 'burn' of output.
|
||||
|
||||
**Skew** changes the 'direction' of generation, which should result in changes to the content and composition of the image.
|
||||
**Stretch** changes to 'amplification' of generation, which should result in stronger prompt representation.
|
||||
**Squash** 'normalizes' the resulting guidance back towards the original amplitude with 1.0 being the same amplitude, while 0.0 is the unmodified amplitude resulting from the Squash and Stretch functions.
|
||||
- **Skew** changes the 'direction' of generation, which should result in changes to the content and composition of the image.
|
||||
- **Stretch** changes to 'amplification' of generation, which should result in stronger prompt representation.
|
||||
- **Squash** 'normalizes' the resulting guidance back towards the original amplitude. This results in a removal of 'burn-in' and artifacting of the output, transforming these defects into alternative guidance.
|
||||
|
||||
## Beginner How-To
|
||||
1. Set Squash to 0.0
|
||||
2. Set Skew & Stretch each to 1/2 your normal CFG Scale setting
|
||||
3. Test some generation. Results should be 'similar' in quality to CFG
|
||||
4. Adjust Skew up/down to change content and composition
|
||||
5. Adjust Stretch up/down to change strength of image aspects and colors
|
||||
6. Adjust Squash up to remove artifacts and color burn (these will tend to be replaced by additional or extraneous details and elements)
|
||||
1. Set Skew to your normal CFG Scale setting and Stretch to 1/2 your normal CFG Scale.
|
||||
2. Set Squash to 0.0.
|
||||
3. Test some outputs. Results should be similar in quality to CFG.
|
||||
4. Adjust Skew up/down to change content and composition.
|
||||
5. Adjust Stretch up/down to change strength of positive prompt aspects and colors.
|
||||
6. Adjust Squash up to remove artifacts and color burn (these will tend to be replaced by additional details and elements).
|
||||
|
||||
**Tip**: You can experiment with negative values for Skew and Stretch as well to see what the model 'believes' your negative prompt 'means'.
|
||||
**Tip**: You can experiment with negative values for Skew and Stretch as well, to see how the model is interpeting your negative prompt.
|
||||
|
||||
## Examples
|
||||
| User | CFG | NRS |
|
||||
|---|---|---|
|
||||
| Mohnjiles from StabilityMatrix |  |  |
|
||||
|
||||
@@ -25,14 +25,11 @@ class NRSScript(scripts.Script):
|
||||
|
||||
def ui(self, *args, **kwargs):
|
||||
with gr.Accordion(open=False, label=self.title()):
|
||||
gr.HTML("<p><i>Adjust the settings for Negative Rejection Steering.</i></p>")
|
||||
enabled = gr.Checkbox(label="Enable NRS", value=self.enabled)
|
||||
gr.HTML("<p><i>Adjust the amount guidance is steered.</i></p>")
|
||||
skew = gr.Slider(label="NRS Skew Scale", minimum=-30.0, maximum=30.0, step=0.01, value=self.skew)
|
||||
gr.HTML("<p><i>Adjust the amount guidance is amplified.</i></p>")
|
||||
stretch = gr.Slider(label="NRS Stretch Scale", minimum=-30.0, maximum=30.0, step=0.01, value=self.stretch)
|
||||
gr.HTML("<p><i>Adjust the amount final guidance is normalized.</i></p>")
|
||||
squash = gr.Slider(label="NRS Squash Multiplier", minimum=0.0, maximum=1.0, step=0.01, value=self.squash)
|
||||
gr.HTML("<p><i>Adjust the settings for Negative Rejection Steering.</i></p>")
|
||||
skew = gr.Slider(label="NRS Skew Scale", info="Adjusts the amount guidance is steered.", minimum=-30.0, maximum=30.0, step=0.01, value=self.skew)
|
||||
stretch = gr.Slider(label="NRS Stretch Scale", info="Adjusts the amount guidance is amplified.", minimum=-30.0, maximum=30.0, step=0.01, value=self.stretch)
|
||||
squash = gr.Slider(label="NRS Squash Multiplier", info="Adjusts the amount final guidance is normalized.", minimum=0.0, maximum=1.0, step=0.01, value=self.squash)
|
||||
|
||||
enabled.change(
|
||||
lambda x: self.update_enabled(x),
|
||||
|
||||
Reference in New Issue
Block a user