Update app.py
Browse files
app.py
CHANGED
|
@@ -79,31 +79,48 @@ def safe_load_pretrained_model(model_path, model_base=None, model_name=None, **k
|
|
| 79 |
tokenizer.add_special_tokens({'pad_token': '[PAD]'})
|
| 80 |
print('[INFO] Added [PAD] token to tokenizer')
|
| 81 |
|
| 82 |
-
# Force all model components to CPU
|
| 83 |
-
print('[INFO]
|
| 84 |
try:
|
| 85 |
-
model
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
except Exception as e:
|
| 88 |
-
print(f"[WARN] Could not move model to
|
| 89 |
|
| 90 |
try:
|
| 91 |
if hasattr(model, 'get_vision_tower'):
|
| 92 |
vt = model.get_vision_tower()
|
| 93 |
if vt is not None:
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
except Exception as e:
|
| 97 |
-
print(f"[WARN] Could not move vision_tower to
|
| 98 |
|
| 99 |
try:
|
| 100 |
if hasattr(model, 'get_model'):
|
| 101 |
inner_model = model.get_model()
|
| 102 |
if inner_model is not None:
|
| 103 |
-
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
except Exception as e:
|
| 106 |
-
print(f"[WARN] Could not move inner model to
|
| 107 |
|
| 108 |
return tokenizer, model, image_processor, context_len
|
| 109 |
|
|
@@ -125,9 +142,43 @@ import ccd.ccd_utils as ccd_utils_module
|
|
| 125 |
ccd_utils_module._DEVICE = torch.device('cpu')
|
| 126 |
print('[INFO] Forced ccd_utils._DEVICE to CPU')
|
| 127 |
|
| 128 |
-
# Now import
|
| 129 |
-
from ccd import ccd_eval as _original_ccd_eval, run_eval
|
| 130 |
from libra.eval.run_libra import load_model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
# Wrap ccd_eval to ensure all tensors stay on CPU
|
| 133 |
def ccd_eval_cpu_wrapper(*args, **kwargs):
|
|
|
|
| 79 |
tokenizer.add_special_tokens({'pad_token': '[PAD]'})
|
| 80 |
print('[INFO] Added [PAD] token to tokenizer')
|
| 81 |
|
| 82 |
+
# Force all model components to CPU (keep original dtype if possible, fallback to float32)
|
| 83 |
+
print('[INFO] Ensuring all components are on CPU...')
|
| 84 |
try:
|
| 85 |
+
# Only convert to float32 if model is in float16 (which is slow on CPU)
|
| 86 |
+
current_dtype = next(model.parameters()).dtype
|
| 87 |
+
if current_dtype == torch.float16 or current_dtype == torch.bfloat16:
|
| 88 |
+
print(f'[INFO] Converting model from {current_dtype} to float32 for CPU compatibility...')
|
| 89 |
+
model = model.to(device='cpu', dtype=torch.float32)
|
| 90 |
+
else:
|
| 91 |
+
print(f'[INFO] Keeping model dtype as {current_dtype} (already CPU-compatible)')
|
| 92 |
+
model = model.to(device='cpu')
|
| 93 |
+
print('[INFO] Model moved to CPU.')
|
| 94 |
except Exception as e:
|
| 95 |
+
print(f"[WARN] Could not move model to CPU: {e}")
|
| 96 |
|
| 97 |
try:
|
| 98 |
if hasattr(model, 'get_vision_tower'):
|
| 99 |
vt = model.get_vision_tower()
|
| 100 |
if vt is not None:
|
| 101 |
+
vt_dtype = next(vt.parameters()).dtype
|
| 102 |
+
if vt_dtype == torch.float16 or vt_dtype == torch.bfloat16:
|
| 103 |
+
vt = vt.to(device='cpu', dtype=torch.float32)
|
| 104 |
+
print(f'[INFO] Vision tower converted to float32 for CPU.')
|
| 105 |
+
else:
|
| 106 |
+
vt = vt.to(device='cpu')
|
| 107 |
+
print(f'[INFO] Vision tower moved to CPU (keeping {vt_dtype}).')
|
| 108 |
except Exception as e:
|
| 109 |
+
print(f"[WARN] Could not move vision_tower to CPU: {e}")
|
| 110 |
|
| 111 |
try:
|
| 112 |
if hasattr(model, 'get_model'):
|
| 113 |
inner_model = model.get_model()
|
| 114 |
if inner_model is not None:
|
| 115 |
+
inner_dtype = next(inner_model.parameters()).dtype
|
| 116 |
+
if inner_dtype == torch.float16 or inner_dtype == torch.bfloat16:
|
| 117 |
+
inner_model = inner_model.to(device='cpu', dtype=torch.float32)
|
| 118 |
+
print(f'[INFO] Inner model converted to float32 for CPU.')
|
| 119 |
+
else:
|
| 120 |
+
inner_model = inner_model.to(device='cpu')
|
| 121 |
+
print(f'[INFO] Inner model moved to CPU (keeping {inner_dtype}).')
|
| 122 |
except Exception as e:
|
| 123 |
+
print(f"[WARN] Could not move inner model to CPU: {e}")
|
| 124 |
|
| 125 |
return tokenizer, model, image_processor, context_len
|
| 126 |
|
|
|
|
| 142 |
ccd_utils_module._DEVICE = torch.device('cpu')
|
| 143 |
print('[INFO] Forced ccd_utils._DEVICE to CPU')
|
| 144 |
|
| 145 |
+
# Now import and patch libra functions
|
|
|
|
| 146 |
from libra.eval.run_libra import load_model
|
| 147 |
+
import libra.eval.run_libra as run_libra_module
|
| 148 |
+
|
| 149 |
+
# Patch get_image_tensors_batch to force CPU
|
| 150 |
+
def get_image_tensors_batch_cpu(images, image_processor, model=None):
|
| 151 |
+
"""CPU-only version of get_image_tensors_batch"""
|
| 152 |
+
from PIL import Image
|
| 153 |
+
|
| 154 |
+
if not isinstance(images, list):
|
| 155 |
+
images = [images]
|
| 156 |
+
|
| 157 |
+
image_tensors = []
|
| 158 |
+
for image in images:
|
| 159 |
+
if isinstance(image, str):
|
| 160 |
+
image = Image.open(image).convert('RGB')
|
| 161 |
+
|
| 162 |
+
# Process image
|
| 163 |
+
if hasattr(image_processor, 'preprocess'):
|
| 164 |
+
image_tensor = image_processor.preprocess(image, return_tensors='pt')['pixel_values'][0]
|
| 165 |
+
else:
|
| 166 |
+
image_tensor = image_processor(image, return_tensors='pt')['pixel_values'][0]
|
| 167 |
+
|
| 168 |
+
# Force to CPU (no GPU check)
|
| 169 |
+
image_tensor = image_tensor.to(device='cpu', dtype=torch.float32)
|
| 170 |
+
image_tensors.append(image_tensor)
|
| 171 |
+
|
| 172 |
+
if len(image_tensors) == 1:
|
| 173 |
+
return image_tensors[0].unsqueeze(0)
|
| 174 |
+
else:
|
| 175 |
+
return torch.stack(image_tensors, dim=0)
|
| 176 |
+
|
| 177 |
+
# Replace the function in the module
|
| 178 |
+
run_libra_module.get_image_tensors_batch = get_image_tensors_batch_cpu
|
| 179 |
+
|
| 180 |
+
# Now import the evaluation functions
|
| 181 |
+
from ccd import ccd_eval as _original_ccd_eval, run_eval
|
| 182 |
|
| 183 |
# Wrap ccd_eval to ensure all tensors stay on CPU
|
| 184 |
def ccd_eval_cpu_wrapper(*args, **kwargs):
|