Skip to content

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

feat: implement ONNX runtime provider selection for CPU and CUDA

myhlolid0ed169081f2
diff --git a/mineru/model/table/rec/onnxruntime_provider.py b/mineru/model/table/rec/onnxruntime_provider.py
new file mode 100644
index 00000000..7682f73c
--- /dev/null
+++ b/mineru/model/table/rec/onnxruntime_provider.py
@@ -0,0 +1,42 @@
+# Copyright (c) Opendatalab. All rights reserved.
+from typing import Any, List, Sequence, Tuple
+
+from loguru import logger
+
+from mineru.utils.config_reader import get_device
+
+
+CPU_PROVIDER = "CPUExecutionProvider"
+CUDA_PROVIDER = "CUDAExecutionProvider"
+CPU_PROVIDER_OPTS = {
+ "arena_extend_strategy": "kSameAsRequested",
+}
+
+
+def _normalize_device(device: object) -> str:
+ """归一化 MinerU 设备名,兼容 cuda:0 这类带索引的写法。"""
+ if not isinstance(device, str):
+ return ""
+ return device.split(":", 1)[0].strip().lower()
+
+
+def _build_cpu_provider() -> Tuple[str, dict[str, Any]]:
+ """构建 CPU provider 配置,避免复用可变的模块级字典。"""
+ return (CPU_PROVIDER, dict(CPU_PROVIDER_OPTS))
+
+
+def build_table_onnx_providers(
+ available_providers: Sequence[str],
+) -> List[Tuple[str, dict[str, Any]]]:
+ """根据 MinerU 当前设备为表格 ONNX 模型选择 onnxruntime providers。"""
+ cpu_provider = _build_cpu_provider()
+ device = _normalize_device(get_device())
+
+ # 只有 MinerU 设备明确为 CUDA 时才尝试 CUDAExecutionProvider,保持默认 CPU 行为。
+ if device != "cuda":
+ return [cpu_provider]
+
+ if CUDA_PROVIDER in available_providers:
+ return [(CUDA_PROVIDER, {}), cpu_provider]
+
+ return [cpu_provider]
diff --git a/mineru/model/table/rec/slanet_plus/table_structure_utils.py b/mineru/model/table/rec/slanet_plus/table_structure_utils.py
index 9a1b3b76..f865ff29 100644
--- a/mineru/model/table/rec/slanet_plus/table_structure_utils.py
+++ b/mineru/model/table/rec/slanet_plus/table_structure_utils.py
@@ -12,9 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import os
-import platform
import traceback
-from enum import Enum
from pathlib import Path
from typing import Any, Dict, List, Tuple, Union
@@ -25,14 +23,10 @@ from onnxruntime import (
InferenceSession,
SessionOptions,
get_available_providers,
- get_device,
)
from loguru import logger
-
-
-class EP(Enum):
- CPU_EP = "CPUExecutionProvider"
+from ..onnxruntime_provider import build_table_onnx_providers
class OrtInferSession:
@@ -76,11 +70,7 @@ class OrtInferSession:
return content_list
def _get_ep_list(self) -> List[Tuple[str, Dict[str, Any]]]:
- cpu_provider_opts = {
- "arena_extend_strategy": "kSameAsRequested",
- }
- EP_list = [(EP.CPU_EP.value, cpu_provider_opts)]
- return EP_list
+ return build_table_onnx_providers(self.had_providers)
def __call__(self, input_content: List[np.ndarray]) -> np.ndarray:
input_dict = dict(zip(self.get_input_names(), input_content))
diff --git a/mineru/model/table/rec/unet_table/utils.py b/mineru/model/table/rec/unet_table/utils.py
index ab03eb55..fb1c35e4 100644
--- a/mineru/model/table/rec/unet_table/utils.py
+++ b/mineru/model/table/rec/unet_table/utils.py
@@ -1,7 +1,6 @@
# Copyright (c) Opendatalab. All rights reserved.
import os
import traceback
-from enum import Enum
from io import BytesIO
from pathlib import Path
from typing import List, Union, Dict, Any, Tuple, Optional
@@ -17,15 +16,13 @@ from onnxruntime import (
)
from PIL import Image, UnidentifiedImageError
+from ..onnxruntime_provider import build_table_onnx_providers
+
root_dir = Path(__file__).resolve().parent
InputType = Union[str, np.ndarray, bytes, Path]
-class EP(Enum):
- CPU_EP = "CPUExecutionProvider"
-
-
class OrtInferSession:
def __init__(self, config: Dict[str, Any]):
self.logger = loguru.logger
@@ -61,13 +58,7 @@ class OrtInferSession:
return sess_opt
def _get_ep_list(self) -> List[Tuple[str, Dict[str, Any]]]:
- cpu_provider_opts = {
- "arena_extend_strategy": "kSameAsRequested",
- }
- EP_list = [(EP.CPU_EP.value, cpu_provider_opts)]
-
- return EP_list
-
+ return build_table_onnx_providers(self.had_providers)
def __call__(self, input_content: List[np.ndarray]) -> np.ndarray:
input_dict = dict(zip(self.get_input_names(), input_content))