From 3fb4fe9a2bf1c000c4d3e8791ed843569cabea2c Mon Sep 17 00:00:00 2001 From: root Date: Mon, 9 Mar 2026 14:43:54 +0800 Subject: [PATCH 1/3] Update .gitignore to ignore large files && Adapt to obtaining resources within Docker. --- .gitignore | 1 + comfy/model_management.py | 28 +++++++++++++++++++++++++--- comfy_execution/caching.py | 33 ++++++++++++++++++++++++++++++++- styles/default.csv | 3 +++ 4 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 styles/default.csv diff --git a/.gitignore b/.gitignore index 2700ad5c2..522e0a8f6 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ __pycache__/ *.py[cod] /output/ /input/ +/cache/ !/input/example.png /models/ /temp/ diff --git a/comfy/model_management.py b/comfy/model_management.py index 07bc8ad67..b96a7eefc 100644 --- a/comfy/model_management.py +++ b/comfy/model_management.py @@ -32,6 +32,26 @@ import comfy.memory_management import comfy.utils import comfy.quant_ops + +def get_container_ram_total(): + cgroup_v2_limit = "/sys/fs/cgroup/memory.max" + cgroup_v1_limit = "/sys/fs/cgroup/memory/memory.limit_in_bytes" + try: + if os.path.exists(cgroup_v2_limit): + with open(cgroup_v2_limit, "r") as f: + limit = int(f.read().strip()) + elif os.path.exists(cgroup_v1_limit): + with open(cgroup_v1_limit, "r") as f: + limit = int(f.read().strip()) + else: + limit = psutil.virtual_memory().total + if limit == 9223372036854771712: + limit = psutil.virtual_memory().total + return limit + except: + return psutil.virtual_memory().total + + class VRAMState(Enum): DISABLED = 0 #No vram present: no need to move models to vram NO_VRAM = 1 #Very low vram: enable all the options to save vram @@ -211,11 +231,13 @@ def get_total_memory(dev=None, torch_total_too=False): dev = get_torch_device() if hasattr(dev, 'type') and (dev.type == 'cpu' or dev.type == 'mps'): - mem_total = psutil.virtual_memory().total + # 核心修改:读取容器内存 + mem_total = get_container_ram_total() mem_total_torch = mem_total else: + # GPU逻辑保持不变 if directml_enabled: - mem_total = 1024 * 1024 * 1024 #TODO + mem_total = 1024 * 1024 * 1024 mem_total_torch = mem_total elif is_intel_xpu(): stats = torch.xpu.memory_stats(dev) @@ -254,7 +276,7 @@ def mac_version(): return None total_vram = get_total_memory(get_torch_device()) / (1024 * 1024) -total_ram = psutil.virtual_memory().total / (1024 * 1024) +total_ram = get_total_memory(torch.device("cpu")) / (1024 * 1024) logging.info("Total VRAM {:0.0f} MB, total RAM {:0.0f} MB".format(total_vram, total_ram)) try: diff --git a/comfy_execution/caching.py b/comfy_execution/caching.py index 326a279fc..5a8856aba 100644 --- a/comfy_execution/caching.py +++ b/comfy_execution/caching.py @@ -383,7 +383,38 @@ class RAMPressureCache(LRUCache): def poll(self, ram_headroom): def _ram_gb(): - return psutil.virtual_memory().available / (1024**3) + """读取容器可用内存(GB),优先从 cgroup v2/v1 读取""" + # === 容器内存检测核心逻辑 === + # 1. 尝试读取 cgroup v2 内存限制(Docker/K8s 主流) + cgroup_mem_limit_path = "/sys/fs/cgroup/memory.max" + cgroup_mem_usage_path = "/sys/fs/cgroup/memory.current" + + # 兼容 cgroup v1 + if not os.path.exists(cgroup_mem_limit_path): + cgroup_mem_limit_path = "/sys/fs/cgroup/memory/memory.limit_in_bytes" + cgroup_mem_usage_path = "/sys/fs/cgroup/memory/memory.usage_in_bytes" + + # 读取内存限制(字节) + try: + with open(cgroup_mem_limit_path, "r") as f: + mem_limit = int(f.read().strip()) + # cgroup v1 中 limit_in_bytes 为 9223372036854771712 表示无限制, fallback 到系统内存 + if mem_limit == 9223372036854771712: + mem_limit = psutil.virtual_memory().total + except (FileNotFoundError, ValueError): + mem_limit = psutil.virtual_memory().total + + # 读取已使用内存(字节) + try: + with open(cgroup_mem_usage_path, "r") as f: + mem_used = int(f.read().strip()) + except (FileNotFoundError, ValueError): + mem_used = psutil.virtual_memory().total - psutil.virtual_memory().available + + # 计算可用内存(GB) + mem_available = mem_limit - mem_used + return mem_available / (1024**3) + # === 替换结束 === if _ram_gb() > ram_headroom: return diff --git a/styles/default.csv b/styles/default.csv new file mode 100644 index 000000000..b7977e597 --- /dev/null +++ b/styles/default.csv @@ -0,0 +1,3 @@ +name,prompt,negative_prompt +❌Low Token,,"embedding:EasyNegative, NSFW, Cleavage, Pubic Hair, Nudity, Naked, censored" +✅Line Art / Manga,"(Anime Scene, Toonshading, Satoshi Kon, Ken Sugimori, Hiromu Arakawa:1.2), (Anime Style, Manga Style:1.3), Low detail, sketch, concept art, line art, webtoon, manhua, hand drawn, defined lines, simple shades, minimalistic, High contrast, Linear compositions, Scalable artwork, Digital art, High Contrast Shadows, glow effects, humorous illustration, big depth of field, Masterpiece, colors, concept art, trending on artstation, Vivid colors, dramatic", From 6fdde2271a2d24b6160697157b821fc7674d312a Mon Sep 17 00:00:00 2001 From: root Date: Mon, 9 Mar 2026 15:45:31 +0800 Subject: [PATCH 2/3] Adapt to obtaining resources within Docker. --- comfy_execution/caching.py | 81 ++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 39 deletions(-) diff --git a/comfy_execution/caching.py b/comfy_execution/caching.py index 5a8856aba..bbfbd2ce8 100644 --- a/comfy_execution/caching.py +++ b/comfy_execution/caching.py @@ -1,4 +1,5 @@ import bisect +import os import gc import itertools import psutil @@ -382,45 +383,47 @@ class RAMPressureCache(LRUCache): return super().get(node_id) def poll(self, ram_headroom): - def _ram_gb(): - """读取容器可用内存(GB),优先从 cgroup v2/v1 读取""" - # === 容器内存检测核心逻辑 === - # 1. 尝试读取 cgroup v2 内存限制(Docker/K8s 主流) - cgroup_mem_limit_path = "/sys/fs/cgroup/memory.max" - cgroup_mem_usage_path = "/sys/fs/cgroup/memory.current" - - # 兼容 cgroup v1 - if not os.path.exists(cgroup_mem_limit_path): - cgroup_mem_limit_path = "/sys/fs/cgroup/memory/memory.limit_in_bytes" - cgroup_mem_usage_path = "/sys/fs/cgroup/memory/memory.usage_in_bytes" - - # 读取内存限制(字节) - try: - with open(cgroup_mem_limit_path, "r") as f: - mem_limit = int(f.read().strip()) - # cgroup v1 中 limit_in_bytes 为 9223372036854771712 表示无限制, fallback 到系统内存 - if mem_limit == 9223372036854771712: - mem_limit = psutil.virtual_memory().total - except (FileNotFoundError, ValueError): - mem_limit = psutil.virtual_memory().total - - # 读取已使用内存(字节) - try: - with open(cgroup_mem_usage_path, "r") as f: - mem_used = int(f.read().strip()) - except (FileNotFoundError, ValueError): - mem_used = psutil.virtual_memory().total - psutil.virtual_memory().available - - # 计算可用内存(GB) - mem_available = mem_limit - mem_used - return mem_available / (1024**3) - # === 替换结束 === - - if _ram_gb() > ram_headroom: - return - gc.collect() - if _ram_gb() > ram_headroom: - return + def _ram_gb(): + fallback_to_host_available = False + cgroup_mem_limit_path = "/sys/fs/cgroup/memory.max" + cgroup_mem_usage_path = "/sys/fs/cgroup/memory.current" + + if not os.path.exists(cgroup_mem_limit_path): + cgroup_mem_limit_path = "/sys/fs/cgroup/memory/memory.limit_in_bytes" + cgroup_mem_usage_path = "/sys/fs/cgroup/memory/memory.usage_in_bytes" + + try: + with open(cgroup_mem_limit_path, "r") as f: + raw_limit = f.read().strip() + if raw_limit == "max": + fallback_to_host_available = True + mem_limit = psutil.virtual_memory().total + else: + mem_limit = int(raw_limit) + if mem_limit == 9223372036854771712: + fallback_to_host_available = True + mem_limit = psutil.virtual_memory().total + except (FileNotFoundError, ValueError): + fallback_to_host_available = True + mem_limit = psutil.virtual_memory().total + + if fallback_to_host_available: + return psutil.virtual_memory().available / (1024**3) + + try: + with open(cgroup_mem_usage_path, "r") as f: + mem_used = int(f.read().strip()) + except (FileNotFoundError, ValueError): + mem_used = psutil.virtual_memory().total - psutil.virtual_memory().available + + mem_available = max(0, mem_limit - mem_used) + return mem_available / (1024**3) + + if _ram_gb() > ram_headroom: + return + gc.collect() + if _ram_gb() > ram_headroom: + return clean_list = [] From 8bfa6958de1156677b735709a2a1571e8582eca0 Mon Sep 17 00:00:00 2001 From: chukk Date: Mon, 9 Mar 2026 16:24:13 +0800 Subject: [PATCH 3/3] Fix indentation for Docker memory detection --- comfy_execution/caching.py | 82 +++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/comfy_execution/caching.py b/comfy_execution/caching.py index bbfbd2ce8..8dfac7a07 100644 --- a/comfy_execution/caching.py +++ b/comfy_execution/caching.py @@ -383,47 +383,47 @@ class RAMPressureCache(LRUCache): return super().get(node_id) def poll(self, ram_headroom): - def _ram_gb(): - fallback_to_host_available = False - cgroup_mem_limit_path = "/sys/fs/cgroup/memory.max" - cgroup_mem_usage_path = "/sys/fs/cgroup/memory.current" - - if not os.path.exists(cgroup_mem_limit_path): - cgroup_mem_limit_path = "/sys/fs/cgroup/memory/memory.limit_in_bytes" - cgroup_mem_usage_path = "/sys/fs/cgroup/memory/memory.usage_in_bytes" - - try: - with open(cgroup_mem_limit_path, "r") as f: - raw_limit = f.read().strip() - if raw_limit == "max": - fallback_to_host_available = True - mem_limit = psutil.virtual_memory().total - else: - mem_limit = int(raw_limit) - if mem_limit == 9223372036854771712: - fallback_to_host_available = True - mem_limit = psutil.virtual_memory().total - except (FileNotFoundError, ValueError): - fallback_to_host_available = True - mem_limit = psutil.virtual_memory().total - - if fallback_to_host_available: - return psutil.virtual_memory().available / (1024**3) - - try: - with open(cgroup_mem_usage_path, "r") as f: - mem_used = int(f.read().strip()) - except (FileNotFoundError, ValueError): - mem_used = psutil.virtual_memory().total - psutil.virtual_memory().available - - mem_available = max(0, mem_limit - mem_used) - return mem_available / (1024**3) - - if _ram_gb() > ram_headroom: - return - gc.collect() - if _ram_gb() > ram_headroom: - return + def _ram_gb(): + fallback_to_host_available = False + cgroup_mem_limit_path = "/sys/fs/cgroup/memory.max" + cgroup_mem_usage_path = "/sys/fs/cgroup/memory.current" + + if not os.path.exists(cgroup_mem_limit_path): + cgroup_mem_limit_path = "/sys/fs/cgroup/memory/memory.limit_in_bytes" + cgroup_mem_usage_path = "/sys/fs/cgroup/memory/memory.usage_in_bytes" + + try: + with open(cgroup_mem_limit_path, "r") as f: + raw_limit = f.read().strip() + if raw_limit == "max": + fallback_to_host_available = True + mem_limit = psutil.virtual_memory().total + else: + mem_limit = int(raw_limit) + if mem_limit == 9223372036854771712: + fallback_to_host_available = True + mem_limit = psutil.virtual_memory().total + except (FileNotFoundError, ValueError): + fallback_to_host_available = True + mem_limit = psutil.virtual_memory().total + + if fallback_to_host_available: + return psutil.virtual_memory().available / (1024**3) + + try: + with open(cgroup_mem_usage_path, "r") as f: + mem_used = int(f.read().strip()) + except (FileNotFoundError, ValueError): + mem_used = psutil.virtual_memory().total - psutil.virtual_memory().available + + mem_available = max(0, mem_limit - mem_used) + return mem_available / (1024**3) + + if _ram_gb() > ram_headroom: + return + gc.collect() + if _ram_gb() > ram_headroom: + return clean_list = []