From 707aa86d5a4a03d72a1a9e345732b877e7f4fbce Mon Sep 17 00:00:00 2001 From: chflame Date: Sat, 18 May 2024 17:11:28 +0800 Subject: [PATCH] commit PurgeVRAM node, BooleanOperator node add min and max method --- README.MD | 11 ++++++++++- README_CN.MD | 12 +++++++++++- py/data_nodes.py | 8 ++++++-- py/purge_vram.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 py/purge_vram.py diff --git a/README.MD b/README.MD index 8ee76b5..0404910 100644 --- a/README.MD +++ b/README.MD @@ -70,6 +70,7 @@ When this error has occurred, please check the network environment. ## Update **If the dependency package error after updating, please reinstall the relevant dependency packages.
+* Commit [PurgeVRAM](#PurgeVRAM) node, Clean up VRAM an RAM. * Commit [AutoAdjust](#AutoAdjust) node, it can automatically adjust image contrast and white balance. * Commit [RGBValue](#RGBValue) node to output the color value as a single decimal value of R, G, B. This idea is from [vxinhao](https://github.com/vxinhao/color2rgb), Thanks. * Commit [seed](#seed) node to output the seed value. The [ImageMaskScaleAs](#ImageMaskScaleAs), [ImageScaleBySspectRatio](#ImageScaleBySspectRatio), [ImageScaleBySspectRatioV2](#ImageScaleBySspectRatioV2), [ImageScaleRestore](#ImageScaleRestore), [ImageScaleRestoreV2](#ImageScaleRestoreV2) nodes increase ```width```, ```height``` output. @@ -1077,11 +1078,19 @@ Performs mathematical operations on two numeric values and outputs integer and f ### BooleanOperator ![image](image/boolean_operator_node.png) -Perform a Boolean operation on two numeric values and output the result*. Supported operations include```==```, ```!=```, ```and```, ```or```, ```xor```, ```not```. +Perform a Boolean operation on two numeric values and output the result*. Supported operations include```==```, ```!=```, ```and```, ```or```, ```xor```, ```not```, ```min```, ```max```. * The input only supports boolean, integer, and floating point numbers, forcing in other data will result in error. The ```and``` operation between the values outputs a larger number, and the ```or``` operation outputs a smaller number. +### PurgeVRAM +![image](image/purge_vram_example.png) +Clean up GPU VRAM and system RAM. any type of input can be accessed, and when executed to this node, the VRAM and garbage objects in the RAM will be cleaned up. Usually placed after the node where the inference task is completed, such as the VAE Decode node. + +Node Options: +* purge_cache: Clean up cache。 +* purge_models: Unload all loaded models。 + # LayerMask ![image](image/layermask_nodes.png) diff --git a/README_CN.MD b/README_CN.MD index c8aaeaa..2ca2f21 100644 --- a/README_CN.MD +++ b/README_CN.MD @@ -70,6 +70,7 @@ git clone https://github.com/chflame163/ComfyUI_LayerStyle.git ## 更新说明 **如果本插件更新后出现依赖包错误,请重新安装相关依赖包。 +* 添加 [PurgeVRAM](#PurgeVRAM) 节点, 清理显存和内存。 * 添加 [AutoAdjust](#AutoAdjust) 节点, 自动调整图片对比度和白平衡。 * 添加 [RGBValue](#RGBValue) 节点, 将色值输出为单独的R, G, B三个10进制数值。这个想法来自[vxinhao](https://github.com/vxinhao/color2rgb),感谢。 * 添加[seed](#seed) 节点,输出种子值。[ImageMaskScaleAs](#ImageMaskScaleAs), [ImageScaleBySspectRatio](#ImageScaleBySspectRatio), [ImageScaleBySspectRatioV2](#ImageScaleBySspectRatioV2), [ImageScaleRestore](#ImageScaleRestore), [ImageScaleRestoreV2](#ImageScaleRestoreV2) 节点增加```width```, ```height``` 输出。 @@ -1066,11 +1067,20 @@ cropped_mask: 裁切后的遮罩。 ### BooleanOperator ![image](image/boolean_operator_node.png) -对两个数值进行布尔运算并输出结果*。支持的运算包括```==```、```!=```、```and```、```or```、```xor```、```not```。 +对两个数值进行布尔运算并输出结果*。支持的运算包括```==```、```!=```、```and```、```or```、```xor```、```not```、```min```、```max```。 * 输入仅支持布尔值、整数和浮点数,强行接入其他数据将导致错误。数值之间的```and```运算输出较大的数,```or```运算输出较小的数。 +### PurgeVRAM +![image](image/purge_vram_example.png) +清理GPU显存。可以接入任意类型的输入,当执行到这个节点时将清理VRAM以及RAM中的垃圾对象。通常放置在推理任务完成的节点之后,例如VAE Decode节点。 + +节点选项说明: +* purge_cache: 清理缓存。 +* purge_models: 清理已加载的模型。 + + # LayerMask ![image](image/layermask_nodes.png) diff --git a/py/data_nodes.py b/py/data_nodes.py index 3893d65..f69b914 100644 --- a/py/data_nodes.py +++ b/py/data_nodes.py @@ -25,7 +25,7 @@ class BooleanOperator: pass @classmethod def INPUT_TYPES(self): - operator_list = ["==", "!=", "and", "or", "xor", "not(a)"] + operator_list = ["==", "!=", "and", "or", "xor", "not(a)", "min", "max"] return {"required": { "a": (any, {}), "b": (any, {}), @@ -33,7 +33,7 @@ class BooleanOperator: },} RETURN_TYPES = ("BOOLEAN",) - RETURN_NAMES = ("boolean",) + RETURN_NAMES = ("output",) FUNCTION = 'bool_operator_node' CATEGORY = '😺dzNodes/LayerUtility' @@ -51,6 +51,10 @@ class BooleanOperator: ret_value = not(a == b) if operator == "not(a)": ret_value = not a + if operator == "min": + ret_value = a or b + if operator == "max": + ret_value = a and b return (ret_value,) diff --git a/py/purge_vram.py b/py/purge_vram.py new file mode 100644 index 0000000..29bf9f9 --- /dev/null +++ b/py/purge_vram.py @@ -0,0 +1,50 @@ +from .imagefunc import AnyType + +any = AnyType("*") + +NODE_NAME = 'PurgeVRAM' + +class PurgeVRAM: + + def __init__(self): + pass + + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + "anything": (any, {}), + "purge_cache": ("BOOLEAN", {"default": True}), + "purge_models": ("BOOLEAN", {"default": True}), + }, + "optional": { + } + } + + RETURN_TYPES = () + FUNCTION = "purge_vram" + CATEGORY = '😺dzNodes/LayerUtility' + OUTPUT_NODE = True + + def purge_vram(self, anything, purge_cache, purge_models): + import torch.cuda + import gc + import comfy.model_management + if purge_cache: + if torch.cuda.is_available(): + gc.collect() + torch.cuda.empty_cache() + torch.cuda.ipc_collect() + if purge_models: + comfy.model_management.unload_all_models() + + return (None,) + + +NODE_CLASS_MAPPINGS = { + "LayerUtility: PurgeVRAM": PurgeVRAM +} + +NODE_DISPLAY_NAME_MAPPINGS = { + "LayerUtility: PurgeVRAM": "LayerUtility: Purge VRAM" +} \ No newline at end of file