Skip to content

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

feat: enhance device detection to support NPU and improve error handling

myhloli7ae4f80dadc8
diff --git a/mineru/backend/pipeline/pipeline_analyze.py b/mineru/backend/pipeline/pipeline_analyze.py
index 7c629213..3df9d4a5 100644
--- a/mineru/backend/pipeline/pipeline_analyze.py
+++ b/mineru/backend/pipeline/pipeline_analyze.py
@@ -1,6 +1,7 @@
import os
import time
-import numpy as np
+from typing import List, Tuple
+import PIL.Image
import torch
from .model_init import MineruPipelineModel
@@ -150,7 +151,7 @@ def doc_analyze(
def batch_image_analyze(
- images_with_extra_info: list[(np.ndarray, bool, str)],
+ images_with_extra_info: List[Tuple[PIL.Image.Image, bool, str]],
formula_enable=None,
table_enable=None):
# os.environ['CUDA_VISIBLE_DEVICES'] = str(idx)
@@ -163,9 +164,15 @@ def batch_image_analyze(
device = get_device()
if str(device).startswith('npu'):
- import torch_npu
- if torch_npu.npu.is_available():
- torch.npu.set_compile_mode(jit_compile=False)
+ try:
+ import torch_npu
+ if torch_npu.npu.is_available():
+ torch.npu.set_compile_mode(jit_compile=False)
+ except Exception as e:
+ raise RuntimeError(
+ "NPU is selected as device, but torch_npu is not available. "
+ "Please ensure that the torch_npu package is installed correctly."
+ ) from e
if str(device).startswith('npu') or str(device).startswith('cuda'):
vram = get_vram(device)
diff --git a/mineru/utils/config_reader.py b/mineru/utils/config_reader.py
index 6fe51867..0a88b841 100644
--- a/mineru/utils/config_reader.py
+++ b/mineru/utils/config_reader.py
@@ -74,8 +74,15 @@ def get_device():
else:
if torch.cuda.is_available():
return "cuda"
- if torch.backends.mps.is_available():
+ elif torch.backends.mps.is_available():
return "mps"
+ else:
+ try:
+ import torch_npu
+ if torch_npu.npu.is_available():
+ return "npu"
+ except Exception as e:
+ pass
return "cpu"