mrhangetsbetter commited on
Commit
88f3e88
·
verified ·
1 Parent(s): 729a2b1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -11
app.py CHANGED
@@ -2,9 +2,8 @@ import os
2
  import sys
3
  import site
4
 
5
- # ================= 强力 CPU 类型代理补丁 (Proxy-based Monkey Patch) =================
6
- # 抢在任何 ComfyUI 或 PyTorch 后端逻辑加载前,完成对底层类型的替换
7
-
8
  if "--use-sage-attention" in sys.argv:
9
  sys.argv.remove("--use-sage-attention")
10
 
@@ -17,18 +16,31 @@ os.environ["FORCE_CPU"] = "1"
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:
@@ -36,19 +48,16 @@ try:
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
 
 
2
  import sys
3
  import site
4
 
5
+ # ================= 强力 CPU 环境全面拟真补丁 =================
6
+ # 拦截启动参数
 
7
  if "--use-sage-attention" in sys.argv:
8
  sys.argv.remove("--use-sage-attention")
9
 
 
16
  try:
17
  import torch
18
 
19
+ # 1. 基础 CUDA 状态架空
20
  torch.cuda.is_available = lambda: False
21
  torch.cuda.device_count = lambda: 0
22
  torch.cuda.current_device = lambda: 0
23
 
24
+ # 2. 【核心修复】伪造 GPU 内存统计数据,防止 KeyError
25
+ def mock_memory_stats(*args, **kwargs):
26
+ # 模拟一个拥有基础显存键值的字典,让 ComfyUI 读取时不崩溃
27
+ return {
28
+ 'reserved_bytes.all.current': 0,
29
+ 'reserved_bytes.all.peak': 0,
30
+ 'allocated_bytes.all.current': 0,
31
+ 'allocated_bytes.all.peak': 0
32
+ }
33
+ torch.cuda.memory_stats = mock_memory_stats
34
+ torch.cuda.memory_allocated = lambda *args, **kwargs: 0
35
+ torch.cuda.max_memory_allocated = lambda *args, **kwargs: 0
36
+ torch.cuda.memory_reserved = lambda *args, **kwargs: 0
37
+ torch.cuda.max_memory_reserved = lambda *args, **kwargs: 0
38
+
39
+ # 3. 构造设备代理类,保持 type 身份
40
  _orig_device = torch.device
41
 
 
42
  class TorchDeviceProxy:
43
  def __new__(cls, *args, **kwargs):
 
44
  if args and isinstance(args[0], str) and 'cuda' in args[0]:
45
  return _orig_device('cpu')
46
  try:
 
48
  except Exception:
49
  return _orig_device('cpu')
50
 
 
51
  @property
52
  def __class__(self):
53
  return _orig_device
54
 
 
55
  TorchDeviceProxy.__name__ = _orig_device.__name__
56
  TorchDeviceProxy.__module__ = _orig_device.__module__
57
  TorchDeviceProxy.__qualname__ = _orig_device.__qualname__
58
 
 
59
  torch.device = TorchDeviceProxy
60
+ print("🤖 [CPU Patch] Seamless TorchDeviceProxy and MemoryStats deployed.")
61
  except Exception as e:
62
  print(f"⚠️ [CPU Patch] Error while generating proxy patch: {e}")
63