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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -49
app.py CHANGED
@@ -2,42 +2,58 @@ import os
2
  import sys
3
  import site
4
 
5
- # ================= 强力 CPU 猴子补丁 (Monkey Patch) =================
6
- # 必须导入任何 comfytorch 模块之执行!
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:
@@ -47,49 +63,19 @@ 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
 
@@ -98,7 +84,6 @@ def main():
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... ---")
@@ -117,6 +102,5 @@ def main():
117
  print("--- Launching Gradio Interface ---")
118
  demo.queue().launch(server_name="0.0.0.0", server_port=7860)
119
 
120
-
121
  if __name__ == "__main__":
122
  main()
 
2
  import sys
3
  import site
4
 
5
+ # ================= 强力 CPU 类型代理补丁 (Proxy-based Monkey Patch) =================
6
+ # 在任何 ComfyUIPyTorch 后端逻辑加载,完成对底层类型的替换
7
 
 
8
  if "--use-sage-attention" in sys.argv:
9
  sys.argv.remove("--use-sage-attention")
10
 
11
  if "--cpu" not in sys.argv:
12
  sys.argv.append("--cpu")
13
 
 
14
  os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
15
  os.environ["FORCE_CPU"] = "1"
16
 
 
17
  try:
18
  import torch
19
 
20
+ # 1. 彻底架空 CUDA 状态查询
21
  torch.cuda.is_available = lambda: False
22
  torch.cuda.device_count = lambda: 0
23
+ torch.cuda.current_device = lambda: 0
24
 
25
+ # 备份原始的 torch.device 类型(用于底层真正实例化)
26
+ _orig_device = torch.device
27
+
28
+ # 2. 构造一个完美的代理类,既是正统的 'type' 身份,又能动态重定向
29
+ class TorchDeviceProxy:
30
+ def __new__(cls, *args, **kwargs):
31
+ # 核心掉包逻辑:只要触碰 cuda,律无缝替换为 cpu
32
+ if args and isinstance(args[0], str) and 'cuda' in args[0]:
33
+ return _orig_device('cpu')
34
+ try:
35
+ return _orig_device(*args, **kwargs)
36
+ except Exception:
37
+ return _orig_device('cpu')
38
+
39
+ # 补全可能用到的类属性,使其在静态类型检查或 isinstance 检查时完美伪装
40
+ @property
41
+ def __class__(self):
42
+ return _orig_device
43
+
44
+ # 3. 将原类的关键标识和名称无缝同步给代理类,欺骗 `str | torch.device` 表达式
45
+ TorchDeviceProxy.__name__ = _orig_device.__name__
46
+ TorchDeviceProxy.__module__ = _orig_device.__module__
47
+ TorchDeviceProxy.__qualname__ = _orig_device.__qualname__
48
+
49
+ # 替换全局 torch.device
50
+ torch.device = TorchDeviceProxy
51
+ print("🤖 [CPU Patch] Successfully deployed seamless TorchDeviceProxy class.")
52
  except Exception as e:
53
+ print(f"⚠️ [CPU Patch] Error while generating proxy patch: {e}")
54
 
55
  # ====================================================================
56
 
 
57
  try:
58
  import spaces
59
  except ImportError:
 
63
  if APP_DIR not in sys.path:
64
  sys.path.insert(0, APP_DIR)
65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  def apply_sage_attention_patch():
67
  print("--- [Runtime Patch] 🤖 CPU mode. SageAttention disabled. ---")
68
  return "Skipped (CPU Mode)"
69
 
70
  def dummy_gpu_decorator(func):
71
+ return func
72
 
73
  @dummy_gpu_decorator
74
  def dummy_gpu_for_startup():
75
  print("--- [GPU Startup] Bypassed for CPU mode. ---")
76
  return "Bypassed"
77
 
 
78
  def main():
 
 
79
  from comfy_integration import setup as setup_comfyui
80
  from utils.app_utils import load_ipadapter_presets
81
 
 
84
  setup_comfyui.initialize_comfyui()
85
  except Exception as e:
86
  print(f"🚨 [Init Error] ComfyUI initialization failed: {e}")
 
87
  raise e
88
 
89
  print("--- [Setup] Reloading site-packages... ---")
 
102
  print("--- Launching Gradio Interface ---")
103
  demo.queue().launch(server_name="0.0.0.0", server_port=7860)
104
 
 
105
  if __name__ == "__main__":
106
  main()