X-iZhang commited on
Commit
d7d2496
·
verified ·
1 Parent(s): f2e8d27

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -2
app.py CHANGED
@@ -137,12 +137,55 @@ def safe_load_model(model_path, model_base=None, model_name=None):
137
 
138
  run_libra.load_model = safe_load_model
139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  # IMPORTANT: Patch libra functions BEFORE importing CCD
141
  # (because ccd imports these functions during module load)
142
  import libra.eval.run_libra as run_libra_module
143
 
144
  # Replace the function in the module BEFORE ccd imports it
145
- # run_libra_module.get_image_tensors_batch = get_image_tensors_batch_cpu
146
  # print('[INFO] Replaced get_image_tensors_batch with CPU-only version')
147
 
148
  # Now import CCD and hook ccd_utils to force CPU for expert models
@@ -153,7 +196,7 @@ print('[INFO] Forced ccd_utils._DEVICE to CPU')
153
  # Now import CCD module and patch its imported function
154
  import ccd.run_ccd as ccd_run_ccd_module
155
  # Replace the function that ccd.run_ccd imported
156
- # ccd_run_ccd_module.get_image_tensors_batch = get_image_tensors_batch_cpu
157
  # print('[INFO] Patched ccd.run_ccd.get_image_tensors_batch')
158
 
159
  # Now import the evaluation functions
 
137
 
138
  run_libra.load_model = safe_load_model
139
 
140
+ def get_image_tensors_batch_cpu(images, image_processor, model, device='cpu'):
141
+ """
142
+ CPU-only version of get_image_tensors_batch.
143
+ Keeps the same structure and behaviour as the original function.
144
+ """
145
+ import torch
146
+
147
+ batch_size = len(images)
148
+ all_processed = []
149
+
150
+ if isinstance(images, str):
151
+ images = [images]
152
+ elif not isinstance(images, (list, tuple)):
153
+ raise TypeError("images must be a string or a list/tuple of strings")
154
+
155
+ for i in range(batch_size):
156
+ images_i = [images[i]]
157
+
158
+ # Ensure two images are present
159
+ if len(images_i) != 2:
160
+ images_i.append(images_i[0])
161
+ if hasattr(model, "config") and getattr(model.config, "mm_projector_type", None) == "TAC":
162
+ print("Contains only current image. Adding a dummy prior image for TAC.")
163
+
164
+ processed_images = []
165
+ for img_data in images_i:
166
+ image_tensor = process_images([img_data], image_processor, model.config)[0]
167
+
168
+ # ✅ Force to CPU instead of CUDA
169
+ image_tensor = image_tensor.to(device=device, non_blocking=False)
170
+ processed_images.append(image_tensor)
171
+
172
+ cur_images = processed_images[0]
173
+ prior_images = processed_images[1]
174
+
175
+ batch_images = torch.stack([cur_images, prior_images])
176
+ all_processed.append(batch_images)
177
+
178
+ # ⚠️ Keep the original structure (dim=1)
179
+ batch_images = torch.stack(all_processed, dim=1)
180
+
181
+ return batch_images
182
+
183
  # IMPORTANT: Patch libra functions BEFORE importing CCD
184
  # (because ccd imports these functions during module load)
185
  import libra.eval.run_libra as run_libra_module
186
 
187
  # Replace the function in the module BEFORE ccd imports it
188
+ run_libra_module.get_image_tensors_batch = get_image_tensors_batch_cpu
189
  # print('[INFO] Replaced get_image_tensors_batch with CPU-only version')
190
 
191
  # Now import CCD and hook ccd_utils to force CPU for expert models
 
196
  # Now import CCD module and patch its imported function
197
  import ccd.run_ccd as ccd_run_ccd_module
198
  # Replace the function that ccd.run_ccd imported
199
+ ccd_run_ccd_module.get_image_tensors_batch = get_image_tensors_batch_cpu
200
  # print('[INFO] Patched ccd.run_ccd.get_image_tensors_batch')
201
 
202
  # Now import the evaluation functions