X-iZhang commited on
Commit
842642b
·
verified ·
1 Parent(s): ab315c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -47
app.py CHANGED
@@ -1,15 +1,11 @@
1
  import os
2
  # Force CPU-only in this process by hiding CUDA devices (set before importing heavy libs)
3
- os.environ['CUDA_VISIBLE_DEVICES'] = ''
4
- os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
5
 
6
  import torch
7
  import gradio as gr
8
  import time
9
 
10
- # Force CPU device globally by overriding torch.cuda.is_available
11
- torch.cuda.is_available = lambda: False
12
-
13
  # =========================================
14
  # Safe Libra Hook (CPU fallback + dtype fix)
15
  # This hook must run before any heavyweight libra model-loading occurs.
@@ -23,16 +19,14 @@ _original_load_pretrained_model = getattr(builder, 'load_pretrained_model', None
23
  def safe_load_pretrained_model(model_path, model_base=None, model_name=None, **kwargs):
24
  print("[INFO] Hook activated: safe_load_pretrained_model()")
25
 
26
- # Complete model_name to avoid .lower() on None
27
  if model_name is None:
28
  model_name = model_path
29
 
30
- # Force CPU parameters when calling original function
31
  kwargs = dict(kwargs)
32
- kwargs['device'] = 'cpu'
33
- kwargs['device_map'] = 'cpu'
34
- kwargs.setdefault('torch_dtype', torch.float32)
35
- kwargs.setdefault('low_cpu_mem_usage', True)
36
 
37
  if _original_load_pretrained_model is None:
38
  raise RuntimeError('Original load_pretrained_model not found in builder')
@@ -56,31 +50,20 @@ def safe_load_pretrained_model(model_path, model_base=None, model_name=None, **k
56
  # propagate other errors
57
  raise
58
 
59
- # Force all model components to CPU with float32 for compatibility
60
- print('[INFO] Forcing all components to CPU with float32 dtype...')
61
- try:
62
- model = model.to(device='cpu', dtype=torch.float32)
63
- print('[INFO] Model moved to CPU (float32).')
64
- except Exception as e:
65
- print(f"[WARN] Could not move model to cpu/float32: {e}")
66
-
67
- try:
68
- if hasattr(model, 'get_vision_tower'):
69
  vt = model.get_vision_tower()
70
- if vt is not None:
71
- vt = vt.to(device='cpu', dtype=torch.float32)
72
- print('[INFO] Vision tower moved to CPU (float32).')
73
- except Exception as e:
74
- print(f"[WARN] Could not move vision_tower to cpu/float32: {e}")
75
-
76
- try:
77
- if hasattr(model, 'get_model'):
78
- inner_model = model.get_model()
79
- if inner_model is not None:
80
- inner_model = inner_model.to(device='cpu', dtype=torch.float32)
81
- print('[INFO] Inner model moved to CPU (float32).')
82
- except Exception as e:
83
- print(f"[WARN] Could not move inner model to cpu/float32: {e}")
84
 
85
  return tokenizer, model, image_processor, context_len
86
 
@@ -97,12 +80,7 @@ def safe_load_model(model_path, model_base=None, model_name=None):
97
 
98
  run_libra.load_model = safe_load_model
99
 
100
- # Now import CCD and hook ccd_utils to force CPU for expert models
101
- import ccd.ccd_utils as ccd_utils_module
102
- ccd_utils_module._DEVICE = torch.device('cpu')
103
- print('[INFO] Forced ccd_utils._DEVICE to CPU')
104
-
105
- # Now import the evaluation functions
106
  from ccd import ccd_eval, run_eval
107
  from libra.eval.run_libra import load_model
108
 
@@ -126,14 +104,13 @@ _loaded_models = {}
126
  # Environment Setup
127
  # =========================================
128
  def setup_environment():
129
- print("🔹 Running in CPU-only mode (forced for Hugging Face Spaces)")
 
 
 
130
  os.environ['TOKENIZERS_PARALLELISM'] = 'false'
131
  os.environ['TRANSFORMERS_CACHE'] = './cache'
132
-
133
- # Set number of threads for CPU inference
134
- num_threads = min(os.cpu_count() or 4, 8)
135
- torch.set_num_threads(num_threads)
136
- print(f"🔹 Using {num_threads} CPU threads")
137
 
138
 
139
  # =========================================
 
1
  import os
2
  # Force CPU-only in this process by hiding CUDA devices (set before importing heavy libs)
3
+ os.environ.setdefault('CUDA_VISIBLE_DEVICES', '')
 
4
 
5
  import torch
6
  import gradio as gr
7
  import time
8
 
 
 
 
9
  # =========================================
10
  # Safe Libra Hook (CPU fallback + dtype fix)
11
  # This hook must run before any heavyweight libra model-loading occurs.
 
19
  def safe_load_pretrained_model(model_path, model_base=None, model_name=None, **kwargs):
20
  print("[INFO] Hook activated: safe_load_pretrained_model()")
21
 
22
+ # 补全 model_name,避免 .lower() on None
23
  if model_name is None:
24
  model_name = model_path
25
 
26
+ # 强制以 CPU 参数调用原函数,尽量避免 CUDA 初始化
27
  kwargs = dict(kwargs)
28
+ kwargs.setdefault('device', 'cpu')
29
+ kwargs.setdefault('device_map', 'cpu')
 
 
30
 
31
  if _original_load_pretrained_model is None:
32
  raise RuntimeError('Original load_pretrained_model not found in builder')
 
50
  # propagate other errors
51
  raise
52
 
53
+ # CPU 情况下尝试把模型和视觉塔上调到 float32,减少 CPU 上的兼容问题
54
+ if not torch.cuda.is_available():
55
+ try:
56
+ model.to(dtype=torch.float32)
57
+ except Exception as e:
58
+ print(f"[WARN] Could not upcast LM to float32: {e}")
59
+ try:
 
 
 
60
  vt = model.get_vision_tower()
61
+ vt.to(device='cpu', dtype=torch.float32)
62
+ print('[INFO] Vision tower moved to cpu (float32).')
63
+ except Exception as e:
64
+ print(f"[WARN] Could not move vision_tower to cpu/float32: {e}")
65
+ else:
66
+ print('[INFO] GPU available — keeping original device/dtype behavior.')
 
 
 
 
 
 
 
 
67
 
68
  return tokenizer, model, image_processor, context_len
69
 
 
80
 
81
  run_libra.load_model = safe_load_model
82
 
83
+ # 现在导入 CCD 与其他被 hook 的符号(导入放在 hook 之后以确保生效)
 
 
 
 
 
84
  from ccd import ccd_eval, run_eval
85
  from libra.eval.run_libra import load_model
86
 
 
104
  # Environment Setup
105
  # =========================================
106
  def setup_environment():
107
+ if torch.cuda.is_available():
108
+ print("🔹 Using GPU:", torch.cuda.get_device_name(0))
109
+ else:
110
+ print("🔹 Using CPU")
111
  os.environ['TOKENIZERS_PARALLELISM'] = 'false'
112
  os.environ['TRANSFORMERS_CACHE'] = './cache'
113
+ torch.set_num_threads(4)
 
 
 
 
114
 
115
 
116
  # =========================================