HimalayaGPT commited on
Commit
f2fd1df
·
verified ·
1 Parent(s): 72a4652

Handle Colab meta-tensor load path with automatic device_map fallback

Browse files
Files changed (1) hide show
  1. run_standalone_inference.py +26 -1
run_standalone_inference.py CHANGED
@@ -154,6 +154,18 @@ def _top_k_filter(logits, top_k: int):
154
  return masked
155
 
156
 
 
 
 
 
 
 
 
 
 
 
 
 
157
  def generate_compat(model, input_ids, max_new_tokens: int, temperature: float, top_k: int, seed: int):
158
  import torch
159
  import torch.nn.functional as F
@@ -212,7 +224,20 @@ def main() -> None:
212
  low_cpu_mem_usage=bool(device.type == "cuda" and args.use_device_map),
213
  )
214
  if device.type == "cpu" or (device.type == "cuda" and not args.use_device_map):
215
- model = model.to(device)
 
 
 
 
 
 
 
 
 
 
 
 
 
216
  model.eval()
217
 
218
  cfg = model.config
 
154
  return masked
155
 
156
 
157
+ def _has_meta_tensors(model) -> bool:
158
+ import torch
159
+
160
+ for p in model.parameters():
161
+ if isinstance(p, torch.Tensor) and p.is_meta:
162
+ return True
163
+ for b in model.buffers():
164
+ if isinstance(b, torch.Tensor) and b.is_meta:
165
+ return True
166
+ return False
167
+
168
+
169
  def generate_compat(model, input_ids, max_new_tokens: int, temperature: float, top_k: int, seed: int):
170
  import torch
171
  import torch.nn.functional as F
 
224
  low_cpu_mem_usage=bool(device.type == "cuda" and args.use_device_map),
225
  )
226
  if device.type == "cpu" or (device.type == "cuda" and not args.use_device_map):
227
+ if _has_meta_tensors(model):
228
+ print("[warn] Detected meta tensors after load; retrying with device_map='auto' fallback.")
229
+ del model
230
+ if torch.cuda.is_available():
231
+ torch.cuda.empty_cache()
232
+ model = AutoModelForCausalLM.from_pretrained(
233
+ local,
234
+ trust_remote_code=True,
235
+ torch_dtype=torch_dtype,
236
+ device_map="auto" if device.type == "cuda" else None,
237
+ low_cpu_mem_usage=bool(device.type == "cuda"),
238
+ )
239
+ else:
240
+ model = model.to(device)
241
  model.eval()
242
 
243
  cfg = model.config