Skip to content

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

Make UniMERNet MPS cold loading safe on Python 3.14

myhloli810e3fac04a4
diff --git a/docs/next/transformers5-migration.md b/docs/next/transformers5-migration.md
index b556bab3..b2118d01 100644
--- a/docs/next/transformers5-migration.md
+++ b/docs/next/transformers5-migration.md
@@ -90,3 +90,8 @@ macOS 同时记录系统 `proc_pid_rusage` 的 `physical_footprint_bytes`,用
- 检查脚本显式验证 wheel 的 Requires-Python 和每个目标 ABI 的发行文件;jieba 为唯一允许的纯 Python sdist。
- macOS full/all + Transformers 5.10.1 应拒绝;Python 3.14 + vLLM 0.19.1 应拒绝,其余范围由解析器选择兼容版本。
- 发布顺序为 DocVortex、utils、MinerU。本次只做本地合入、构建与验证,未发布依赖必须显式提供三个 wheel。
+
+
+### UniMERNet MPS 冷启动
+
+本轮 Python 3.14 / Torch 2.14 / Transformers 5.16.1 独立模型验收复现了并行 MPS 权重物化的原生崩溃。UniMERNet 的 MPS 路径改为在 CPU 物化目标 FP16 权重,再通过公开 `model.to(mps)` 串行搬运;CPU/CUDA 继续直接按目标设备加载。不会设置或修改进程级 HF_DEACTIVATE_ASYNC_LOAD。真实公式 token 与迁移基线一致。
diff --git a/mineru/model/mfr/unimernet/Unimernet.py b/mineru/model/mfr/unimernet/Unimernet.py
index 6cfa1154..017dc063 100644
--- a/mineru/model/mfr/unimernet/Unimernet.py
+++ b/mineru/model/mfr/unimernet/Unimernet.py
@@ -25,16 +25,19 @@ class MathDataset(Dataset):
class UnimernetModel(object):
def __init__(self, weight_dir: str, _device_: str = "cpu") -> None:
- """直接按目标设备和精度加载权重,避免保留一次完整的 CPU FP32 中转分配。"""
+ """按目标精度加载权重;MPS 先在 CPU 物化 FP16,再串行转移,避开并行加载的原生崩溃。"""
from .unimernet_hf import UnimernetModel
self.device = torch.device(_device_)
+ load_device = torch.device("cpu") if self.device.type == "mps" else self.device
self.model = UnimernetModel.from_pretrained(
weight_dir,
- device_map={"": self.device},
+ device_map={"": load_device},
dtype=torch.float32 if self.device.type == "cpu" else torch.float16,
attn_implementation="eager" if self.device.type in {"mps", "npu", "musa"} else None,
)
+ if self.device.type == "mps":
+ self.model.to(self.device)
self.model.eval()
@staticmethod
diff --git a/tests/unittest/test_transformers5_models.py b/tests/unittest/test_transformers5_models.py
index d304b3e0..2cde6c6f 100644
--- a/tests/unittest/test_transformers5_models.py
+++ b/tests/unittest/test_transformers5_models.py
@@ -226,3 +226,27 @@ def test_layout_position_embedding_preserves_native_math_on_mps() -> None:
assert actual.device.type == "mps"
torch.testing.assert_close(actual.cpu(), expected, rtol=0, atol=0)
assert not adapter.state_dict()
+
+
[email protected]("device", ["cpu", "mps", "cuda"])
+def test_unimernet_stages_only_mps_weights_on_cpu(monkeypatch: pytest.MonkeyPatch, device: str) -> None:
+ """MPS 在目标精度下经 CPU 物化后串行搬运,CPU/CUDA 保持直接加载。"""
+ from types import SimpleNamespace
+
+ from mineru.model.mfr.unimernet.Unimernet import UnimernetModel as Wrapper
+ from mineru.model.mfr.unimernet.unimernet_hf import UnimernetModel as Model
+
+ calls = []
+ moves = []
+ model = SimpleNamespace(to=lambda target: moves.append(str(target)), eval=lambda: None)
+
+ def load(*args: object, **kwargs: object) -> object:
+ """只记录公开加载协议,测试不分配真实设备或模型权重。"""
+ calls.append(kwargs)
+ return model
+
+ monkeypatch.setattr(Model, "from_pretrained", load)
+ Wrapper("local-checkpoint", device)
+ assert str(calls[0]["device_map"][""]) == ("cpu" if device == "mps" else device)
+ assert calls[0]["dtype"] == (torch.float32 if device == "cpu" else torch.float16)
+ assert moves == (["mps"] if device == "mps" else [])