Fix ZeroGPU detection: read spaces Config.zero_gpu, not exact env match
Browse filesThe 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>
33 unmodified lines34353637383940414243445 unmodified lines50515253545533 unmodified lines# ZeroGPU: on a Hugging Face ZeroGPU Space a GPU is attached only for the# duration of a function wrapped in @spaces.GPU, then released. The `spaces`# package exists only in that runtime (which sets SPACES_ZERO_GPU=true); locally# we shim @spaces.GPU to a no-op so the exact same code runs on mps/cpu untouched.IS_ZEROGPU = os.environ.get("SPACES_ZERO_GPU") == "true"try:import spaces # provided by the ZeroGPU Space runtimeexcept ImportError: # local dev / non-ZeroGPU — make the decorator harmlessclass _SpacesShim:@staticmethod5 unmodified linesspaces = _SpacesShim()ENGINE = os.getenv("LOFINITY_ENGINE", "musicgen")# 30s is musicgen-small's single-shot max (1500 tokens). Longer tapes are# stitched from 30s chunks: each one re-seeds the model with the last OVERLAP_S33 unmodified lines343536373839404142434445464748495051525354555 unmodified lines616263646566676869707133 unmodified lines# ZeroGPU: on a Hugging Face ZeroGPU Space a GPU is attached only for the# duration of a function wrapped in @spaces.GPU, then released. The `spaces`# package exists only in that runtime; locally we shim @spaces.GPU to a no-op so# the exact same code runs on mps/cpu untouched.## Detect ZeroGPU via the spaces library's OWN config rather than parsing the env# var ourselves: Config.zero_gpu is the very flag that gates @spaces.GPU (see# spaces/zero/decorator.py), so IS_ZEROGPU ends up True precisely when the# decorator will really allocate a GPU. (Our first attempt compared# SPACES_ZERO_GPU to the literal "true", but the runtime sets it to a value the# library parses loosely — "1"/"t"/"true" — so the exact match failed and the# GPU path never fired: generation silently fell back to CPU.)IS_ZEROGPU = Falsetry:import spaces # provided by the ZeroGPU Space runtimefrom spaces.config import Config as _ZeroGPUConfigIS_ZEROGPU = bool(_ZeroGPUConfig.zero_gpu)except ImportError: # local dev / non-ZeroGPU — make the decorator harmlessclass _SpacesShim:@staticmethod5 unmodified linesspaces = _SpacesShim()print(f"[lofinity] startup: IS_ZEROGPU={IS_ZEROGPU} "f"(SPACES_ZERO_GPU={os.environ.get('SPACES_ZERO_GPU')!r})")ENGINE = os.getenv("LOFINITY_ENGINE", "musicgen")# 30s is musicgen-small's single-shot max (1500 tokens). Longer tapes are# stitched from 30s chunks: each one re-seeds the model with the last OVERLAP_S
app.py+19-3