someone-in-the-world Claude Sonnet 5 commited on
Commit
926cf86
·
1 Parent(s): 994c9a0

Gate full-GPU fast path on slice size; it OOMs on this Space's 47GB MIG

Browse files

Verified on the FireRed-Dev logs: the previous commit's full pipe.to(device)
attempt OOM'd on the 2g.48gb (47.4GB) MIG slice this Space actually gets
(peaked at 46.82GB), wasting ~40s on the failed attempt + cleanup before
falling back to cpu offload — strictly worse than the offload-only baseline.
Only attempt the fast path when the allocation has real headroom (>=60GB);
otherwise go straight to enable_model_cpu_offload as before.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

Files changed (1) hide show
  1. app.py +19 -12
app.py CHANGED
@@ -404,20 +404,27 @@ def _infer_gpu(pil_images, prompt, seed, guidance_scale, steps, width, height, m
404
 
405
  # Each ZeroGPU call runs in a fresh worker (hooks are always unset here),
406
  # so cpu_offload buys no cross-call reuse — it only trades one bulk
407
- # to(device) transfer for several slower hook-managed ones. Try moving
408
- # the full ~37GB pipeline to GPU in one shot first (fast); only fall back
409
- # to enable_model_cpu_offload (keeps one component resident at a time) if
410
- # that OOMs on a smaller MIG slice (e.g. the 47GB 2g.48gb partition).
 
 
 
411
  if getattr(pipe.transformer, "_hf_hook", None) is None:
412
- try:
413
- pipe.to(device)
414
- print(f"[infer] moved full pipe to {device} — t={time.perf_counter()-t0:.1f}s")
415
- except torch.cuda.OutOfMemoryError:
416
- print(f"[infer] OOM moving full pipe to {device}, falling back to cpu offload")
417
- pipe.to("cpu")
418
- torch.cuda.empty_cache()
 
 
 
 
419
  pipe.enable_model_cpu_offload(device=device)
420
- print(f"[infer] enabled cpu offload on {device} (fallback)")
421
  print(f"[infer] {_gpu_mem_str(_cuda_ok)} — t={time.perf_counter()-t0:.1f}s")
422
 
423
  print(f"[infer] {len(pil_images)} image(s) pre-decoded, output={width}x{height}, seed={seed}")
 
404
 
405
  # Each ZeroGPU call runs in a fresh worker (hooks are always unset here),
406
  # so cpu_offload buys no cross-call reuse — it only trades one bulk
407
+ # to(device) transfer for several slower hook-managed ones. On a
408
+ # generously-sized allocation, move the full ~37GB pipeline to GPU in one
409
+ # shot (fast); but the 47GB 2g.48gb MIG slice this Space actually gets
410
+ # measured a peak of 46.82GB and still OOM'd on that path, wasting ~40s
411
+ # before falling back — so only attempt it when there's real headroom
412
+ # above that, otherwise go straight to enable_model_cpu_offload.
413
+ _FAST_PATH_MIN_GB = 60
414
  if getattr(pipe.transformer, "_hf_hook", None) is None:
415
+ if _cuda_ok and p.total_memory / 1024**3 >= _FAST_PATH_MIN_GB:
416
+ try:
417
+ pipe.to(device)
418
+ print(f"[infer] moved full pipe to {device} — t={time.perf_counter()-t0:.1f}s")
419
+ except torch.cuda.OutOfMemoryError:
420
+ print(f"[infer] OOM moving full pipe to {device}, falling back to cpu offload")
421
+ pipe.to("cpu")
422
+ torch.cuda.empty_cache()
423
+ pipe.enable_model_cpu_offload(device=device)
424
+ print(f"[infer] enabled cpu offload on {device} (fallback)")
425
+ else:
426
  pipe.enable_model_cpu_offload(device=device)
427
+ print(f"[infer] enabled cpu offload on {device} (slice too small for fast path)")
428
  print(f"[infer] {_gpu_mem_str(_cuda_ok)} — t={time.perf_counter()-t0:.1f}s")
429
 
430
  print(f"[infer] {len(pil_images)} image(s) pre-decoded, output={width}x{height}, seed={seed}")