-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmemory_manager.py
More file actions
140 lines (118 loc) · 4.99 KB
/
memory_manager.py
File metadata and controls
140 lines (118 loc) · 4.99 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
"""
Memory Manager nodes for DistorchMemoryManager
"""
import torch
import gc
# 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'):
# Only free CUDA memory, skip CPU as it may cause errors
if torch.cuda.is_available():
try:
comfy.model_management.free_memory(0, 'cuda:0')
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'):
# Only free CUDA memory, skip CPU as it may cause errors
if torch.cuda.is_available():
try:
comfy.model_management.free_memory(0, 'cuda:0')
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,)