Daankular commited on
Commit
0998a0d
·
1 Parent(s): cae2ac0

Move CUDA compilation into @spaces.GPU function — nvcc unavailable at APP_STARTING

Browse files

On ZeroGPU, nvcc is only accessible inside @spaces.GPU decorated calls.
Remove CUDA packages from _install_runtime_packages() (CPU-only startup).
Add _install_cuda_packages() with @spaces.GPU(duration=300) called once
at module level after `import spaces`, so nvcc is available when it runs.

Files changed (1) hide show
  1. app.py +29 -22
app.py CHANGED
@@ -126,15 +126,34 @@ def _install_runtime_packages():
126
  [sys.executable, "-m", "pip", "install", "--quiet", "--no-deps"]
127
  + _NO_DEPS_PACKAGES, check=True,
128
  )
129
- # A10G = sm_86. Set arch list explicitly because NVML is unavailable at install
130
- # time (no GPU allocated yet), so torch can't auto-detect it.
131
- # Detect CUDA home dynamically — path varies between ZeroGPU container builds.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
  import shutil as _shutil
133
  _nvcc = _shutil.which("nvcc")
134
  if _nvcc:
135
  _cuda_home = str(Path(_nvcc).parent.parent)
136
  else:
137
- # Scan common ZeroGPU / CUDA container paths
138
  for _cand in [
139
  "/usr/local/cuda",
140
  "/usr/local/cuda-12.9",
@@ -148,10 +167,8 @@ def _install_runtime_packages():
148
  _cuda_home = _cand
149
  break
150
  else:
151
- raise RuntimeError(
152
- "nvcc not found — no CUDA toolkit in PATH or standard locations. "
153
- "Cannot build nvdiffrast/detectron2/diso."
154
- )
155
  print(f"[startup] CUDA home: {_cuda_home}")
156
  _cuda_env = {
157
  **os.environ,
@@ -165,8 +182,7 @@ def _install_runtime_packages():
165
  [sys.executable, "-m", "pip", "install", "--quiet", "--no-build-isolation"]
166
  + _CUDA_PACKAGES, env=_cuda_env, check=True,
167
  )
168
- # diso: pip install git+... does not fetch git submodules, causing undefined
169
- # CUDA symbols at import time. Clone with --recurse-submodules first.
170
  _diso_src = Path("/tmp/diso-build")
171
  if not _diso_src.exists():
172
  subprocess.run(
@@ -179,19 +195,10 @@ def _install_runtime_packages():
179
  str(_diso_src)],
180
  env=_cuda_env, check=True,
181
  )
182
- _RUNTIME_PKG_MARKER.touch()
183
- print("[startup] Runtime packages installed.")
184
-
185
- _install_runtime_packages()
186
- # ──────────────────────────────────────────────────────────────────────────────
187
-
188
- import cv2
189
- import gradio as gr
190
- import spaces
191
- import torch
192
- import numpy as np
193
- from PIL import Image
194
 
 
195
  # ── Paths ─────────────────────────────────────────────────────────────────────
196
  HERE = Path(__file__).parent
197
  PIPELINE_DIR = HERE / "pipeline"
 
126
  [sys.executable, "-m", "pip", "install", "--quiet", "--no-deps"]
127
  + _NO_DEPS_PACKAGES, check=True,
128
  )
129
+ _RUNTIME_PKG_MARKER.touch()
130
+ print("[startup] CPU runtime packages installed.")
131
+
132
+ _install_runtime_packages()
133
+ # ──────────────────────────────────────────────────────────────────────────────
134
+
135
+ import cv2
136
+ import gradio as gr
137
+ import spaces
138
+ import torch
139
+ import numpy as np
140
+ from PIL import Image
141
+
142
+ # ── CUDA package installation ─────────────────────────────────────────────────
143
+ # nvcc is only available inside a @spaces.GPU call on ZeroGPU (not at APP_STARTING).
144
+ # Compile nvdiffrast / detectron2 / diso here, on first GPU allocation at startup.
145
+ _CUDA_PKG_MARKER = Path("/tmp/.cuda_pkgs_installed")
146
+
147
+ @spaces.GPU(duration=300)
148
+ def _install_cuda_packages():
149
+ if _CUDA_PKG_MARKER.exists():
150
+ return
151
+ print("[startup] Installing CUDA packages (nvdiffrast, detectron2, diso)...")
152
  import shutil as _shutil
153
  _nvcc = _shutil.which("nvcc")
154
  if _nvcc:
155
  _cuda_home = str(Path(_nvcc).parent.parent)
156
  else:
 
157
  for _cand in [
158
  "/usr/local/cuda",
159
  "/usr/local/cuda-12.9",
 
167
  _cuda_home = _cand
168
  break
169
  else:
170
+ print("[startup] WARNING: nvcc not found even with GPU allocated — CUDA extensions unavailable")
171
+ return
 
 
172
  print(f"[startup] CUDA home: {_cuda_home}")
173
  _cuda_env = {
174
  **os.environ,
 
182
  [sys.executable, "-m", "pip", "install", "--quiet", "--no-build-isolation"]
183
  + _CUDA_PACKAGES, env=_cuda_env, check=True,
184
  )
185
+ # diso must be cloned with --recurse-submodules; pip install git+... skips submodules
 
186
  _diso_src = Path("/tmp/diso-build")
187
  if not _diso_src.exists():
188
  subprocess.run(
 
195
  str(_diso_src)],
196
  env=_cuda_env, check=True,
197
  )
198
+ _CUDA_PKG_MARKER.touch()
199
+ print("[startup] CUDA packages installed.")
 
 
 
 
 
 
 
 
 
 
200
 
201
+ _install_cuda_packages()
202
  # ── Paths ─────────────────────────────────────────────────────────────────────
203
  HERE = Path(__file__).parent
204
  PIPELINE_DIR = HERE / "pipeline"