Hermes Bot commited on
Commit
e327a4a
·
1 Parent(s): 4734897

Remove stray '+' characters and clean setup.py

Browse files
Files changed (1) hide show
  1. comfy_integration/setup.py +72 -72
comfy_integration/setup.py CHANGED
@@ -88,78 +88,78 @@ def initialize_comfyui():
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
 
 
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