Skip to content

Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.

feat: enhance device detection for CUDA and Ascend NPU support in router

myhlolif18f31835d40
diff --git a/mineru/cli/router.py b/mineru/cli/router.py
index 525eba9f..6659341d 100644
--- a/mineru/cli/router.py
+++ b/mineru/cli/router.py
@@ -190,15 +190,45 @@ def resolve_connect_host(host: str) -> str:
return host
-def detect_visible_cuda_devices() -> list[str]:
- configured_visible_devices = os.getenv("CUDA_VISIBLE_DEVICES")
- if configured_visible_devices is not None:
- return [
- item.strip()
- for item in configured_visible_devices.split(",")
- if item.strip()
- ]
+def normalize_local_device_type(device: str | None) -> str:
+ """将 get_device() 返回值规范化为基础设备类型。"""
+ if not device:
+ return "cuda"
+ return str(device).strip().lower().split(":", 1)[0]
+
+
+def get_local_device_type() -> str:
+ """懒加载读取当前设备类型,避免 router 导入阶段提前加载 torch。"""
+ try:
+ from mineru.utils.config_reader import get_device
+
+ return normalize_local_device_type(get_device())
+ except Exception as exc:
+ logger.warning("Failed to resolve local device type, fallback to cuda: {}", exc)
+ return "cuda"
+
+
+def get_local_device_visible_env_name() -> str:
+ """根据当前设备类型选择本地 worker 的可见设备环境变量。"""
+ if get_local_device_type() == "npu":
+ return "ASCEND_RT_VISIBLE_DEVICES"
+ return "CUDA_VISIBLE_DEVICES"
+
+
+def _parse_visible_devices_env(env_name: str) -> list[str] | None:
+ """解析显式配置的可见设备列表,未配置时返回 None。"""
+ configured_visible_devices = os.getenv(env_name)
+ if configured_visible_devices is None:
+ return None
+ return [
+ item.strip()
+ for item in configured_visible_devices.split(",")
+ if item.strip()
+ ]
+
+def _detect_cuda_devices() -> list[str]:
+ """自动探测当前 CUDA 可见设备编号。"""
try:
import torch # type: ignore
except ImportError:
@@ -208,12 +238,35 @@ def detect_visible_cuda_devices() -> list[str]:
return [str(index) for index in range(torch.cuda.device_count())]
+def _detect_npu_devices() -> list[str]:
+ """自动探测当前 Ascend NPU 可见设备编号。"""
+ try:
+ import torch_npu # type: ignore
+ except ImportError:
+ return []
+ if not torch_npu.npu.is_available():
+ return []
+ return [str(index) for index in range(torch_npu.npu.device_count())]
+
+
+def detect_visible_local_devices() -> list[str]:
+ """探测当前设备类型对应的可见本地设备编号。"""
+ visible_devices_env_name = get_local_device_visible_env_name()
+ configured_visible_devices = _parse_visible_devices_env(visible_devices_env_name)
+ if configured_visible_devices is not None:
+ return configured_visible_devices
+
+ if visible_devices_env_name == "ASCEND_RT_VISIBLE_DEVICES":
+ return _detect_npu_devices()
+ return _detect_cuda_devices()
+
+
def parse_local_gpus(local_gpus: str) -> list[str | None]:
value = local_gpus.strip().lower()
if value == LOCAL_GPU_NONE:
return []
if value == LOCAL_GPU_AUTO:
- detected = detect_visible_cuda_devices()
+ detected = detect_visible_local_devices()
if detected:
return detected
return [None]
@@ -350,10 +403,7 @@ class ManagedLocalServer:
env["MINERU_API_OUTPUT_ROOT"] = str(output_root)
env["MINERU_API_DISABLE_ACCESS_LOG"] = "1"
if self.gpu is not None:
- if os.getenv("MINERU_DEVICE_MODE") == "npu":
- env["ASCEND_RT_VISIBLE_DEVICES"] = str(self.gpu)
- else:
- env["CUDA_VISIBLE_DEVICES"] = str(self.gpu)
+ env[get_local_device_visible_env_name()] = str(self.gpu)
command = [
sys.executable,