[BUG] tools/vqgan/extract_vq.py forces audio onto CUDA and ignores the codec's device

Author: li-lizheCreated Sep 24, 2026Updated Sep 24, 2026

Describe the bug

tools/vqgan/extract_vq.py loads the codec on whatever device it is given (get_model(..., device=...)), but process_batch() then moves every waveform to CUDA unconditionally:

python
# tools/vqgan/extract_vq.py:108
wav = torchaudio.functional.resample(wav.cuda(), sr, model.sample_rate)[0]

Two lines later the same function uses model.device for the length tensor:

python
audio_lengths = torch.tensor(audio_lengths, device=model.device, dtype=torch.long)

so the audio and the codec can end up on different devices. On any non-CUDA accelerator (Ascend NPU, MPS, CPU) .cuda() raises immediately, which means the VQ extraction tool cannot be used even when the model itself is placed correctly.

To reproduce

On an Ascend 910B2 host (torch 2.13.0a0 + torch_npu):

>>> wav = torch.randn(16000)      # what torchaudio.load(...) returns
>>> wav.cuda()
AssertionError: Torch not compiled with CUDA enabled

Proposed fix

Use the device the codec already lives on, so it works on any backend without new device plumbing:

python
wav = torchaudio.functional.resample(wav.to(model.device), sr, model.sample_rate)[0]

Verification

Same host, after the change: audios.device=npu:0, and model.encode(audios, audio_lengths) completes on the NPU with results matching the CPU reference (rtol 1e-3). Before/after logs and the patch are in the PR.