-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmemory_manager.py
More file actions
190 lines (161 loc) · 6.87 KB
/
Copy pathmemory_manager.py
File metadata and controls
190 lines (161 loc) · 6.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
"""
Memory Manager nodes for DistorchMemoryManager
"""
import torch
import gc
# Safe import for pynvml (NVIDIA Management Library)
try:
import pynvml
try:
pynvml.nvmlInit()
_pynvml_available = True
except Exception as e:
_pynvml_available = False
pynvml = None
print(f"[ComfyUI-VRAM-Manager] WARNING: pynvml imported but NVML init failed: {e}")
except ImportError:
_pynvml_available = False
pynvml = None
print("[ComfyUI-VRAM-Manager] INFO: pynvml (nvidia-ml-py) not installed. general_manage_vram will be unavailable.")
def get_non_torch_vram_usage_bytes():
"""
Returns the amount of VRAM (in bytes) consumed by non-PyTorch processes.
This is the difference between system-wide GPU usage (via NVML) and
PyTorch's own reported usage.
Returns None if detection is not possible.
"""
if not _pynvml_available or pynvml is None:
return None
if not torch.cuda.is_available():
return None
try:
handle = pynvml.nvmlDeviceGetHandleByIndex(0)
nvml_info = pynvml.nvmlDeviceGetMemoryInfo(handle)
system_used = nvml_info.used # bytes, all processes
torch_free, torch_total = torch.cuda.mem_get_info()
torch_used = torch_total - torch_free # bytes, PyTorch only
non_torch = system_used - torch_used
return max(0, non_torch)
except Exception as e:
print(f"[ComfyUI-VRAM-Manager] Error detecting non-PyTorch VRAM usage: {e}")
return None
# AnyType mirrors the behavior of the original Purge VRAM node
class AnyType(str):
"""A special class that is always equal in not equal comparisons. Credit to pythongosssss"""
def __eq__(self, __value: object) -> bool:
return True
def __ne__(self, __value: object) -> bool:
return False
def __repr__(self):
return str(self)
any = AnyType("*")
# Helper used by several nodes to release memory
def clear_memory():
import gc
# Cleanup
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
torch.cuda.ipc_collect()
class MemoryManager:
"""
Advanced memory management node with fine-grained controls.
"""
@classmethod
def INPUT_TYPES(s):
return {"required": {
"anything": (any, {}),
"clean_gpu": ("BOOLEAN", {"default": True}),
"clean_cpu": ("BOOLEAN", {"default": False, "tooltip": "CPU memory cleanup (use with caution)"}),
"force_gc": ("BOOLEAN", {"default": True}),
"reset_virtual_memory": ("BOOLEAN", {"default": True}),
"restore_original_functions": ("BOOLEAN", {"default": False, "tooltip": "Restore original model_management functions"}),
}}
RETURN_TYPES = (any,)
RETURN_NAMES = ("any",)
FUNCTION = "manage_memory"
CATEGORY = "Memory"
def manage_memory(self, anything, clean_gpu, clean_cpu, force_gc, reset_virtual_memory, restore_original_functions):
try:
if clean_gpu and torch.cuda.is_available():
torch.cuda.empty_cache()
torch.cuda.synchronize()
print("GPU memory cleared")
if clean_cpu:
gc.collect()
print("CPU memory cleared")
if force_gc:
gc.collect()
print("Forced garbage collection completed")
if reset_virtual_memory:
try:
import comfy.model_management
if hasattr(comfy.model_management, 'free_memory'):
# Request a huge free so Comfy actually unloads / empties CUDA.
# free_memory(0, ...) is a no-op and left Krea2 NVFP4 ~9GB resident.
if torch.cuda.is_available():
try:
for di in range(torch.cuda.device_count()):
comfy.model_management.free_memory(
1e30, torch.device(f"cuda:{di}")
)
except Exception as e:
print(f"Virtual memory reset (CUDA) failed: {e}")
print("Virtual memory reset")
except Exception as e:
print(f"Virtual memory reset failed: {e}")
if restore_original_functions:
try:
import comfy.model_management
print("Original functions restored")
except Exception as e:
print(f"Function restoration failed: {e}")
print("Comprehensive memory management completed")
except Exception as e:
print(f"Memory management error: {e}")
return (anything,)
class SafeMemoryManager:
"""
Recommended memory management node that prioritizes safe cleanup.
"""
@classmethod
def INPUT_TYPES(s):
return {"required": {
"anything": (any, {}),
"clean_gpu": ("BOOLEAN", {"default": True}),
"force_gc": ("BOOLEAN", {"default": True}),
"reset_virtual_memory": ("BOOLEAN", {"default": True}),
}}
RETURN_TYPES = (any,)
RETURN_NAMES = ("any",)
FUNCTION = "safe_manage_memory"
CATEGORY = "Memory"
def safe_manage_memory(self, anything, clean_gpu, force_gc, reset_virtual_memory):
try:
if clean_gpu and torch.cuda.is_available():
torch.cuda.empty_cache()
torch.cuda.synchronize()
print("Safe GPU memory cleared")
if force_gc:
gc.collect()
print("Safe garbage collection completed")
if reset_virtual_memory:
try:
import comfy.model_management
if hasattr(comfy.model_management, 'free_memory'):
# free_memory(0, ...) is a no-op; request a huge free on all devices.
if torch.cuda.is_available():
try:
for di in range(torch.cuda.device_count()):
comfy.model_management.free_memory(
1e30, torch.device(f"cuda:{di}")
)
except Exception as e:
print(f"Safe virtual memory reset (CUDA) failed: {e}")
print("Safe virtual memory reset")
except Exception as e:
print(f"Safe virtual memory reset failed: {e}")
print("Safe memory management completed")
except Exception as e:
print(f"Safe memory management error: {e}")
return (anything,)