mrhangetsbetter commited on
Commit
4bbc5ee
·
verified ·
1 Parent(s): f6afb9a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -54
app.py CHANGED
@@ -2,21 +2,42 @@ import os
2
  import sys
3
  import site
4
 
5
- # ================= CPU 兼容性补丁 =================
6
- # 1. 强制屏蔽 SageAttention(因为它是纯 GPU 算子,在 CPU 下必报错)
 
 
7
  if "--use-sage-attention" in sys.argv:
8
  sys.argv.remove("--use-sage-attention")
9
 
10
- # 2. 注入 ComfyUI 官方的 CPU 启动参数,防止其初始化时强制寻找 CUDA 驱动
11
  if "--cpu" not in sys.argv:
12
  sys.argv.append("--cpu")
13
- print("🤖 [CPU Mode] Injected '--cpu' into sys.argv to bypass GPU check.")
14
 
15
- # 3. 伪造环境变量,防止某些依赖库(如 PyTorch/Sage)在导入时去初始化 CUDA
16
- os.environ["CUDA_VISIBLE_DEVICES"] = ""
17
- # =================================================
18
 
19
- # 尝试导入 spaces(保持原样,防止其他地方有依赖,但 CPU 模式下它不会起作用)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  try:
21
  import spaces
22
  except ImportError:
@@ -25,79 +46,74 @@ except ImportError:
25
  APP_DIR = os.path.dirname(os.path.abspath(__file__))
26
  if APP_DIR not in sys.path:
27
  sys.path.insert(0, APP_DIR)
28
- print(f"✅ Added project root '{APP_DIR}' to sys.path.")
29
 
30
- SAGE_PATCH_APPLIED = False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
  def apply_sage_attention_patch():
33
- # CPU 模式下,直接跳过 SageAttention 补丁
34
- print("--- [Runtime Patch] 🤖 CPU mode detected. Skipping SageAttention patch. ---")
35
  return "Skipped (CPU Mode)"
36
 
37
- # 修改装饰器:如果 spaces 存在则保留,否则就是普通函数
38
  def dummy_gpu_decorator(func):
39
- if spaces and hasattr(spaces, "GPU"):
40
- return spaces.GPU(func)
41
- return func
42
 
43
  @dummy_gpu_decorator
44
  def dummy_gpu_for_startup():
45
- try:
46
- print("--- [GPU Startup] Dummy function for startup check initiated. ---")
47
- patch_result = apply_sage_attention_patch()
48
- print(f"--- [GPU Startup] {patch_result} ---")
49
- print("--- [GPU Startup] Startup check passed. ---")
50
- return "Startup check passed."
51
- except BaseException as e:
52
- err_msg = str(e)
53
- if "uncorrectable ECC error" in err_msg or "cudaErrorECCUncorrectable" in err_msg:
54
- print("\n" + "="*80)
55
- print(f"🚨 [Fatal GPU Error] Captured uncorrectable ECC error during inference: {err_msg}")
56
- print("🚨 Terminating process to trigger an automatic container restart...")
57
- print("="*80 + "\n")
58
- os._exit(1)
59
- raise e
60
 
61
 
62
  def main():
 
 
63
  from comfy_integration import setup as setup_comfyui
64
  from utils.app_utils import load_ipadapter_presets
65
 
66
  print("--- [Setup] Starting ComfyUI initialization ---")
67
- setup_comfyui.initialize_comfyui()
68
-
69
- print("--- [Setup] Applying SageAttention Runtime Patch ---")
70
- patch_result = apply_sage_attention_patch()
71
- print(f"--- [Setup] {patch_result} ---")
72
-
73
- print("--- [Setup] Reloading site-packages to detect newly installed packages... ---")
74
  try:
75
- site.main()
76
- print("--- [Setup] ✅ Site-packages reloaded. ---")
77
  except Exception as e:
78
- print(f"--- [Setup] ⚠️ Warning: Could not fully reload site-packages: {e} ---")
79
-
80
- print("--- Initiating GPU Startup Check & SageAttention Patch Verification ---")
 
 
81
  try:
82
- dummy_gpu_for_startup()
83
- except Exception as e:
84
- print(f"--- [GPU Startup] ⚠️ Warning: Startup check failed: {e} ---")
85
 
86
  print("--- Starting Application Setup ---")
87
-
88
- print("--- Loading IPAdapter presets ---")
89
  load_ipadapter_presets()
90
- print("--- ✅ IPAdapter setup complete. ---")
91
 
92
-
93
- print("--- Environment configured. Proceeding with module imports. ---")
94
  from ui.layout import build_ui
95
  from ui.events import attach_event_handlers
96
 
97
- print(f"✅ Working directory is stable: {os.getcwd()}")
98
-
99
  demo = build_ui(attach_event_handlers)
100
-
101
  print("--- Launching Gradio Interface ---")
102
  demo.queue().launch(server_name="0.0.0.0", server_port=7860)
103
 
 
2
  import sys
3
  import site
4
 
5
+ # ================= 强力 CPU 猴子补丁 (Monkey Patch) =================
6
+ # 必须在导入任何 comfy torch 模块之前执行!
7
+
8
+ # 1. 强行清理和重写启动参数
9
  if "--use-sage-attention" in sys.argv:
10
  sys.argv.remove("--use-sage-attention")
11
 
 
12
  if "--cpu" not in sys.argv:
13
  sys.argv.append("--cpu")
 
14
 
15
+ # 2. 伪造 CUDA 环境变量
16
+ os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
17
+ os.environ["FORCE_CPU"] = "1"
18
 
19
+ # 3. 拦截并重写 torch.cuda 的核心方法,彻底废除其硬件检测能力
20
+ try:
21
+ import torch
22
+
23
+ # 强制让 torch 认为 cuda 不可用
24
+ torch.cuda.is_available = lambda: False
25
+ torch.cuda.device_count = lambda: 0
26
+
27
+ # 劫持 current_device 和 get_device_properties,让其不触发底层 C++ 的 _cuda_init()
28
+ def mock_current_device():
29
+ raise AssertionError("CUDA is not available in CPU-only mode.")
30
+ torch.cuda.current_device = mock_current_device
31
+
32
+ # 关键:直接重写 ComfyUI 报错的那一行代码所依赖的底层逻辑
33
+ # 模拟一个假的设备对象或直接让 get_torch_device 返回 cpu
34
+ print("🤖 [CPU Patch] Successfully mocked torch.cuda to prevent driver crash.")
35
+ except Exception as e:
36
+ print(f"⚠️ [CPU Patch] Failed to pre-patch torch: {e}")
37
+
38
+ # ====================================================================
39
+
40
+ # 尝试导入 spaces(ZeroGPU 专用,CPU 环境下保留即可)
41
  try:
42
  import spaces
43
  except ImportError:
 
46
  APP_DIR = os.path.dirname(os.path.abspath(__file__))
47
  if APP_DIR not in sys.path:
48
  sys.path.insert(0, APP_DIR)
 
49
 
50
+ # 动态劫持即将被导入的 comfy.model_management 的硬件获取函数
51
+ # 这是一个更激进的拦截:直接干涉 Python 的模块系统
52
+ class ComfyManagementHacker:
53
+ def __init__(self):
54
+ self.patched = False
55
+
56
+ def find_spec(self, fullname, path, target=None):
57
+ # 当系统尝试导入 comfy.model_management 时,我们提前介入
58
+ if fullname == "comfy.model_management" and not self.patched:
59
+ self.patched = True
60
+ print("🎯 [Interceptor] Caught comfy.model_management loading! Injecting CPU device detour...")
61
+ # 提前把 torch.cuda 再次阉割
62
+ import torch
63
+ torch.device = self.make_cpu_device_wrapper(torch.device)
64
+ return None
65
+
66
+ def make_cpu_device_wrapper(self, original_device):
67
+ def wrapped_device(*args, **kwargs):
68
+ # 任何时候如果尝试创建 cuda 设备,强行扭转为 cpu
69
+ if args and ('cuda' in str(args[0])):
70
+ return original_device('cpu')
71
+ return original_device(*args, **kwargs)
72
+ return wrapped_device
73
+
74
+ sys.meta_path.insert(0, ComfyManagementHacker())
75
+
76
 
77
  def apply_sage_attention_patch():
78
+ print("--- [Runtime Patch] 🤖 CPU mode. SageAttention disabled. ---")
 
79
  return "Skipped (CPU Mode)"
80
 
 
81
  def dummy_gpu_decorator(func):
82
+ return func # 完全废除 @spaces.GPU 带来的潜在干扰
 
 
83
 
84
  @dummy_gpu_decorator
85
  def dummy_gpu_for_startup():
86
+ print("--- [GPU Startup] Bypassed for CPU mode. ---")
87
+ return "Bypassed"
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
89
 
90
  def main():
91
+ # 在这里,当 setup_comfyui 内部导入 comfy.model_management 时,
92
+ # 我们的拦截器或上面的 torch 补丁会确保它不会去触发 C++ 的 _cuda_init()
93
  from comfy_integration import setup as setup_comfyui
94
  from utils.app_utils import load_ipadapter_presets
95
 
96
  print("--- [Setup] Starting ComfyUI initialization ---")
 
 
 
 
 
 
 
97
  try:
98
+ setup_comfyui.initialize_comfyui()
 
99
  except Exception as e:
100
+ print(f"🚨 [Init Error] ComfyUI initialization failed: {e}")
101
+ print("💡 If it still mentions 'RuntimeError: Found no NVIDIA driver', ComfyUI's specific script bypassed the patch. You may need a GPU instance.")
102
+ raise e
103
+
104
+ print("--- [Setup] Reloading site-packages... ---")
105
  try:
106
+ site.main()
107
+ except Exception:
108
+ pass
109
 
110
  print("--- Starting Application Setup ---")
 
 
111
  load_ipadapter_presets()
 
112
 
 
 
113
  from ui.layout import build_ui
114
  from ui.events import attach_event_handlers
115
 
 
 
116
  demo = build_ui(attach_event_handlers)
 
117
  print("--- Launching Gradio Interface ---")
118
  demo.queue().launch(server_name="0.0.0.0", server_port=7860)
119