2023-11-16 18:23:25 +00:00
|
|
|
import torch
|
2023-11-22 08:23:16 +00:00
|
|
|
import comfy.utils
|
2023-11-16 18:23:25 +00:00
|
|
|
|
|
|
|
class PatchModelAddDownscale:
|
2023-11-22 08:23:16 +00:00
|
|
|
upscale_methods = ["bicubic", "nearest-exact", "bilinear", "area", "bislerp"]
|
2023-11-16 18:23:25 +00:00
|
|
|
@classmethod
|
|
|
|
def INPUT_TYPES(s):
|
|
|
|
return {"required": { "model": ("MODEL",),
|
|
|
|
"block_number": ("INT", {"default": 3, "min": 1, "max": 32, "step": 1}),
|
|
|
|
"downscale_factor": ("FLOAT", {"default": 2.0, "min": 0.1, "max": 9.0, "step": 0.001}),
|
|
|
|
"start_percent": ("FLOAT", {"default": 0.0, "min": 0.0, "max": 1.0, "step": 0.001}),
|
|
|
|
"end_percent": ("FLOAT", {"default": 0.35, "min": 0.0, "max": 1.0, "step": 0.001}),
|
2023-11-16 20:26:28 +00:00
|
|
|
"downscale_after_skip": ("BOOLEAN", {"default": True}),
|
2023-11-22 08:23:16 +00:00
|
|
|
"downscale_method": (s.upscale_methods,),
|
|
|
|
"upscale_method": (s.upscale_methods,),
|
2023-11-16 18:23:25 +00:00
|
|
|
}}
|
|
|
|
RETURN_TYPES = ("MODEL",)
|
|
|
|
FUNCTION = "patch"
|
|
|
|
|
2024-09-25 07:24:13 +00:00
|
|
|
CATEGORY = "model_patches/unet"
|
2023-11-16 18:23:25 +00:00
|
|
|
|
2023-11-22 08:23:16 +00:00
|
|
|
def patch(self, model, block_number, downscale_factor, start_percent, end_percent, downscale_after_skip, downscale_method, upscale_method):
|
2024-05-06 22:39:39 +00:00
|
|
|
model_sampling = model.get_model_object("model_sampling")
|
|
|
|
sigma_start = model_sampling.percent_to_sigma(start_percent)
|
|
|
|
sigma_end = model_sampling.percent_to_sigma(end_percent)
|
2023-11-16 18:23:25 +00:00
|
|
|
|
|
|
|
def input_block_patch(h, transformer_options):
|
|
|
|
if transformer_options["block"][1] == block_number:
|
|
|
|
sigma = transformer_options["sigmas"][0].item()
|
|
|
|
if sigma <= sigma_start and sigma >= sigma_end:
|
2023-11-22 08:23:16 +00:00
|
|
|
h = comfy.utils.common_upscale(h, round(h.shape[-1] * (1.0 / downscale_factor)), round(h.shape[-2] * (1.0 / downscale_factor)), downscale_method, "disabled")
|
2023-11-16 18:23:25 +00:00
|
|
|
return h
|
|
|
|
|
|
|
|
def output_block_patch(h, hsp, transformer_options):
|
|
|
|
if h.shape[2] != hsp.shape[2]:
|
2023-11-22 08:23:16 +00:00
|
|
|
h = comfy.utils.common_upscale(h, hsp.shape[-1], hsp.shape[-2], upscale_method, "disabled")
|
2023-11-16 18:23:25 +00:00
|
|
|
return h, hsp
|
|
|
|
|
|
|
|
m = model.clone()
|
2023-11-16 20:26:28 +00:00
|
|
|
if downscale_after_skip:
|
|
|
|
m.set_model_input_block_patch_after_skip(input_block_patch)
|
|
|
|
else:
|
|
|
|
m.set_model_input_block_patch(input_block_patch)
|
2023-11-16 18:23:25 +00:00
|
|
|
m.set_model_output_block_patch(output_block_patch)
|
|
|
|
return (m, )
|
|
|
|
|
|
|
|
NODE_CLASS_MAPPINGS = {
|
|
|
|
"PatchModelAddDownscale": PatchModelAddDownscale,
|
|
|
|
}
|
|
|
|
|
|
|
|
NODE_DISPLAY_NAME_MAPPINGS = {
|
|
|
|
# Sampling
|
|
|
|
"PatchModelAddDownscale": "PatchModelAddDownscale (Kohya Deep Shrink)",
|
|
|
|
}
|