nanye commited on
Commit
e41c23f
·
1 Parent(s): 4f7577e

handle ZeroGPU tensor packing crash on huggingface

Browse files
Files changed (4) hide show
  1. README.md +1 -1
  2. app.py +4 -0
  3. generate.py +6 -0
  4. requirements.txt +1 -0
README.md CHANGED
@@ -51,7 +51,7 @@ python download_cifar10.py 42 --split train --output assets/cifar10-train-42.png
51
 
52
  Push this repository to a Gradio Space and select ZeroGPU in the Space hardware settings. The Dockerfile and legacy server are not used by the Gradio deployment. Account eligibility and daily GPU quotas apply.
53
 
54
- The app imports `spaces` before PyTorch and places both GPU models on CUDA at startup when `SPACES_ZERO_GPU` is enabled. Reverse sampling tries GPU first and automatically restarts on CPU if GPU allocation, quota, or supported CUDA availability errors prevent the run. Locally, the app prefers CUDA, then Apple Silicon MPS, then CPU. MPS availability failures also fall back to CPU. Separate pipelines are cached for each model and device; the CPU pipeline loads on first use. CPU sampling is substantially slower. GPU sampling runs inside a decorated function called by the Gradio handler. `GPU_DURATION_SECONDS` defaults to 120; benchmark a full 1,000-step run and adjust this Space variable if necessary. A timeout is reported by the Gradio queue; selecting fewer checkpoints does not reduce the sampling steps.
55
 
56
  For persistent caching, attach a writable storage bucket and set the Space variable `MODEL_CACHE_DIR` to its mount path (for example `/data/models`). The old Docker path `/app/models` is also usable if explicitly configured with that variable.
57
 
 
51
 
52
  Push this repository to a Gradio Space and select ZeroGPU in the Space hardware settings. The Dockerfile and legacy server are not used by the Gradio deployment. Account eligibility and daily GPU quotas apply.
53
 
54
+ The app imports `spaces` before PyTorch and copies model tensors into owned CPU storage before placing both GPU models on CUDA at startup when `SPACES_ZERO_GPU` is enabled. Reverse sampling tries GPU first and automatically restarts on CPU if GPU allocation, quota, or supported CUDA availability errors prevent the run. Locally, the app prefers CUDA, then Apple Silicon MPS, then CPU. MPS availability failures also fall back to CPU. Separate pipelines are cached for each model and device; the CPU pipeline loads on first use. CPU sampling is substantially slower. GPU sampling runs inside a decorated function called by the Gradio handler. `GPU_DURATION_SECONDS` defaults to 120; benchmark a full 1,000-step run and adjust this Space variable if necessary. A timeout is reported by the Gradio queue; selecting fewer checkpoints does not reduce the sampling steps.
55
 
56
  For persistent caching, attach a writable storage bucket and set the Space variable `MODEL_CACHE_DIR` to its mount path (for example `/data/models`). The old Docker path `/app/models` is also usable if explicitly configured with that variable.
57
 
app.py CHANGED
@@ -1,4 +1,8 @@
1
  """Gradio entry point for local use and Hugging Face ZeroGPU."""
 
 
 
 
2
  import spaces # Must initialize ZeroGPU before importing torch/diffusers.
3
 
4
  import base64
 
1
  """Gradio entry point for local use and Hugging Face ZeroGPU."""
2
+ import faulthandler
3
+
4
+ faulthandler.enable() # Include Python stacks if native startup code crashes.
5
+
6
  import spaces # Must initialize ZeroGPU before importing torch/diffusers.
7
 
8
  import base64
generate.py CHANGED
@@ -52,6 +52,12 @@ def load_model(device=None, model_id=MODEL_ID):
52
  print(f"Loading {model_id} on device: {device}")
53
  pipeline = DDPMPipeline.from_pretrained(
54
  model_id, cache_dir=Path(os.environ.get("MODEL_CACHE_DIR", ROOT / "models")))
 
 
 
 
 
 
55
  pipeline.to(device)
56
  _PIPELINES[key] = pipeline
57
  _PIPELINE = _PIPELINES[key]
 
52
  print(f"Loading {model_id} on device: {device}")
53
  pipeline = DDPMPipeline.from_pretrained(
54
  model_id, cache_dir=Path(os.environ.get("MODEL_CACHE_DIR", ROOT / "models")))
55
+ if device == "cuda" and os.environ.get("SPACES_ZERO_GPU", "").lower() in {"1", "true", "yes"}:
56
+ # ZeroGPU packs CUDA aliases at startup. Give it owned CPU
57
+ # storage rather than tensors backed by mapped checkpoint files.
58
+ for component in pipeline.components.values():
59
+ if isinstance(component, torch.nn.Module):
60
+ component._apply(lambda tensor: tensor.clone())
61
  pipeline.to(device)
62
  _PIPELINES[key] = pipeline
63
  _PIPELINE = _PIPELINES[key]
requirements.txt CHANGED
@@ -1,3 +1,4 @@
 
1
  diffusers>=0.30.0
2
  matplotlib>=3.8.0
3
  Pillow>=10.0.0
 
1
+ accelerate>=0.30.0
2
  diffusers>=0.30.0
3
  matplotlib>=3.8.0
4
  Pillow>=10.0.0