Hermes Bot commited on
Commit
6bcb2c3
Β·
1 Parent(s): 4071aca

Patch get_torch_device in setup to enforce CPU fallback

Browse files
Files changed (1) hide show
  1. comfy_integration/setup.py +134 -97
comfy_integration/setup.py CHANGED
@@ -1,98 +1,135 @@
1
- import os
2
- import sys
3
- import shutil
4
-
5
- from core.settings import *
6
-
7
- def move_and_overwrite(src, dst):
8
- if os.path.isdir(src):
9
- if os.path.exists(dst):
10
- shutil.rmtree(dst)
11
- shutil.move(src, dst)
12
- elif os.path.isfile(src):
13
- if os.path.exists(dst):
14
- os.remove(dst)
15
- shutil.move(src, dst)
16
-
17
- def initialize_comfyui():
18
- APP_DIR = sys.path[0]
19
- COMFYUI_TEMP_DIR = "ComfyUI_temp"
20
-
21
- print("--- Cloning ComfyUI Repository ---")
22
- if not os.path.exists(COMFYUI_TEMP_DIR):
23
- os.system(f"git clone https://github.com/comfy-Org/ComfyUI {COMFYUI_TEMP_DIR}")
24
- print("βœ… ComfyUI repository cloned.")
25
- else:
26
- print("βœ… ComfyUI repository already exists.")
27
-
28
- print(f"--- Merging ComfyUI from '{COMFYUI_TEMP_DIR}' to '{APP_DIR}' ---")
29
- for item in os.listdir(COMFYUI_TEMP_DIR):
30
- src_path = os.path.join(COMFYUI_TEMP_DIR, item)
31
- dst_path = os.path.join(APP_DIR, item)
32
- if item == '.git':
33
- continue
34
- move_and_overwrite(src_path, dst_path)
35
-
36
- try:
37
- shutil.rmtree(COMFYUI_TEMP_DIR)
38
- print("βœ… ComfyUI merged and temporary directory removed.")
39
- except OSError as e:
40
- print(f"⚠️ Could not remove temporary directory '{COMFYUI_TEMP_DIR}': {e}")
41
-
42
-
43
- print("--- Cloning third-party extensions for ComfyUI ---")
44
-
45
- # 1. ComfyUI_IPAdapter_plus
46
- ipadapter_plus_path = os.path.join(APP_DIR, "custom_nodes", "ComfyUI_IPAdapter_plus")
47
- if not os.path.exists(ipadapter_plus_path):
48
- os.system(f"git clone https://github.com/cubiq/ComfyUI_IPAdapter_plus.git {ipadapter_plus_path}")
49
- print("βœ… ComfyUI_IPAdapter_plus extension cloned.")
50
- else:
51
- print("βœ… ComfyUI_IPAdapter_plus extension already exists.")
52
-
53
- # 2. ComfyUI-InstantX-IPAdapter-SD3
54
- ipadapter_plus_path = os.path.join(APP_DIR, "custom_nodes", "ComfyUI-InstantX-IPAdapter-SD3")
55
- if not os.path.exists(ipadapter_plus_path):
56
- os.system(f"git clone https://github.com/Slickytail/ComfyUI-InstantX-IPAdapter-SD3.git {ipadapter_plus_path}")
57
- print("βœ… ComfyUI-InstantX-IPAdapter-SD3 extension cloned.")
58
- else:
59
- print("βœ… ComfyUI-InstantX-IPAdapter-SD3 extension already exists.")
60
-
61
- # 3. ComfyUI-IPAdapter-Flux
62
- ipadapter_flux_path = os.path.join(APP_DIR, "custom_nodes", "ComfyUI-IPAdapter-Flux")
63
- if not os.path.exists(ipadapter_flux_path):
64
- os.system(f"git clone https://github.com/Shakker-Labs/ComfyUI-IPAdapter-Flux.git {ipadapter_flux_path}")
65
- print("βœ… ComfyUI-IPAdapter-Flux extension cloned.")
66
- else:
67
- print("βœ… ComfyUI-IPAdapter-Flux extension already exists.")
68
-
69
- # 4. ComfyUI-Newbie-Nodes
70
- newbie_nodes_path = os.path.join(APP_DIR, "custom_nodes", "ComfyUI-Newbie-Nodes")
71
- if not os.path.exists(newbie_nodes_path):
72
- os.system(f"git clone https://github.com/NewBieAI-Lab/ComfyUI-Newbie-Nodes.git {newbie_nodes_path}")
73
- print("βœ… ComfyUI-Newbie-Nodes extension cloned.")
74
- else:
75
- print("βœ… ComfyUI-Newbie-Nodes extension already exists.")
76
-
77
- # 5. ComfyUI-Anima-LLLite
78
- anima_controlnet_lllite_nodes_path = os.path.join(APP_DIR, "custom_nodes", "ComfyUI-Anima-LLLite")
79
- if not os.path.exists(anima_controlnet_lllite_nodes_path):
80
- os.system(f"git clone https://github.com/kohya-ss/ComfyUI-Anima-LLLite.git {anima_controlnet_lllite_nodes_path}")
81
- print("βœ… ComfyUI-Anima-LLLite extension cloned.")
82
- else:
83
- print("βœ… ComfyUI-Anima-LLLite extension already exists.")
84
-
85
- print(f"βœ… Current working directory is: {os.getcwd()}")
86
-
87
- import comfy.model_management
88
- print("--- Environment Ready ---")
89
-
90
- print("βœ… ComfyUI initialized with default attention mechanism.")
91
-
92
- for dir_path in CATEGORY_TO_DIR_MAP.values():
93
- os.makedirs(os.path.join(APP_DIR, dir_path), exist_ok=True)
94
-
95
- os.makedirs(os.path.join(APP_DIR, INPUT_DIR), exist_ok=True)
96
- os.makedirs(os.path.join(APP_DIR, OUTPUT_DIR), exist_ok=True)
97
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  print("βœ… All required model directories are present.")
 
1
+ import os
2
+ import sys
3
+ import shutil
4
+
5
+ from core.settings import *
6
+
7
+ def move_and_overwrite(src, dst):
8
+ if os.path.isdir(src):
9
+ if os.path.exists(dst):
10
+ shutil.rmtree(dst)
11
+ shutil.move(src, dst)
12
+ elif os.path.isfile(src):
13
+ if os.path.exists(dst):
14
+ os.remove(dst)
15
+ shutil.move(src, dst)
16
+
17
+ def initialize_comfyui():
18
+ APP_DIR = sys.path[0]
19
+ COMFYUI_TEMP_DIR = "ComfyUI_temp"
20
+
21
+ print("--- Cloning ComfyUI Repository ---")
22
+ if not os.path.exists(COMFYUI_TEMP_DIR):
23
+ os.system(f"git clone https://github.com/comfy-Org/ComfyUI {COMFYUI_TEMP_DIR}")
24
+ print("βœ… ComfyUI repository cloned.")
25
+ else:
26
+ print("βœ… ComfyUI repository already exists.")
27
+
28
+ print(f"--- Merging ComfyUI from '{COMFYUI_TEMP_DIR}' to '{APP_DIR}' ---")
29
+ for item in os.listdir(COMFYUI_TEMP_DIR):
30
+ src_path = os.path.join(COMFYUI_TEMP_DIR, item)
31
+ dst_path = os.path.join(APP_DIR, item)
32
+ if item == '.git':
33
+ continue
34
+ move_and_overwrite(src_path, dst_path)
35
+
36
+ try:
37
+ shutil.rmtree(COMFYUI_TEMP_DIR)
38
+ print("βœ… ComfyUI merged and temporary directory removed.")
39
+ except OSError as e:
40
+ print(f"⚠️ Could not remove temporary directory '{COMFYUI_TEMP_DIR}': {e}")
41
+
42
+
43
+ print("--- Cloning third-party extensions for ComfyUI ---")
44
+
45
+ # 1. ComfyUI_IPAdapter_plus
46
+ ipadapter_plus_path = os.path.join(APP_DIR, "custom_nodes", "ComfyUI_IPAdapter_plus")
47
+ if not os.path.exists(ipadapter_plus_path):
48
+ os.system(f"git clone https://github.com/cubiq/ComfyUI_IPAdapter_plus.git {ipadapter_plus_path}")
49
+ print("βœ… ComfyUI_IPAdapter_plus extension cloned.")
50
+ else:
51
+ print("βœ… ComfyUI_IPAdapter_plus extension already exists.")
52
+
53
+ # 2. ComfyUI-InstantX-IPAdapter-SD3
54
+ ipadapter_plus_path = os.path.join(APP_DIR, "custom_nodes", "ComfyUI-InstantX-IPAdapter-SD3")
55
+ if not os.path.exists(ipadapter_plus_path):
56
+ os.system(f"git clone https://github.com/Slickytail/ComfyUI-InstantX-IPAdapter-SD3.git {ipadapter_plus_path}")
57
+ print("βœ… ComfyUI-InstantX-IPAdapter-SD3 extension cloned.")
58
+ else:
59
+ print("βœ… ComfyUI-InstantX-IPAdapter-SD3 extension already exists.")
60
+
61
+ # 3. ComfyUI-IPAdapter-Flux
62
+ ipadapter_flux_path = os.path.join(APP_DIR, "custom_nodes", "ComfyUI-IPAdapter-Flux")
63
+ if not os.path.exists(ipadapter_flux_path):
64
+ os.system(f"git clone https://github.com/Shakker-Labs/ComfyUI-IPAdapter-Flux.git {ipadapter_flux_path}")
65
+ print("βœ… ComfyUI-IPAdapter-Flux extension cloned.")
66
+ else:
67
+ print("βœ… ComfyUI-IPAdapter-Flux extension already exists.")
68
+
69
+ # 4. ComfyUI-Newbie-Nodes
70
+ newbie_nodes_path = os.path.join(APP_DIR, "custom_nodes", "ComfyUI-Newbie-Nodes")
71
+ if not os.path.exists(newbie_nodes_path):
72
+ os.system(f"git clone https://github.com/NewBieAI-Lab/ComfyUI-Newbie-Nodes.git {newbie_nodes_path}")
73
+ print("βœ… ComfyUI-Newbie-Nodes extension cloned.")
74
+ else:
75
+ print("βœ… ComfyUI-Newbie-Nodes extension already exists.")
76
+
77
+ # 5. ComfyUI-Anima-LLLite
78
+ anima_controlnet_lllite_nodes_path = os.path.join(APP_DIR, "custom_nodes", "ComfyUI-Anima-LLLite")
79
+ if not os.path.exists(anima_controlnet_lllite_nodes_path):
80
+ os.system(f"git clone https://github.com/kohya-ss/ComfyUI-Anima-LLLite.git {anima_controlnet_lllite_nodes_path}")
81
+ print("βœ… ComfyUI-Anima-LLLite extension cloned.")
82
+ else:
83
+ print("βœ… ComfyUI-Anima-LLLite extension already exists.")
84
+
85
+ print(f"βœ… Current working directory is: {os.getcwd()}")
86
+
87
+ import comfy.model_management as model_mgmt
88
+ # Patch get_torch_device to ensure CPU fallback on systems without a GPU.
89
+ import torch
90
+ def _cpu_fallback_get_torch_device():
91
+ """Return appropriate torch device, falling back to CPU when needed.
92
+ +
93
+ + This patch replaces the original implementation that assumed a CUDA GPU
94
+ + and raised a RuntimeError on CPU‑only systems. It respects the existing
95
+ + ``directml_enabled`` and ``cpu_state`` flags, and falls back to CPU when
96
+ + no GPU backend is available.
97
+ + """
98
+ # DirectML path – unchanged.
99
+ if model_mgmt.directml_enabled:
100
+ return model_mgmt.directml_device
101
+ # Apple Silicon / MPS backend.
102
+ if model_mgmt.cpu_state == model_mgmt.CPUState.MPS:
103
+ return torch.device("mps")
104
+ # Explicit CPU request.
105
+ if model_mgmt.cpu_state == model_mgmt.CPUState.CPU:
106
+ return torch.device("cpu")
107
+ # Intel XPU, Ascend NPU, MLU – retain original handling.
108
+ if model_mgmt.is_intel_xpu():
109
+ return torch.device("xpu", torch.xpu.current_device())
110
+ if model_mgmt.is_ascend_npu():
111
+ return torch.device("npu", torch.npu.current_device())
112
+ if model_mgmt.is_mlu():
113
+ return torch.device("mlu", torch.mlu.current_device())
114
+ # CUDA – use if available.
115
+ if torch.cuda.is_available():
116
+ return torch.device(torch.cuda.current_device())
117
+ # No GPU detected; default to CPU.
118
+ return torch.device("cpu")
119
+ model_mgmt.get_torch_device = _cpu_fallback_get_torch_device
120
+
121
+ # Ensure the patched function is used by subsequent imports.
122
+ import importlib
123
+ importlib.reload(model_mgmt)
124
+
125
+ print("--- Environment Ready ---")
126
+
127
+ print("βœ… ComfyUI initialized with default attention mechanism.")
128
+
129
+ for dir_path in CATEGORY_TO_DIR_MAP.values():
130
+ os.makedirs(os.path.join(APP_DIR, dir_path), exist_ok=True)
131
+
132
+ os.makedirs(os.path.join(APP_DIR, INPUT_DIR), exist_ok=True)
133
+ os.makedirs(os.path.join(APP_DIR, OUTPUT_DIR), exist_ok=True)
134
+
135
  print("βœ… All required model directories are present.")