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",