From b5367edfa0be682c6b0432fc77bc2c4846c90232 Mon Sep 17 00:00:00 2001 From: Extraltodeus Date: Wed, 14 Feb 2024 14:14:31 +0100 Subject: [PATCH 1/2] add delayed activation to SAG node --- comfy_extras/nodes_sag.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/comfy_extras/nodes_sag.py b/comfy_extras/nodes_sag.py index bbd380807..25b0e7a0b 100644 --- a/comfy_extras/nodes_sag.py +++ b/comfy_extras/nodes_sag.py @@ -99,15 +99,19 @@ class SelfAttentionGuidance: return {"required": { "model": ("MODEL",), "scale": ("FLOAT", {"default": 0.5, "min": -2.0, "max": 5.0, "step": 0.1}), "blur_sigma": ("FLOAT", {"default": 2.0, "min": 0.0, "max": 10.0, "step": 0.1}), + "activation_proportion": ("FLOAT", {"default": 0.75, "min": 0.0, "max": 1.0, "step": 0.05, "round": 0.01}), }} RETURN_TYPES = ("MODEL",) FUNCTION = "patch" CATEGORY = "_for_testing" - def patch(self, model, scale, blur_sigma): + def patch(self, model, scale, blur_sigma, activation_proportion): m = model.clone() - + model_sampling = m.model.model_sampling + sigmin = model_sampling.sigma(model_sampling.timestep(model_sampling.sigma_min)) + sigmax = model_sampling.sigma(model_sampling.timestep(model_sampling.sigma_max)) + attn_scores = None # TODO: make this work properly with chunked batches @@ -145,7 +149,8 @@ class SelfAttentionGuidance: x = args["input"] if min(cfg_result.shape[2:]) <= 4: #skip when too small to add padding return cfg_result - + if 1-activation_proportion < (sigma - sigmin) / (sigmax - sigmin): + return cfg_result # create the adversarially blurred image degraded = create_blur_map(uncond_pred, uncond_attn, sag_sigma, sag_threshold) degraded_noised = degraded + x - uncond_pred From 721fd8b321726df42ed3de68927de3250eb16f33 Mon Sep 17 00:00:00 2001 From: Extraltodeus Date: Fri, 16 Feb 2024 23:42:46 +0100 Subject: [PATCH 2/2] used built-in functions --- comfy_extras/nodes_sag.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/comfy_extras/nodes_sag.py b/comfy_extras/nodes_sag.py index 25b0e7a0b..8db84374c 100644 --- a/comfy_extras/nodes_sag.py +++ b/comfy_extras/nodes_sag.py @@ -108,9 +108,7 @@ class SelfAttentionGuidance: def patch(self, model, scale, blur_sigma, activation_proportion): m = model.clone() - model_sampling = m.model.model_sampling - sigmin = model_sampling.sigma(model_sampling.timestep(model_sampling.sigma_min)) - sigmax = model_sampling.sigma(model_sampling.timestep(model_sampling.sigma_max)) + sigma_start = m.model.model_sampling.percent_to_sigma(activation_proportion) attn_scores = None @@ -149,7 +147,7 @@ class SelfAttentionGuidance: x = args["input"] if min(cfg_result.shape[2:]) <= 4: #skip when too small to add padding return cfg_result - if 1-activation_proportion < (sigma - sigmin) / (sigmax - sigmin): + if sigma > sigma_start: return cfg_result # create the adversarially blurred image degraded = create_blur_map(uncond_pred, uncond_attn, sag_sigma, sag_threshold)