Skip to content

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

perf(runtime): disable NNPACK by default for CPU convolutions

myhlolid05105e92525
diff --git a/mineru/model/runtime/cpu_threads.py b/mineru/model/runtime/cpu_threads.py
index 555c0076..37d12912 100644
--- a/mineru/model/runtime/cpu_threads.py
+++ b/mineru/model/runtime/cpu_threads.py
@@ -1,5 +1,5 @@
# Copyright (c) Opendatalab. All rights reserved.
-"""在首次 Torch 模型初始化和工作线程执行前限制 CPU 算子线程。"""
+"""在首次 Torch 初始化关闭 CPU NNPACK,并限制后续工作线程的 CPU 算子线程。"""
from __future__ import annotations
@@ -43,12 +43,14 @@ def _positive_env(name: str) -> int | None:
def initialize_torch_threads() -> int:
- """仅在 Torch 模型边界加载 Torch,不调高调用方已有的更低线程数。"""
+ """惰性初始化 Torch 的 CPU 默认后端与线程,不调高调用方已有的更低线程数。"""
global _limit
import torch
with _lock:
if _limit is None:
+ # NNPACK 只参与 CPU 卷积;首次默认关闭,后续线程准备保留调用方显式设置。
+ torch.backends.nnpack.set_flags(False)
explicit = [_positive_env(name) for name in ("OMP_NUM_THREADS", "MKL_NUM_THREADS")]
configured = (
min(value for value in explicit if value is not None)
diff --git a/tests/unittest/test_torch_cpu_threads.py b/tests/unittest/test_torch_cpu_threads.py
index 3da413f7..cff4e433 100644
--- a/tests/unittest/test_torch_cpu_threads.py
+++ b/tests/unittest/test_torch_cpu_threads.py
@@ -44,6 +44,7 @@ def test_torch_thread_limits(monkeypatch: pytest.MonkeyPatch, env: dict[str, str
monkeypatch.setattr(cpu_threads, "effective_cpu_count", lambda: 12)
local = threading.local()
calls: list[int] = []
+ nnpack_calls: list[bool] = []
def init() -> None:
"""模拟新 OpenMP 工作线程需要显式初始化线程状态。"""
@@ -54,12 +55,18 @@ def test_torch_thread_limits(monkeypatch: pytest.MonkeyPatch, env: dict[str, str
"""只修改当前模拟工作线程,检测配置是否传播。"""
local.count = count
- fake = SimpleNamespace(init_num_threads=init, get_num_threads=lambda: local.count, set_num_threads=set_threads)
+ fake = SimpleNamespace(
+ init_num_threads=init,
+ get_num_threads=lambda: local.count,
+ set_num_threads=set_threads,
+ backends=SimpleNamespace(nnpack=SimpleNamespace(set_flags=nnpack_calls.append)),
+ )
monkeypatch.setitem(sys.modules, "torch", fake)
assert cpu_threads.initialize_torch_threads() == expected
with ThreadPoolExecutor(max_workers=3) as pool:
assert pool.submit(cpu_threads.initialize_torch_threads).result() == expected
assert len(set(calls)) >= 2
+ assert nnpack_calls == [False]
def test_thread_module_import_and_uninitialized_stage_are_light() -> None:
@@ -85,12 +92,20 @@ os.environ.pop("MKL_NUM_THREADS",None)
from mineru.model.runtime.cpu_threads import initialize_torch_threads
from concurrent.futures import ThreadPoolExecutor
import torch
+torch.backends.nnpack.set_flags(True)
initialize_torch_threads()
+assert not torch._C._get_nnpack_enabled()
def run(_):
initialize_torch_threads()
assert torch.get_num_threads() <= 2
+ assert not torch._C._get_nnpack_enabled()
return (torch.ones(64,64) @ torch.ones(64,64)).sum().item()
with ThreadPoolExecutor(max_workers=3) as pool:
assert list(pool.map(run,range(6))) == [262144.0]*6
+with torch.backends.nnpack.flags(enabled=True):
+ with ThreadPoolExecutor(max_workers=1) as pool:
+ pool.submit(initialize_torch_threads).result()
+ assert torch._C._get_nnpack_enabled()
+assert not torch._C._get_nnpack_enabled()
"""
subprocess.run([sys.executable, "-c", code], check=True, timeout=60)