BoxOfColors commited on
Commit
60d3e36
·
1 Parent(s): 0691643

fix: load FlashSR on CPU only to avoid ZeroGPU CUDA init violation

Browse files

ZeroGPU forbids CUDA initialization outside @spaces.GPU decorated
functions. FlashSR was calling torch.cuda.is_available() and .cuda()
in the main process, which poisoned subsequent @spaces.GPU calls
(causing 'GPU task aborted' on xregen). FlashSR is tiny (1.72MB) and
fast enough on CPU — no GPU needed for the upsampling step.

Files changed (1) hide show
  1. app.py +6 -9
app.py CHANGED
@@ -527,28 +527,25 @@ def _load_flashsr():
527
  filename="upsampler.pth",
528
  local_dir=os.path.join(os.path.dirname(os.path.abspath(__file__)), ".flashsr_cache"),
529
  )
 
 
530
  model = FASR(ckpt_path)
531
- if torch.cuda.is_available():
532
- model.model.half().cuda()
533
- print("[FlashSR] Model loaded on GPU (fp16)")
534
- else:
535
- print("[FlashSR] Model loaded on CPU (fp32)")
536
  _FLASHSR_MODEL = model
537
  return model
538
 
539
 
540
  def _apply_flashsr(wav_16k: np.ndarray) -> np.ndarray:
541
- """Upsample a mono 16 kHz numpy array to 48 kHz using FlashSR.
542
 
543
  Returns a mono float32 numpy array at 48 kHz.
544
  Falls back to torchaudio sinc resampling if FlashSR fails.
545
  """
546
  try:
547
  model = _load_flashsr()
 
548
  t = torch.from_numpy(wav_16k.astype(np.float32)).unsqueeze(0)
549
- if torch.cuda.is_available():
550
- t = t.half().cuda()
551
- print(f"[FlashSR] Upsampling {len(wav_16k)/FLASHSR_SR_IN:.2f}s @ 16kHz → 48kHz …")
552
  with torch.no_grad():
553
  out = model.run(t)
554
  # out is a tensor or numpy array — normalise to numpy float32 cpu
 
527
  filename="upsampler.pth",
528
  local_dir=os.path.join(os.path.dirname(os.path.abspath(__file__)), ".flashsr_cache"),
529
  )
530
+ # Always load on CPU — ZeroGPU forbids CUDA init outside @spaces.GPU.
531
+ # FlashSR is tiny (1.72 MB) and fast enough on CPU for post-processing.
532
  model = FASR(ckpt_path)
533
+ print("[FlashSR] Model loaded on CPU (fp32)")
 
 
 
 
534
  _FLASHSR_MODEL = model
535
  return model
536
 
537
 
538
  def _apply_flashsr(wav_16k: np.ndarray) -> np.ndarray:
539
+ """Upsample a mono 16 kHz numpy array to 48 kHz using FlashSR (CPU).
540
 
541
  Returns a mono float32 numpy array at 48 kHz.
542
  Falls back to torchaudio sinc resampling if FlashSR fails.
543
  """
544
  try:
545
  model = _load_flashsr()
546
+ # Keep on CPU — no CUDA outside @spaces.GPU in ZeroGPU environment
547
  t = torch.from_numpy(wav_16k.astype(np.float32)).unsqueeze(0)
548
+ print(f"[FlashSR] Upsampling {len(wav_16k)/FLASHSR_SR_IN:.2f}s @ 16kHz → 48kHz (CPU) …")
 
 
549
  with torch.no_grad():
550
  out = model.run(t)
551
  # out is a tensor or numpy array — normalise to numpy float32 cpu