Skip to content

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

fix(hardware): preserve Windows DXGI memory in GPU inventory

Xiaomeng Zhao5c832ae37ff8
diff --git a/mineru/model/runtime/hardware.py b/mineru/model/runtime/hardware.py
index a5b6ff65..f6479ea3 100644
--- a/mineru/model/runtime/hardware.py
+++ b/mineru/model/runtime/hardware.py
@@ -17,7 +17,7 @@ from .hardware_probe import run_probe
from .hardware_system import collect_system_hardware
from .hardware_types import GpuInfo, HardwareInfo, HardwareInfoSection, PackageInfo, RuntimeProbe
from .onnx_calibration_cache import inspect_calibration_record
-from .gpu import GPU_KIND_ORDER, GpuIdentity, gpu_identities_match, match_gpu_identities
+from .gpu import GPU_KIND_ORDER, GpuIdentity, PciAddress, gpu_identities_match, match_gpu_identities
if TYPE_CHECKING:
from .webgpu import WebGPUDevice
@@ -99,8 +99,26 @@ def config_summary() -> dict[str, Any]:
}
+def _merge_gpu_facts(target: GpuInfo, row: dict[str, Any], *, native_unique: bool = True) -> None:
+ kind = row.get("kind", "unknown")
+ target.kind = kind if kind in GPU_KIND_ORDER and native_unique else "unknown"
+ target.kind_source = row.get("kind_source", "unknown")
+ target.unified_memory = row.get("unified_memory")
+ for attribute in ("dedicated_memory_bytes", "shared_memory_bytes"):
+ if row.get(attribute) is not None:
+ setattr(target, attribute, row[attribute])
+
+
+def _match_dxgi_pci(identities: list[GpuIdentity], targets: list[GpuIdentity]) -> dict[int, int]:
+ """仅比较已知 PCI 地址,避免无地址的同型号条目阻止物理匹配。"""
+ sources = [(index, identity) for index, identity in enumerate(identities) if identity.pci_address is not None]
+ destinations = [(index, identity) for index, identity in enumerate(targets) if identity.bus_id]
+ matches = match_gpu_identities([identity for _, identity in sources], [identity for _, identity in destinations])
+ return {index: destinations[match][0] for (index, _), match in zip(sources, matches) if match is not None}
+
+
def merge_gpu_inventory(gpus: list[GpuInfo], probe: RuntimeProbe) -> None:
- """按明确身份补充原生类型和显存;无法唯一匹配的多卡库存不猜测归属。"""
+ """优先按身份补充显卡属性;DXGI 的同型号条目可补充一致的属性。"""
if probe.status != "ok":
return
vendors = {0x8086: "Intel", 0x10DE: "NVIDIA", 0x1002: "AMD"}
@@ -108,18 +126,26 @@ def merge_gpu_inventory(gpus: list[GpuInfo], probe: RuntimeProbe) -> None:
row for row in probe.data.get("devices", []) if row.get("vendor_id") is not None and row.get("device_id") is not None
]
identities = [
- GpuIdentity(row["vendor_id"], row["device_id"], luid=row.get("luid"), bus_id=row.get("bus_id"), name=row.get("name"))
+ GpuIdentity(
+ row["vendor_id"],
+ row["device_id"],
+ luid=row.get("luid"),
+ bus_id=row.get("bus_id"),
+ name=row.get("name"),
+ pci_address=PciAddress(**row["pci_address"]) if row.get("pci_address") is not None else None,
+ )
for row in rows
]
targets = [gpu.identity for gpu in gpus]
matches = match_gpu_identities(identities, targets)
+ if probe.name == "dxgi":
+ pci_matches = _match_dxgi_pci(identities, targets)
+ pci_targets = set(pci_matches.values())
+ matches = tuple(pci_matches.get(index, None if match in pci_targets else match) for index, match in enumerate(matches))
native_matches = match_gpu_identities(identities, identities)
for index, (row, matched) in enumerate(zip(rows, matches)):
vendor, chip = row["vendor_id"], row["device_id"]
name = row.get("name")
- kind = row.get("kind", "unknown")
- if kind not in GPU_KIND_ORDER or native_matches[index] is None:
- kind = "unknown"
if matched is not None:
target = gpus[matched]
target.name = target.name or name
@@ -133,12 +159,25 @@ def merge_gpu_inventory(gpus: list[GpuInfo], probe: RuntimeProbe) -> None:
source=probe.name,
)
gpus.append(target)
- target.kind = kind
- target.kind_source = row.get("kind_source", "unknown")
- target.unified_memory = row.get("unified_memory")
- for attribute in ("dedicated_memory_bytes", "shared_memory_bytes"):
- if row.get(attribute) is not None:
- setattr(target, attribute, row[attribute])
+ _merge_gpu_facts(target, row, native_unique=native_matches[index] is not None)
+
+ if probe.name != "dxgi":
+ return
+ for index, identity in enumerate(targets):
+ model = (identity.vendor_id, identity.device_id)
+ if index in matches or sum((target.vendor_id, target.device_id) == model for target in targets) != 1:
+ continue
+ compatible = [i for i, native in enumerate(identities) if gpu_identities_match(identity, native)]
+ if len(compatible) < 2:
+ continue
+ # 缺少可区分的身份时只报告一致的事实。选型可用的最小容量不能作为单卡的实际容量。
+ first, *others = [rows[i] for i in compatible]
+ facts = {
+ key: first[key]
+ for key in ("kind", "kind_source", "unified_memory", "dedicated_memory_bytes", "shared_memory_bytes")
+ if key in first and all(row.get(key) == first[key] for row in others)
+ }
+ _merge_gpu_facts(gpus[index], facts)
def _calibration_adapters(probes: list[RuntimeProbe]) -> tuple[GpuAdapter | WebGPUDevice, ...] | None:
diff --git a/tests/unittest/test_hardware_info.py b/tests/unittest/test_hardware_info.py
index e2b0a7bf..9a945bad 100644
--- a/tests/unittest/test_hardware_info.py
+++ b/tests/unittest/test_hardware_info.py
@@ -237,6 +237,73 @@ def test_windows_cim_and_dxgi_memory() -> None:
assert len(result.gpus) == 1 and result.gpus[0].dedicated_memory_bytes == 8 * 1024**3
[email protected]("reverse", [False, True])
+ ("bus_id", "alias_memory_gib", "expected_memory_gib"),
+ [(None, 8, 8), (None, 16, None), (None, None, None), ("0000:01:00.0", 16, 8)],
+)
+def test_windows_dxgi_duplicate_model_preserves_known_memory(
+ reverse: bool, bus_id: str | None, alias_memory_gib: int | None, expected_memory_gib: int | None
+) -> None:
+ """优先使用可唯一关联的 PCI 条目,否则只补充同型号条目一致的数据。"""
+ gib = 1024**3
+ result = SystemHardware()
+ hardware_system.parse_windows_hardware(
+ {"gpus": {"Name": "Radeon", "PNPDeviceID": "PCI\\VEN_1002&DEV_7550", "DriverVersion": "test"}}, result
+ )
+ result.gpus[0].bus_id = bus_id
+ physical = {
+ "vendor_id": 0x1002,
+ "device_id": 0x7550,
+ "luid": "physical",
+ "pci_address": {"bus": 1, "device": 0, "domain": None, "function": 0},
+ "kind": "discrete",
+ "kind_source": "d3d12",
+ "unified_memory": False,
+ "dedicated_memory_bytes": 8 * gib,
+ "shared_memory_bytes": 16 * gib,
+ }
+ alias = {
+ **physical,
+ "luid": "other",
+ "pci_address": None,
+ "dedicated_memory_bytes": alias_memory_gib * gib if alias_memory_gib is not None else None,
+ }
+ devices = [alias, physical] if reverse else [physical, alias]
+
+ hardware.merge_gpu_inventory(result.gpus, RuntimeProbe("dxgi", "ok", {"devices": devices}))
+
+ (gpu,) = result.gpus
+ assert gpu.name == "Radeon" and gpu.driver == "test" and gpu.source == "cim"
+ assert (gpu.kind, gpu.kind_source, gpu.unified_memory) == ("discrete", "d3d12", False)
+ assert gpu.shared_memory_bytes == 16 * gib
+ assert gpu.dedicated_memory_bytes == (expected_memory_gib * gib if expected_memory_gib is not None else None)
+
+
[email protected]("has_bus_id", [False, True])
+def test_windows_dxgi_same_model_physical_cards_use_pci_addresses(has_bus_id: bool) -> None:
+ """同型号多张卡按 PCI 地址保留各自的容量,缺少系统地址时不猜测归属。"""
+ gpus = [
+ GpuInfo(name="Radeon", device_id="0x1002:0x7550", bus_id=f"0000:0{bus}:00.0" if has_bus_id else None) for bus in (1, 2)
+ ]
+ devices = [
+ {
+ "vendor_id": 0x1002,
+ "device_id": 0x7550,
+ "luid": str(bus),
+ "pci_address": {"bus": bus, "device": 0, "domain": None, "function": 0} if bus else None,
+ "kind": "discrete",
+ "dedicated_memory_bytes": capacity * 1024**3,
+ }
+ for bus, capacity in ((2, 16), (0, 4), (1, 8))
+ ]
+
+ hardware.merge_gpu_inventory(gpus, RuntimeProbe("dxgi", "ok", {"devices": devices}))
+
+ expected = [8 * 1024**3, 16 * 1024**3] if has_bus_id else [None, None]
+ assert [gpu.dedicated_memory_bytes for gpu in gpus] == expected
+
+
def test_windows_cim_partial_failure_keeps_memory_and_gpu(monkeypatch: pytest.MonkeyPatch) -> None:
"""一个 CIM 类不可用时,其余硬件查询仍独立运行并进入报告。"""
run = Mock(side_effect=[None, '{"TotalVisibleMemorySize": 1024}', '{"Name": "GPU"}'])