2025-10-09 09:10:23 +03:00
from typing_extensions import override
2023-11-22 03:23:16 -05:00
import comfy . utils
2025-10-09 09:10:23 +03:00
from comfy_api . latest import ComfyExtension , io
class PatchModelAddDownscale ( io . ComfyNode ) :
UPSCALE_METHODS = [ " bicubic " , " nearest-exact " , " bilinear " , " area " , " bislerp " ]
@classmethod
def define_schema ( cls ) :
return io . Schema (
node_id = " PatchModelAddDownscale " ,
display_name = " PatchModelAddDownscale (Kohya Deep Shrink) " ,
category = " model_patches/unet " ,
2026-02-16 14:02:17 -08:00
description = " Patches the UNet to downscale internal feature maps at a specified block during a configurable sigma range, then upscale on output, implementing the Kohya Deep Shrink technique for faster generation. " ,
short_description = " Kohya Deep Shrink: downscale UNet internals for speed. " ,
2025-10-09 09:10:23 +03:00
inputs = [
io . Model . Input ( " model " ) ,
io . Int . Input ( " block_number " , default = 3 , min = 1 , max = 32 , step = 1 ) ,
io . Float . Input ( " downscale_factor " , default = 2.0 , min = 0.1 , max = 9.0 , step = 0.001 ) ,
io . Float . Input ( " start_percent " , default = 0.0 , min = 0.0 , max = 1.0 , step = 0.001 ) ,
io . Float . Input ( " end_percent " , default = 0.35 , min = 0.0 , max = 1.0 , step = 0.001 ) ,
io . Boolean . Input ( " downscale_after_skip " , default = True ) ,
io . Combo . Input ( " downscale_method " , options = cls . UPSCALE_METHODS ) ,
io . Combo . Input ( " upscale_method " , options = cls . UPSCALE_METHODS ) ,
] ,
outputs = [
io . Model . Output ( ) ,
] ,
)
2023-11-16 13:23:25 -05:00
@classmethod
2025-10-09 09:10:23 +03:00
def execute ( cls , model , block_number , downscale_factor , start_percent , end_percent , downscale_after_skip , downscale_method , upscale_method ) - > io . NodeOutput :
2024-05-07 03:39:39 +05: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 13:23:25 -05: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 03:23:16 -05: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 13:23:25 -05:00
return h
def output_block_patch ( h , hsp , transformer_options ) :
if h . shape [ 2 ] != hsp . shape [ 2 ] :
2023-11-22 03:23:16 -05:00
h = comfy . utils . common_upscale ( h , hsp . shape [ - 1 ] , hsp . shape [ - 2 ] , upscale_method , " disabled " )
2023-11-16 13:23:25 -05:00
return h , hsp
m = model . clone ( )
2023-11-16 15:26:28 -05: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 13:23:25 -05:00
m . set_model_output_block_patch ( output_block_patch )
2025-10-09 09:10:23 +03:00
return io . NodeOutput ( m )
2023-11-16 13:23:25 -05:00
2025-10-09 09:10:23 +03:00
class ModelDownscaleExtension ( ComfyExtension ) :
@override
async def get_node_list ( self ) - > list [ type [ io . ComfyNode ] ] :
return [
PatchModelAddDownscale ,
]
async def comfy_entrypoint ( ) - > ModelDownscaleExtension :
return ModelDownscaleExtension ( )