|
1 | 1 | """ |
2 | 2 | Memory Manager nodes for DistorchMemoryManager |
| 3 | +
|
| 4 | +Reserved VRAM auto-detection based on ComfyUI-ReservedVRAM by Windecay |
| 5 | +https://github.com/Windecay/ComfyUI-ReservedVRAM |
| 6 | +Original code licensed under Apache License 2.0 |
3 | 7 | """ |
4 | 8 | import torch |
5 | 9 | import gc |
6 | 10 |
|
| 11 | +# Safe import for pynvml (NVIDIA Management Library) |
| 12 | +try: |
| 13 | + import pynvml |
| 14 | + try: |
| 15 | + pynvml.nvmlInit() |
| 16 | + _pynvml_available = True |
| 17 | + except Exception as e: |
| 18 | + _pynvml_available = False |
| 19 | + pynvml = None |
| 20 | + print(f"[ComfyUI-VRAM-Manager] WARNING: pynvml imported but NVML init failed: {e}") |
| 21 | +except ImportError: |
| 22 | + _pynvml_available = False |
| 23 | + pynvml = None |
| 24 | + print("[ComfyUI-VRAM-Manager] INFO: pynvml (nvidia-ml-py) not installed. general_manage_vram will be unavailable.") |
| 25 | + |
| 26 | + |
| 27 | +def get_non_torch_vram_usage_bytes(): |
| 28 | + """ |
| 29 | + Returns the amount of VRAM (in bytes) consumed by non-PyTorch processes. |
| 30 | + This is the difference between system-wide GPU usage (via NVML) and |
| 31 | + PyTorch's own reported usage. |
| 32 | + Returns None if detection is not possible. |
| 33 | + """ |
| 34 | + if not _pynvml_available or pynvml is None: |
| 35 | + return None |
| 36 | + if not torch.cuda.is_available(): |
| 37 | + return None |
| 38 | + try: |
| 39 | + handle = pynvml.nvmlDeviceGetHandleByIndex(0) |
| 40 | + nvml_info = pynvml.nvmlDeviceGetMemoryInfo(handle) |
| 41 | + system_used = nvml_info.used # bytes, all processes |
| 42 | + |
| 43 | + torch_free, torch_total = torch.cuda.mem_get_info() |
| 44 | + torch_used = torch_total - torch_free # bytes, PyTorch only |
| 45 | + |
| 46 | + non_torch = system_used - torch_used |
| 47 | + return max(0, non_torch) |
| 48 | + except Exception as e: |
| 49 | + print(f"[ComfyUI-VRAM-Manager] Error detecting non-PyTorch VRAM usage: {e}") |
| 50 | + return None |
| 51 | + |
7 | 52 | # AnyType mirrors the behavior of the original Purge VRAM node |
8 | 53 | class AnyType(str): |
9 | 54 | """A special class that is always equal in not equal comparisons. Credit to pythongosssss""" |
@@ -38,14 +83,15 @@ def INPUT_TYPES(s): |
38 | 83 | "force_gc": ("BOOLEAN", {"default": True}), |
39 | 84 | "reset_virtual_memory": ("BOOLEAN", {"default": True}), |
40 | 85 | "restore_original_functions": ("BOOLEAN", {"default": False, "tooltip": "Restore original model_management functions"}), |
| 86 | + "general_manage_vram": ("BOOLEAN", {"default": False, "tooltip": "General VRAM management: auto-detect non-PyTorch VRAM usage (browsers, other apps) via NVML and adjust ComfyUI memory management accordingly. Requires nvidia-ml-py."}), |
41 | 87 | }} |
42 | 88 |
|
43 | 89 | RETURN_TYPES = (any,) |
44 | 90 | RETURN_NAMES = ("any",) |
45 | 91 | FUNCTION = "manage_memory" |
46 | 92 | CATEGORY = "Memory" |
47 | 93 |
|
48 | | - def manage_memory(self, anything, clean_gpu, clean_cpu, force_gc, reset_virtual_memory, restore_original_functions): |
| 94 | + def manage_memory(self, anything, clean_gpu, clean_cpu, force_gc, reset_virtual_memory, restore_original_functions, general_manage_vram=False): |
49 | 95 | try: |
50 | 96 | if clean_gpu and torch.cuda.is_available(): |
51 | 97 | torch.cuda.empty_cache() |
@@ -81,6 +127,17 @@ def manage_memory(self, anything, clean_gpu, clean_cpu, force_gc, reset_virtual_ |
81 | 127 | except Exception as e: |
82 | 128 | print(f"Function restoration failed: {e}") |
83 | 129 |
|
| 130 | + # Auto-detect non-PyTorch VRAM usage and adjust ComfyUI memory management |
| 131 | + if general_manage_vram: |
| 132 | + non_torch = get_non_torch_vram_usage_bytes() |
| 133 | + if non_torch is not None: |
| 134 | + import comfy.model_management as mm |
| 135 | + mm.EXTRA_RESERVED_VRAM = non_torch |
| 136 | + non_torch_gb = non_torch / (1024 * 1024 * 1024) |
| 137 | + print(f"[ComfyUI-VRAM-Manager] Detected {non_torch_gb:.2f} GB non-PyTorch VRAM usage; adjusted memory management accordingly") |
| 138 | + else: |
| 139 | + print("[ComfyUI-VRAM-Manager] general_manage_vram: Could not detect non-PyTorch VRAM usage (pynvml unavailable or error)") |
| 140 | + |
84 | 141 | print("Comprehensive memory management completed") |
85 | 142 |
|
86 | 143 | except Exception as e: |
|
0 commit comments