Hermes Bot commited on
Commit
35cd9cd
·
1 Parent(s): 6ec371b

Patch external ComfyUI model_management for CPU fallback before import

Browse files
comfy_integration/setup.py CHANGED
@@ -86,43 +86,80 @@ def initialize_comfyui():
86
  # Ensure torch treats CUDA as unavailable on CPU‑only environment.
87
  import torch
88
  torch.cuda.is_available = lambda: False
89
-
90
- import comfy.model_management as model_mgmt
91
- # Patch get_torch_device to enforce CPU fallback.
92
- def _cpu_fallback_get_torch_device():
93
- """Return appropriate torch device, falling back to CPU when needed.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  +
95
  + This patch replaces the original implementation that assumed a CUDA GPU
96
  + and raised a RuntimeError on CPU‑only systems. It respects the existing
97
  + ``directml_enabled`` and ``cpu_state`` flags, and falls back to CPU when
98
  + no GPU backend is available.
99
  + """
100
- # DirectML path – unchanged.
101
- if model_mgmt.directml_enabled:
102
- return model_mgmt.directml_device
103
- # Apple Silicon / MPS backend.
104
- if model_mgmt.cpu_state == model_mgmt.CPUState.MPS:
105
- return torch.device("mps")
106
- # Explicit CPU request.
107
- if model_mgmt.cpu_state == model_mgmt.CPUState.CPU:
108
- return torch.device("cpu")
109
- # Intel XPU, Ascend NPU, MLU – retain original handling.
110
- if model_mgmt.is_intel_xpu():
111
- return torch.device("xpu", torch.xpu.current_device())
112
- if model_mgmt.is_ascend_npu():
113
- return torch.device("npu", torch.npu.current_device())
114
- if model_mgmt.is_mlu():
115
- return torch.device("mlu", torch.mlu.current_device())
116
- # CUDA – use if available (will be False due to monkey‑patch).
117
- if torch.cuda.is_available():
118
- return torch.device(torch.cuda.current_device())
119
- # Default to CPU.
120
- return torch.device("cpu")
121
- model_mgmt.get_torch_device = _cpu_fallback_get_torch_device
122
- import importlib
123
- importlib.reload(model_mgmt)
124
-
125
- print("--- Environment Ready ---")
126
 
127
  print("✅ ComfyUI initialized with default attention mechanism.")
128
 
 
86
  # Ensure torch treats CUDA as unavailable on CPU‑only environment.
87
  import torch
88
  torch.cuda.is_available = lambda: False
89
+ +
90
+ + # Apply patch to the external ComfyUI model_management before import.
91
+ + external_model_mgmt_path = os.path.join(APP_DIR, "comfy", "model_management.py")
92
+ + if os.path.exists(external_model_mgmt_path):
93
+ + try:
94
+ + with open(external_model_mgmt_path, "r", encoding="utf-8") as f:
95
+ + content = f.read()
96
+ + start = content.find("def get_torch_device():")
97
+ + if start != -1:
98
+ + # Find the end of the function (next def or end of file)
99
+ + end = content.find("\ndef ", start + 1)
100
+ + if end == -1:
101
+ + end = len(content)
102
+ + new_func = """def get_torch_device():
103
+ + \"\"\"Return appropriate torch device, falling back to CPU when needed.\"\"\"
104
+ + global directml_enabled, cpu_state
105
+ + if directml_enabled:
106
+ + return directml_device
107
+ + if cpu_state == CPUState.MPS:
108
+ + return torch.device(\"mps\")
109
+ + if cpu_state == CPUState.CPU:
110
+ + return torch.device(\"cpu\")
111
+ + if is_intel_xpu():
112
+ + return torch.device(\"xpu\", torch.xpu.current_device())
113
+ + if is_ascend_npu():
114
+ + return torch.device(\"npu\", torch.npu.current_device())
115
+ + if is_mlu():
116
+ + return torch.device(\"mlu\", torch.mlu.current_device())
117
+ + if torch.cuda.is_available():
118
+ + return torch.device(torch.cuda.current_device())
119
+ + return torch.device(\"cpu\")
120
+ +"""
121
+ + patched_content = content[:start] + new_func + content[end:]
122
+ + with open(external_model_mgmt_path, "w", encoding="utf-8") as f:
123
+ + f.write(patched_content)
124
+ + except Exception as e:
125
+ + print(f"⚠️ Failed to patch external model_management: {e}")
126
+ +
127
+ + import comfy.model_management as model_mgmt
128
+ + # Patch get_torch_device to enforce CPU fallback.
129
+ + def _cpu_fallback_get_torch_device():
130
+ + """Return appropriate torch device, falling back to CPU when needed.
131
  +
132
  + This patch replaces the original implementation that assumed a CUDA GPU
133
  + and raised a RuntimeError on CPU‑only systems. It respects the existing
134
  + ``directml_enabled`` and ``cpu_state`` flags, and falls back to CPU when
135
  + no GPU backend is available.
136
  + """
137
+ + # DirectML path – unchanged.
138
+ + if model_mgmt.directml_enabled:
139
+ + return model_mgmt.directml_device
140
+ + # Apple Silicon / MPS backend.
141
+ + if model_mgmt.cpu_state == model_mgmt.CPUState.MPS:
142
+ + return torch.device("mps")
143
+ + # Explicit CPU request.
144
+ + if model_mgmt.cpu_state == model_mgmt.CPUState.CPU:
145
+ + return torch.device("cpu")
146
+ + # Intel XPU, Ascend NPU, MLU – retain original handling.
147
+ + if model_mgmt.is_intel_xpu():
148
+ + return torch.device("xpu", torch.xpu.current_device())
149
+ + if model_mgmt.is_ascend_npu():
150
+ + return torch.device("npu", torch.npu.current_device())
151
+ + if model_mgmt.is_mlu():
152
+ + return torch.device("mlu", torch.mlu.current_device())
153
+ + # CUDA – use if available (will be False due to monkey‑patch).
154
+ + if torch.cuda.is_available():
155
+ + return torch.device(torch.cuda.current_device())
156
+ + # Default to CPU.
157
+ + return torch.device("cpu")
158
+ + model_mgmt.get_torch_device = _cpu_fallback_get_torch_device
159
+ + import importlib
160
+ + importlib.reload(model_mgmt)
161
+ +
162
+ + print("--- Environment Ready ---")
163
 
164
  print("✅ ComfyUI initialized with default attention mechanism.")
165
 
core/__pycache__/settings.cpython-311.pyc CHANGED
Binary files a/core/__pycache__/settings.cpython-311.pyc and b/core/__pycache__/settings.cpython-311.pyc differ