From 1b8059cf610c1e89bd7798cab721145ea1fc50a6 Mon Sep 17 00:00:00 2001 From: li-lizhe <147392333@qq.com> Date: Fri, 4 Sep 2026 10:55:22 +0800 Subject: [PATCH] fix(export_torch_script_v3v4): device-agnostic accelerator detection Replace the hard-coded `"cuda" if torch.cuda.is_available() else "cpu"` device heuristic with `torch.accelerator.current_accelerator()` (guarded by `hasattr` for PyTorch < 2.5). This lets the script run out of the box on non-CUDA accelerators (Ascend NPU, Intel XPU, Apple MPS) without edits, while preserving the original CUDA-or-CPU fallback on older torch builds. Tested on Ascend 910B2 (torch 2.14, torch_npu): `str(current_accelerator())` returns `"npu"`, so the model tensors are placed on NPU instead of CPU. --- GPT_SoVITS/export_torch_script_v3v4.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/GPT_SoVITS/export_torch_script_v3v4.py b/GPT_SoVITS/export_torch_script_v3v4.py index 94f88b4f..927ad480 100644 --- a/GPT_SoVITS/export_torch_script_v3v4.py +++ b/GPT_SoVITS/export_torch_script_v3v4.py @@ -27,7 +27,15 @@ logging.config.dictConfig(uvicorn.config.LOGGING_CONFIG) logger = logging.getLogger("uvicorn") is_half = True -device = "cuda" if torch.cuda.is_available() else "cpu" +# Detect the current accelerator (CUDA, NPU, XPU, MPS, …) in a device-agnostic way. +# Using torch.accelerator (available since PyTorch 2.5) ensures the script works +# out of the box on Ascend NPU, Intel XPU, Apple MPS, etc. without hard-coding +# "cuda". Falls back to the traditional CUDA-or-CPU heuristic when torch.accelerator +# is not available (PyTorch < 2.5). +if hasattr(torch, "accelerator") and torch.accelerator.is_available(): + device = str(torch.accelerator.current_accelerator()) +else: + device = "cuda" if torch.cuda.is_available() else "cpu" now_dir = os.getcwd()