Skip to content

Commit

Permalink
feat: add ScheduledPerpNegCFGGuider
Browse files Browse the repository at this point in the history
  • Loading branch information
ltdrdata committed Jun 22, 2024
1 parent 68fb465 commit 8f8cac5
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ This repository offers various extension nodes for ComfyUI. Nodes here have diff
### Sampler nodes
* `KSampler Progress (Inspire)` - In KSampler, the sampling process generates latent batches. By using `Video Combine` node from [ComfyUI-VideoHelperSuite](https://github.com/Kosinkadink/ComfyUI-VideoHelperSuite), you can create a video from the progress.
* `Scheduled CFGGuider (Inspire)` - This is a CFGGuider that adjusts the schedule from from_cfg to to_cfg using linear, log, and exp methods.
* `Scheduled PerpNeg CFGGuider (Inspire)` - This is a PerpNeg CFGGuider that adjusts the schedule from from_cfg to to_cfg using linear, log, and exp methods.


### Prompt Support - These are nodes for supporting prompt processing.
Expand Down
2 changes: 1 addition & 1 deletion __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import importlib

version_code = [0, 79]
version_code = [0, 80]
version_str = f"V{version_code[0]}.{version_code[1]}" + (f'.{version_code[2]}' if len(version_code) > 2 else '')
print(f"### Loading: ComfyUI-Inspire-Pack ({version_str})")

Expand Down
73 changes: 70 additions & 3 deletions inspire/sampler_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .libs import common
from comfy import model_management
from comfy.samplers import CFGGuider
import numpy
from comfy_extras.nodes_perpneg import Guider_PerpNeg
import math

class KSampler_progress(a1111_compat.KSampler_inspire):
Expand Down Expand Up @@ -202,6 +202,44 @@ def predict_noise(self, x, timestep, model_options={}, seed=None):
return super().predict_noise(x, timestep, model_options, seed)


class Guider_PerpNeg_scheduled(Guider_PerpNeg):
def __init__(self, model_patcher, sigmas, from_cfg, to_cfg, schedule, neg_scale):
super().__init__(model_patcher)
self.default_cfg = self.cfg
self.sigmas = sigmas
self.cfg_sigmas = None
self.from_cfg = from_cfg
self.to_cfg = to_cfg
self.schedule = schedule
self.neg_scale = neg_scale
self.renew_cfg_sigmas()

def set_cfg(self, cfg):
self.default_cfg = cfg
self.renew_cfg_sigmas()

def renew_cfg_sigmas(self):
self.cfg_sigmas = {}
i = 0
steps = len(self.sigmas) - 1
for x in self.sigmas:
k = float(x)
delta = self.to_cfg - self.from_cfg
if self.schedule == 'exp':
self.cfg_sigmas[k] = exponential_interpolation(self.from_cfg, self.to_cfg, i, steps)
elif self.schedule == 'log':
self.cfg_sigmas[k] = logarithmic_interpolation(self.from_cfg, self.to_cfg, i, steps)
else:
self.cfg_sigmas[k] = self.from_cfg + delta * i / steps

i += 1

def predict_noise(self, x, timestep, model_options={}, seed=None):
k = float(timestep[0])
self.cfg = self.cfg_sigmas[k]
return super().predict_noise(x, timestep, model_options, seed)


class ScheduledCFGGuider:
@classmethod
def INPUT_TYPES(s):
Expand All @@ -227,14 +265,43 @@ def get_guider(self, model, positive, negative, sigmas, from_cfg, to_cfg, schedu
return guider, sigmas


class ScheduledPerpNegCFGGuider:
@classmethod
def INPUT_TYPES(s):
return {"required": {
"model": ("MODEL", ),
"positive": ("CONDITIONING", ),
"negative": ("CONDITIONING", ),
"empty_conditioning": ("CONDITIONING", ),
"neg_scale": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 100.0, "step": 0.01}),
"sigmas": ("SIGMAS", ),
"from_cfg": ("FLOAT", {"default": 8.0, "min": 0.0, "max": 100.0, "step": 0.1, "round": 0.01}),
"to_cfg": ("FLOAT", {"default": 2.0, "min": 0.0, "max": 100.0, "step": 0.1, "round": 0.01}),
"schedule": (["linear", "log", "exp"], )
}
}

RETURN_TYPES = ("GUIDER", "SIGMAS")

FUNCTION = "get_guider"
CATEGORY = "sampling/custom_sampling/guiders"

def get_guider(self, model, positive, negative, empty_conditioning, neg_scale, sigmas, from_cfg, to_cfg, schedule):
guider = Guider_PerpNeg_scheduled(model, sigmas, from_cfg, to_cfg, schedule, neg_scale)
guider.set_conds(positive, negative, empty_conditioning)
return guider, sigmas


NODE_CLASS_MAPPINGS = {
"KSamplerProgress //Inspire": KSampler_progress,
"KSamplerAdvancedProgress //Inspire": KSamplerAdvanced_progress,
"ScheduledCFGGuider //Inspire": ScheduledCFGGuider
"ScheduledCFGGuider //Inspire": ScheduledCFGGuider,
"ScheduledPerpNegCFGGuider //Inspire": ScheduledPerpNegCFGGuider
}

NODE_DISPLAY_NAME_MAPPINGS = {
"KSamplerProgress //Inspire": "KSampler Progress (Inspire)",
"KSamplerAdvancedProgress //Inspire": "KSampler Advanced Progress (Inspire)",
"ScheduledCFGGuider //Inspire": "Scheduled CFGGuider (Inspire)"
"ScheduledCFGGuider //Inspire": "Scheduled CFGGuider (Inspire)",
"ScheduledPerpNegCFGGuider //Inspire": "Scheduled PerpNeg CFGGuider (Inspire)"
}
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[project]
name = "comfyui-inspire-pack"
description = "This extension provides various nodes to support Lora Block Weight and the Impact Pack. Provides many easily applicable regional features and applications for Variation Seed."
version = "0.79"
version = "0.80"
license = "LICENSE"
dependencies = ["matplotlib", "cachetools"]

Expand Down

0 comments on commit 8f8cac5

Please sign in to comment.