From 5f65c5f8eda183b28b8e7940a7027084027290bd Mon Sep 17 00:00:00 2001 From: admin Date: Sat, 10 Feb 2024 03:29:17 +0800 Subject: [PATCH 1/5] [feat] upgrade --- comfy/model_management.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/comfy/model_management.py b/comfy/model_management.py index a8dc91b9e..282cfb911 100644 --- a/comfy/model_management.py +++ b/comfy/model_management.py @@ -4,6 +4,7 @@ from comfy.cli_args import args import comfy.utils import torch import sys +import os.path class VRAMState(Enum): DISABLED = 0 #No vram present: no need to move models to vram @@ -92,8 +93,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_torch = mem_total + if os.path.isfile('/sys/fs/cgroup/memory/memory.limit_in_bytes'): + with open('/sys/fs/cgroup/memory/memory.limit_in_bytes', 'r') as f: + mem_total = int(f.read()) + mem_total_torch = mem_total + else: + mem_total = psutil.virtual_memory().total + mem_total_torch = mem_total else: if directml_enabled: mem_total = 1024 * 1024 * 1024 #TODO @@ -650,8 +656,15 @@ def get_free_memory(dev=None, torch_free_too=False): dev = get_torch_device() if hasattr(dev, 'type') and (dev.type == 'cpu' or dev.type == 'mps'): - mem_free_total = psutil.virtual_memory().available - mem_free_torch = mem_free_total + if os.path.isfile('/sys/fs/cgroup/memory/memory.limit_in_bytes'): + with open('/sys/fs/cgroup/memory/memory.limit_in_bytes', 'r') as f: + mem_used_total = psutil.virtual_memory().used + mem_total = int(f.read()) + mem_free_total = mem_total - mem_used_total + mem_free_torch = mem_free_total + else: + mem_free_total = psutil.virtual_memory().available + mem_free_torch = mem_free_total else: if directml_enabled: mem_free_total = 1024 * 1024 * 1024 #TODO From eaf3e705562d6a4425dafbcf5ed07af9440fdf02 Mon Sep 17 00:00:00 2001 From: admin Date: Sat, 10 Feb 2024 03:40:45 +0800 Subject: [PATCH 2/5] [feat] get total_ram from get_total_memory --- comfy/model_management.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/comfy/model_management.py b/comfy/model_management.py index 282cfb911..3eeee67dc 100644 --- a/comfy/model_management.py +++ b/comfy/model_management.py @@ -122,7 +122,7 @@ def get_total_memory(dev=None, torch_total_too=False): return mem_total 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) print("Total VRAM {:0.0f} MB, total RAM {:0.0f} MB".format(total_vram, total_ram)) if not args.normalvram and not args.cpu: if lowvram_available and total_vram <= 4096: From 777ee90206dd38a656c8d00b104ffb36539c6208 Mon Sep 17 00:00:00 2001 From: admin Date: Mon, 12 Feb 2024 02:17:30 +0800 Subject: [PATCH 3/5] [feat] upgrade --- comfy/model_management.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/comfy/model_management.py b/comfy/model_management.py index 3eeee67dc..90cf83d84 100644 --- a/comfy/model_management.py +++ b/comfy/model_management.py @@ -93,10 +93,9 @@ 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'): - if os.path.isfile('/sys/fs/cgroup/memory/memory.limit_in_bytes'): - with open('/sys/fs/cgroup/memory/memory.limit_in_bytes', 'r') as f: - mem_total = int(f.read()) - mem_total_torch = mem_total + mem_total = get_containerd_memory_limit() + if mem_total > 0: + mem_total_torch = mem_total else: mem_total = psutil.virtual_memory().total mem_total_torch = mem_total @@ -656,12 +655,11 @@ def get_free_memory(dev=None, torch_free_too=False): dev = get_torch_device() if hasattr(dev, 'type') and (dev.type == 'cpu' or dev.type == 'mps'): - if os.path.isfile('/sys/fs/cgroup/memory/memory.limit_in_bytes'): - with open('/sys/fs/cgroup/memory/memory.limit_in_bytes', 'r') as f: - mem_used_total = psutil.virtual_memory().used - mem_total = int(f.read()) - mem_free_total = mem_total - mem_used_total - mem_free_torch = mem_free_total + mem_total = get_containerd_memory_limit() + if mem_total > 0: + mem_used_total = psutil.virtual_memory().used + mem_free_total = mem_total - mem_used_total + mem_free_torch = mem_free_total else: mem_free_total = psutil.virtual_memory().available mem_free_torch = mem_free_total @@ -709,6 +707,13 @@ def is_device_mps(device): return True return False +def get_containerd_memory_limit(): + cgroup_memory_limit = '/sys/fs/cgroup/memory/memory.limit_in_bytes' + if os.path.isfile(cgroup_memory_limit): + with open(cgroup_memory_limit, 'r') as f: + return int(f.read()) + return 0 + def should_use_fp16(device=None, model_params=0, prioritize_performance=True, manual_cast=False): global directml_enabled From 0215f8013f7d019a5b18f094f72dd9b072f8c66e Mon Sep 17 00:00:00 2001 From: admin Date: Mon, 12 Feb 2024 03:08:45 +0800 Subject: [PATCH 4/5] [feat] move func up --- comfy/model_management.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/comfy/model_management.py b/comfy/model_management.py index 90cf83d84..e278c1210 100644 --- a/comfy/model_management.py +++ b/comfy/model_management.py @@ -87,6 +87,13 @@ def get_torch_device(): else: return torch.device(torch.cuda.current_device()) +def get_containerd_memory_limit(): + cgroup_memory_limit = '/sys/fs/cgroup/memory/memory.limit_in_bytes' + if os.path.isfile(cgroup_memory_limit): + with open(cgroup_memory_limit, 'r') as f: + return int(f.read()) + return 0 + def get_total_memory(dev=None, torch_total_too=False): global directml_enabled if dev is None: @@ -707,12 +714,6 @@ def is_device_mps(device): return True return False -def get_containerd_memory_limit(): - cgroup_memory_limit = '/sys/fs/cgroup/memory/memory.limit_in_bytes' - if os.path.isfile(cgroup_memory_limit): - with open(cgroup_memory_limit, 'r') as f: - return int(f.read()) - return 0 def should_use_fp16(device=None, model_params=0, prioritize_performance=True, manual_cast=False): global directml_enabled From 55f9f76a56f2def46abf5370da142b3ccc06665e Mon Sep 17 00:00:00 2001 From: xuexuexue Date: Wed, 10 Jul 2024 00:59:00 +0800 Subject: [PATCH 5/5] [feat] get correct memory used in container --- comfy/model_management.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/comfy/model_management.py b/comfy/model_management.py index e278c1210..949f3f839 100644 --- a/comfy/model_management.py +++ b/comfy/model_management.py @@ -94,6 +94,13 @@ def get_containerd_memory_limit(): return int(f.read()) return 0 +def get_containerd_memory_used(): + cgroup_memory_used = '/sys/fs/cgroup/memory/memory.usage_in_bytes' + if os.path.isfile(cgroup_memory_used): + with open(cgroup_memory_used, 'r') as f: + return int(f.read()) + return 0 + def get_total_memory(dev=None, torch_total_too=False): global directml_enabled if dev is None: @@ -664,7 +671,7 @@ def get_free_memory(dev=None, torch_free_too=False): if hasattr(dev, 'type') and (dev.type == 'cpu' or dev.type == 'mps'): mem_total = get_containerd_memory_limit() if mem_total > 0: - mem_used_total = psutil.virtual_memory().used + mem_used_total = get_containerd_memory_used() mem_free_total = mem_total - mem_used_total mem_free_torch = mem_free_total else: