From f15dce71fde5eee12a5689e86468368a1791d200 Mon Sep 17 00:00:00 2001 From: AYF Date: Wed, 27 Dec 2023 00:55:11 -0500 Subject: [PATCH 01/11] Add title to the API workflow json. (#2380) * Add `title` to the API workflow json. * API: Move `title` to `_meta` dictionary, imply unused. --- web/scripts/app.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/web/scripts/app.js b/web/scripts/app.js index 62169abfb..73dba65cc 100644 --- a/web/scripts/app.js +++ b/web/scripts/app.js @@ -1784,6 +1784,10 @@ export class ComfyApp { output[String(node.id)] = { inputs, class_type: node.comfyClass, + // Ignored by the backend. + "_meta": { + title: node.title, + }, }; } } From e478b1794e91977c50dc6eea6228ef1248044507 Mon Sep 17 00:00:00 2001 From: comfyanonymous Date: Wed, 27 Dec 2023 01:07:02 -0500 Subject: [PATCH 02/11] Only add _meta title to api prompt when dev mode is enabled in UI. --- web/scripts/app.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/web/scripts/app.js b/web/scripts/app.js index 73dba65cc..62b71c0a1 100644 --- a/web/scripts/app.js +++ b/web/scripts/app.js @@ -1781,14 +1781,19 @@ export class ComfyApp { } } - output[String(node.id)] = { + let node_data = { inputs, class_type: node.comfyClass, - // Ignored by the backend. - "_meta": { - title: node.title, - }, }; + + if (this.ui.settings.getSettingValue("Comfy.DevMode")) { + // Ignored by the backend. + node_data["_meta"] = { + title: node.title, + } + } + + output[String(node.id)] = node_data; } } From c782144433e41c21ae2dfd75d0bc28255d2e966d Mon Sep 17 00:00:00 2001 From: comfyanonymous Date: Wed, 27 Dec 2023 13:50:57 -0500 Subject: [PATCH 03/11] Fix clip vision lowvram mode not working. --- comfy/clip_model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/comfy/clip_model.py b/comfy/clip_model.py index 850b5fdbe..7397b7a26 100644 --- a/comfy/clip_model.py +++ b/comfy/clip_model.py @@ -151,7 +151,7 @@ class CLIPVisionEmbeddings(torch.nn.Module): def forward(self, pixel_values): embeds = self.patch_embedding(pixel_values).flatten(2).transpose(1, 2) - return torch.cat([self.class_embedding.expand(pixel_values.shape[0], 1, -1), embeds], dim=1) + self.position_embedding.weight + return torch.cat([self.class_embedding.to(embeds.device).expand(pixel_values.shape[0], 1, -1), embeds], dim=1) + self.position_embedding.weight.to(embeds.device) class CLIPVision(torch.nn.Module): From a8baa40d85aafb7d0d33221ce86eb6ca1402b4c7 Mon Sep 17 00:00:00 2001 From: comfyanonymous Date: Thu, 28 Dec 2023 12:23:07 -0500 Subject: [PATCH 04/11] Cleanup. --- .vscode/settings.json | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 202121e10..000000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "path-intellisense.mappings": { - "../": "${workspaceFolder}/web/extensions/core" - }, - "[python]": { - "editor.defaultFormatter": "ms-python.autopep8" - }, - "python.formatting.provider": "none" -} From e1e322cf69319d125680d791822d8f4733fea027 Mon Sep 17 00:00:00 2001 From: comfyanonymous Date: Thu, 28 Dec 2023 21:41:10 -0500 Subject: [PATCH 05/11] Load weights that can't be lowvramed to target device. --- comfy/model_management.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/comfy/model_management.py b/comfy/model_management.py index 3adc42702..c0cb4130c 100644 --- a/comfy/model_management.py +++ b/comfy/model_management.py @@ -259,6 +259,14 @@ print("VAE dtype:", VAE_DTYPE) current_loaded_models = [] +def module_size(module): + module_mem = 0 + sd = module.state_dict() + for k in sd: + t = sd[k] + module_mem += t.nelement() * t.element_size() + return module_mem + class LoadedModel: def __init__(self, model): self.model = model @@ -296,14 +304,14 @@ class LoadedModel: if hasattr(m, "comfy_cast_weights"): m.prev_comfy_cast_weights = m.comfy_cast_weights m.comfy_cast_weights = True - module_mem = 0 - sd = m.state_dict() - for k in sd: - t = sd[k] - module_mem += t.nelement() * t.element_size() + module_mem = module_size(m) if mem_counter + module_mem < lowvram_model_memory: m.to(self.device) mem_counter += module_mem + elif hasattr(m, "weight"): #only modules with comfy_cast_weights can be set to lowvram mode + m.to(self.device) + mem_counter += module_size(m) + print("lowvram: loaded module regularly", m) self.model_accelerated = True From 12e822c6c8a9019abd1127e0e61f1405de8d14e3 Mon Sep 17 00:00:00 2001 From: comfyanonymous Date: Thu, 28 Dec 2023 21:46:20 -0500 Subject: [PATCH 06/11] Use function to calculate model size in model patcher. --- comfy/model_patcher.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/comfy/model_patcher.py b/comfy/model_patcher.py index 6acb2d647..b1b5ea6a8 100644 --- a/comfy/model_patcher.py +++ b/comfy/model_patcher.py @@ -28,13 +28,9 @@ class ModelPatcher: if self.size > 0: return self.size model_sd = self.model.state_dict() - size = 0 - for k in model_sd: - t = model_sd[k] - size += t.nelement() * t.element_size() - self.size = size + self.size = comfy.model_management.module_size(self.model) self.model_keys = set(model_sd.keys()) - return size + return self.size def clone(self): n = ModelPatcher(self.model, self.load_device, self.offload_device, self.size, self.current_device, weight_inplace_update=self.weight_inplace_update) From 04b713dda1c4109f84386b17b0f7c25722f0ae15 Mon Sep 17 00:00:00 2001 From: comfyanonymous Date: Fri, 29 Dec 2023 17:33:30 -0500 Subject: [PATCH 07/11] Fix VALIDATE_INPUTS getting called multiple times. Allow VALIDATE_INPUTS to only validate specific inputs. --- execution.py | 58 +++++++++++++++++++++++++++++++--------------------- nodes.py | 5 +---- 2 files changed, 36 insertions(+), 27 deletions(-) diff --git a/execution.py b/execution.py index 7ad171313..53ba2e0f8 100644 --- a/execution.py +++ b/execution.py @@ -7,6 +7,7 @@ import threading import heapq import traceback import gc +import inspect import torch import nodes @@ -402,6 +403,10 @@ def validate_inputs(prompt, item, validated): errors = [] valid = True + validate_function_inputs = [] + if hasattr(obj_class, "VALIDATE_INPUTS"): + validate_function_inputs = inspect.getfullargspec(obj_class.VALIDATE_INPUTS).args + for x in required_inputs: if x not in inputs: error = { @@ -531,29 +536,7 @@ def validate_inputs(prompt, item, validated): errors.append(error) continue - if hasattr(obj_class, "VALIDATE_INPUTS"): - input_data_all = get_input_data(inputs, obj_class, unique_id) - #ret = obj_class.VALIDATE_INPUTS(**input_data_all) - ret = map_node_over_list(obj_class, input_data_all, "VALIDATE_INPUTS") - for i, r in enumerate(ret): - if r is not True: - details = f"{x}" - if r is not False: - details += f" - {str(r)}" - - error = { - "type": "custom_validation_failed", - "message": "Custom validation failed for node", - "details": details, - "extra_info": { - "input_name": x, - "input_config": info, - "received_value": val, - } - } - errors.append(error) - continue - else: + if x not in validate_function_inputs: if isinstance(type_input, list): if val not in type_input: input_config = info @@ -580,6 +563,35 @@ def validate_inputs(prompt, item, validated): errors.append(error) continue + if len(validate_function_inputs) > 0: + input_data_all = get_input_data(inputs, obj_class, unique_id) + input_filtered = {} + for x in input_data_all: + if x in validate_function_inputs: + input_filtered[x] = input_data_all[x] + + #ret = obj_class.VALIDATE_INPUTS(**input_filtered) + ret = map_node_over_list(obj_class, input_filtered, "VALIDATE_INPUTS") + for x in input_filtered: + for i, r in enumerate(ret): + if r is not True: + details = f"{x}" + if r is not False: + details += f" - {str(r)}" + + error = { + "type": "custom_validation_failed", + "message": "Custom validation failed for node", + "details": details, + "extra_info": { + "input_name": x, + "input_config": info, + "received_value": val, + } + } + errors.append(error) + continue + if len(errors) > 0 or valid is not True: ret = (False, errors, unique_id) else: diff --git a/nodes.py b/nodes.py index 027bf55d9..8e3ec947c 100644 --- a/nodes.py +++ b/nodes.py @@ -1491,13 +1491,10 @@ class LoadImageMask: return m.digest().hex() @classmethod - def VALIDATE_INPUTS(s, image, channel): + def VALIDATE_INPUTS(s, image): if not folder_paths.exists_annotated_filepath(image): return "Invalid image file: {}".format(image) - if channel not in s._color_channels: - return "Invalid color channel: {}".format(channel) - return True class ImageScale: From 144e6580a4a43d5390769665a5032bb584481ff1 Mon Sep 17 00:00:00 2001 From: comfyanonymous Date: Fri, 29 Dec 2023 17:47:24 -0500 Subject: [PATCH 08/11] This cache timeout is pretty useless in practice. --- folder_paths.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/folder_paths.py b/folder_paths.py index 98704945e..a8726d8dd 100644 --- a/folder_paths.py +++ b/folder_paths.py @@ -184,8 +184,7 @@ def cached_filename_list_(folder_name): if folder_name not in filename_list_cache: return None out = filename_list_cache[folder_name] - if time.perf_counter() < (out[2] + 0.5): - return out + for x in out[1]: time_modified = out[1][x] folder = x From 1b103e0cb2d7aeb05fc8b7e006d4438e7bceca20 Mon Sep 17 00:00:00 2001 From: comfyanonymous Date: Sat, 30 Dec 2023 05:38:21 -0500 Subject: [PATCH 09/11] Add argument to run the VAE on the CPU. --- comfy/cli_args.py | 2 ++ comfy/model_management.py | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/comfy/cli_args.py b/comfy/cli_args.py index 8de0adb53..50d7b62fa 100644 --- a/comfy/cli_args.py +++ b/comfy/cli_args.py @@ -66,6 +66,8 @@ fpvae_group.add_argument("--fp16-vae", action="store_true", help="Run the VAE in fpvae_group.add_argument("--fp32-vae", action="store_true", help="Run the VAE in full precision fp32.") fpvae_group.add_argument("--bf16-vae", action="store_true", help="Run the VAE in bf16.") +parser.add_argument("--cpu-vae", action="store_true", help="Run the VAE on the CPU.") + fpte_group = parser.add_mutually_exclusive_group() fpte_group.add_argument("--fp8_e4m3fn-text-enc", action="store_true", help="Store text encoder weights in fp8 (e4m3fn variant).") fpte_group.add_argument("--fp8_e5m2-text-enc", action="store_true", help="Store text encoder weights in fp8 (e5m2 variant).") diff --git a/comfy/model_management.py b/comfy/model_management.py index c0cb4130c..fefd3c8c9 100644 --- a/comfy/model_management.py +++ b/comfy/model_management.py @@ -186,6 +186,9 @@ except: if is_intel_xpu(): VAE_DTYPE = torch.bfloat16 +if args.cpu_vae: + VAE_DTYPE = torch.float32 + if args.fp16_vae: VAE_DTYPE = torch.float16 elif args.bf16_vae: @@ -555,6 +558,8 @@ def intermediate_device(): return torch.device("cpu") def vae_device(): + if args.cpu_vae: + return torch.device("cpu") return get_torch_device() def vae_offload_device(): From 36e15f2507ee81e27140cf15ffcda40070968928 Mon Sep 17 00:00:00 2001 From: comfyanonymous Date: Sun, 31 Dec 2023 05:05:14 -0500 Subject: [PATCH 10/11] Reregister nodes when pressing refresh button. --- web/scripts/app.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/web/scripts/app.js b/web/scripts/app.js index 62b71c0a1..7353f5a3b 100644 --- a/web/scripts/app.js +++ b/web/scripts/app.js @@ -2020,12 +2020,8 @@ export class ComfyApp { async refreshComboInNodes() { const defs = await api.getNodeDefs(); - for(const nodeId in LiteGraph.registered_node_types) { - const node = LiteGraph.registered_node_types[nodeId]; - const nodeDef = defs[nodeId]; - if(!nodeDef) continue; - - node.nodeData = nodeDef; + for (const nodeId in defs) { + this.registerNodeDef(nodeId, defs[nodeId]); } for(let nodeNum in this.graph._nodes) { From d1f3637a5a944d0607b899babd8ff11d87100503 Mon Sep 17 00:00:00 2001 From: comfyanonymous Date: Sun, 31 Dec 2023 15:37:20 -0500 Subject: [PATCH 11/11] Add a denoise parameter to BasicScheduler node. --- comfy_extras/nodes_custom_sampler.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/comfy_extras/nodes_custom_sampler.py b/comfy_extras/nodes_custom_sampler.py index 8791d8ae3..d5f9ba007 100644 --- a/comfy_extras/nodes_custom_sampler.py +++ b/comfy_extras/nodes_custom_sampler.py @@ -13,6 +13,7 @@ class BasicScheduler: {"model": ("MODEL",), "scheduler": (comfy.samplers.SCHEDULER_NAMES, ), "steps": ("INT", {"default": 20, "min": 1, "max": 10000}), + "denoise": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.01}), } } RETURN_TYPES = ("SIGMAS",) @@ -20,8 +21,13 @@ class BasicScheduler: FUNCTION = "get_sigmas" - def get_sigmas(self, model, scheduler, steps): - sigmas = comfy.samplers.calculate_sigmas_scheduler(model.model, scheduler, steps).cpu() + def get_sigmas(self, model, scheduler, steps, denoise): + total_steps = steps + if denoise < 1.0: + total_steps = int(steps/denoise) + + sigmas = comfy.samplers.calculate_sigmas_scheduler(model.model, scheduler, total_steps).cpu() + sigmas = sigmas[-(steps + 1):] return (sigmas, )