Skip to content

Commit ce6ce4b

Browse files
feat: auto pick device for mineru
1 parent d88c479 commit ce6ce4b

3 files changed

Lines changed: 50 additions & 2 deletions

File tree

graphgen/models/reader/pdf_reader.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from graphgen.bases.base_reader import BaseReader
99
from graphgen.models.reader.txt_reader import TXTReader
10-
from graphgen.utils import logger
10+
from graphgen.utils import logger, pick_device
1111

1212

1313
class PDFReader(BaseReader):
@@ -26,7 +26,7 @@ def __init__(
2626
backend: Optional[
2727
str
2828
] = None, # pipeline | vlm-transformers | vlm-sglang-engine | vlm-sglang-client
29-
device: Optional[str] = None, # cpu | cuda | cuda:0 | npu | mps
29+
device: Optional[str] = "auto", # cpu | cuda | cuda:0 | npu | mps | auto
3030
source: Optional[str] = None, # huggingface | modelscope | local
3131
vlm_url: Optional[str] = None, # 当 backend=vlm-sglang-client 时必填
3232
start_page: Optional[int] = None, # 0-based
@@ -39,6 +39,9 @@ def __init__(
3939
super().__init__()
4040
self.output_dir = os.path.join(output_dir, "mineru") if output_dir else None
4141

42+
if device == "auto":
43+
device = pick_device()
44+
4245
self._default_kwargs: Dict[str, Any] = {
4346
"method": method,
4447
"lang": lang,

graphgen/utils/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from .calculate_confidence import yes_no_loss_entropy
22
from .detect_lang import detect_if_chinese, detect_main_language
3+
from .device import pick_device
34
from .format import (
45
handle_single_entity_extraction,
56
handle_single_relationship_extraction,

graphgen/utils/device.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import shutil
2+
import subprocess
3+
import sys
4+
5+
6+
def pick_device() -> str:
7+
"""Return the best available device string for MinerU."""
8+
# 1. NVIDIA GPU
9+
if shutil.which("nvidia-smi") is not None:
10+
try:
11+
# check if there's any free GPU memory
12+
out = subprocess.check_output(
13+
[
14+
"nvidia-smi",
15+
"--query-gpu=memory.free",
16+
"--format=csv,noheader,nounits",
17+
],
18+
text=True,
19+
)
20+
if any(int(line) > 0 for line in out.strip().splitlines()):
21+
return "cuda:0"
22+
except Exception: # pylint: disable=broad-except
23+
pass
24+
25+
# 2. Apple Silicon
26+
if sys.platform == "darwin" and shutil.which("sysctl"):
27+
try:
28+
brand = subprocess.check_output(
29+
["sysctl", "-n", "machdep.cpu.brand_string"], text=True
30+
)
31+
if "Apple" in brand:
32+
return "mps"
33+
except Exception: # pylint: disable=broad-except
34+
pass
35+
36+
# 3. Ascend NPU
37+
if shutil.which("npu-smi") is not None:
38+
try:
39+
subprocess.check_call(["npu-smi", "info"], stdout=subprocess.DEVNULL)
40+
return "npu"
41+
except Exception: # pylint: disable=broad-except
42+
pass
43+
44+
return "cpu"

0 commit comments

Comments
 (0)