Skip to content

fix: resolve the default device through torch.accelerator - #1327

Merged
SWivid merged 2 commits into
SWivid:mainfrom
li-lizhe:fix/default-device-accelerator-resolution
Oct 14, 2026
Merged

SWivid merged 2 commits into
SWivid:mainfrom
li-lizhe:fix/default-device-accelerator-resolution

Conversation

@li-lizhe

Copy link
Copy Markdown
Contributor

Fixes #1326

What does this PR do?

Six entry points resolved their default device with their own copy of

"cuda" if torch.cuda.is_available() else "xpu" if torch.xpu.is_available() else "mps" if ... else "cpu"

which only knows CUDA / XPU / MPS. On an Ascend NPU machine none of those probes is true (torch.cuda.is_available() is False there), so utils_infer, speech_edit, api.F5TTS, socket_server, finetune_gradio and eval_utmos all silently fell back to the CPU even though an NPU was available.

This PR adds f5_tts/device_utils.get_default_device(), which asks torch.accelerator.current_accelerator() first (PyTorch >= 2.6 reports whichever backend is actually registered — CUDA, NPU, XPU, ...) and keeps explicit CUDA / NPU / XPU / MPS / CPU probes as a fallback for older builds, then routes the six call sites through it. torch_npu is imported lazily inside the NPU probe so the helper stays invisible on CUDA/XPU/MPS/CPU-only installs.

Verification

Real Ascend 910B machine, container npu-lizhe, 4 cards, torch 2.15.0.dev20260917+cpu + torch_npu:

interpreter torch.accelerator.current_accelerator() get_default_device() old chain
NPU env npu npu cpu
same container, non-NPU env (torch 2.14.0a0+cpu) None cpu cpu

torch.randn(4, device=get_default_device()) created the tensor on npu:0 in the NPU env.

Not verified: end-to-end TTS synthesis on the NPU (the F5-TTS runtime deps are not installed in that container), and the CUDA / XPU / MPS branches were kept equivalent by inspection, not re-run.

Behaviour change

None on CUDA / XPU / MPS / CPU-only machines — the helper returns exactly what the six old chains returned there. Ascend NPU machines now resolve to npu instead of cpu.

ruff check and ruff format are clean on the touched files.

li-lizhe and others added 2 commits October 14, 2026 22:28
Each entry point selected its default device with a hand-rolled
`"cuda" if torch.cuda.is_available() else "xpu" if ... else "cpu"` chain that
only knows CUDA, XPU and MPS. On an Ascend NPU machine none of those probes is
true, so inference (`utils_infer`), `speech_edit`, the `F5TTS` API, the socket
server, `finetune_gradio` and the UTMOS evaluation all quietly fell back to the
CPU even when the NPU was available.

Add `f5_tts/device_utils.get_default_device()`, which asks
`torch.accelerator` (PyTorch >= 2.6) for the accelerator that is actually
registered and available, and keeps the explicit CUDA/NPU/XPU/MPS/CPU probes
as a fallback for older builds. Route the six duplicated call sites through it.

Behaviour on CUDA / XPU / MPS / CPU-only machines is unchanged; NPU machines
now pick `npu` instead of `cpu`.

Signed-off-by: li-lizhe <147392333@qq.com>
@SWivid
SWivid merged commit 2f86664 into SWivid:main Oct 14, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Default device resolution ignores Ascend NPU (inference runs on CPU)

2 participants