eloigil6 Claude Opus 4.8 commited on
Commit
53feffb
·
1 Parent(s): 3ae6607

Fix ZeroGPU detection: read spaces Config.zero_gpu, not exact env match

Browse files

The previous check compared SPACES_ZERO_GPU to the literal "true", but the ZeroGPU runtime sets a value the spaces library parses loosely ("1"/"t"/"true"), so IS_ZEROGPU was False on the Space and generation fell back to CPU (loaded lazily on cpu mid-brew). Read the library's own Config.zero_gpu - the same flag that gates the @spaces.GPU decorator - so detection is True exactly when a GPU will be allocated. Add a startup log of the resolved flag for diagnosis.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

Files changed (1) hide show
  1. app.py +19 -3
app.py CHANGED
@@ -34,11 +34,22 @@ FRONTEND = ROOT / "frontend"
34
 
35
  # ZeroGPU: on a Hugging Face ZeroGPU Space a GPU is attached only for the
36
  # duration of a function wrapped in @spaces.GPU, then released. The `spaces`
37
- # package exists only in that runtime (which sets SPACES_ZERO_GPU=true); locally
38
- # we shim @spaces.GPU to a no-op so the exact same code runs on mps/cpu untouched.
39
- IS_ZEROGPU = os.environ.get("SPACES_ZERO_GPU") == "true"
 
 
 
 
 
 
 
 
40
  try:
41
  import spaces # provided by the ZeroGPU Space runtime
 
 
 
42
  except ImportError: # local dev / non-ZeroGPU — make the decorator harmless
43
  class _SpacesShim:
44
  @staticmethod
@@ -50,6 +61,11 @@ except ImportError: # local dev / non-ZeroGPU — make the decorator harmless
50
 
51
  spaces = _SpacesShim()
52
 
 
 
 
 
 
53
  ENGINE = os.getenv("LOFINITY_ENGINE", "musicgen")
54
  # 30s is musicgen-small's single-shot max (1500 tokens). Longer tapes are
55
  # stitched from 30s chunks: each one re-seeds the model with the last OVERLAP_S
 
34
 
35
  # ZeroGPU: on a Hugging Face ZeroGPU Space a GPU is attached only for the
36
  # duration of a function wrapped in @spaces.GPU, then released. The `spaces`
37
+ # package exists only in that runtime; locally we shim @spaces.GPU to a no-op so
38
+ # the exact same code runs on mps/cpu untouched.
39
+ #
40
+ # Detect ZeroGPU via the spaces library's OWN config rather than parsing the env
41
+ # var ourselves: Config.zero_gpu is the very flag that gates @spaces.GPU (see
42
+ # spaces/zero/decorator.py), so IS_ZEROGPU ends up True precisely when the
43
+ # decorator will really allocate a GPU. (Our first attempt compared
44
+ # SPACES_ZERO_GPU to the literal "true", but the runtime sets it to a value the
45
+ # library parses loosely — "1"/"t"/"true" — so the exact match failed and the
46
+ # GPU path never fired: generation silently fell back to CPU.)
47
+ IS_ZEROGPU = False
48
  try:
49
  import spaces # provided by the ZeroGPU Space runtime
50
+ from spaces.config import Config as _ZeroGPUConfig
51
+
52
+ IS_ZEROGPU = bool(_ZeroGPUConfig.zero_gpu)
53
  except ImportError: # local dev / non-ZeroGPU — make the decorator harmless
54
  class _SpacesShim:
55
  @staticmethod
 
61
 
62
  spaces = _SpacesShim()
63
 
64
+ print(
65
+ f"[lofinity] startup: IS_ZEROGPU={IS_ZEROGPU} "
66
+ f"(SPACES_ZERO_GPU={os.environ.get('SPACES_ZERO_GPU')!r})"
67
+ )
68
+
69
  ENGINE = os.getenv("LOFINITY_ENGINE", "musicgen")
70
  # 30s is musicgen-small's single-shot max (1500 tokens). Longer tapes are
71
  # stitched from 30s chunks: each one re-seeds the model with the last OVERLAP_S