Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.
Python ai4sciencedocument-analysisdocxextract-datalayout-analysisocrparserpdfpdf-converterpdf-extractor-llmpdf-extractor-pretrainpdf-extractor-ragpdf-parserpptxpythonxlsx
feat(onnx): add Linux WebGPU and compact OCR recognition outputs
Xiaomeng Zhaoa2a4960b088a
diff --git a/docs/next/benchmarks/onnx-compact-rec-linux-20261021.json b/docs/next/benchmarks/onnx-compact-rec-linux-20261021.jsonnew file mode 100644index 00000000..e1f01ffa--- /dev/null+++ b/docs/next/benchmarks/onnx-compact-rec-linux-20261021.json@@ -0,0 +1,1166 @@+{+ "source_sha256": {+ "mineru/model/runtime/onnx_rec.py": "f5a39fe197259db8155a5593e657f6b6f528c24a62a4c99cd1a431e0cfb9ee05",+ "mineru/model/runtime/onnx_calibration.py": "43619463ab179d70ccaf21fa3bf31e689b6c1eef7e423af70f403669b1656ad9",+ "mineru/model/ocr/pp_ocr_v6_onnx.py": "5c3914cfeb81dd80bb598480308f95aa7136f1d20e9ab32d22430b0727805de1",+ "scripts/benchmark_onnx_selection.py": "d5e4799a14c3cafd99ad479705cc0afb22c363520d8137669c835552d8b829fd",+ "pyproject.toml": "5516ee13419dd6858186642cfa615dead866ccd7d6ad57b45a6fd592578ea8dd"+ },+ "packages": {+ "mineru": "4.0.12",+ "docvortex": "0.5.16",+ "onnxruntime": "1.31.0",+ "onnxruntime-ep-webgpu": "0.4.0",+ "onnx": "1.23.2"+ },+ "input_sha256": "06d0e74125b5d2e1fc04e658bbe4af39422d70abfaa453a87353df4d3dad3432",+ "pages": 19,+ "tier": "basic",+ "parse_mode": "ocr",+ "cpu_threads": [+ 4,+ 1+ ],+ "parse_runs_s": {+ "cpu": [+ 58.01219562500046,+ 58.36799849199815+ ],+ "webgpu": [+ 34.72620028100209,+ 35.07572708299995+ ]+ },+ "parse_mean_s": {+ "cpu": 58.190097058499305,+ "webgpu": 34.90096368200102+ },+ "speedup": 1.6672919862241184,+ "latency_reduction_percent": 40.02250306110574,+ "output_equality": {+ "cpu-1": {+ "files": 50,+ "identical_to_pre_optimization": true,+ "different": []+ },+ "webgpu-1": {+ "files": 50,+ "identical_to_pre_optimization": true,+ "different": []+ },+ "webgpu-2": {+ "files": 50,+ "identical_to_pre_optimization": true,+ "different": []+ },+ "cpu-2": {+ "files": 50,+ "identical_to_pre_optimization": true,+ "different": []+ },+ "cpu-observed": {+ "files": 50,+ "identical_to_pre_optimization": true,+ "different": []+ },+ "webgpu-observed": {+ "files": 50,+ "identical_to_pre_optimization": true,+ "different": []+ }+ },+ "output_sha256": {+ "markdown.md": "9142d0839d53eaa7c4729ac98a527f5964d285c62c88d28ca27777ea037a4967",+ "model_output.json": "fa9931a3e5ded5c8a34be2f1aaf41b663a64cbb1bbc27bdd8004adb1af66a4d4",+ "structured_content.json": "eb79172580f792a4221559b1404da57b94bf22683f6b7bef71104659b47b89a5",+ "middle_json.json": "c1fa7b7db802f712fe0a5e610a4e48a0b9f28c00a5b8029e8eaa511fb1cb14ff",+ "images/page_13_image_11.jpg": "be117bf500682716150d9dd1f1b3b922200130b39c7cbe5ca620f63ee04632dc",+ "images/page_15_equation_16.jpg": "868742debacf51f77328b76b3c5f8d5fa73a26bef9bad2fe12b14d65611cdcb2",+ "images/page_16_chart_18.jpg": "d01c202706b4d448134e1e253fd7f6dd4c14daf76b709cdf8da0d5f46507fe9e",+ "images/page_17_table_8.jpg": "394019c1ef702472f2f238e10a1737a6da7c39d6ffdf496c1020327b4c28b1fd",+ "images/page_16_image_3.jpg": "cbecc1f8730c93d067205797ee1dbe88260feace7d7ecad485d34ecefe857bbc",+ "images/page_15_equation_9.jpg": "08d67ef1776675e989f9573029e19d0c19348868eeb1b3786da3a89da4744fc4",+ "images/page_9_chart_2.jpg": "3c46e7e9e5465ea1cbbf6afaf385320797be53e25b5ba4d15e5c989002539a9c",+ "images/page_9_chart_4.jpg": "6d805da34431e18db08f93bec3080d7c9ead385e899f68e38f53c2fa6cdb2839",+ "images/page_16_chart_14.jpg": "d979e20eaf23109d7e54fba34603d4d1caeabb70b6333850e9b43aadeedcfd2e",+ "images/page_16_table_21.jpg": "51f55e1584a0c7fa6e9f879a3b9f93e961c9443a6d74e8affc47bda38a3b8319",+ "images/page_15_equation_14.jpg": "2270e1dfc84b58916076cf78cb8f2bc23779cebd03af9a173b2b155b036548a4",+ "images/page_7_table_8.jpg": "9ed42df9904ddcd5d5af2f667b9e5446056925668dbb6d862102f967da29b3d8",+ "images/page_15_equation_3.jpg": "11564c2a567e6da0589509a7ef6e585c35492e85c4a9b9fa8fd513b47a9c75d0",+ "images/page_8_table_3.jpg": "60fe378920ea852fad86de41886800728c5d036efdaade99e1a7289ef2a995d1",+ "images/page_15_equation_11.jpg": "0a075b375893a207aeb1c7b240ffc9c07acb1afda15c71b477c859235ddcf464",+ "images/page_3_equation_7.jpg": "9ccb3c83e55162e7c1280e4a3f54c45aa9001ce9a6bc74adb3f6353d1102d573",+ "images/page_5_table_3.jpg": "3111348c57b8e83bc643895dd73402ce69c89fc96aa1198064b3ca56894479e9",+ "images/page_3_equation_12.jpg": "d88b0bc8ba9a653c449065c04fe87fd57c5bb2e6234d1b75bc3a4d16ccc820f5",+ "images/page_16_image_1.jpg": "647acd6d3af08d5940c8ef1bfb39bfd9c4c56cf747fdd539c2ba0d9c4e9e9a62",+ "images/page_14_equation_14.jpg": "57ef7d35dd9649a0fa5deac616e2ff91ea4587aeaffb1a3e95fca60d1f867054",+ "images/page_9_chart_3.jpg": "75d98040f6e379bf9a2696c03138a407a49825f68d00e96de3826f661e9c1b90",+ "images/page_16_chart_23.jpg": "6ac062212e9a6fa347a4b28024b23d93099e5fa5408912186b976ddc7a13ff23",+ "images/page_4_table_2.jpg": "0dc7186c401c1d905c6d81d46b0ae6445a75268c1d403371ddd31be1f3997203",+ "images/page_16_image_9.jpg": "182194635b484c5fd1b4c456a9d65d32e23a21f7be03253c2ea349ee9a6c88db",+ "images/page_6_chart_6.jpg": "feb686096d47cc37495d5b21e1cc51f0f3ca3a64c78ea6ae749e436c555e5779",+ "images/page_14_equation_9.jpg": "b0c12293bb6d0058b57e2f2c54b1d33b6d3179a1494ed5febbf977ef423a5b9f",+ "images/page_3_equation_15.jpg": "18a2e8bd83607954b3e1eb8a5dd721b90e541c7b9c198b9ad422798aba275883",+ "images/page_3_chart_3.jpg": "c4c90e869a40933316ae48c298101209c14cc5c5ac1a9e0eeb9170f24b34db8b",+ "images/page_16_image_6.jpg": "95d410c578f368239664e599221d283d18ca0e19eb19833d64690cfb43cfb46d",+ "images/page_3_chart_2.jpg": "8a8b16ea4b86652bb02c00ff10cdace6129adb4dc19d3a36bbbf5750ce061d12",+ "images/page_4_equation_6.jpg": "88f7be69d49ca8ccf85cfd0f8657297eede1a854f5c09460f5d3cfc4d06d7f89",+ "images/page_7_chart_2.jpg": "c5c21a815198e7506e84d623df06d7f7e3ae20b211f408d50d975e33b27561d1",+ "images/page_15_equation_5.jpg": "9daebf881158c3f664f87d7e0496a16fabcd009836b3c9ef51b319e8d8ceeffb",+ "images/page_16_image_7.jpg": "a8e871bc97ab38db8f5931eec24bb906378e49a554d36e040027029d85b0c3ab",+ "images/page_16_image_5.jpg": "3c5903ab9284b355a119ec17588de9754bb6202f42f14bee35060c53443f3ed1",+ "images/page_16_image_2.jpg": "ab6238c6e9844c4ecfde441abb3963f16f5e7a27257b893a441dd179ecc51513",+ "images/page_1_chart_5.jpg": "d1ee19731defdb300d9580d6d329d958fad630030990f7415c34a3830e54b841",+ "images/page_17_image_1.jpg": "e4ea650de3b18db3a173d5ff5d740253fbf1db8f96e6cd17dbff22bb18056251",+ "images/page_16_image_4.jpg": "8a4510bc7d8d1f7442f73604c17cb6ed2615c968d2fa61842041c4f283d15287",+ "images/page_3_equation_17.jpg": "1baa062a34c09942467bcc14c10674293395a7704858749bf453dd77a9a637c0",+ "images/page_14_equation_12.jpg": "657d2d96ba69612291f00774b7a0d5eaa279961441ff29472fd83168909ee925",+ "images/page_5_table_11.jpg": "cc2b17956f522cd92ca1783f04f09cac27e8ba9967be5e303d99d9d2025f172f",+ "images/page_16_chart_16.jpg": "2279720fe903a7f4361f3526202a9704d6990f2f0fd43b9e4e6689191df9a8f8",+ "images/page_2_equation_8.jpg": "c972cd1cb152da6e2a640710f3d4d2749e652969523ebdf4fe2df71892909328",+ "images/page_17_image_2.jpg": "6b032342d4b1984e3d5d51ecc5833a529d2d4061c3deb10af9d17a1ffa7c55e4",+ "images/page_2_equation_10.jpg": "c4e81fd6d6915ede281f8690f6c4a41a67f34c06254edd0e9599b358ecd50400"+ },+ "stages": [+ {+ "kind": "layout",+ "cpu": {+ "run_s": 4.870401120999304,+ "calls": 10,+ "init_s": 0.43910526699983166,+ "first_call_s": 0.517011445001117,+ "providers": [+ [+ "CPUExecutionProvider"+ ]+ ]+ },+ "webgpu": {+ "run_s": 2.007709272002103,+ "calls": 10,+ "init_s": 0.4077780369989341,+ "first_call_s": 0.2706489000011061,+ "providers": [+ [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ]+ ]+ }+ },+ {+ "kind": "mfr",+ "cpu": {+ "run_s": 22.89177096300409,+ "calls": 19,+ "init_s": 0.4750565270005609,+ "first_call_s": 1.0405509030024405,+ "providers": [+ [+ "CPUExecutionProvider"+ ]+ ]+ },+ "webgpu": {+ "run_s": 12.725902801008488,+ "calls": 19,+ "init_s": 0.9487311100019724,+ "first_call_s": 3.35410297099952,+ "providers": [+ [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ],+ [+ "CPUExecutionProvider"+ ]+ ]+ }+ },+ {+ "kind": "ocr_det",+ "cpu": {+ "run_s": 1.1751816109826905,+ "calls": 84,+ "init_s": 0.027225731002545217,+ "first_call_s": 0.08009637899886002,+ "providers": [+ [+ "CPUExecutionProvider"+ ],+ [+ "CPUExecutionProvider"+ ]+ ]+ },+ "webgpu": {+ "run_s": 0.7148913109886053,+ "calls": 84,+ "init_s": 0.06776729800185421,+ "first_call_s": 0.04853435699624242,+ "providers": [+ [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ],+ [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ]+ ]+ }+ },+ {+ "kind": "ocr_rec",+ "cpu": {+ "run_s": 18.462896179997188,+ "calls": 336,+ "init_s": 0.060626273003435927,+ "first_call_s": 0.046313678998558316,+ "providers": [+ [+ "CPUExecutionProvider"+ ],+ [+ "CPUExecutionProvider"+ ]+ ]+ },+ "webgpu": {+ "run_s": 7.276892408011918,+ "calls": 336,+ "init_s": 0.060488333998364396,+ "first_call_s": 0.026489790001505753,+ "providers": [+ [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ],+ [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ]+ ]+ }+ },+ {+ "kind": "unet",+ "cpu": {+ "run_s": 0.2372134430006554,+ "calls": 5,+ "init_s": 0.019886376001522876,+ "first_call_s": 0.061711741000181064,+ "providers": [+ [+ "CPUExecutionProvider"+ ]+ ]+ },+ "webgpu": {+ "run_s": 0.10968197900001542,+ "calls": 5,+ "init_s": 0.026280217000021366,+ "first_call_s": 0.03117835000011837,+ "providers": [+ [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ]+ ]+ }+ },+ {+ "kind": "table_cls",+ "cpu": {+ "run_s": 0.006203835000633262,+ "calls": 1,+ "init_s": null,+ "first_call_s": 0.006203835000633262,+ "providers": [+ [+ "CPUExecutionProvider"+ ]+ ]+ },+ "webgpu": {+ "run_s": 0.006124595001892885,+ "calls": 1,+ "init_s": null,+ "first_call_s": 0.006124595001892885,+ "providers": [+ [+ "CPUExecutionProvider"+ ]+ ]+ }+ },+ {+ "kind": "slanet_plus",+ "cpu": {+ "run_s": 0.13035511299676728,+ "calls": 2,+ "init_s": null,+ "first_call_s": 0.08862819299974944,+ "providers": [+ [+ "CPUExecutionProvider"+ ]+ ]+ },+ "webgpu": {+ "run_s": 0.11231618400051957,+ "calls": 2,+ "init_s": null,+ "first_call_s": 0.07153398700029356,+ "providers": [+ [+ "CPUExecutionProvider"+ ]+ ]+ }+ }+ ],+ "mfr_batches": [+ {+ "route": "cpu",+ "batch": 8,+ "calls": 18,+ "provider": "CPUExecutionProvider",+ "run_s": 19.992539091002982+ },+ {+ "route": "cpu",+ "batch": 7,+ "calls": 1,+ "provider": "CPUExecutionProvider",+ "run_s": 2.89923187200111+ },+ {+ "route": "webgpu",+ "batch": 8,+ "calls": 18,+ "provider": "WebGpuExecutionProvider",+ "run_s": 9.773598293009854+ },+ {+ "route": "webgpu",+ "batch": 7,+ "calls": 1,+ "provider": "CPUExecutionProvider",+ "run_s": 2.952304507998633+ }+ ],+ "rec_validation": {+ "source": "/home/myhloli/.mineru/models/MinerU-4_models_onnx/OCR/paddleocr/ch_PP-OCRv6_small_rec_infer.onnx",+ "compact": "/home/myhloli/.mineru/models/.derived/ocr-rec/ctc-ids-scores-v1-5435fd747c9e0efe15a96d0b378d5bd157e9492ed8fd80edf08f30d02fa24634.onnx",+ "comparisons": [+ {+ "device": "cpu",+ "shape": [+ 1,+ 3,+ 48,+ 320+ ],+ "providers": [+ "CPUExecutionProvider"+ ],+ "ids_equal": true,+ "scores_equal": true,+ "scores_max_abs": 0.0,+ "dense_bytes": 2993600,+ "compact_bytes": 480+ },+ {+ "device": "cpu",+ "shape": [+ 6,+ 3,+ 48,+ 320+ ],+ "providers": [+ "CPUExecutionProvider"+ ],+ "ids_equal": true,+ "scores_equal": true,+ "scores_max_abs": 0.0,+ "dense_bytes": 17961600,+ "compact_bytes": 2880+ },+ {+ "device": "cpu",+ "shape": [+ 7,+ 3,+ 48,+ 320+ ],+ "providers": [+ "CPUExecutionProvider"+ ],+ "ids_equal": true,+ "scores_equal": true,+ "scores_max_abs": 0.0,+ "dense_bytes": 20955200,+ "compact_bytes": 3360+ },+ {+ "device": "cpu",+ "shape": [+ 6,+ 3,+ 48,+ 1012+ ],+ "providers": [+ "CPUExecutionProvider"+ ],+ "ids_equal": true,+ "scores_equal": true,+ "scores_max_abs": 0.0,+ "dense_bytes": 56579040,+ "compact_bytes": 9072+ },+ {+ "device": "cpu",+ "shape": [+ 1,+ 3,+ 48,+ 1012+ ],+ "providers": [+ "CPUExecutionProvider"+ ],+ "ids_equal": true,+ "scores_equal": true,+ "scores_max_abs": 0.0,+ "dense_bytes": 9429840,+ "compact_bytes": 1512+ },+ {+ "device": "webgpu",+ "shape": [+ 1,+ 3,+ 48,+ 320+ ],+ "providers": [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ],+ "ids_equal": true,+ "scores_equal": true,+ "scores_max_abs": 0.0,+ "dense_bytes": 2993600,+ "compact_bytes": 480+ },+ {+ "device": "webgpu",+ "shape": [+ 6,+ 3,+ 48,+ 320+ ],+ "providers": [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ],+ "ids_equal": true,+ "scores_equal": true,+ "scores_max_abs": 0.0,+ "dense_bytes": 17961600,+ "compact_bytes": 2880+ },+ {+ "device": "webgpu",+ "shape": [+ 7,+ 3,+ 48,+ 320+ ],+ "providers": [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ],+ "ids_equal": true,+ "scores_equal": true,+ "scores_max_abs": 0.0,+ "dense_bytes": 20955200,+ "compact_bytes": 3360+ },+ {+ "device": "webgpu",+ "shape": [+ 6,+ 3,+ 48,+ 1012+ ],+ "providers": [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ],+ "ids_equal": true,+ "scores_equal": true,+ "scores_max_abs": 0.0,+ "dense_bytes": 56579040,+ "compact_bytes": 9072+ },+ {+ "device": "webgpu",+ "shape": [+ 1,+ 3,+ 48,+ 1012+ ],+ "providers": [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ],+ "ids_equal": true,+ "scores_equal": true,+ "scores_max_abs": 0.0,+ "dense_bytes": 9429840,+ "compact_bytes": 1512+ }+ ],+ "profile": {+ "path": "/home/myhloli/PycharmProjects/mineru/Magic-PDF/demo/output/compact-rec-production-20261021/compact-rec-profile_2026-10-21_18-49-38_826.json",+ "reductions": [+ {+ "node": "mineru_rec_scores_kernel_time",+ "op": "ReduceMax",+ "provider": "WebGpuExecutionProvider"+ },+ {+ "node": "mineru_rec_ids_kernel_time",+ "op": "ArgMax",+ "provider": "WebGpuExecutionProvider"+ }+ ],+ "providers": {+ "WebGpuExecutionProvider": 206,+ "CPUExecutionProvider": 3+ },+ "cpu_nodes": [+ "Gather_kernel_time",+ "Slice.2_kernel_time",+ "Concat.2_kernel_time"+ ]+ }+ },+ "preparation": {+ "cold_s": 0.29513571099960245,+ "warm_s": 0.03516311000203132,+ "model": {+ "path": "/home/myhloli/.mineru/models/.derived/ocr-rec/ctc-ids-scores-v1-5435fd747c9e0efe15a96d0b378d5bd157e9492ed8fd80edf08f30d02fa24634.onnx",+ "num_classes": 18710,+ "source_sha256": "5435fd747c9e0efe15a96d0b378d5bd157e9492ed8fd80edf08f30d02fa24634",+ "model_sha256": "ec2795ae0a82d64c9a7c5ab21dbff6ca797d1a3f40b0211d8176fa4fbcbf99d4"+ }+ },+ "calibration": {+ "label": "8060s-compact-rec",+ "backend": "webgpu",+ "expected": "gpu",+ "environment": {+ "host": "myhloli-ubuntu-ssd",+ "os": "Linux-7.0.0-38-generic-x86_64-with-glibc2.43",+ "cpu": ""+ },+ "adapter": {+ "vendor_id": 4098,+ "device_id": 5510,+ "metadata": [+ [+ "card_idx",+ "1"+ ],+ [+ "pci_bus_id",+ "0000:c3:00.0"+ ]+ ]+ },+ "threshold": null,+ "runs": [+ {+ "schema_version": 3,+ "backend": "webgpu",+ "ep_version": "0.4.0",+ "webgpu_options": {+ "preferredLayout": "NHWC",+ "enableGraphCapture": "0",+ "dawnBackendType": "Vulkan",+ "powerPreference": "high-performance"+ },+ "adapter": {+ "vendor_id": 4098,+ "device_id": 5510,+ "metadata": [+ [+ "card_idx",+ "1"+ ],+ [+ "pci_bus_id",+ "0000:c3:00.0"+ ]+ ]+ },+ "input": {+ "manifest_sha256": "0d98e210345258583f72b784e0114359f9459bbcb1250716566c02e5d1851e06",+ "input_sha256": "474fa06cbc4c20303081e85084b45f3a4f24190d1141e4bd3bf3f3feb432c078",+ "shape": [+ 6,+ 3,+ 48,+ 1012+ ],+ "sample_order": [+ 0,+ 2,+ 4,+ 1,+ 5,+ 3+ ]+ },+ "model_sha256": "ec2795ae0a82d64c9a7c5ab21dbff6ca797d1a3f40b0211d8176fa4fbcbf99d4",+ "source_model_sha256": "5435fd747c9e0efe15a96d0b378d5bd157e9492ed8fd80edf08f30d02fa24634",+ "output_format": "ctc-ids-scores-v1",+ "ort_version": "1.31.0",+ "ort_path": "/home/myhloli/PycharmProjects/mineru/Magic-PDF/.venv/lib/python3.14/site-packages/onnxruntime/__init__.py",+ "python": "3.14.4 (main, Aug 20 2026, 10:41:58) [GCC 15.2.0]",+ "intra_threads": 4,+ "inter_threads": 1,+ "rec_metacommands": null,+ "gpu_first": false,+ "cpu": {+ "init_s": 0.031883453997579636,+ "first_s": 0.0750287369992293,+ "timed_s": [+ 0.06333165099931648,+ 0.06591120399752981+ ],+ "providers": [+ "CPUExecutionProvider"+ ]+ },+ "cpu_elapsed": 0.1292428549968463,+ "gpu": {+ "init_s": 0.06016382600137149,+ "first_s": 0.055584640002052765,+ "timed_s": [+ 0.02562274299998535,+ 0.02406474899908062+ ],+ "providers": [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ]+ },+ "gpu_elapsed": 0.04968749199906597,+ "ratio": 0.38445059110059443,+ "outputs_match": true,+ "worker_wall_s": 0.7388802360001137+ },+ {+ "schema_version": 3,+ "backend": "webgpu",+ "ep_version": "0.4.0",+ "webgpu_options": {+ "preferredLayout": "NHWC",+ "enableGraphCapture": "0",+ "dawnBackendType": "Vulkan",+ "powerPreference": "high-performance"+ },+ "adapter": {+ "vendor_id": 4098,+ "device_id": 5510,+ "metadata": [+ [+ "card_idx",+ "1"+ ],+ [+ "pci_bus_id",+ "0000:c3:00.0"+ ]+ ]+ },+ "input": {+ "manifest_sha256": "0d98e210345258583f72b784e0114359f9459bbcb1250716566c02e5d1851e06",+ "input_sha256": "474fa06cbc4c20303081e85084b45f3a4f24190d1141e4bd3bf3f3feb432c078",+ "shape": [+ 6,+ 3,+ 48,+ 1012+ ],+ "sample_order": [+ 0,+ 2,+ 4,+ 1,+ 5,+ 3+ ]+ },+ "model_sha256": "ec2795ae0a82d64c9a7c5ab21dbff6ca797d1a3f40b0211d8176fa4fbcbf99d4",+ "source_model_sha256": "5435fd747c9e0efe15a96d0b378d5bd157e9492ed8fd80edf08f30d02fa24634",+ "output_format": "ctc-ids-scores-v1",+ "ort_version": "1.31.0",+ "ort_path": "/home/myhloli/PycharmProjects/mineru/Magic-PDF/.venv/lib/python3.14/site-packages/onnxruntime/__init__.py",+ "python": "3.14.4 (main, Aug 20 2026, 10:41:58) [GCC 15.2.0]",+ "intra_threads": 4,+ "inter_threads": 1,+ "rec_metacommands": null,+ "gpu_first": true,+ "gpu": {+ "init_s": 0.06043093699918245,+ "first_s": 0.044275343003391754,+ "timed_s": [+ 0.02494789699994726,+ 0.024185956997825997+ ],+ "providers": [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ]+ },+ "gpu_elapsed": 0.04913385399777326,+ "cpu": {+ "init_s": 0.027198871997825336,+ "first_s": 0.08122084899878246,+ "timed_s": [+ 0.06365568000182975,+ 0.06321708600080456+ ],+ "providers": [+ "CPUExecutionProvider"+ ]+ },+ "cpu_elapsed": 0.1268727660026343,+ "ratio": 0.38726872240456295,+ "outputs_match": true,+ "worker_wall_s": 0.7655493839993142+ },+ {+ "schema_version": 3,+ "backend": "webgpu",+ "ep_version": "0.4.0",+ "webgpu_options": {+ "preferredLayout": "NHWC",+ "enableGraphCapture": "0",+ "dawnBackendType": "Vulkan",+ "powerPreference": "high-performance"+ },+ "adapter": {+ "vendor_id": 4098,+ "device_id": 5510,+ "metadata": [+ [+ "card_idx",+ "1"+ ],+ [+ "pci_bus_id",+ "0000:c3:00.0"+ ]+ ]+ },+ "input": {+ "manifest_sha256": "0d98e210345258583f72b784e0114359f9459bbcb1250716566c02e5d1851e06",+ "input_sha256": "474fa06cbc4c20303081e85084b45f3a4f24190d1141e4bd3bf3f3feb432c078",+ "shape": [+ 6,+ 3,+ 48,+ 1012+ ],+ "sample_order": [+ 0,+ 2,+ 4,+ 1,+ 5,+ 3+ ]+ },+ "model_sha256": "ec2795ae0a82d64c9a7c5ab21dbff6ca797d1a3f40b0211d8176fa4fbcbf99d4",+ "source_model_sha256": "5435fd747c9e0efe15a96d0b378d5bd157e9492ed8fd80edf08f30d02fa24634",+ "output_format": "ctc-ids-scores-v1",+ "ort_version": "1.31.0",+ "ort_path": "/home/myhloli/PycharmProjects/mineru/Magic-PDF/.venv/lib/python3.14/site-packages/onnxruntime/__init__.py",+ "python": "3.14.4 (main, Aug 20 2026, 10:41:58) [GCC 15.2.0]",+ "intra_threads": 4,+ "inter_threads": 1,+ "rec_metacommands": null,+ "gpu_first": false,+ "cpu": {+ "init_s": 0.03344544599895016,+ "first_s": 0.08504245199947036,+ "timed_s": [+ 0.06492397100009839,+ 0.06502255500163301+ ],+ "providers": [+ "CPUExecutionProvider"+ ]+ },+ "cpu_elapsed": 0.1299465260017314,+ "gpu": {+ "init_s": 0.06098806299996795,+ "first_s": 0.056735982001555385,+ "timed_s": [+ 0.028000246002193308,+ 0.024180025000532623+ ],+ "providers": [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ]+ },+ "gpu_elapsed": 0.05218027100272593,+ "ratio": 0.40155187374559503,+ "outputs_match": true,+ "worker_wall_s": 0.7758937399994466+ },+ {+ "schema_version": 3,+ "backend": "webgpu",+ "ep_version": "0.4.0",+ "webgpu_options": {+ "preferredLayout": "NHWC",+ "enableGraphCapture": "0",+ "dawnBackendType": "Vulkan",+ "powerPreference": "high-performance"+ },+ "adapter": {+ "vendor_id": 4098,+ "device_id": 5510,+ "metadata": [+ [+ "card_idx",+ "1"+ ],+ [+ "pci_bus_id",+ "0000:c3:00.0"+ ]+ ]+ },+ "input": {+ "manifest_sha256": "0d98e210345258583f72b784e0114359f9459bbcb1250716566c02e5d1851e06",+ "input_sha256": "474fa06cbc4c20303081e85084b45f3a4f24190d1141e4bd3bf3f3feb432c078",+ "shape": [+ 6,+ 3,+ 48,+ 1012+ ],+ "sample_order": [+ 0,+ 2,+ 4,+ 1,+ 5,+ 3+ ]+ },+ "model_sha256": "ec2795ae0a82d64c9a7c5ab21dbff6ca797d1a3f40b0211d8176fa4fbcbf99d4",+ "source_model_sha256": "5435fd747c9e0efe15a96d0b378d5bd157e9492ed8fd80edf08f30d02fa24634",+ "output_format": "ctc-ids-scores-v1",+ "ort_version": "1.31.0",+ "ort_path": "/home/myhloli/PycharmProjects/mineru/Magic-PDF/.venv/lib/python3.14/site-packages/onnxruntime/__init__.py",+ "python": "3.14.4 (main, Aug 20 2026, 10:41:58) [GCC 15.2.0]",+ "intra_threads": 4,+ "inter_threads": 1,+ "rec_metacommands": null,+ "gpu_first": true,+ "gpu": {+ "init_s": 0.06369562499821768,+ "first_s": 0.04779300600057468,+ "timed_s": [+ 0.043066253998404136,+ 0.03259326700208476+ ],+ "providers": [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ]+ },+ "gpu_elapsed": 0.0756595210004889,+ "cpu": {+ "init_s": 0.030113531996903475,+ "first_s": 0.07573088400022243,+ "timed_s": [+ 0.06892265499845962,+ 0.06573464199755108+ ],+ "providers": [+ "CPUExecutionProvider"+ ]+ },+ "cpu_elapsed": 0.1346572969960107,+ "ratio": 0.5618672191432028,+ "outputs_match": true,+ "worker_wall_s": 0.7759551549970638+ },+ {+ "schema_version": 3,+ "backend": "webgpu",+ "ep_version": "0.4.0",+ "webgpu_options": {+ "preferredLayout": "NHWC",+ "enableGraphCapture": "0",+ "dawnBackendType": "Vulkan",+ "powerPreference": "high-performance"+ },+ "adapter": {+ "vendor_id": 4098,+ "device_id": 5510,+ "metadata": [+ [+ "card_idx",+ "1"+ ],+ [+ "pci_bus_id",+ "0000:c3:00.0"+ ]+ ]+ },+ "input": {+ "manifest_sha256": "0d98e210345258583f72b784e0114359f9459bbcb1250716566c02e5d1851e06",+ "input_sha256": "474fa06cbc4c20303081e85084b45f3a4f24190d1141e4bd3bf3f3feb432c078",+ "shape": [+ 6,+ 3,+ 48,+ 1012+ ],+ "sample_order": [+ 0,+ 2,+ 4,+ 1,+ 5,+ 3+ ]+ },+ "model_sha256": "ec2795ae0a82d64c9a7c5ab21dbff6ca797d1a3f40b0211d8176fa4fbcbf99d4",+ "source_model_sha256": "5435fd747c9e0efe15a96d0b378d5bd157e9492ed8fd80edf08f30d02fa24634",+ "output_format": "ctc-ids-scores-v1",+ "ort_version": "1.31.0",+ "ort_path": "/home/myhloli/PycharmProjects/mineru/Magic-PDF/.venv/lib/python3.14/site-packages/onnxruntime/__init__.py",+ "python": "3.14.4 (main, Aug 20 2026, 10:41:58) [GCC 15.2.0]",+ "intra_threads": 4,+ "inter_threads": 1,+ "rec_metacommands": null,+ "gpu_first": false,+ "cpu": {+ "init_s": 0.031958233998011565,+ "first_s": 0.07483805900119478,+ "timed_s": [+ 0.06482541600053082,+ 0.06541317799928947+ ],+ "providers": [+ "CPUExecutionProvider"+ ]+ },+ "cpu_elapsed": 0.1302385939998203,+ "gpu": {+ "init_s": 0.05753888899926096,+ "first_s": 0.06137950799893588,+ "timed_s": [+ 0.02701583800080698,+ 0.025222251999366563+ ],+ "providers": [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ]+ },+ "gpu_elapsed": 0.05223809000017354,+ "ratio": 0.4010953158803728,+ "outputs_match": true,+ "worker_wall_s": 0.7534385609978926+ }+ ],+ "errors": []+ },+ "previous_parse_mean_s": {+ "cpu": 56.730917255999884,+ "webgpu": 36.16387028100144+ },+ "previous_stages": [+ {+ "kind": "layout",+ "cpu": {+ "run_s": 4.70213598799819,+ "init_s": 0.42865849000008893,+ "first_call_s_per_session_sum": 0.49580722500104457,+ "run_count": 10,+ "providers": [+ [+ "CPUExecutionProvider"+ ]+ ]+ },+ "webgpu": {+ "run_s": 1.9731370310000784,+ "init_s": 0.4100277699981234,+ "first_call_s_per_session_sum": 0.2580047369992826,+ "run_count": 10,+ "providers": [+ [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ]+ ]+ },+ "speedup": 2.383076245654833+ },+ {+ "kind": "mfr",+ "cpu": {+ "run_s": 21.53509693700107,+ "init_s": 0.469981201000337,+ "first_call_s_per_session_sum": 0.967377127999498,+ "run_count": 19,+ "providers": [+ [+ "CPUExecutionProvider"+ ]+ ]+ },+ "webgpu": {+ "run_s": 12.283002249994752,+ "init_s": 0.9152905230002943,+ "first_call_s_per_session_sum": 3.2817498360018362,+ "run_count": 19,+ "providers": [+ [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ],+ [+ "CPUExecutionProvider"+ ]+ ]+ },+ "speedup": 1.7532437508924394+ },+ {+ "kind": "ocr_det",+ "cpu": {+ "run_s": 1.1539976229687454,+ "init_s": 0.027887805001228116,+ "first_call_s_per_session_sum": 0.0785359299989068,+ "run_count": 84,+ "providers": [+ [+ "CPUExecutionProvider"+ ],+ [+ "CPUExecutionProvider"+ ]+ ]+ },+ "webgpu": {+ "run_s": 0.70471396299763,+ "init_s": 0.0710007849993417,+ "first_call_s_per_session_sum": 0.04831984499833197,+ "run_count": 84,+ "providers": [+ [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ],+ [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ]+ ]+ },+ "speedup": 1.637540454087223+ },+ {+ "kind": "ocr_rec",+ "cpu": {+ "run_s": 17.393700485023146,+ "init_s": 0.061026143997878535,+ "first_call_s_per_session_sum": 0.039372651001031045,+ "run_count": 336,+ "providers": [+ [+ "CPUExecutionProvider"+ ],+ [+ "CPUExecutionProvider"+ ]+ ]+ },+ "webgpu": {+ "run_s": 8.936705280044407,+ "init_s": 0.06538126900341013,+ "first_call_s_per_session_sum": 0.0348890860004758,+ "run_count": 336,+ "providers": [+ [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ],+ [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ]+ ]+ },+ "speedup": 1.94632137235891+ },+ {+ "kind": "unet",+ "cpu": {+ "run_s": 0.22482453900011024,+ "init_s": 0.01874217799922917,+ "first_call_s_per_session_sum": 0.0455595639978128,+ "run_count": 5,+ "providers": [+ [+ "CPUExecutionProvider"+ ]+ ]+ },+ "webgpu": {+ "run_s": 0.11035850899497746,+ "init_s": 0.025348870000016177,+ "first_call_s_per_session_sum": 0.03055164399847854,+ "run_count": 5,+ "providers": [+ [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ]+ ]+ },+ "speedup": 2.0372197943553427+ }+ ],+ "timing_notes": "Parse: two fresh processes/device, profiler off, includes init/first inference, excludes imports/save. Stages: separate sys.setprofile observations of ORT run; include first runs, exclude pre/postprocess. No extra parse warmup. Derived cache prepared before timing.",+ "raw_directory": "demo/output/compact-rec-production-20261021",+ "checks": {+ "tests_passed": 401,+ "ruff": "passed",+ "uv_pip_check": "passed"+ }+}diff --git a/docs/next/benchmarks/onnx-webgpu-linux-20261021.json b/docs/next/benchmarks/onnx-webgpu-linux-20261021.jsonnew file mode 100644index 00000000..ca077bf7--- /dev/null+++ b/docs/next/benchmarks/onnx-webgpu-linux-20261021.json@@ -0,0 +1,2050 @@+{+ "date": "2026-10-21",+ "host": "Linux x86_64, AMD Radeon 8060S / RADV STRIX_HALO, Mesa 26.0.8",+ "base_commit": "3702cd7c569f57578b85ef81b792b235e32b2bec",+ "source_sha256": {+ "mineru/model/runtime/webgpu.py": "14394cc70d6b0cb4aa7ff714e0b329ec43f3b00be2f30b307f8e3560b1fe457d",+ "mineru/model/runtime/onnx.py": "65c13749a0fa43974c6844bbe70d88669675e4be8450b7c953197e73e77d28b2",+ "mineru/model/runtime/onnx_device.py": "248fdc64aa244678625d624dab63ab3e78628e1503909a39bfa51a53f9f5c19b",+ "mineru/model/runtime/onnx_calibration.py": "28fc9a0d1102417eb8e9bf7bb1d08fd6978c54fea8bf40d1d08a727495a87a10",+ "mineru/model/mfr/pp_formulanet_plus_m_onnx.py": "b3e0edcdd868c1446e5af04193848422441de7dc46d9fe91b202180cc34e6e56",+ "mineru/model/ocr/pp_ocr_v6_onnx.py": "542caff31fd6597280462c3388504be432b27be97bcd8ceda9250560d0bafbbf",+ "scripts/benchmark_onnx_selection.py": "3346ba9a5f936c0378184c76eb7ad1682101c9102279079ca36965624503ae77",+ "pyproject.toml": "87c8809e7cfb0ebd50db12b062813cc8817d746eb48906ea7b6f501c34645397"+ },+ "raw_directory": "demo/output/webgpu-production-20261021",+ "packages": {+ "mineru": "4.0.12",+ "docvortex": "0.5.16",+ "onnxruntime": "1.31.0",+ "onnxruntime-ep-webgpu": "0.4.0"+ },+ "cpu_threads": {+ "intra": 4,+ "inter": 1+ },+ "thresholds": {+ "dml": 0.8,+ "webgpu": 0.8+ },+ "calibration": {+ "label": "linux-8060s",+ "backend": "webgpu",+ "expected": "gpu",+ "environment": {+ "host": "myhloli-ubuntu-ssd",+ "os": "Linux-7.0.0-38-generic-x86_64-with-glibc2.43",+ "cpu": ""+ },+ "adapter": {+ "vendor_id": 4098,+ "device_id": 5510,+ "metadata": [+ [+ "card_idx",+ "1"+ ],+ [+ "pci_bus_id",+ "0000:c3:00.0"+ ]+ ]+ },+ "threshold": null,+ "runs": [+ {+ "schema_version": 2,+ "backend": "webgpu",+ "ep_version": "0.4.0",+ "webgpu_options": {+ "preferredLayout": "NHWC",+ "enableGraphCapture": "0",+ "dawnBackendType": "Vulkan",+ "powerPreference": "high-performance"+ },+ "adapter": {+ "vendor_id": 4098,+ "device_id": 5510,+ "metadata": [+ [+ "card_idx",+ "1"+ ],+ [+ "pci_bus_id",+ "0000:c3:00.0"+ ]+ ]+ },+ "input": {+ "manifest_sha256": "0d98e210345258583f72b784e0114359f9459bbcb1250716566c02e5d1851e06",+ "input_sha256": "474fa06cbc4c20303081e85084b45f3a4f24190d1141e4bd3bf3f3feb432c078",+ "shape": [+ 6,+ 3,+ 48,+ 1012+ ],+ "sample_order": [+ 0,+ 2,+ 4,+ 1,+ 5,+ 3+ ]+ },+ "model_sha256": "5435fd747c9e0efe15a96d0b378d5bd157e9492ed8fd80edf08f30d02fa24634",+ "ort_version": "1.31.0",+ "ort_path": "/home/myhloli/PycharmProjects/mineru/Magic-PDF/.venv/lib/python3.14/site-packages/onnxruntime/__init__.py",+ "python": "3.14.4 (main, Aug 20 2026, 10:41:58) [GCC 15.2.0]",+ "intra_threads": 4,+ "inter_threads": 1,+ "rec_metacommands": null,+ "gpu_first": false,+ "cpu": {+ "init_s": 0.03644871300275554,+ "first_s": 0.06895848299973295,+ "timed_s": [+ 0.06506703900231514,+ 0.060159857999678934+ ],+ "providers": [+ "CPUExecutionProvider"+ ]+ },+ "cpu_elapsed": 0.12522689700199408,+ "gpu": {+ "init_s": 0.06081931600056123,+ "first_s": 0.0692131209980289,+ "timed_s": [+ 0.037063878000481054,+ 0.027318416003254242+ ],+ "providers": [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ]+ },+ "gpu_elapsed": 0.0643822940037353,+ "ratio": 0.5141251244348096,+ "outputs_match": true,+ "worker_wall_s": 0.901972786999977+ },+ {+ "schema_version": 2,+ "backend": "webgpu",+ "ep_version": "0.4.0",+ "webgpu_options": {+ "preferredLayout": "NHWC",+ "enableGraphCapture": "0",+ "dawnBackendType": "Vulkan",+ "powerPreference": "high-performance"+ },+ "adapter": {+ "vendor_id": 4098,+ "device_id": 5510,+ "metadata": [+ [+ "card_idx",+ "1"+ ],+ [+ "pci_bus_id",+ "0000:c3:00.0"+ ]+ ]+ },+ "input": {+ "manifest_sha256": "0d98e210345258583f72b784e0114359f9459bbcb1250716566c02e5d1851e06",+ "input_sha256": "474fa06cbc4c20303081e85084b45f3a4f24190d1141e4bd3bf3f3feb432c078",+ "shape": [+ 6,+ 3,+ 48,+ 1012+ ],+ "sample_order": [+ 0,+ 2,+ 4,+ 1,+ 5,+ 3+ ]+ },+ "model_sha256": "5435fd747c9e0efe15a96d0b378d5bd157e9492ed8fd80edf08f30d02fa24634",+ "ort_version": "1.31.0",+ "ort_path": "/home/myhloli/PycharmProjects/mineru/Magic-PDF/.venv/lib/python3.14/site-packages/onnxruntime/__init__.py",+ "python": "3.14.4 (main, Aug 20 2026, 10:41:58) [GCC 15.2.0]",+ "intra_threads": 4,+ "inter_threads": 1,+ "rec_metacommands": null,+ "gpu_first": true,+ "gpu": {+ "init_s": 0.06195559900152148,+ "first_s": 0.051644684997881996,+ "timed_s": [+ 0.03642447799938964,+ 0.025907156999892322+ ],+ "providers": [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ]+ },+ "gpu_elapsed": 0.06233163499928196,+ "cpu": {+ "init_s": 0.027336809998814715,+ "first_s": 0.07003630600229371,+ "timed_s": [+ 0.0630405449992395,+ 0.05967439699816168+ ],+ "providers": [+ "CPUExecutionProvider"+ ]+ },+ "cpu_elapsed": 0.12271494199740118,+ "ratio": 0.5079384301921603,+ "outputs_match": true,+ "worker_wall_s": 0.8147090950005804+ },+ {+ "schema_version": 2,+ "backend": "webgpu",+ "ep_version": "0.4.0",+ "webgpu_options": {+ "preferredLayout": "NHWC",+ "enableGraphCapture": "0",+ "dawnBackendType": "Vulkan",+ "powerPreference": "high-performance"+ },+ "adapter": {+ "vendor_id": 4098,+ "device_id": 5510,+ "metadata": [+ [+ "card_idx",+ "1"+ ],+ [+ "pci_bus_id",+ "0000:c3:00.0"+ ]+ ]+ },+ "input": {+ "manifest_sha256": "0d98e210345258583f72b784e0114359f9459bbcb1250716566c02e5d1851e06",+ "input_sha256": "474fa06cbc4c20303081e85084b45f3a4f24190d1141e4bd3bf3f3feb432c078",+ "shape": [+ 6,+ 3,+ 48,+ 1012+ ],+ "sample_order": [+ 0,+ 2,+ 4,+ 1,+ 5,+ 3+ ]+ },+ "model_sha256": "5435fd747c9e0efe15a96d0b378d5bd157e9492ed8fd80edf08f30d02fa24634",+ "ort_version": "1.31.0",+ "ort_path": "/home/myhloli/PycharmProjects/mineru/Magic-PDF/.venv/lib/python3.14/site-packages/onnxruntime/__init__.py",+ "python": "3.14.4 (main, Aug 20 2026, 10:41:58) [GCC 15.2.0]",+ "intra_threads": 4,+ "inter_threads": 1,+ "rec_metacommands": null,+ "gpu_first": false,+ "cpu": {+ "init_s": 0.032998967999446904,+ "first_s": 0.06613971199840307,+ "timed_s": [+ 0.06185674299922539,+ 0.06100053599948296+ ],+ "providers": [+ "CPUExecutionProvider"+ ]+ },+ "cpu_elapsed": 0.12285727899870835,+ "gpu": {+ "init_s": 0.056202060000941856,+ "first_s": 0.06899642400094308,+ "timed_s": [+ 0.036928974997863406,+ 0.025959394999517826+ ],+ "providers": [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ]+ },+ "gpu_elapsed": 0.06288836999738123,+ "ratio": 0.5118815141432719,+ "outputs_match": true,+ "worker_wall_s": 0.7869903769969824+ },+ {+ "schema_version": 2,+ "backend": "webgpu",+ "ep_version": "0.4.0",+ "webgpu_options": {+ "preferredLayout": "NHWC",+ "enableGraphCapture": "0",+ "dawnBackendType": "Vulkan",+ "powerPreference": "high-performance"+ },+ "adapter": {+ "vendor_id": 4098,+ "device_id": 5510,+ "metadata": [+ [+ "card_idx",+ "1"+ ],+ [+ "pci_bus_id",+ "0000:c3:00.0"+ ]+ ]+ },+ "input": {+ "manifest_sha256": "0d98e210345258583f72b784e0114359f9459bbcb1250716566c02e5d1851e06",+ "input_sha256": "474fa06cbc4c20303081e85084b45f3a4f24190d1141e4bd3bf3f3feb432c078",+ "shape": [+ 6,+ 3,+ 48,+ 1012+ ],+ "sample_order": [+ 0,+ 2,+ 4,+ 1,+ 5,+ 3+ ]+ },+ "model_sha256": "5435fd747c9e0efe15a96d0b378d5bd157e9492ed8fd80edf08f30d02fa24634",+ "ort_version": "1.31.0",+ "ort_path": "/home/myhloli/PycharmProjects/mineru/Magic-PDF/.venv/lib/python3.14/site-packages/onnxruntime/__init__.py",+ "python": "3.14.4 (main, Aug 20 2026, 10:41:58) [GCC 15.2.0]",+ "intra_threads": 4,+ "inter_threads": 1,+ "rec_metacommands": null,+ "gpu_first": true,+ "gpu": {+ "init_s": 0.06222111799797858,+ "first_s": 0.05471379599839565,+ "timed_s": [+ 0.036214273000950925,+ 0.026760338001622586+ ],+ "providers": [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ]+ },+ "gpu_elapsed": 0.06297461100257351,+ "cpu": {+ "init_s": 0.02722062199973152,+ "first_s": 0.07797067899809917,+ "timed_s": [+ 0.06169361700085574,+ 0.058782553001947235+ ],+ "providers": [+ "CPUExecutionProvider"+ ]+ },+ "cpu_elapsed": 0.12047617000280297,+ "ratio": 0.5227142513005547,+ "outputs_match": true,+ "worker_wall_s": 0.8130726230010623+ },+ {+ "schema_version": 2,+ "backend": "webgpu",+ "ep_version": "0.4.0",+ "webgpu_options": {+ "preferredLayout": "NHWC",+ "enableGraphCapture": "0",+ "dawnBackendType": "Vulkan",+ "powerPreference": "high-performance"+ },+ "adapter": {+ "vendor_id": 4098,+ "device_id": 5510,+ "metadata": [+ [+ "card_idx",+ "1"+ ],+ [+ "pci_bus_id",+ "0000:c3:00.0"+ ]+ ]+ },+ "input": {+ "manifest_sha256": "0d98e210345258583f72b784e0114359f9459bbcb1250716566c02e5d1851e06",+ "input_sha256": "474fa06cbc4c20303081e85084b45f3a4f24190d1141e4bd3bf3f3feb432c078",+ "shape": [+ 6,+ 3,+ 48,+ 1012+ ],+ "sample_order": [+ 0,+ 2,+ 4,+ 1,+ 5,+ 3+ ]+ },+ "model_sha256": "5435fd747c9e0efe15a96d0b378d5bd157e9492ed8fd80edf08f30d02fa24634",+ "ort_version": "1.31.0",+ "ort_path": "/home/myhloli/PycharmProjects/mineru/Magic-PDF/.venv/lib/python3.14/site-packages/onnxruntime/__init__.py",+ "python": "3.14.4 (main, Aug 20 2026, 10:41:58) [GCC 15.2.0]",+ "intra_threads": 4,+ "inter_threads": 1,+ "rec_metacommands": null,+ "gpu_first": false,+ "cpu": {+ "init_s": 0.033197531000041636,+ "first_s": 0.07456994499807479,+ "timed_s": [+ 0.06369072500092443,+ 0.06263014499927522+ ],+ "providers": [+ "CPUExecutionProvider"+ ]+ },+ "cpu_elapsed": 0.12632087000019965,+ "gpu": {+ "init_s": 0.05514488499829895,+ "first_s": 0.07436587299889652,+ "timed_s": [+ 0.037422752000566106,+ 0.027836397999635665+ ],+ "providers": [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ]+ },+ "gpu_elapsed": 0.06525915000020177,+ "ratio": 0.5166141588488001,+ "outputs_match": true,+ "worker_wall_s": 0.8362074470023799+ }+ ],+ "errors": []+ },+ "pipeline": {+ "pages": 19,+ "input_sha256": "06d0e74125b5d2e1fc04e658bbe4af39422d70abfaa453a87353df4d3dad3432",+ "tier": "basic",+ "parse_mode": "ocr",+ "cpu_runs_s": [+ 56.92744022500119,+ 56.53439428699858+ ],+ "webgpu_runs_s": [+ 36.002764800003206,+ 36.32497576199967+ ],+ "cpu_mean_s": 56.730917255999884,+ "webgpu_mean_s": 36.16387028100144,+ "speedup": 1.5687180828597118,+ "latency_reduction_percent": 36.25368312341762,+ "outputs": {+ "cpu-1": {+ "file_count": 50,+ "all_files_identical": true+ },+ "webgpu-1": {+ "file_count": 50,+ "all_files_identical": true+ },+ "webgpu-2": {+ "file_count": 50,+ "all_files_identical": true+ },+ "cpu-2": {+ "file_count": 50,+ "all_files_identical": true+ },+ "cpu-observed": {+ "file_count": 50,+ "all_files_identical": true+ },+ "webgpu-observed": {+ "file_count": 50,+ "all_files_identical": true+ }+ },+ "output_sha256": {+ "markdown.md": "9142d0839d53eaa7c4729ac98a527f5964d285c62c88d28ca27777ea037a4967",+ "model_output.json": "fa9931a3e5ded5c8a34be2f1aaf41b663a64cbb1bbc27bdd8004adb1af66a4d4",+ "structured_content.json": "eb79172580f792a4221559b1404da57b94bf22683f6b7bef71104659b47b89a5",+ "middle_json.json": "c1fa7b7db802f712fe0a5e610a4e48a0b9f28c00a5b8029e8eaa511fb1cb14ff",+ "images/page_13_image_11.jpg": "be117bf500682716150d9dd1f1b3b922200130b39c7cbe5ca620f63ee04632dc",+ "images/page_15_equation_16.jpg": "868742debacf51f77328b76b3c5f8d5fa73a26bef9bad2fe12b14d65611cdcb2",+ "images/page_16_chart_18.jpg": "d01c202706b4d448134e1e253fd7f6dd4c14daf76b709cdf8da0d5f46507fe9e",+ "images/page_17_table_8.jpg": "394019c1ef702472f2f238e10a1737a6da7c39d6ffdf496c1020327b4c28b1fd",+ "images/page_16_image_3.jpg": "cbecc1f8730c93d067205797ee1dbe88260feace7d7ecad485d34ecefe857bbc",+ "images/page_15_equation_9.jpg": "08d67ef1776675e989f9573029e19d0c19348868eeb1b3786da3a89da4744fc4",+ "images/page_9_chart_2.jpg": "3c46e7e9e5465ea1cbbf6afaf385320797be53e25b5ba4d15e5c989002539a9c",+ "images/page_9_chart_4.jpg": "6d805da34431e18db08f93bec3080d7c9ead385e899f68e38f53c2fa6cdb2839",+ "images/page_16_chart_14.jpg": "d979e20eaf23109d7e54fba34603d4d1caeabb70b6333850e9b43aadeedcfd2e",+ "images/page_16_table_21.jpg": "51f55e1584a0c7fa6e9f879a3b9f93e961c9443a6d74e8affc47bda38a3b8319",+ "images/page_15_equation_14.jpg": "2270e1dfc84b58916076cf78cb8f2bc23779cebd03af9a173b2b155b036548a4",+ "images/page_7_table_8.jpg": "9ed42df9904ddcd5d5af2f667b9e5446056925668dbb6d862102f967da29b3d8",+ "images/page_15_equation_3.jpg": "11564c2a567e6da0589509a7ef6e585c35492e85c4a9b9fa8fd513b47a9c75d0",+ "images/page_8_table_3.jpg": "60fe378920ea852fad86de41886800728c5d036efdaade99e1a7289ef2a995d1",+ "images/page_15_equation_11.jpg": "0a075b375893a207aeb1c7b240ffc9c07acb1afda15c71b477c859235ddcf464",+ "images/page_3_equation_7.jpg": "9ccb3c83e55162e7c1280e4a3f54c45aa9001ce9a6bc74adb3f6353d1102d573",+ "images/page_5_table_3.jpg": "3111348c57b8e83bc643895dd73402ce69c89fc96aa1198064b3ca56894479e9",+ "images/page_3_equation_12.jpg": "d88b0bc8ba9a653c449065c04fe87fd57c5bb2e6234d1b75bc3a4d16ccc820f5",+ "images/page_16_image_1.jpg": "647acd6d3af08d5940c8ef1bfb39bfd9c4c56cf747fdd539c2ba0d9c4e9e9a62",+ "images/page_14_equation_14.jpg": "57ef7d35dd9649a0fa5deac616e2ff91ea4587aeaffb1a3e95fca60d1f867054",+ "images/page_9_chart_3.jpg": "75d98040f6e379bf9a2696c03138a407a49825f68d00e96de3826f661e9c1b90",+ "images/page_16_chart_23.jpg": "6ac062212e9a6fa347a4b28024b23d93099e5fa5408912186b976ddc7a13ff23",+ "images/page_4_table_2.jpg": "0dc7186c401c1d905c6d81d46b0ae6445a75268c1d403371ddd31be1f3997203",+ "images/page_16_image_9.jpg": "182194635b484c5fd1b4c456a9d65d32e23a21f7be03253c2ea349ee9a6c88db",+ "images/page_6_chart_6.jpg": "feb686096d47cc37495d5b21e1cc51f0f3ca3a64c78ea6ae749e436c555e5779",+ "images/page_14_equation_9.jpg": "b0c12293bb6d0058b57e2f2c54b1d33b6d3179a1494ed5febbf977ef423a5b9f",+ "images/page_3_equation_15.jpg": "18a2e8bd83607954b3e1eb8a5dd721b90e541c7b9c198b9ad422798aba275883",+ "images/page_3_chart_3.jpg": "c4c90e869a40933316ae48c298101209c14cc5c5ac1a9e0eeb9170f24b34db8b",+ "images/page_16_image_6.jpg": "95d410c578f368239664e599221d283d18ca0e19eb19833d64690cfb43cfb46d",+ "images/page_3_chart_2.jpg": "8a8b16ea4b86652bb02c00ff10cdace6129adb4dc19d3a36bbbf5750ce061d12",+ "images/page_4_equation_6.jpg": "88f7be69d49ca8ccf85cfd0f8657297eede1a854f5c09460f5d3cfc4d06d7f89",+ "images/page_7_chart_2.jpg": "c5c21a815198e7506e84d623df06d7f7e3ae20b211f408d50d975e33b27561d1",+ "images/page_15_equation_5.jpg": "9daebf881158c3f664f87d7e0496a16fabcd009836b3c9ef51b319e8d8ceeffb",+ "images/page_16_image_7.jpg": "a8e871bc97ab38db8f5931eec24bb906378e49a554d36e040027029d85b0c3ab",+ "images/page_16_image_5.jpg": "3c5903ab9284b355a119ec17588de9754bb6202f42f14bee35060c53443f3ed1",+ "images/page_16_image_2.jpg": "ab6238c6e9844c4ecfde441abb3963f16f5e7a27257b893a441dd179ecc51513",+ "images/page_1_chart_5.jpg": "d1ee19731defdb300d9580d6d329d958fad630030990f7415c34a3830e54b841",+ "images/page_17_image_1.jpg": "e4ea650de3b18db3a173d5ff5d740253fbf1db8f96e6cd17dbff22bb18056251",+ "images/page_16_image_4.jpg": "8a4510bc7d8d1f7442f73604c17cb6ed2615c968d2fa61842041c4f283d15287",+ "images/page_3_equation_17.jpg": "1baa062a34c09942467bcc14c10674293395a7704858749bf453dd77a9a637c0",+ "images/page_14_equation_12.jpg": "657d2d96ba69612291f00774b7a0d5eaa279961441ff29472fd83168909ee925",+ "images/page_5_table_11.jpg": "cc2b17956f522cd92ca1783f04f09cac27e8ba9967be5e303d99d9d2025f172f",+ "images/page_16_chart_16.jpg": "2279720fe903a7f4361f3526202a9704d6990f2f0fd43b9e4e6689191df9a8f8",+ "images/page_2_equation_8.jpg": "c972cd1cb152da6e2a640710f3d4d2749e652969523ebdf4fe2df71892909328",+ "images/page_17_image_2.jpg": "6b032342d4b1984e3d5d51ecc5833a529d2d4061c3deb10af9d17a1ffa7c55e4",+ "images/page_2_equation_10.jpg": "c4e81fd6d6915ede281f8690f6c4a41a67f34c06254edd0e9599b358ecd50400"+ }+ },+ "stage_timing": {+ "method": "separate Python sys.setprofile call/return observation of native ORT session.run; first calls included; preprocessing/postprocessing excluded; observed parse totals excluded from pipeline comparison",+ "rows": [+ {+ "kind": "layout",+ "cpu": {+ "run_s": 4.70213598799819,+ "init_s": 0.42865849000008893,+ "first_call_s_per_session_sum": 0.49580722500104457,+ "run_count": 10,+ "providers": [+ [+ "CPUExecutionProvider"+ ]+ ]+ },+ "webgpu": {+ "run_s": 1.9731370310000784,+ "init_s": 0.4100277699981234,+ "first_call_s_per_session_sum": 0.2580047369992826,+ "run_count": 10,+ "providers": [+ [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ]+ ]+ },+ "speedup": 2.383076245654833+ },+ {+ "kind": "mfr",+ "cpu": {+ "run_s": 21.53509693700107,+ "init_s": 0.469981201000337,+ "first_call_s_per_session_sum": 0.967377127999498,+ "run_count": 19,+ "providers": [+ [+ "CPUExecutionProvider"+ ]+ ]+ },+ "webgpu": {+ "run_s": 12.283002249994752,+ "init_s": 0.9152905230002943,+ "first_call_s_per_session_sum": 3.2817498360018362,+ "run_count": 19,+ "providers": [+ [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ],+ [+ "CPUExecutionProvider"+ ]+ ]+ },+ "speedup": 1.7532437508924394+ },+ {+ "kind": "ocr_det",+ "cpu": {+ "run_s": 1.1539976229687454,+ "init_s": 0.027887805001228116,+ "first_call_s_per_session_sum": 0.0785359299989068,+ "run_count": 84,+ "providers": [+ [+ "CPUExecutionProvider"+ ],+ [+ "CPUExecutionProvider"+ ]+ ]+ },+ "webgpu": {+ "run_s": 0.70471396299763,+ "init_s": 0.0710007849993417,+ "first_call_s_per_session_sum": 0.04831984499833197,+ "run_count": 84,+ "providers": [+ [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ],+ [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ]+ ]+ },+ "speedup": 1.637540454087223+ },+ {+ "kind": "ocr_rec",+ "cpu": {+ "run_s": 17.393700485023146,+ "init_s": 0.061026143997878535,+ "first_call_s_per_session_sum": 0.039372651001031045,+ "run_count": 336,+ "providers": [+ [+ "CPUExecutionProvider"+ ],+ [+ "CPUExecutionProvider"+ ]+ ]+ },+ "webgpu": {+ "run_s": 8.936705280044407,+ "init_s": 0.06538126900341013,+ "first_call_s_per_session_sum": 0.0348890860004758,+ "run_count": 336,+ "providers": [+ [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ],+ [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ]+ ]+ },+ "speedup": 1.94632137235891+ },+ {+ "kind": "unet",+ "cpu": {+ "run_s": 0.22482453900011024,+ "init_s": 0.01874217799922917,+ "first_call_s_per_session_sum": 0.0455595639978128,+ "run_count": 5,+ "providers": [+ [+ "CPUExecutionProvider"+ ]+ ]+ },+ "webgpu": {+ "run_s": 0.11035850899497746,+ "init_s": 0.025348870000016177,+ "first_call_s_per_session_sum": 0.03055164399847854,+ "run_count": 5,+ "providers": [+ [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ]+ ]+ },+ "speedup": 2.0372197943553427+ }+ ],+ "mfr_batches": [+ {+ "run": "cpu-observed",+ "provider": "CPUExecutionProvider",+ "batch": 8,+ "calls": 18,+ "run_s": 18.67427411799872+ },+ {+ "run": "cpu-observed",+ "provider": "CPUExecutionProvider",+ "batch": 7,+ "calls": 1,+ "run_s": 2.860822819002351+ },+ {+ "run": "webgpu-observed",+ "provider": "WebGpuExecutionProvider",+ "batch": 8,+ "calls": 18,+ "run_s": 9.374111268993147+ },+ {+ "run": "webgpu-observed",+ "provider": "CPUExecutionProvider",+ "batch": 7,+ "calls": 1,+ "run_s": 2.908890981001605+ }+ ]+ },+ "warm_replay": {+ "mfr_tail": {+ "kind": "mfr_tail",+ "count": 1,+ "model": "/home/myhloli/.mineru/models/MinerU-4_models_onnx/MFR/pp_formulanet_plus_m/PP-FormulaNet_plus-M.onnx",+ "model_sha256": "20a32595c2b30282dcd069e6295e9fad96115697beb66acb9b681b814006d193",+ "webgpu_options": {+ "preferredLayout": "NHWC",+ "enableGraphCapture": "0",+ "dawnBackendType": "Vulkan",+ "powerPreference": "high-performance"+ },+ "timing": "synchronous production session.run only; transfers and CPU operator fallback included; profiling off",+ "input_shapes": [+ {+ "encoder/image": [+ 7,+ 1,+ 384,+ 384+ ]+ }+ ],+ "cpu": {+ "init_s": 0.5656328460026998,+ "providers": [+ "CPUExecutionProvider"+ ],+ "warmup_pass_s": 2.870758618999389,+ "rounds_s": [+ 2.869975940000586,+ 2.894684036000399,+ 2.870337158001348,+ 2.7577058319984644,+ 2.753012042001501+ ],+ "median_s": 2.869975940000586+ },+ "webgpu": {+ "init_s": 0.4280498070002068,+ "providers": [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ],+ "warmup_pass_s": 5.359566946997802,+ "rounds_s": [+ 5.26302053299878,+ 5.396823557999596,+ 5.246855952002079,+ 5.216805058000318,+ 5.016516823001439+ ],+ "median_s": 5.246855952002079+ },+ "comparisons": [+ {+ "id": "mfr_tail",+ "outputs": [+ {+ "shape": [+ 7,+ 305+ ],+ "equal": true,+ "max_abs": 0.0,+ "mean_abs": 0.0+ }+ ],+ "tokens_equal": true+ }+ ],+ "speedup": 0.5469896574739144,+ "profile": {+ "path": "demo/output/webgpu-production-20261021/replay/mfr_tail-profile_2026-10-21_17-48-03_030.json",+ "provider_events": {+ "WebGpuExecutionProvider": 108181,+ "CPUExecutionProvider": 79661+ },+ "unique_nodes": {+ "CPUExecutionProvider": 275,+ "WebGpuExecutionProvider": 616+ }+ }+ },+ "ocr_det": {+ "kind": "ocr_det",+ "count": 1,+ "model": "/home/myhloli/.mineru/models/MinerU-4_models_onnx/OCR/paddleocr/ch_PP-OCRv6_tiny_det_infer.onnx",+ "model_sha256": "193bab7a04fca699a6c82e6abb5b81bdb28177f0abd4062552b04908dafb19f8",+ "webgpu_options": {+ "preferredLayout": "NHWC",+ "enableGraphCapture": "0",+ "dawnBackendType": "Vulkan",+ "powerPreference": "high-performance"+ },+ "timing": "synchronous production session.run only; transfers and CPU operator fallback included; profiling off",+ "input_shapes": [+ {+ "x": [+ 1,+ 3,+ 960,+ 320+ ]+ }+ ],+ "cpu": {+ "init_s": 0.013473268998495769,+ "providers": [+ "CPUExecutionProvider"+ ],+ "warmup_pass_s": 0.011081049000495113,+ "rounds_s": [+ 0.010702628998842556,+ 0.006809602000430459,+ 0.005751896002038848,+ 0.006752524997864384,+ 0.005581355999311199+ ],+ "median_s": 0.006752524997864384+ },+ "webgpu": {+ "init_s": 0.046050765999098076,+ "providers": [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ],+ "warmup_pass_s": 0.02547380300165969,+ "rounds_s": [+ 0.006659098999080015,+ 0.005725085997255519,+ 0.00822639100078959,+ 0.0060534419972100295,+ 0.007255056996655185+ ],+ "median_s": 0.006659098999080015+ },+ "comparisons": [+ {+ "id": "ocr_det",+ "outputs": [+ {+ "shape": [+ 1,+ 1,+ 960,+ 320+ ],+ "equal": false,+ "max_abs": 0.00012096762657165527,+ "mean_abs": 3.4568036525943185e-07+ }+ ]+ }+ ],+ "speedup": 1.0140298257763214,+ "profile": {+ "path": "demo/output/webgpu-production-20261021/replay/ocr_det-profile_2026-10-21_17-47-11_844.json",+ "provider_events": {+ "WebGpuExecutionProvider": 158+ },+ "unique_nodes": {+ "WebGpuExecutionProvider": 158+ }+ }+ },+ "ocr_rec": {+ "kind": "ocr_rec",+ "count": 1,+ "model": "/home/myhloli/.mineru/models/MinerU-4_models_onnx/OCR/paddleocr/ch_PP-OCRv6_small_rec_infer.onnx",+ "model_sha256": "5435fd747c9e0efe15a96d0b378d5bd157e9492ed8fd80edf08f30d02fa24634",+ "webgpu_options": {+ "preferredLayout": "NHWC",+ "enableGraphCapture": "0",+ "dawnBackendType": "Vulkan",+ "powerPreference": "high-performance"+ },+ "timing": "synchronous production session.run only; transfers and CPU operator fallback included; profiling off",+ "input_shapes": [+ {+ "x": [+ 6,+ 3,+ 48,+ 320+ ]+ }+ ],+ "cpu": {+ "init_s": 0.035012907999771414,+ "providers": [+ "CPUExecutionProvider"+ ],+ "warmup_pass_s": 0.02667466700222576,+ "rounds_s": [+ 0.020095138002943713,+ 0.017542446999868844,+ 0.016821542998513905,+ 0.01767310200011707,+ 0.01666659300099127+ ],+ "median_s": 0.017542446999868844+ },+ "webgpu": {+ "init_s": 0.0597460310018505,+ "providers": [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ],+ "warmup_pass_s": 0.046178245000191964,+ "rounds_s": [+ 0.02451714799826732,+ 0.021081398997921497,+ 0.02318623000246589,+ 0.022789495000324678,+ 0.02626016000067466+ ],+ "median_s": 0.02318623000246589+ },+ "comparisons": [+ {+ "id": "ocr_rec",+ "outputs": [+ {+ "shape": [+ 6,+ 40,+ 18710+ ],+ "equal": false,+ "max_abs": 1.9490718841552734e-05,+ "mean_abs": 6.105439544144658e-10+ }+ ],+ "tokens_equal": true+ }+ ],+ "speedup": 0.7565890184822275,+ "profile": {+ "path": "demo/output/webgpu-production-20261021/replay/ocr_rec-profile_2026-10-21_17-47-12_649.json",+ "provider_events": {+ "WebGpuExecutionProvider": 204,+ "CPUExecutionProvider": 3+ },+ "unique_nodes": {+ "CPUExecutionProvider": 3,+ "WebGpuExecutionProvider": 204+ }+ }+ },+ "mfr": {+ "kind": "mfr",+ "count": 1,+ "model": "/home/myhloli/.mineru/models/MinerU-4_models_onnx/MFR/pp_formulanet_plus_m/PP-FormulaNet_plus-M.onnx",+ "model_sha256": "20a32595c2b30282dcd069e6295e9fad96115697beb66acb9b681b814006d193",+ "webgpu_options": {+ "preferredLayout": "NHWC",+ "enableGraphCapture": "0",+ "dawnBackendType": "Vulkan",+ "powerPreference": "high-performance"+ },+ "timing": "synchronous production session.run only; transfers and CPU operator fallback included; profiling off",+ "input_shapes": [+ {+ "encoder/image": [+ 8,+ 1,+ 384,+ 384+ ]+ }+ ],+ "cpu": {+ "init_s": 0.6033340399990266,+ "providers": [+ "CPUExecutionProvider"+ ],+ "warmup_pass_s": 1.0827462720008043,+ "rounds_s": [+ 0.9821987379982602,+ 0.9763298619982379,+ 0.9755643850003253,+ 1.0035604330005299,+ 0.9640385319980851+ ],+ "median_s": 0.9763298619982379+ },+ "webgpu": {+ "init_s": 0.4494644420010445,+ "providers": [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ],+ "warmup_pass_s": 0.3791601829980209,+ "rounds_s": [+ 0.3804843379984959,+ 0.3541415740000957,+ 0.36716993599839043,+ 0.340868639999826,+ 0.36491733900038525+ ],+ "median_s": 0.36491733900038525+ },+ "comparisons": [+ {+ "id": "mfr",+ "outputs": [+ {+ "shape": [+ 8,+ 11+ ],+ "equal": true,+ "max_abs": 0.0,+ "mean_abs": 0.0+ }+ ],+ "tokens_equal": true+ }+ ],+ "speedup": 2.6754822466717787,+ "profile": {+ "path": "demo/output/webgpu-production-20261021/replay/mfr-profile_2026-10-21_17-47-10_155.json",+ "provider_events": {+ "WebGpuExecutionProvider": 3811,+ "CPUExecutionProvider": 2633+ },+ "unique_nodes": {+ "CPUExecutionProvider": 275,+ "WebGpuExecutionProvider": 616+ }+ }+ },+ "layout": {+ "kind": "layout",+ "count": 1,+ "model": "/home/myhloli/.mineru/models/MinerU-4_models_onnx/Layout/PP-DocLayoutV2/inference.onnx",+ "model_sha256": "cd540dc296ff3115fe78efa65b68501e6a8dc74b198acc9834a725ffaa095aac",+ "webgpu_options": {+ "preferredLayout": "NHWC",+ "enableGraphCapture": "0",+ "dawnBackendType": "Vulkan",+ "powerPreference": "high-performance"+ },+ "timing": "synchronous production session.run only; transfers and CPU operator fallback included; profiling off",+ "input_shapes": [+ {+ "image": [+ 2,+ 3,+ 800,+ 800+ ],+ "scale_factor": [+ 2,+ 2+ ],+ "im_shape": [+ 2,+ 2+ ]+ }+ ],+ "cpu": {+ "init_s": 0.4524440940003842,+ "providers": [+ "CPUExecutionProvider"+ ],+ "warmup_pass_s": 0.5047930420005287,+ "rounds_s": [+ 0.5102006519991846,+ 0.5421152740018442,+ 0.49126757500198437,+ 0.5164678350010945,+ 0.5261939199990593+ ],+ "median_s": 0.5164678350010945+ },+ "webgpu": {+ "init_s": 0.4273039260006044,+ "providers": [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ],+ "warmup_pass_s": 0.26653647100101807,+ "rounds_s": [+ 0.23237873800098896,+ 0.20581930700063822,+ 0.22307763999924646,+ 0.20205856800021138,+ 0.22296567000012146+ ],+ "median_s": 0.22296567000012146+ },+ "comparisons": [+ {+ "id": "layout",+ "outputs": [+ {+ "shape": [+ 600,+ 8+ ],+ "equal": false,+ "max_abs": 0.004638671875,+ "mean_abs": 6.817117876683672e-05+ },+ {+ "shape": [+ 2+ ],+ "equal": true,+ "max_abs": 0.0,+ "mean_abs": 0.0+ }+ ]+ }+ ],+ "speedup": 2.3163558542479348,+ "profile": {+ "path": "demo/output/webgpu-production-20261021/replay/layout-profile_2026-10-21_17-46-59_472.json",+ "provider_events": {+ "WebGpuExecutionProvider": 1397,+ "CPUExecutionProvider": 304+ },+ "unique_nodes": {+ "CPUExecutionProvider": 304,+ "WebGpuExecutionProvider": 1397+ }+ }+ }+ },+ "unet_24_tables": {+ "kind": "unet",+ "count": 24,+ "model": "/home/myhloli/.mineru/models/MinerU-4_models_onnx/Table/unet.onnx",+ "model_sha256": "0ea48d3a17e35ef5c2e498a5e799566073234d39b1079ca21d9f4fafe73c6d20",+ "webgpu_options": {+ "preferredLayout": "NHWC",+ "enableGraphCapture": "0",+ "dawnBackendType": "Vulkan",+ "powerPreference": "high-performance"+ },+ "timing": "synchronous production session.run only; transfers and CPU operator fallback included; profiling off",+ "input_shapes": [+ {+ "input": [+ 1,+ 3,+ 911,+ 1024+ ]+ },+ {+ "input": [+ 1,+ 3,+ 402,+ 1024+ ]+ },+ {+ "input": [+ 1,+ 3,+ 189,+ 1024+ ]+ },+ {+ "input": [+ 1,+ 3,+ 275,+ 1024+ ]+ },+ {+ "input": [+ 1,+ 3,+ 120,+ 1024+ ]+ },+ {+ "input": [+ 1,+ 3,+ 389,+ 1024+ ]+ },+ {+ "input": [+ 1,+ 3,+ 313,+ 1024+ ]+ },+ {+ "input": [+ 1,+ 3,+ 311,+ 1024+ ]+ },+ {+ "input": [+ 1,+ 3,+ 118,+ 1024+ ]+ },+ {+ "input": [+ 1,+ 3,+ 725,+ 1024+ ]+ },+ {+ "input": [+ 1,+ 3,+ 447,+ 1024+ ]+ },+ {+ "input": [+ 1,+ 3,+ 270,+ 1024+ ]+ },+ {+ "input": [+ 1,+ 3,+ 121,+ 1024+ ]+ },+ {+ "input": [+ 1,+ 3,+ 40,+ 1024+ ]+ },+ {+ "input": [+ 1,+ 3,+ 664,+ 1024+ ]+ },+ {+ "input": [+ 1,+ 3,+ 521,+ 1024+ ]+ },+ {+ "input": [+ 1,+ 3,+ 680,+ 1024+ ]+ },+ {+ "input": [+ 1,+ 3,+ 187,+ 1024+ ]+ },+ {+ "input": [+ 1,+ 3,+ 78,+ 1024+ ]+ },+ {+ "input": [+ 1,+ 3,+ 307,+ 1024+ ]+ },+ {+ "input": [+ 1,+ 3,+ 549,+ 1024+ ]+ },+ {+ "input": [+ 1,+ 3,+ 1024,+ 878+ ]+ },+ {+ "input": [+ 1,+ 3,+ 229,+ 1024+ ]+ },+ {+ "input": [+ 1,+ 3,+ 173,+ 1024+ ]+ }+ ],+ "cpu": {+ "init_s": 0.017773120001947973,+ "providers": [+ "CPUExecutionProvider"+ ],+ "warmup_pass_s": 1.551150993000192,+ "rounds_s": [+ 1.4260688109898183,+ 1.4428032769938,+ 1.4424924550148717,+ 1.4254797439898539,+ 1.4329133379942505+ ],+ "median_s": 1.4329133379942505+ },+ "webgpu": {+ "init_s": 0.05522156000006362,+ "providers": [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ],+ "warmup_pass_s": 0.5450169699943217,+ "rounds_s": [+ 0.50821205799366,+ 0.4906430640075996,+ 0.5128213389980374,+ 0.494259412997053,+ 0.5015037400044093+ ],+ "median_s": 0.5015037400044093+ },+ "comparisons": [+ {+ "id": "p02-t01",+ "outputs": [+ {+ "shape": [+ 1,+ 1,+ 911,+ 1024+ ],+ "equal": true,+ "max_abs": 0.0,+ "mean_abs": 0.0+ }+ ],+ "mask_pixels": 932864,+ "mask_different": 0+ },+ {+ "id": "p03-t01",+ "outputs": [+ {+ "shape": [+ 1,+ 1,+ 402,+ 1024+ ],+ "equal": true,+ "max_abs": 0.0,+ "mean_abs": 0.0+ }+ ],+ "mask_pixels": 411648,+ "mask_different": 0+ },+ {+ "id": "p03-t02",+ "outputs": [+ {+ "shape": [+ 1,+ 1,+ 189,+ 1024+ ],+ "equal": true,+ "max_abs": 0.0,+ "mean_abs": 0.0+ }+ ],+ "mask_pixels": 193536,+ "mask_different": 0+ },+ {+ "id": "p03-t03",+ "outputs": [+ {+ "shape": [+ 1,+ 1,+ 275,+ 1024+ ],+ "equal": true,+ "max_abs": 0.0,+ "mean_abs": 0.0+ }+ ],+ "mask_pixels": 281600,+ "mask_different": 0+ },+ {+ "id": "p04-t01",+ "outputs": [+ {+ "shape": [+ 1,+ 1,+ 120,+ 1024+ ],+ "equal": true,+ "max_abs": 0.0,+ "mean_abs": 0.0+ }+ ],+ "mask_pixels": 122880,+ "mask_different": 0+ },+ {+ "id": "p04-t02",+ "outputs": [+ {+ "shape": [+ 1,+ 1,+ 389,+ 1024+ ],+ "equal": false,+ "max_abs": 1.0,+ "mean_abs": 2.510443444730077e-06+ }+ ],+ "mask_pixels": 398336,+ "mask_different": 1+ },+ {+ "id": "p04-t03",+ "outputs": [+ {+ "shape": [+ 1,+ 1,+ 313,+ 1024+ ],+ "equal": true,+ "max_abs": 0.0,+ "mean_abs": 0.0+ }+ ],+ "mask_pixels": 320512,+ "mask_different": 0+ },+ {+ "id": "p04-t04",+ "outputs": [+ {+ "shape": [+ 1,+ 1,+ 311,+ 1024+ ],+ "equal": true,+ "max_abs": 0.0,+ "mean_abs": 0.0+ }+ ],+ "mask_pixels": 318464,+ "mask_different": 0+ },+ {+ "id": "p06-t01",+ "outputs": [+ {+ "shape": [+ 1,+ 1,+ 118,+ 1024+ ],+ "equal": true,+ "max_abs": 0.0,+ "mean_abs": 0.0+ }+ ],+ "mask_pixels": 120832,+ "mask_different": 0+ },+ {+ "id": "p07-t01",+ "outputs": [+ {+ "shape": [+ 1,+ 1,+ 725,+ 1024+ ],+ "equal": true,+ "max_abs": 0.0,+ "mean_abs": 0.0+ }+ ],+ "mask_pixels": 742400,+ "mask_different": 0+ },+ {+ "id": "p09-t01",+ "outputs": [+ {+ "shape": [+ 1,+ 1,+ 447,+ 1024+ ],+ "equal": true,+ "max_abs": 0.0,+ "mean_abs": 0.0+ }+ ],+ "mask_pixels": 457728,+ "mask_different": 0+ },+ {+ "id": "p09-t02",+ "outputs": [+ {+ "shape": [+ 1,+ 1,+ 270,+ 1024+ ],+ "equal": true,+ "max_abs": 0.0,+ "mean_abs": 0.0+ }+ ],+ "mask_pixels": 276480,+ "mask_different": 0+ },+ {+ "id": "p09-t03",+ "outputs": [+ {+ "shape": [+ 1,+ 1,+ 121,+ 1024+ ],+ "equal": true,+ "max_abs": 0.0,+ "mean_abs": 0.0+ }+ ],+ "mask_pixels": 123904,+ "mask_different": 0+ },+ {+ "id": "p10-t01",+ "outputs": [+ {+ "shape": [+ 1,+ 1,+ 40,+ 1024+ ],+ "equal": true,+ "max_abs": 0.0,+ "mean_abs": 0.0+ }+ ],+ "mask_pixels": 40960,+ "mask_different": 0+ },+ {+ "id": "p10-t02",+ "outputs": [+ {+ "shape": [+ 1,+ 1,+ 664,+ 1024+ ],+ "equal": true,+ "max_abs": 0.0,+ "mean_abs": 0.0+ }+ ],+ "mask_pixels": 679936,+ "mask_different": 0+ },+ {+ "id": "p10-t03",+ "outputs": [+ {+ "shape": [+ 1,+ 1,+ 521,+ 1024+ ],+ "equal": true,+ "max_abs": 0.0,+ "mean_abs": 0.0+ }+ ],+ "mask_pixels": 533504,+ "mask_different": 0+ },+ {+ "id": "p11-t01",+ "outputs": [+ {+ "shape": [+ 1,+ 1,+ 680,+ 1024+ ],+ "equal": true,+ "max_abs": 0.0,+ "mean_abs": 0.0+ }+ ],+ "mask_pixels": 696320,+ "mask_different": 0+ },+ {+ "id": "p11-t02",+ "outputs": [+ {+ "shape": [+ 1,+ 1,+ 187,+ 1024+ ],+ "equal": true,+ "max_abs": 0.0,+ "mean_abs": 0.0+ }+ ],+ "mask_pixels": 191488,+ "mask_different": 0+ },+ {+ "id": "p11-t03",+ "outputs": [+ {+ "shape": [+ 1,+ 1,+ 78,+ 1024+ ],+ "equal": true,+ "max_abs": 0.0,+ "mean_abs": 0.0+ }+ ],+ "mask_pixels": 79872,+ "mask_different": 0+ },+ {+ "id": "p12-t01",+ "outputs": [+ {+ "shape": [+ 1,+ 1,+ 307,+ 1024+ ],+ "equal": true,+ "max_abs": 0.0,+ "mean_abs": 0.0+ }+ ],+ "mask_pixels": 314368,+ "mask_different": 0+ },+ {+ "id": "p12-t02",+ "outputs": [+ {+ "shape": [+ 1,+ 1,+ 549,+ 1024+ ],+ "equal": true,+ "max_abs": 0.0,+ "mean_abs": 0.0+ }+ ],+ "mask_pixels": 562176,+ "mask_different": 0+ },+ {+ "id": "p13-t01",+ "outputs": [+ {+ "shape": [+ 1,+ 1,+ 1024,+ 878+ ],+ "equal": true,+ "max_abs": 0.0,+ "mean_abs": 0.0+ }+ ],+ "mask_pixels": 899072,+ "mask_different": 0+ },+ {+ "id": "p13-t02",+ "outputs": [+ {+ "shape": [+ 1,+ 1,+ 229,+ 1024+ ],+ "equal": true,+ "max_abs": 0.0,+ "mean_abs": 0.0+ }+ ],+ "mask_pixels": 234496,+ "mask_different": 0+ },+ {+ "id": "p14-t01",+ "outputs": [+ {+ "shape": [+ 1,+ 1,+ 173,+ 1024+ ],+ "equal": true,+ "max_abs": 0.0,+ "mean_abs": 0.0+ }+ ],+ "mask_pixels": 177152,+ "mask_different": 0+ }+ ],+ "speedup": 2.857233603046813,+ "profile": {+ "path": "demo/output/webgpu-production-20261021/unet/unet-profile_2026-10-21_17-48-24_378.json",+ "provider_events": {+ "WebGpuExecutionProvider": 155,+ "CPUExecutionProvider": 38+ },+ "unique_nodes": {+ "CPUExecutionProvider": 38,+ "WebGpuExecutionProvider": 155+ }+ }+ },+ "operator_placement": {+ "mfr_tail": {+ "unique_nodes": {+ "CPUExecutionProvider": 275,+ "WebGpuExecutionProvider": 616+ },+ "cpu_op_counts": {+ "Gather": 29,+ "Concat": 90,+ "ConstantOfShape": 3,+ "Slice": 2,+ "Unsqueeze": 123,+ "Shape": 1,+ "Reshape": 2,+ "Equal": 3,+ "Where": 3,+ "Add": 2,+ "Range": 1,+ "Expand": 1,+ "Mul": 12,+ "ReduceMax": 1,+ "Cast": 1,+ "Loop": 1+ }+ },+ "ocr_det": {+ "unique_nodes": {+ "WebGpuExecutionProvider": 158+ },+ "cpu_op_counts": {}+ },+ "ocr_rec": {+ "unique_nodes": {+ "CPUExecutionProvider": 3,+ "WebGpuExecutionProvider": 204+ },+ "cpu_op_counts": {+ "Gather": 1,+ "Slice": 1,+ "Concat": 1+ }+ },+ "mfr": {+ "unique_nodes": {+ "CPUExecutionProvider": 275,+ "WebGpuExecutionProvider": 616+ },+ "cpu_op_counts": {+ "Gather": 29,+ "Concat": 90,+ "ConstantOfShape": 3,+ "Slice": 2,+ "Unsqueeze": 123,+ "Shape": 1,+ "Reshape": 2,+ "Equal": 3,+ "Where": 3,+ "Add": 2,+ "Range": 1,+ "Expand": 1,+ "Mul": 12,+ "ReduceMax": 1,+ "Cast": 1,+ "Loop": 1+ }+ },+ "layout": {+ "unique_nodes": {+ "CPUExecutionProvider": 304,+ "WebGpuExecutionProvider": 1397+ },+ "cpu_op_counts": {+ "Gather": 26,+ "Slice": 68,+ "Concat": 65,+ "Transpose": 4,+ "Unsqueeze": 16,+ "Squeeze": 20,+ "Range": 4,+ "Tile": 4,+ "Reshape": 15,+ "TopK": 1,+ "Shape": 19,+ "Max": 11,+ "Expand": 14,+ "Cast": 13,+ "ReduceSum": 1,+ "Less": 2,+ "Where": 4,+ "GatherElements": 2,+ "Mul": 3,+ "Sub": 2,+ "LessOrEqual": 1,+ "Add": 1,+ "Min": 1,+ "ScatterElements": 1,+ "ScatterND": 2,+ "Equal": 1,+ "CumSum": 1,+ "GatherND": 1,+ "Mod": 1+ }+ },+ "unet": {+ "unique_nodes": {+ "CPUExecutionProvider": 38,+ "WebGpuExecutionProvider": 155+ },+ "cpu_op_counts": {+ "Gather": 15,+ "Slice": 5,+ "Unsqueeze": 9,+ "Concat": 9+ }+ }+ },+ "lifecycle": {+ "auto_processes": [+ {+ "selection": {+ "device": "gpu",+ "adapter": {+ "vendor_id": 4098,+ "device_id": 5510,+ "metadata": [+ [+ "card_idx",+ "1"+ ],+ [+ "pci_bus_id",+ "0000:c3:00.0"+ ]+ ]+ },+ "reason": "rec_calibration",+ "backend": "webgpu"+ },+ "providers": [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ]+ },+ {+ "selection": {+ "device": "gpu",+ "adapter": {+ "vendor_id": 4098,+ "device_id": 5510,+ "metadata": [+ [+ "card_idx",+ "1"+ ],+ [+ "pci_bus_id",+ "0000:c3:00.0"+ ]+ ]+ },+ "reason": "configured_gpu",+ "backend": "webgpu"+ },+ "providers": [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ]+ }+ ],+ "saved_config": {+ "model": {+ "onnx_device": "gpu",+ "source": "local"+ }+ },+ "real_concurrency": {+ "sessions": 2,+ "threads": 4,+ "calls": 12,+ "all_tokens_equal": true,+ "providers": [+ [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ],+ [+ "WebGpuExecutionProvider",+ "CPUExecutionProvider"+ ]+ ]+ }+ },+ "checks": {+ "targeted_tests_passed": 352,+ "runtime_preload_cli_tests_passed": 29,+ "ruff": "passed",+ "uv_pip_check": "passed"+ }+}diff --git a/docs/next/onnx-compact-rec-validation.md b/docs/next/onnx-compact-rec-validation.mdnew file mode 100644index 00000000..0181f979--- /dev/null+++ b/docs/next/onnx-compact-rec-validation.md@@ -0,0 +1,87 @@+# ONNX OCR rec 精简输出生产验证(2026-10-21)++Linux x86_64,AMD Radeon 8060S / RADV STRIX_HALO;Python 3.14.4、ORT 1.31.0、WebGPU EP 0.4.0。+basic+强制 OCR,demo1+demo2 合并 PDF 共 19 页。CPU intra-op=4、inter-op=1;WebGPU 为 NHWC、Vulkan、high-performance、graph capture 关闭。++## 实现与正确性++CPU、DML、WebGPU 的 ONNX rec 共用精简图,图内 ArgMax / ReduceMax 返回 ID 和置信度。原 Softmax、字符表、CTC 去重与顺序还原语义保留。Torch 和 DocVortex / Rust 未修改。+派生图按源 SHA256 和格式版本缓存,文件锁、校验与原子替换保护多进程使用。自动设备校准使用同一派生图,报告区分输出格式。+10 个真实张量对照(CPU/WebGPU,各含 batch 1/6/7、宽 320/1012)中,精简输出的 ID、置信度与各自原图逐值一致。+四次正式解析、两次阶段采样的 Markdown、三个 JSON、46 个素材,共 50 个文件全部逐字节一致,且都与优化前结果一致。+DML 共用路径由单元回归覆盖;本机为 Linux,未获得 Windows DML 硬件耗时。+401 项相关回归测试、Ruff、git diff --check 与 uv pip check 通过。++## 完整解析耗时++每种模式两个新进程,按 CPU→GPU→GPU→CPU 执行,无额外预热,关闭采样器。计时包含模型初始化与首次推理,不含模块导入、结果写盘。派生图缓存已准备好。++| 路线 | 第一次(秒) | 第二次(秒) | 平均(秒) |+|---|---:|---:|---:|+| cpu | 58.012 | 58.368 | 58.190 |+| webgpu | 34.726 | 35.076 | 34.901 |++WebGPU 相对 CPU 加速 **1.67×**,总耗时减少 **40.0%**。++## 各模型阶段累计推理++单独使用 Python call/return observer 记录同步 ORT run,包含首次调用、数据传输与图内 CPU 算子,不含预处理/后处理。采样解析的总耗时不参与上表比较,阶段相加不会等于上表完整解析耗时。+新增的两个归约算子计入 rec 推理,因此 CPU 列也使用精简输出图。表格分类与 SLANet Plus 两条路线都使用 CPU。++| 阶段 | CPU(秒) | WebGPU 路线(秒) | CPU/GPU 调用次数 |+|---|---:|---:|---:|+| layout | 4.870 | 2.008 | 10 / 10 |+| mfr | 22.892 | 12.726 | 19 / 19 |+| ocr_det | 1.175 | 0.715 | 84 / 84 |+| ocr_rec | 18.463 | 7.277 | 336 / 336 |+| unet | 0.237 | 0.110 | 5 / 5 |+| table_cls | 0.006 | 0.006 | 1 / 1 |+| slanet_plus | 0.130 | 0.112 | 2 / 2 |++## MFR 实际分流++| 路线 | EP | 批量 | 调用次数 | 推理(秒) |+|---|---|---:|---:|---:|+| cpu | CPUExecutionProvider | 8 | 18 | 19.993 |+| cpu | CPUExecutionProvider | 7 | 1 | 2.899 |+| webgpu | WebGpuExecutionProvider | 8 | 18 | 9.774 |+| webgpu | CPUExecutionProvider | 7 | 1 | 2.952 |++## 初始化与首轮调用++未额外预热完整解析。下列首轮调用已经包含在对应阶段推理中,不能再次加总;它们含实际推理,不应全部视为预热开销。+CPU/GPU 初始化为生产 create_ort_session 墙钟;表格分类及 SLANet 的其他工厂初始化未由本 observer 采集。++| 模型 | CPU 初始化(秒) | GPU 路线初始化(秒) | CPU 首轮(秒) | GPU 路线首轮(秒) |+|---|---:|---:|---:|---:|+| layout | 0.439 | 0.408 | 0.517 | 0.271 |+| mfr | 0.475 | 0.949 | 1.041 | 3.354 |+| ocr_det | 0.027 | 0.068 | 0.080 | 0.049 |+| ocr_rec | 0.061 | 0.060 | 0.046 | 0.026 |+| unet | 0.020 | 0.026 | 0.062 | 0.031 |++单独冷生成派生缓存 0.295 秒,复用验证 0.035 秒。++## 优化前后的同机参考++优化前报告来自同一天此前的运行。其他阶段会受系统状态影响,这里保留实际数值,不将全部解析时间差归因于 rec 改动。++| 指标 | 优化前(秒) | 优化后(秒) |+|---|---:|---:|+| cpu 完整解析平均 | 56.731 | 58.190 |+| webgpu 完整解析平均 | 36.164 | 34.901 |+| cpu rec 累计推理 | 17.394 | 18.463 |+| webgpu rec 累计推理 | 8.937 | 7.277 |++## EP 与自动校准++精简 rec profile:{'WebGpuExecutionProvider': 206, 'CPUExecutionProvider': 3};ArgMax、ReduceMax 均在 WebGPU 上执行,CPU 仅执行 Gather、Slice、Concat 三个形状算子。+五种 GPU 目标模型实际启用 WebGPU,没有整会话 CPU 回退;MFR 尾批明确为 CPU。+5 个新进程六图校准 GPU/CPU 比值:0.3845, 0.3873, 0.4016, 0.5619, 0.4011;输出一致。保持各后端阈值 0.8,本次不重新推导 DML 阈值。++## 复现++原始脚本、每次输出、捕获张量、日志、profile 与报告位于 `demo/output/compact-rec-production-20261021/`。+从项目根目录设置 `MINERU_MODEL_SMALL_BACKEND=onnx`、`MINERU_MODEL_ONNX_DEVICE=cpu|gpu`、+`MINERU_INTRA_OP_NUM_THREADS=4`、`MINERU_INTER_OP_NUM_THREADS=1`,运行该目录的 `benchmark.py parse --output <新目录>`;独立阶段观测增加 `--observe`。+[机器可读报告](benchmarks/onnx-compact-rec-linux-20261021.json) 保存源码/模型/输入校验值、完整阶段数据、EP 证据和各次校准。diff --git a/docs/next/onnx-device.md b/docs/next/onnx-device.mdindex 7c228cd8..25d56e85 100644--- a/docs/next/onnx-device.md+++ b/docs/next/onnx-device.md@@ -1,6 +1,6 @@-# ONNX 设备选择与 DirectML+# ONNX 设备选择、DirectML 与 WebGPU-`model.onnx_device` 控制 ONNX 小模型栈的 CPU / Windows DirectML 选择:+`model.onnx_device` 控制 ONNX 小模型栈的 CPU / Windows DirectML / Linux WebGPU 选择:```yamlmodel:@@ -11,21 +11,28 @@ model:环境变量 `MINERU_MODEL_ONNX_DEVICE` 高于文件配置。`small_backend=auto` 仍按原有规则选择Torch 或 ONNX;此选项不强制切换模型栈,也不改变 Torch / VLM 的设备。-| 值 | 行为 |-|---|---|-| `cpu` | ONNX 小模型全部使用 CPU,不枚举显卡、不测速 |-| `gpu` | 每个进程首次用模型时重新发现 GPU,独显优先,再选高性能排序中的集显,不测速 |-| `auto` | 独显直接选 GPU;仅有集显时执行一次 rec 测速;没有硬件 GPU 时选 CPU |+| 值 | Windows DML | Linux WebGPU |+|---|---|---|+| `cpu` | 全部使用 CPU,不枚举显卡、不测速 | 全部使用 CPU,不加载插件、不探测 GPU |+| `gpu` | 首次使用时发现 GPU,独显优先,不测速 | 首次使用时加载插件,使用默认高性能适配器,不测速 |+| `auto` | 独显直接使用;集显执行 rec 测速 | 集显和独显均执行 rec 测速 |Windows 使用 DXGI 高性能顺序、D3D12 UMA 属性和 LUID 匹配;适配器编号不写入配置。已保存的 `gpu` 使用 Windows 高性能排序及无设备创建的 D3D12 能力检查,避免逐张创建设备查询 UMA;`auto` 才需要 UMA 分类。旧系统缺少高性能排序接口时仍执行分类以保证独显优先。`gpu` 模式下移除显卡会在本次使用 CPU,仍保留配置中的 `gpu`。-非 Windows、驱动探测错误、运行时缺少 DML 或 GPU 会话初始化失败时,日志会说明 CPU 回退原因。+其他操作系统、驱动探测错误、运行时缺少所需 EP 或 GPU 会话初始化失败时,日志会说明 CPU 回退原因。++Linux 使用原生 `onnxruntime-ep-webgpu`,通过 Dawn/Vulkan 执行。+插件在每个进程首次需要 GPU 时注册;只保留可哈希、可序列化的设备描述,原生 EP 句柄在创建会话时重新获取。+WebGPU 0.4 的 ORT 设备枚举项用于注册 EP,并不绑定 Dawn 的物理显卡;使用 `powerPreference=high-performance`+选择默认适配器,不提供逐卡选优或手动编号选择。参数固定为 `preferredLayout=NHWC`、`enableGraphCapture=0`、+`dawnBackendType=Vulkan`,其他 WebGPU 参数沿用运行时默认值。首次实际使用模型时才选型。界面启动、导入 SDK、查询配置和模型列表不会运行测速。自动决策成功后原子更新有效配置文件中的 `model.onnx_device`,保留其余键;进程间通过文件锁协调。环境变量覆盖不写入文件;只读配置保留本进程选择。测速失败不保存为 CPU 性能优胜,同进程不重复尝试。+Linux 插件缺失、Vulkan/设备发现失败(包括未发现可用硬件设备)、校准超时或输出不一致时保留 `auto`。通过环境变量强制 `auto` 时,每个新进程重新选择,并使用选择锁避免同时测速。显式或已保存的 `cpu` 不会因后来接入独显而改变。@@ -33,7 +40,13 @@ Windows 使用 DXGI 高性能顺序、D3D12 UMA 属性和 LUID 匹配;适配## 模型预设-GPU 路线将 layout、rec、MFR 和 UNet 有线表格结构模型交给 DML,检测、表格分类和无线表格等其他 ONNX 模型保持 CPU。+| 模型 | Windows DML | Linux WebGPU |+|---|---|---|+| layout / OCR rec / MFR / UNet | GPU | GPU |+| 普通 OCR det | CPU | GPU |+| 印章检测 / 表格分类 / SLANet Plus | CPU | CPU |++普通文字检测和印章检测在模型入口通过 `ocr_det` / `seal_det` 明确区分。UNet 与其他模型共享 `model.onnx_device` 的进程内选择,不单独测速;ONNX 栈的 UNet 不受旧 `MINERU_TABLE_DEVICE` 覆盖。Torch 栈中的表格仍使用其原有 CPU/CUDA 策略。旧实验引擎和实验开关已经移除。@@ -44,26 +57,51 @@ Torch 栈中的表格仍使用其原有 CPU/CUDA 策略。旧实验引擎和实| NVIDIA | 开 | 开 | 关 | 关 || 其他 | ORT 默认 | ORT 默认 | ORT 默认 | ORT 默认 |-表格中的开关指 metacommands,关闭对应 `disable_metacommands=true`。+厂商表中的开关只适用于 DML 的 metacommands,关闭对应 `disable_metacommands=true`;WebGPU 不使用这些预设。Intel layout 优先首轮响应;NVIDIA layout 采用后续补测结论。-MFR 只有实际批量为 8 时使用 DML,1–7 张使用惰性创建、复用的 CPU 会话,不补齐。+MFR 只有实际批量为 8 时使用 DML/WebGPU,1–7 张使用惰性创建、复用的 CPU 会话,不补齐。+自定义批量小于 8 时全部使用 CPU;依据实际 EP 判断是否分流,GPU 初始化已回退 CPU 时不创建多余尾批会话。每个 DML 会话的 `run` 都串行化,不依赖宿主是否打开全局推理锁。+WebGPU 的所有会话创建和 `run` 共用进程内锁,因为多个会话共享 Dawn 状态;CPU 会话不使用此锁。+参见 [上游并发问题](https://github.com/microsoft/onnxruntime/issues/32561)。+GPU 会话必须实际启用目标 EP,关闭 Python 层整会话静默回退;不支持的图中算子仍可由 ORT 分配给 CPU。+WebGPU 初始化失败时使用未绑定插件的新 SessionOptions 创建 CPU 会话,并保留线程、内存及 profiling 设置。## 六图测速和阈值+### OCR rec 精简输出++CPU、DML 和 WebGPU 的 ONNX rec 共用 `ctc-ids-scores-v1` 派生图。保留原始 Softmax,在图末端增加+ArgMax / ReduceMax,只返回 `[batch,time]` 的 int64 字符 ID 与 float32 最大置信度。+CTC 去重、blank 删除、置信度平均和输入顺序还原继续由识别器处理;Torch 路径保持现有实现。+对于 `[6,3,48,320]` 输入,原始 `[6,40,18710]` 概率输出约 17.13 MiB,精简输出仅 2.81 KiB。++原始下载权重不被修改。首次使用时惰性加载 `onnx`,在 `model.base_dir/.derived/ocr-rec/` 中按+源文件 SHA256 和输出格式版本生成派生模型;文件锁、校验值和原子替换保护并发创建与缓存复用,+损坏的缓存会重建。当前转换支持内嵌权重的 float32、三维概率输出、固定字符数和 opset ≥ 11 的 rec 图。+正常会话和 CPU 回退都使用同一输出契约;精简模型不是 ORT 特定 EP 的预编译文件。++自动选型也使用该派生图,图转换发生在 15 秒 worker 预算之外。报告记录源模型、派生模型 SHA256+及 `output_format`,阈值工具拒绝混合完整概率输出和精简输出的报告。已有保存的 cpu/gpu 不会被覆盖;+需要重新测速时设置 `auto`。历史完整概率输出测速保留作为优化前对照,不据此重新推导精简图阈值。+本机阶段耗时、输出一致性和 EP 验证见 [精简 rec 生产验证](onnx-compact-rec-validation.md)。++### 测量协议+图片和 manifest 位于包内 `resources/onnx_calibration`,包含原始中英数字句子、来源和 SHA256。重新生成工具 `scripts.make_onnx_calibration_samples` 需要显式提供字体;部署时直接读取固定 PNG,不需要系统字体。输入复用生产 rec 的 BGR、动态宽度和归一化逻辑;CPU/GPU 使用同一份 batch6。每种设备预热一次,再计时两次。默认线程为 4/1;生产显式线程配置同时应用于测速。-比较 `ratio = gpu_elapsed / cpu_elapsed`,当 `ratio <= ONNX_GPU_TIME_RATIO_THRESHOLD` 时选 GPU。+比较 `ratio = gpu_elapsed / cpu_elapsed`,当 `ratio <= ONNX_GPU_TIME_RATIO_THRESHOLDS[backend]` 时选 GPU。阈值在 `mineru.model.runtime.onnx_device` 中集中维护。-当前阈值按用户要求简化为 **0.8**,即 GPU 推理耗时至少比 CPU 低 20%。+阈值按后端独立维护:`dml=0.8`、`webgpu=0.8`,即 GPU 推理耗时至少比 CPU 低 20%。+Linux 8060S 六图预检比值为 0.420、0.502、0.523,解码一致;目前没有跨硬件证据支持另设 WebGPU 数值。原三机校准推导值为 0.69639683662382,保留为历史参考;已有 24 次记录按 0.8 判断仍得到相同选择。-详细数据见 [三机校准报告](onnx-selection-validation.md)。+详细数据见 [DML 三机校准报告](onnx-selection-validation.md) 和 [Linux WebGPU 生产验证](onnx-webgpu-validation.md)。下载和设备发现不计入 worker 预算。独立隐藏子进程的总超时为 15 秒,包含解释器启动、加载及推理;-1–2 秒只是小样本推理目标。结果记录模型/输入校验值、ORT 版本与路径、provider、初始化、预热和正式计时。+1–2 秒只是小样本推理目标。结果记录 backend、模型/输入校验值、ORT/EP 版本、参数、provider、初始化、预热和正式计时。CPU/GPU 解码 token 必须一致,整会话回退 CPU 或计时无效都视为失败。### 发布前校准@@ -90,11 +128,37 @@ CPU/GPU 解码 token 必须一致,整会话回退 CPU 或计时无效都视为独立验证进程,要求 Ultra 5、MAX395 全部选 GPU,i7 全部选 CPU。没有有效区间或验证失败时不发布阈值。阈值为 `None` 表示三机校准尚未完成;此时自动集显路径使用 CPU、保留 `auto`,显式 GPU 及独显仍可使用。+Linux 使用同一个工具(从项目根目录运行):++```bash+.venv/bin/python -m scripts.benchmark_onnx_selection devices+.venv/bin/python -m scripts.benchmark_onnx_selection run --label linux-8060s --expect gpu --output output/webgpu-calibration+.venv/bin/python -m scripts.benchmark_onnx_selection run --label linux-8060s --threshold 0.8 --expect gpu --output output/webgpu-validation+```++第一条 run 启动 5 个新进程、交替 CPU/GPU 顺序;第二条启动 3 个独立验证进程。+Linux 标签可自定义,独立验证必须显式提供 `--expect cpu|gpu`;`--luid` 仅用于 DML。+WebGPU 推导新阈值需要至少三台不同标签机器的完整五进程报告,同时有 CPU/GPU 优胜组,+各组通过 `--expect` 按独立性能证据标记。不能混合 DML/WebGPU、EP 版本或 WebGPU 参数来推导阈值。+报告 schema v2 增加 backend/EP 字段;推导工具仍接受历史 v1 DML 报告。+WebGPU 阈值为 `None` 时,所有 auto GPU 路径保留 `auto` 并临时使用 CPU,显式 `gpu` 不受此限制。+## 安装-Windows x64 的直接依赖为 `onnxruntime-directml>=1.23,<2`,其他平台保留 CPU 包约束,+Windows x64 的直接依赖为 `onnxruntime-directml>=1.23,<2`,其他平台使用 `onnxruntime>=1.24.4,<2`,不按 Python 版本拆分条件。DirectML 包本身包含 CPU provider。+项目要求 Python >=3.11。Linux x86_64 同时安装 `onnxruntime-ep-webgpu>=0.4.0,<1`;+这是与 ORT 并存的插件,不替换 `onnxruntime`。Linux ARM64、macOS 和 Windows 不安装此插件。+Linux 还需系统提供 `libvulkan.so.1` 和可用的 GPU Vulkan 驱动。使用 uv 管理环境:++```bash+.venv/bin/uv pip install --python .venv/bin/python -e .+.venv/bin/uv pip check --python .venv/bin/python+```++插件注册前,`ort.get_available_providers()` 不一定列出 WebGPU;用上面的 `devices` 命令检查插件发现能力。+已有环境从 CPU 包迁移时,先卸载 `onnxruntime`,再安装 DirectML,避免两个发行包覆盖同一模块。安装后用 `import onnxruntime; print(onnxruntime.__version__, onnxruntime.get_available_providers())` 检查实际能力。diff --git a/docs/next/onnx-selection-validation.md b/docs/next/onnx-selection-validation.mdindex 9782012e..4b919b8f 100644--- a/docs/next/onnx-selection-validation.md+++ b/docs/next/onnx-selection-validation.md@@ -105,3 +105,10 @@ manifest、正式阈值及互斥的平台运行时依赖。完整依赖验收,`uv pip check` 通过,两个共享模块各自只有一个发行包所有者。DocVortex 的同版本 PyPI 元数据尚未更新;本次源码构建来源、回归与平台求解结果见[依赖验收报告](onnx-dependency-validation.md)。++## Linux WebGPU 后续验证++2026-10-21 已增加独立的 WebGPU 自动校准路径;集显、独显均执行六图 rec 测速。+DML 与 WebGPU 的阈值分开维护,当前均为 0.8,不能混合两种后端的报告推导新阈值。+Linux 本机的五次新进程校准、19 页完整解析、MFR CPU 尾批和 24 张表格 UNet 结果见+[Linux WebGPU 生产验证](onnx-webgpu-validation.md)。上文保留原 Windows 验证记录。diff --git a/docs/next/onnx-webgpu-validation.md b/docs/next/onnx-webgpu-validation.mdnew file mode 100644index 00000000..9c2f2056--- /dev/null+++ b/docs/next/onnx-webgpu-validation.md@@ -0,0 +1,91 @@+# Linux WebGPU 生产接入验证(2026-10-21)++Linux x86_64,AMD Radeon 8060S / RADV STRIX_HALO,Mesa 26.0.8;Python 3.14.4、ORT 1.31.0、WebGPU EP 0.4.0。+CPU intra-op=4、inter-op=1;WebGPU 使用 NHWC、graph capture 关闭、Vulkan、high-performance。DocVortex 和 Rust 扩展未修改。++## 自动选择与回归++5 个新进程的六图 rec 比值依次为 0.514125, 0.507938, 0.511882, 0.522714, 0.516614,全部输出一致且低于 WebGPU 阈值 0.8。+独立配置验证:首次 auto 校准后原子保存 gpu;第二个新进程直接返回 configured_gpu,保留其他配置键。+两个真实 WebGPU 会话通过 4 个调用线程交错运行 12 次,所有解码结果一致,无并发异常。+352 项 ONNX/依赖相关回归与 29 项模型预加载、运行时、CLI 测试通过;Ruff、git diff --check、uv pip check 通过。+Windows DML 本轮通过单元回归验证,未在 Windows 硬件重新测量。++## 完整解析++demo1+demo2 合并 PDF,共 19 页,basic+强制 OCR。CPU/GPU 均在新进程中运行两次,采用 CPU→GPU→GPU→CPU 的顺序。+下表未启用 Python observer 或 ORT profiler,计时为 parse 调用,不含结果写盘和导入模块;没有额外模型预热。++| 模式 | 第一次(秒) | 第二次(秒) | 平均(秒) |+|---|---:|---:|---:|+| CPU | 56.927 | 56.534 | 56.731 |+| WebGPU+MFR CPU 尾批 | 36.003 | 36.325 | 36.164 |++平均加速 **1.569×**,耗时减少 **36.25%**。+四次正式运行和两次 observer 运行的 Markdown、三个 JSON 与 46 个素材,共 50 个文件全部逐字节一致。++## 各阶段累计推理++以下是独立 observer 运行中同步 ORT session.run 的累计墙钟,包含首次调用、数据传输和 EP 内部 CPU 算子,+不含模型初始化、预处理、后处理。Python profiler 通过标准 call/return 事件观测,没有替换生产函数;+observer 运行的整本解析耗时不参与上面的完整解析对比。两个相同类别会话的时间已合计。++| 模型 | CPU 推理(秒) | WebGPU 路线推理(秒) | 加速 | CPU 初始化(秒) | WebGPU 路线初始化(秒) |+|---|---:|---:|---:|---:|---:|+| layout | 4.702 | 1.973 | 2.38× | 0.429 | 0.410 |+| mfr | 21.535 | 12.283 | 1.75× | 0.470 | 0.915 |+| ocr_det | 1.154 | 0.705 | 1.64× | 0.028 | 0.071 |+| ocr_rec | 17.394 | 8.937 | 1.95× | 0.061 | 0.065 |+| unet | 0.225 | 0.110 | 2.04× | 0.019 | 0.025 |++MFR 共 151 个公式:18 个满批(每批 8 个)和一个尾批(7 个)。WebGPU 路线的 MFR 初始化包含额外 CPU 尾批会话。++| 路线 | 实际 EP | 批量 | 调用次数 | 推理(秒) |+|---|---|---:|---:|---:|+| cpu-observed | CPUExecutionProvider | 8 | 18 | 18.674 |+| cpu-observed | CPUExecutionProvider | 7 | 1 | 2.861 |+| webgpu-observed | WebGpuExecutionProvider | 8 | 18 | 9.374 |+| webgpu-observed | CPUExecutionProvider | 7 | 1 | 2.909 |++## 排除预热后的模型回放++前五行使用生产 CPU 解析中捕获的第一批真实输入;UNet 使用 4130107.pdf 的全部 24 张已有 layout 裁切图,不按表格类型分流。+每个后端预热一遍,再交替顺序测量五轮,取总推理时间中位数。初始化和预热另列;后处理不计时。+该单批 rec 输入是 [6,3,48,320],比自动校准的 [6,3,48,1012] 窄,因此不能用其速度代替整份文档的结果。++| 模型/输入 | CPU(秒) | WebGPU(秒) | 加速 | CPU/GPU 初始化(秒) | CPU/GPU 预热(秒) |+|---|---:|---:|---:|---:|---:|+| layout | 0.516468 | 0.222966 | 2.32× | 0.452 / 0.427 | 0.505 / 0.267 |+| mfr | 0.976330 | 0.364917 | 2.68× | 0.603 / 0.449 | 1.083 / 0.379 |+| mfr_tail | 2.869976 | 5.246856 | 0.55× | 0.566 / 0.428 | 2.871 / 5.360 |+| ocr_det | 0.006753 | 0.006659 | 1.01× | 0.013 / 0.046 | 0.011 / 0.025 |+| ocr_rec | 0.017542 | 0.023186 | 0.76× | 0.035 / 0.060 | 0.027 / 0.046 |+| UNet / 24 张表 | 1.432913 | 0.501504 | 2.86× | 0.018 / 0.055 | 1.551 / 0.545 |++mfr_tail 的全 GPU 回放仅用于验证分流收益,生产代码仍将不足 8 的批次送给 CPU。+UNet:24 张表中 23 张掩码完全一致,p04-t02 有 1 个像素不同;总共 9,110,528 个掩码像素。+MFR、MFR 尾批和 rec 回放的解码 token 均一致。小尺寸 rec 输入上 GPU 较慢,完整 PDF 的 rec 累计推理仍有明显加速。++## EP 与 CPU 算子回退++单独创建 profiling 会话,在计时完成后采样首个输入。以下为该次 profile 执行到的独立节点数量,不能当作耗时占比。++| 模型 | WebGPU 节点 | CPU 节点 | CPU 算子种类 |+|---|---:|---:|---|+| layout | 1397 | 304 | Add, Cast, Concat, CumSum, Equal, Expand, Gather, GatherElements, GatherND, Less, LessOrEqual, Max, Min, Mod, Mul, Range, ReduceSum, Reshape, ScatterElements, ScatterND, Shape, Slice, Squeeze, Sub, Tile, TopK, Transpose, Unsqueeze, Where |+| mfr | 616 | 275 | Add, Cast, Concat, ConstantOfShape, Equal, Expand, Gather, Loop, Mul, Range, ReduceMax, Reshape, Shape, Slice, Unsqueeze, Where |+| ocr_det | 158 | 0 | 无 |+| ocr_rec | 204 | 3 | Concat, Gather, Slice |+| unet | 155 | 38 | Concat, Gather, Slice, Unsqueeze |++所有目标会话实际启用 WebGpuExecutionProvider;CPU 节点属于图内分配,没有整会话静默回退。+SLANet Plus、表格分类和印章检测仍走 CPU。此数据只验证当前 AMD Linux 主机,不据此修改 DML 阈值或推广为全部 GPU 的速度保证。++## 复现与原始数据++[机器可读报告](benchmarks/onnx-webgpu-linux-20261021.json) 包含逐次校准、模型/源码 SHA256、输入形状、输出差异、EP 参数及生命周期结果。+原始目录为 `demo/output/webgpu-production-20261021/`,包含 benchmark.py、analyze.py、逐次输出、捕获张量及 profile。+从项目根目录使用 `.venv/bin/python` 运行,完整解析分别设置 `MINERU_MODEL_SMALL_BACKEND=onnx`、+`MINERU_MODEL_ONNX_DEVICE=cpu|gpu`、`MINERU_INTRA_OP_NUM_THREADS=4`、`MINERU_INTER_OP_NUM_THREADS=1`;+调用 `benchmark.py parse --output <新目录>`;需要独立推理观测时增加 `--observe`。+六图校准与正式使用方法见 [ONNX 设备选择](onnx-device.md)。diff --git a/mineru/model/mfr/pp_formulanet_plus_m_onnx.py b/mineru/model/mfr/pp_formulanet_plus_m_onnx.pyindex 03a38b9e..259ab8a2 100644--- a/mineru/model/mfr/pp_formulanet_plus_m_onnx.py+++ b/mineru/model/mfr/pp_formulanet_plus_m_onnx.py@@ -2,7 +2,7 @@"""ONNX 后端的 PP-FormulaNet-Plus-M 推理封装。与 ``UnimernetModel``(torch/transformers 后端)公开接口完全一致,-内部用 onnxruntime CPU 推理从 MinerU Torch 权重导出的 PP-FormulaNet_plus-M ONNX 模型。+内部用 ONNX Runtime 推理从 MinerU Torch 权重导出的 PP-FormulaNet_plus-M ONNX 模型。ONNX 模型输出 token IDs(int64),不是 logits——自回归循环已 bake 进计算图,一次前向推理即可得到完整 LaTeX token 序列。@@ -22,7 +22,7 @@ import yamlfrom loguru import loggerfrom tqdm import tqdm-from ..runtime.onnx import ort_session+from ..runtime.onnx import create_ort_session, ort_sessionfrom .pp_formulanet.processors import LatexImageFormat, UniMERNetDecode, UniMERNetImgDecode, UniMERNetTestTransform__all__ = ["PPFormulaNetPlusMONNX", "CPU_BATCH_SIZE"]@@ -50,10 +50,10 @@ class PPFormulaNetPlusMONNX:device: Optional[str] = None,intra_op_num_threads: int = 0,) -> None:- """初始化 CPU 公式模型,并复用 Torch Plus-M 的纯 Python 处理器。"""+ """初始化统一选型的公式模型,并复用 Plus-M 的纯 Python 处理器。"""self.device = "cpu"self.session = ort_session(model_path, device, intra_op_num_threads, model_kind="mfr")- self.cpu_tail_enabled = "DmlExecutionProvider" in self.session.get_providers()+ self.cpu_tail_enabled = self.session.get_providers()[0] in {"DmlExecutionProvider", "WebGpuExecutionProvider"}self._cpu_tail_session = Noneself._cpu_tail_lock = threading.Lock()self._model_path = model_path@@ -161,7 +161,7 @@ class PPFormulaNetPlusMONNX:# 推理# ------------------------------------------------------------------def _infer_batch(self, crops: List[np.ndarray], batch_size: int = CPU_BATCH_SIZE) -> List[str]:- """按原图面积排序,以最多 8 张的 CPU 批次推理,再恢复原顺序。"""+ """按原图面积排序,以最多 8 张合批;GPU 路线不足 8 张时用 CPU,最后恢复原顺序。"""if not crops:return []@@ -187,7 +187,9 @@ class PPFormulaNetPlusMONNX:if self.cpu_tail_enabled and inputs.shape[0] < CPU_BATCH_SIZE:with self._cpu_tail_lock:if self._cpu_tail_session is None:- self._cpu_tail_session = ort_session(self._model_path, "cpu", self._intra_op_num_threads)+ self._cpu_tail_session = create_ort_session(+ self._model_path, intra_op_num_threads=self._intra_op_num_threads+ )session = self._cpu_tail_sessionpreds = session.run(None, {self.input_name: inputs})[0]if preds.ndim != 2 or preds.shape[0] != inputs.shape[0]:diff --git a/mineru/model/ocr/pp_ocr_v6_onnx.py b/mineru/model/ocr/pp_ocr_v6_onnx.pyindex c80ce054..f7c11543 100644--- a/mineru/model/ocr/pp_ocr_v6_onnx.py+++ b/mineru/model/ocr/pp_ocr_v6_onnx.py@@ -25,6 +25,7 @@ from loguru import loggerfrom tqdm import tqdmfrom ..runtime.onnx import ort_session+from ..runtime.onnx_rec import REC_IDS_OUTPUT, REC_SCORES_OUTPUT, prepare_rec_model, validate_rec_outputsfrom .._internal.pytorchocr.data.imaug.operators import DetResizeForTest, NormalizeImagefrom .db_postprocess import DBPostProcessfrom .resources import PPOCRV6_DICT_PATH@@ -81,9 +82,10 @@ class TextDetectorONNX:use_dilation: bool = False,box_type: Literal["quad", "poly"] = "quad",intra_op_num_threads: int = 0,+ model_kind: Literal["ocr_det", "seal_det"] = "ocr_det",) -> None:- """加载 CPU ONNX 会话并设置推理参数。"""- self.session = ort_session(model_path, device, intra_op_num_threads)+ """按检测类别选择 ONNX 会话并设置推理参数。"""+ self.session = ort_session(model_path, device, intra_op_num_threads, model_kind=model_kind)self.input_name = self.session.get_inputs()[0].nameself.limit_side_len = limit_side_len@@ -261,8 +263,9 @@ class TextRecognizerONNX:drop_score: float = 0.5,intra_op_num_threads: int = 0,) -> None:- """加载 CPU ONNX 会话并设置推理参数。"""- self.session = ort_session(model_path, device, intra_op_num_threads, model_kind="ocr_rec")+ """CPU、DML、WebGPU 共享精简 CTC 输出图,保持原始权重不变。"""+ compact = prepare_rec_model(model_path)+ self.session = ort_session(compact.path, device, intra_op_num_threads, model_kind="ocr_rec")self.input_name = self.session.get_inputs()[0].nameself.img_c, self.img_h, self.img_w = rec_image_shape@@ -273,21 +276,15 @@ class TextRecognizerONNX:# dict_path 可以是 txt 文件(每行一个字符)或 inference.yml(含 character_dict 列表)chars = _load_character_dict(dict_path)self.character = ["blank"] + chars + [" "]- outputs = self.session.get_outputs()- classes = outputs[0].shape[-1]- if isinstance(classes, int) and classes != len(self.character):- raise ValueError(f"OCR CTC output has {classes} classes but dictionary requires {len(self.character)}")+ if compact.num_classes != len(self.character):+ raise ValueError(f"OCR CTC output has {compact.num_classes} classes but dictionary requires {len(self.character)}")def _resize_norm_img(self, img: np.ndarray, max_wh_ratio: float) -> np.ndarray:"""复用标准识别输入处理,包含最小宽度与超长文字宽度上限。"""return resize_text_recognition_image(img, max_wh_ratio, (self.img_c, self.img_h, self.img_w))- def _decode(self, pred: np.ndarray) -> Tuple[str, float]:- """CTC 解码单个 prediction。"""- if pred.ndim != 2 or pred.shape[1] != len(self.character):- raise ValueError(f"Unexpected OCR CTC output shape: {pred.shape}; expected (*, {len(self.character)})")- idx = pred.argmax(axis=1)- prob = pred.max(axis=1)+ def _decode(self, idx: np.ndarray, prob: np.ndarray) -> Tuple[str, float]:+ """解码已在 ONNX 图内归约的 ID/置信度,保留 CTC 和空行语义。"""# collapse consecutive duplicatesselection = np.ones(len(idx), dtype=bool)selection[1:] = idx[1:] != idx[:-1]@@ -336,11 +333,11 @@ class TextRecognizerONNX:norm_img_batch.append(norm_img[np.newaxis, ...])batch_tensor = np.concatenate(norm_img_batch, axis=0).astype(np.float32)- preds = self.session.run(None, {self.input_name: batch_tensor})[0]- # preds shape: [B, T, C]+ outputs = self.session.run([REC_IDS_OUTPUT, REC_SCORES_OUTPUT], {self.input_name: batch_tensor})+ ids, scores = validate_rec_outputs(outputs, batch_size=len(batch_indices), num_classes=len(self.character))for i, idx in enumerate(batch_indices):- text, conf = self._decode(preds[i])+ text, conf = self._decode(ids[i], scores[i])rec_res[idx] = (text, conf)pbar.update(end - beg)finally:@@ -374,7 +371,7 @@ class PPOCRv6ONNX:intra_op_num_threads: int = 0,lang: Literal["ch", "seal"] = "ch",) -> None:- """按普通文字或印章模式初始化 CPU 检测与识别模型。"""+ """按普通文字或印章模式初始化检测与识别模型。"""if lang not in {"ch", "seal"}:raise ValueError(f"Unsupported ONNX OCR mode: {lang}")self.lang = lang@@ -389,6 +386,7 @@ class PPOCRv6ONNX:box_thresh=0.6 if self.is_seal else det_db_box_thresh,unclip_ratio=0.5 if self.is_seal else det_db_unclip_ratio,box_type="poly" if self.is_seal else "quad",+ model_kind="seal_det" if self.is_seal else "ocr_det",use_dilation=False,intra_op_num_threads=intra_op_num_threads,)diff --git a/mineru/model/runtime/onnx.py b/mineru/model/runtime/onnx.pyindex b2e89188..6ab61a8b 100644--- a/mineru/model/runtime/onnx.py+++ b/mineru/model/runtime/onnx.py@@ -2,6 +2,7 @@"""ONNX Runtime CPU 会话与自动选择设备的表格会话。"""import os+import sysimport timeimport threadingfrom pathlib import Path@@ -12,14 +13,15 @@ from loguru import loggerfrom .onnx_device import OnnxSelection, confirm_gpu_session, metacommands_enabled, resolve_onnx_devicefrom .windows_gpu import GpuAdapter+from .webgpu import WEBGPU_PROVIDER, WebGPUDevice, attach_webgpu_provider, execution_lock-class DirectMLSession:- """Serialize Run on a DML session, independently of host inference-lock settings."""+class _SerializedSession:+ """Serialize native calls independently of host inference-lock settings."""- def __init__(self, session: ort.InferenceSession) -> None:+ def __init__(self, session: ort.InferenceSession, lock: Any) -> None:self._session = session- self._lock = threading.Lock()+ self._lock = lockdef run(self, output_names: list[str] | None, input_feed: dict[str, Any], run_options: Any = None) -> list[Any]:with self._lock:@@ -38,7 +40,47 @@ class DirectMLSession:return self._session.get_provider_options()def end_profiling(self) -> str:- return self._session.end_profiling()+ with self._lock:+ return self._session.end_profiling()+++class DirectMLSession(_SerializedSession):+ """DML requires serialization within each session."""++ def __init__(self, session: ort.InferenceSession) -> None:+ super().__init__(session, threading.Lock())+++class WebGPUSession(_SerializedSession):+ """WebGPU sessions share one process-wide Dawn runtime and execution lock."""++ def __init__(self, session: ort.InferenceSession) -> None:+ super().__init__(session, execution_lock())+++def _cpu_fallback_options(source: ort.SessionOptions | None) -> ort.SessionOptions:+ """Preserve the runtime's options without copying a plugin provider attachment."""+ target = ort.SessionOptions()+ if source is not None:+ for name in (+ "intra_op_num_threads",+ "inter_op_num_threads",+ "enable_cpu_mem_arena",+ "enable_mem_pattern",+ "enable_mem_reuse",+ "enable_profiling",+ "profile_file_prefix",+ "execution_mode",+ "execution_order",+ "graph_optimization_level",+ "log_severity_level",+ "log_verbosity_level",+ "logid",+ "optimized_model_filepath",+ "use_deterministic_compute",+ ):+ setattr(target, name, getattr(source, name))+ return targetdef get_op_num_threads(env_name: str) -> int:@@ -78,20 +120,20 @@ def ort_providers(device: str | None = None) -> list[tuple[str, dict[str, objectdef create_ort_session(model_path: str,*,- adapter: GpuAdapter | None = None,+ adapter: GpuAdapter | WebGPUDevice | None = None,model_kind: str = "",intra_op_num_threads: int = 0,inter_op_num_threads: int = 0,profile_prefix: str | None = None,sess_options: ort.SessionOptions | None = None,-) -> ort.InferenceSession | DirectMLSession:- """Create an explicit CPU/DML session without selection, downloads or config writes."""+) -> ort.InferenceSession | DirectMLSession | WebGPUSession:+ """Create an explicit CPU/DML/WebGPU session without selection or config writes."""opts = sess_options if sess_options is not None else ort.SessionOptions()opts.log_severity_level = 3opts.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALLconfigure_ort_threads(opts, intra_op_num_threads=intra_op_num_threads, inter_op_num_threads=inter_op_num_threads)providers = ort_providers()- if adapter is not None:+ if isinstance(adapter, GpuAdapter):if "DmlExecutionProvider" not in ort.get_available_providers():raise RuntimeError("DmlExecutionProvider is unavailable")opts.enable_mem_pattern = False@@ -104,6 +146,15 @@ def create_ort_session(if profile_prefix is not None:opts.enable_profiling = Trueopts.profile_file_prefix = profile_prefix+ if isinstance(adapter, WebGPUDevice):+ with execution_lock():+ attach_webgpu_provider(opts, adapter)+ # Passing providers= here overrides (and removes) the plugin attached to opts.+ session = ort.InferenceSession(model_path, sess_options=opts)+ if not session.get_providers() or session.get_providers()[0] != WEBGPU_PROVIDER:+ raise RuntimeError("Requested WebGPU session fell back to CPU during initialization")+ session.disable_fallback()+ return WebGPUSession(session)session = ort.InferenceSession(model_path, sess_options=opts, providers=providers)if adapter is not None:if session.get_providers()[0] != "DmlExecutionProvider":@@ -121,14 +172,22 @@ def ort_session(model_kind: str = "",selection: OnnxSelection | None = None,sess_options: ort.SessionOptions | None = None,-) -> ort.InferenceSession | DirectMLSession:- """Use the process ONNX choice for layout, rec, MFR and UNet; other models stay on CPU."""- eligible = model_kind in {"layout", "ocr_rec", "mfr", "unet"}+) -> ort.InferenceSession | DirectMLSession | WebGPUSession:+ """Route supported models through DML/ WebGPU; detection is GPU-eligible only on Linux."""+ eligible = model_kind in {"layout", "ocr_rec", "mfr", "unet"} or (model_kind == "ocr_det" and sys.platform == "linux")selection_threads = intra_op_num_threadsif selection_threads <= 0 and sess_options is not None:selection_threads = sess_options.intra_op_num_threads- route = (selection or resolve_onnx_device(intra_op_num_threads=selection_threads)) if eligible else OnnxSelection()+ route = OnnxSelection()+ if eligible:+ route = selection or resolve_onnx_device(+ intra_op_num_threads=selection_threads,+ inter_op_num_threads=sess_options.inter_op_num_threads if sess_options is not None else 0,+ )adapter = route.adapter if route.device == "gpu" else None+ if model_kind == "ocr_det" and not isinstance(adapter, WebGPUDevice):+ adapter = None+ fallback_opts = _cpu_fallback_options(sess_options) if isinstance(adapter, WebGPUDevice) else sess_optionsstarted = time.perf_counter()try:session = create_ort_session(@@ -141,8 +200,8 @@ def ort_session(except Exception as error:if adapter is None:raise- logger.warning("ONNX {} DML initialization failed; using CPU: {}", model_kind, error)- session = create_ort_session(model_path, intra_op_num_threads=intra_op_num_threads, sess_options=sess_options)+ logger.warning("ONNX {} {} initialization failed; using CPU: {}", model_kind, route.backend, error)+ session = create_ort_session(model_path, intra_op_num_threads=intra_op_num_threads, sess_options=fallback_opts)adapter = Noneelse:if adapter is not None:@@ -152,7 +211,7 @@ def ort_session(model_kind,session.get_providers(),adapter,- metacommands_enabled(adapter.vendor, model_kind) if adapter else None,+ metacommands_enabled(adapter.vendor, model_kind) if isinstance(adapter, GpuAdapter) else None,time.perf_counter() - started,)return session@@ -164,7 +223,7 @@ def table_ort_session(sess_options: ort.SessionOptions | None = None,small_backend: str | None = None,model_kind: str = "",-) -> ort.InferenceSession | DirectMLSession:+) -> ort.InferenceSession | DirectMLSession | WebGPUSession:"""ONNX 栈的 UNet 复用统一设备选择;其他表格保留原有 CPU/CUDA 策略。"""from .device import resolve_small_model_backend@@ -231,6 +290,7 @@ def table_ort_session(__all__ = ["DirectMLSession",+ "WebGPUSession","configure_ort_threads","create_ort_session","get_op_num_threads",diff --git a/mineru/model/runtime/onnx_calibration.py b/mineru/model/runtime/onnx_calibration.pyindex 64e826dc..6bd59659 100644--- a/mineru/model/runtime/onnx_calibration.py+++ b/mineru/model/runtime/onnx_calibration.py@@ -3,6 +3,7 @@from __future__ import annotationsfrom dataclasses import asdict+from importlib.metadata import versionimport hashlibimport jsonimport math@@ -14,6 +15,8 @@ import timefrom typing import Anyfrom .windows_gpu import GpuAdapter+from .webgpu import WEBGPU_OPTIONS, WEBGPU_PROVIDER, WebGPUDevice+from .onnx_rec import REC_IDS_OUTPUT, REC_OUTPUT_FORMAT, REC_SCORES_OUTPUT, prepare_rec_model, validate_rec_outputsCALIBRATION_TIMEOUT_SECONDS = 15@@ -55,13 +58,12 @@ def prepare_input() -> tuple[Any, dict[str, Any]]:}-def _tokens(outputs: Any) -> list[list[int]]:+def _tokens(outputs: Any, num_classes: int) -> list[list[int]]:import numpy as np- if outputs.ndim != 3 or outputs.shape[0] != 6 or not np.isfinite(outputs).all():- raise ValueError("Invalid rec calibration output")+ ids, _ = validate_rec_outputs(outputs, batch_size=6, num_classes=num_classes)result = []- for row in outputs.argmax(axis=-1):+ for row in ids:keep = np.ones(len(row), dtype=bool)keep[1:] = row[1:] != row[:-1]keep &= row != 0@@ -71,7 +73,7 @@ def _tokens(outputs: Any) -> list[list[int]]:def measure_rec(model_path: str,- adapter: GpuAdapter,+ adapter: GpuAdapter | WebGPUDevice,*,intra_op_num_threads: int = 0,inter_op_num_threads: int = 0,@@ -82,27 +84,34 @@ def measure_rec(from .onnx import configure_ort_threads, create_ort_sessionfrom .onnx_device import metacommands_enabled+ compact = prepare_rec_model(model_path)tensor, evidence = prepare_input()options = ort.SessionOptions()configure_ort_threads(options, intra_op_num_threads=intra_op_num_threads, inter_op_num_threads=inter_op_num_threads)+ backend = "webgpu" if isinstance(adapter, WebGPUDevice) else "dml"result: dict[str, Any] = {- "schema_version": 1,+ "schema_version": 3,+ "backend": backend,+ "ep_version": version("onnxruntime-ep-webgpu") if backend == "webgpu" else ort.__version__,+ "webgpu_options": dict(WEBGPU_OPTIONS) if backend == "webgpu" else None,"adapter": asdict(adapter),"input": evidence,- "model_sha256": hashlib.sha256(Path(model_path).read_bytes()).hexdigest(),+ "model_sha256": compact.model_sha256,+ "source_model_sha256": compact.source_sha256,+ "output_format": REC_OUTPUT_FORMAT,"ort_version": ort.__version__,"ort_path": ort.__file__,"python": sys.version,"intra_threads": options.intra_op_num_threads,"inter_threads": options.inter_op_num_threads,- "rec_metacommands": metacommands_enabled(adapter.vendor, "ocr_rec"),+ "rec_metacommands": metacommands_enabled(adapter.vendor, "ocr_rec") if isinstance(adapter, GpuAdapter) else None,"gpu_first": gpu_first,}decoded: dict[str, list[list[int]]] = {}for device in ("gpu", "cpu") if gpu_first else ("cpu", "gpu"):started = time.perf_counter()session = create_ort_session(- model_path,+ compact.path,adapter=adapter if device == "gpu" else None,model_kind="ocr_rec",intra_op_num_threads=options.intra_op_num_threads,@@ -114,9 +123,9 @@ def measure_rec(reference = Nonefor _ in range(3):started = time.perf_counter()- outputs = session.run(None, {name: tensor})[0]+ outputs = session.run([REC_IDS_OUTPUT, REC_SCORES_OUTPUT], {name: tensor})elapsed.append(time.perf_counter() - started)- tokens = _tokens(outputs)+ tokens = _tokens(outputs, compact.num_classes)if reference is not None and tokens != reference:raise ValueError(f"{device} rec calibration output changed between runs")reference = tokens@@ -133,14 +142,18 @@ def measure_rec(def run_calibration(model_path: str,- adapter: GpuAdapter,+ adapter: GpuAdapter | WebGPUDevice,*,intra_op_num_threads: int = 0,inter_op_num_threads: int = 0,gpu_first: bool = False,) -> dict[str, Any]:"""Run a fresh, hidden worker and kill it on timeout; failed measurements are never decisions."""+ # Build/hash the derived graph outside the bounded inference worker, like model download.+ prepare_rec_model(model_path)+ backend = "webgpu" if isinstance(adapter, WebGPUDevice) else "dml"request = {+ "backend": backend,"model_path": model_path,"adapter": asdict(adapter),"intra_op_num_threads": intra_op_num_threads,@@ -165,8 +178,15 @@ def run_calibration(for key in ("cpu_elapsed", "gpu_elapsed"):if not isinstance(result.get(key), (float, int)) or not math.isfinite(result[key]) or result[key] <= 0:raise ValueError("Calibration worker returned invalid timings")- if result.get("outputs_match") is not True or result["gpu"]["providers"][0] != "DmlExecutionProvider":- raise ValueError("Calibration worker did not confirm valid DirectML results")+ provider = WEBGPU_PROVIDER if backend == "webgpu" else "DmlExecutionProvider"+ if (+ result.get("backend") != backend+ or result.get("output_format") != REC_OUTPUT_FORMAT+ or result.get("outputs_match") is not True+ or result.get("gpu", {}).get("providers", [])[:1] != [provider]+ or result.get("cpu", {}).get("providers", [])[:1] != ["CPUExecutionProvider"]+ ):+ raise ValueError(f"Calibration worker did not confirm valid {provider} results")result["worker_wall_s"] = time.perf_counter() - startedreturn result@@ -174,7 +194,15 @@ def run_calibration(def main() -> None:"""Private JSON-over-stdin worker; no user files or global configuration are modified."""request = json.load(sys.stdin)- adapter = GpuAdapter(**request.pop("adapter"))+ backend = request.pop("backend")+ descriptor = request.pop("adapter")+ if backend == "webgpu":+ descriptor["metadata"] = tuple(tuple(pair) for pair in descriptor["metadata"])+ adapter = WebGPUDevice(**descriptor)+ elif backend == "dml":+ adapter = GpuAdapter(**descriptor)+ else:+ raise ValueError(f"Unknown calibration backend: {backend}")result = measure_rec(adapter=adapter, **request)print(json.dumps(result, ensure_ascii=True))diff --git a/mineru/model/runtime/onnx_device.py b/mineru/model/runtime/onnx_device.pyindex 7771cfee..683d6969 100644--- a/mineru/model/runtime/onnx_device.py+++ b/mineru/model/runtime/onnx_device.py@@ -13,10 +13,11 @@ from filelock import FileLockfrom loguru import loggerfrom .windows_gpu import GpuAdapter, discover_gpus, preferred_gpu+from .webgpu import WebGPUDevice, discover_webgpu_devices# Require at least 20% lower GPU inference time; simplified to 0.8 at the user's request.-# Evidence: docs/next/onnx-selection-validation.md. None disables unvalidated auto-iGPU choices.-ONNX_GPU_TIME_RATIO_THRESHOLD: float | None = 0.8+# Evidence: docs/next/onnx-selection-validation.md. Each backend can be calibrated independently.+ONNX_GPU_TIME_RATIO_THRESHOLDS: dict[str, float | None] = {"dml": 0.8, "webgpu": 0.8}METACOMMANDS: dict[str, dict[str, bool]] = {"intel": {"layout": False, "mfr": True, "ocr_rec": False, "unet": False},"amd": {"layout": True, "mfr": True, "ocr_rec": True, "unet": True},@@ -29,8 +30,9 @@ class OnnxSelection:"""Immutable actual route. Adapter identity is valid only in this process."""device: Literal["cpu", "gpu"] = "cpu"- adapter: GpuAdapter | None = None+ adapter: GpuAdapter | WebGPUDevice | None = Nonereason: str = "cpu"+ backend: Literal["dml", "webgpu"] | None = None_lock = threading.RLock()@@ -53,43 +55,52 @@ def choose_gpu(cpu_elapsed: float, gpu_elapsed: float, threshold: float) -> boolreturn gpu_elapsed / cpu_elapsed <= threshold-def _select(requested: str, intra_op_num_threads: int) -> tuple[OnnxSelection, bool]:+def _select(requested: str, intra_op_num_threads: int, inter_op_num_threads: int) -> tuple[OnnxSelection, bool]:if requested == "cpu":return OnnxSelection(reason="configured_cpu"), False- if sys.platform != "win32":+ if sys.platform not in {"win32", "linux"}:if requested == "gpu":- logger.warning("ONNX GPU selection requires Windows DirectML; using CPU")+ logger.warning("ONNX GPU selection requires Windows DirectML or Linux WebGPU; using CPU")return OnnxSelection(reason="unsupported_platform"), False- import onnxruntime as ort-- if "DmlExecutionProvider" not in ort.get_available_providers():- logger.warning("DirectML provider unavailable; using CPU without saving a performance decision")- return OnnxSelection(reason="directml_unavailable"), False- adapter = preferred_gpu(discover_gpus(classify=requested == "auto"))+ backend: Literal["dml", "webgpu"] = "webgpu" if sys.platform == "linux" else "dml"+ if backend == "webgpu":+ devices = discover_webgpu_devices()+ adapter = devices[0] if devices else None+ else:+ import onnxruntime as ort++ if "DmlExecutionProvider" not in ort.get_available_providers():+ logger.warning("DirectML provider unavailable; using CPU without saving a performance decision")+ return OnnxSelection(reason="directml_unavailable"), False+ adapter = preferred_gpu(discover_gpus(classify=requested == "auto"))if adapter is None:- return OnnxSelection(reason="no_hardware_gpu"), requested == "auto"- if requested == "gpu" or adapter.uma is False:- return OnnxSelection("gpu", adapter, "configured_gpu" if requested == "gpu" else "discrete_gpu"), False- if ONNX_GPU_TIME_RATIO_THRESHOLD is None:- logger.warning("ONNX integrated-GPU threshold has not completed calibration; using CPU without saving")+ # A plugin can expose no devices after a driver discovery failure. Do not persist that as a CPU win.+ return OnnxSelection(reason="no_hardware_gpu"), requested == "auto" and backend == "dml"+ if requested == "gpu" or (isinstance(adapter, GpuAdapter) and adapter.uma is False):+ return OnnxSelection("gpu", adapter, "configured_gpu" if requested == "gpu" else "discrete_gpu", backend), False+ threshold = ONNX_GPU_TIME_RATIO_THRESHOLDS[backend]+ if threshold is None:+ logger.warning("ONNX {} threshold has not completed calibration; using CPU without saving", backend)return OnnxSelection(reason="calibration_pending"), Falsefrom ..registry import MINERU_4_MODELS_ONNXfrom .onnx_calibration import run_calibration# Download follows the normal model lifecycle; it is outside the 15-second worker timeout.model = MINERU_4_MODELS_ONNX.ocr_rec.ensure()- result = run_calibration(str(model), adapter, intra_op_num_threads=intra_op_num_threads)- use_gpu = choose_gpu(result["cpu_elapsed"], result["gpu_elapsed"], ONNX_GPU_TIME_RATIO_THRESHOLD)+ result = run_calibration(+ str(model), adapter, intra_op_num_threads=intra_op_num_threads, inter_op_num_threads=inter_op_num_threads+ )+ use_gpu = choose_gpu(result["cpu_elapsed"], result["gpu_elapsed"], threshold)logger.bind(onnx_calibration=result).info("ONNX rec calibration: {}", result)- return OnnxSelection("gpu" if use_gpu else "cpu", adapter if use_gpu else None, "rec_calibration"), True+ return OnnxSelection("gpu" if use_gpu else "cpu", adapter if use_gpu else None, "rec_calibration", backend), True-def _select_and_save(requested: str, source: str, intra_op_num_threads: int) -> OnnxSelection:+def _select_and_save(requested: str, source: str, intra_op_num_threads: int, inter_op_num_threads: int) -> OnnxSelection:from ...config import read_onnx_device_from_file, update_config_fileif requested == "auto" and source != "env":requested = read_onnx_device_from_file()- selection, completed = _select(requested, intra_op_num_threads)+ selection, completed = _select(requested, intra_op_num_threads, inter_op_num_threads)if completed and requested == "auto" and source != "env":try:update_config_file({"model": {"onnx_device": selection.device}}, only_if_onnx_auto=True)@@ -98,7 +109,7 @@ def _select_and_save(requested: str, source: str, intra_op_num_threads: int) ->return selection-def resolve_onnx_device(*, intra_op_num_threads: int = 0) -> OnnxSelection:+def resolve_onnx_device(*, intra_op_num_threads: int = 0, inter_op_num_threads: int = 0) -> OnnxSelection:"""Resolve at first model use; imports and diagnostic commands never call this function."""from ...config import config, get_config_file_path, get_config_source@@ -110,18 +121,18 @@ def resolve_onnx_device(*, intra_op_num_threads: int = 0) -> OnnxSelection:if key in _selections:return _selections[key]try:- if requested != "auto" or sys.platform != "win32":- selection = _select_and_save(requested, source, intra_op_num_threads)+ if requested != "auto" or sys.platform not in {"win32", "linux"}:+ selection = _select_and_save(requested, source, intra_op_num_threads, inter_op_num_threads)else:try:Path(path).parent.mkdir(parents=True, exist_ok=True)selection_lock = FileLock(f"{path}.onnx-selection.lock", timeout=30)selection_lock.acquire()except OSError:- selection = _select_and_save(requested, source, intra_op_num_threads)+ selection = _select_and_save(requested, source, intra_op_num_threads, inter_op_num_threads)else:try:- selection = _select_and_save(requested, source, intra_op_num_threads)+ selection = _select_and_save(requested, source, intra_op_num_threads, inter_op_num_threads)finally:selection_lock.release()except Exception as error:@@ -147,7 +158,7 @@ def confirm_gpu_session(selection: OnnxSelection) -> None:__all__ = ["METACOMMANDS",- "ONNX_GPU_TIME_RATIO_THRESHOLD",+ "ONNX_GPU_TIME_RATIO_THRESHOLDS","OnnxSelection","choose_gpu","confirm_gpu_session",diff --git a/mineru/model/runtime/onnx_rec.py b/mineru/model/runtime/onnx_rec.pynew file mode 100644index 00000000..b28fdc98--- /dev/null+++ b/mineru/model/runtime/onnx_rec.py@@ -0,0 +1,165 @@+"""Content-addressed ONNX CTC output reduction shared by CPU, DML and WebGPU."""++from __future__ import annotations++from dataclasses import dataclass+import hashlib+import json+import os+from pathlib import Path+import tempfile+from typing import TYPE_CHECKING++from filelock import FileLock++if TYPE_CHECKING:+ import numpy as np++REC_OUTPUT_FORMAT = "ctc-ids-scores-v1"+REC_IDS_OUTPUT = "mineru_rec_ids"+REC_SCORES_OUTPUT = "mineru_rec_scores"+++@dataclass(frozen=True)+class CompactRecModel:+ """Derived graph identity; contains no native session or device handles."""++ path: str+ num_classes: int+ source_sha256: str+ model_sha256: str+++def _atomic_write(path: Path, data: bytes) -> None:+ temporary: Path | None = None+ try:+ with tempfile.NamedTemporaryFile(dir=path.parent, prefix=f".{path.name}.", delete=False) as stream:+ temporary = Path(stream.name)+ stream.write(data)+ stream.flush()+ os.fsync(stream.fileno())+ os.replace(temporary, path)+ finally:+ if temporary is not None:+ temporary.unlink(missing_ok=True)+++def _reduce_graph(source: bytes) -> tuple[bytes, int]:+ import onnx+ from onnx import TensorProto, helper++ model = onnx.load_model_from_string(source)+ # The distributed rec graph embeds its weights. Reject external tensors rather than+ # reusing a cache key which ignores changes to an external weights file.+ try:+ onnx.external_data_helper.convert_model_from_external_data(model)+ except ValueError as error:+ raise ValueError("Compact OCR rec requires an ONNX model with embedded weights") from error+ if len(model.graph.output) != 1:+ raise ValueError("OCR rec requires one [batch, time, classes] probability output")+ original = model.graph.output[0]+ tensor = original.type.tensor_type+ if tensor.elem_type != TensorProto.FLOAT or len(tensor.shape.dim) != 3 or tensor.shape.dim[2].dim_value < 2:+ raise ValueError("OCR rec requires float32 probabilities with a fixed class dimension")+ classes = tensor.shape.dim[2].dim_value+ opset = next((item.version for item in model.opset_import if item.domain == ""), 0)+ if opset < 11:+ raise ValueError("Compact OCR rec requires ONNX opset >= 11")+ names = {name for node in model.graph.node for name in (*node.input, *node.output, node.name)}+ names.update(item.name for item in model.graph.initializer)+ names.update(item.name for item in model.graph.input)+ axes_name = "mineru_rec_class_axis"+ if names.intersection((REC_IDS_OUTPUT, REC_SCORES_OUTPUT, axes_name)):+ raise ValueError("OCR rec model contains reserved compact output names")+ model.graph.node.append(+ helper.make_node("ArgMax", [original.name], [REC_IDS_OUTPUT], name=REC_IDS_OUTPUT, axis=2, keepdims=0)+ )+ if opset >= 18:+ model.graph.initializer.append(helper.make_tensor(axes_name, TensorProto.INT64, [1], [2]))+ reduction = helper.make_node(+ "ReduceMax", [original.name, axes_name], [REC_SCORES_OUTPUT], name=REC_SCORES_OUTPUT, keepdims=0+ )+ else:+ reduction = helper.make_node(+ "ReduceMax", [original.name], [REC_SCORES_OUTPUT], name=REC_SCORES_OUTPUT, axes=[2], keepdims=0+ )+ model.graph.node.append(reduction)+ outputs = []+ for name, dtype in ((REC_IDS_OUTPUT, TensorProto.INT64), (REC_SCORES_OUTPUT, TensorProto.FLOAT)):+ output = helper.make_tensor_value_info(name, dtype, None)+ output.type.tensor_type.shape.dim.extend(tensor.shape.dim[:2])+ outputs.append(output)+ del model.graph.output[:]+ model.graph.output.extend(outputs)+ onnx.checker.check_model(model)+ return model.SerializeToString(), classes+++def prepare_rec_model(model_path: str, *, cache_dir: Path | None = None) -> CompactRecModel:+ """Keep the source immutable and atomically cache a checked, compact-output graph."""+ if cache_dir is None:+ from ...config import config++ cache_dir = Path(config.model.base_dir).expanduser() / ".derived" / "ocr-rec"+ source = Path(model_path).read_bytes()+ source_sha = hashlib.sha256(source).hexdigest()+ target = cache_dir / f"{REC_OUTPUT_FORMAT}-{source_sha}.onnx"+ manifest = target.with_suffix(".json")+ cache_dir.mkdir(parents=True, exist_ok=True)+ with FileLock(str(target.with_suffix(".lock")), timeout=30):+ try:+ info = json.loads(manifest.read_text(encoding="utf-8"))+ if (+ info["format"] == REC_OUTPUT_FORMAT+ and info["source_sha256"] == source_sha+ and isinstance(info["num_classes"], int)+ and info["num_classes"] >= 2+ and hashlib.sha256(target.read_bytes()).hexdigest() == info["model_sha256"]+ ):+ return CompactRecModel(str(target), info["num_classes"], source_sha, info["model_sha256"])+ except (OSError, ValueError, KeyError, TypeError):+ pass+ derived, classes = _reduce_graph(source)+ derived_sha = hashlib.sha256(derived).hexdigest()+ _atomic_write(target, derived)+ _atomic_write(+ manifest,+ json.dumps(+ {"format": REC_OUTPUT_FORMAT, "source_sha256": source_sha, "model_sha256": derived_sha, "num_classes": classes}+ ).encode("utf-8"),+ )+ return CompactRecModel(str(target), classes, source_sha, derived_sha)+++def validate_rec_outputs(outputs: list[np.ndarray], *, batch_size: int, num_classes: int) -> tuple[np.ndarray, np.ndarray]:+ """Validate the compact contract before CTC decoding or an automatic device decision."""+ import numpy as np++ if len(outputs) != 2:+ raise ValueError("OCR rec requires compact ID and score outputs")+ ids, scores = outputs+ if (+ ids.ndim != 2+ or ids.shape[0] != batch_size+ or ids.shape[1] == 0+ or scores.shape != ids.shape+ or ids.dtype != np.int64+ or scores.dtype != np.float32+ or np.any(ids < 0)+ or np.any(ids >= num_classes)+ or not np.isfinite(scores).all()+ or np.any(scores < 0)+ or np.any(scores > 1 + 1e-5)+ ):+ raise ValueError("Invalid compact OCR rec IDs or confidence scores")+ return ids, scores+++__all__ = [+ "CompactRecModel",+ "REC_IDS_OUTPUT",+ "REC_OUTPUT_FORMAT",+ "REC_SCORES_OUTPUT",+ "prepare_rec_model",+ "validate_rec_outputs",+]diff --git a/mineru/model/runtime/webgpu.py b/mineru/model/runtime/webgpu.pynew file mode 100644index 00000000..63e0c6ad--- /dev/null+++ b/mineru/model/runtime/webgpu.py@@ -0,0 +1,84 @@+"""Lazy native WebGPU registration and process-wide session serialization."""++from __future__ import annotations++from dataclasses import dataclass+import os+import threading+from typing import TYPE_CHECKING++if TYPE_CHECKING:+ import onnxruntime as ort++WEBGPU_PROVIDER = "WebGpuExecutionProvider"+WEBGPU_OPTIONS: dict[str, str] = {+ "preferredLayout": "NHWC",+ "enableGraphCapture": "0",+ "dawnBackendType": "Vulkan",+ "powerPreference": "high-performance",+}+++@dataclass(frozen=True)+class WebGPUDevice:+ """Serializable EP registration identity, NOT a Dawn physical-adapter selector."""++ vendor_id: int+ device_id: int+ metadata: tuple[tuple[str, str], ...] = ()+++_lock = threading.RLock()+_registered_pid: int | None = None+++def execution_lock() -> threading.RLock:+ """All WebGPU sessions share Dawn state; never run two sessions concurrently."""+ return _lock+++def _devices() -> list[ort.OrtEpDevice]:+ """Register once in this process; keep native EP handles local to this call."""+ import onnxruntime as ort+ import onnxruntime_ep_webgpu as plugin++ global _registered_pid+ if _registered_pid != os.getpid():+ ort.register_execution_provider_library(WEBGPU_PROVIDER, plugin.get_library_path())+ _registered_pid = os.getpid()+ return [+ ep+ for ep in ort.get_ep_devices()+ if ep.ep_name == WEBGPU_PROVIDER and ep.device.type == ort.OrtHardwareDeviceType.GPU and ep.device.vendor_id != 0+ ]+++def _describe(ep: ort.OrtEpDevice) -> WebGPUDevice:+ device = ep.device+ return WebGPUDevice(device.vendor_id, device.device_id, tuple(sorted(device.metadata.items())))+++def discover_webgpu_devices() -> tuple[WebGPUDevice, ...]:+ """Return hashable hardware identities without starting a model or writing config."""+ with execution_lock():+ return tuple(_describe(ep) for ep in _devices())+++def attach_webgpu_provider(options: ort.SessionOptions, device: WebGPUDevice) -> None:+ """Resolve a fresh native handle and attach the production provider parameters."""+ with execution_lock():+ ep = next((ep for ep in _devices() if _describe(ep) == device), None)+ if ep is None:+ raise RuntimeError("WebGPU hardware registration device is no longer available")+ # The plugin selects the physical adapter via powerPreference, not this EP handle.+ options.add_provider_for_devices([ep], WEBGPU_OPTIONS)+++__all__ = [+ "WEBGPU_OPTIONS",+ "WEBGPU_PROVIDER",+ "WebGPUDevice",+ "attach_webgpu_provider",+ "discover_webgpu_devices",+ "execution_lock",+]diff --git a/pyproject.toml b/pyproject.tomlindex b4bdce5e..0dd2aba7 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -9,7 +9,7 @@ license = "LicenseRef-MinerU-Open-Source-License"license-files = ["LICENSE.md"]description = "A practical document parsing tool for converting PDF, OFD, EPUB, HTML, images, CSV, RTF, OOXML, and OpenDocument files into Markdown and JSON"readme = "README.md"-requires-python = ">=3.10,<3.15"+requires-python = ">=3.11,<3.15"keywords = ["mineru","magic-pdf",@@ -32,7 +32,6 @@ keywords = ["multimodal",]classifiers = [- "Programming Language :: Python :: 3.10","Programming Language :: Python :: 3.11","Programming Language :: Python :: 3.12","Programming Language :: Python :: 3.13",@@ -72,8 +71,10 @@ dependencies = ["typer>=0.9.0","rich>=13.0.0","jieba>=0.42.1",+ "onnx>=1.19,<2","onnxruntime-directml>=1.23,<2 ; sys_platform == 'win32' and (platform_machine == 'AMD64' or platform_machine == 'amd64' or platform_machine == 'x86_64')",- "onnxruntime>=1.20.1 ; sys_platform != 'win32' or (platform_machine != 'AMD64' and platform_machine != 'amd64' and platform_machine != 'x86_64')",+ "onnxruntime>=1.24.4,<2 ; sys_platform != 'win32' or (platform_machine != 'AMD64' and platform_machine != 'amd64' and platform_machine != 'x86_64')",+ "onnxruntime-ep-webgpu>=0.4.0,<1 ; sys_platform == 'linux' and platform_machine == 'x86_64'","ftfy>=6.3.1,<7","shapely>=2.0.7,<3","pyclipper>=1.3.0,<2",diff --git a/scripts/benchmark_onnx_selection.py b/scripts/benchmark_onnx_selection.pyindex fbe409d4..6d9830ca 100644--- a/scripts/benchmark_onnx_selection.py+++ b/scripts/benchmark_onnx_selection.py@@ -14,6 +14,7 @@ from typing import Anyfrom mineru.model.runtime.onnx_calibration import run_calibrationfrom mineru.model.runtime.onnx_device import choose_gpufrom mineru.model.runtime.windows_gpu import discover_gpus, preferred_gpu+from mineru.model.runtime.webgpu import WEBGPU_PROVIDER, discover_webgpu_devicesdef write_json(path: Path, data: dict[str, Any]) -> None:@@ -57,37 +58,55 @@ def environment() -> dict[str, Any]:def derive_threshold(reports: list[dict[str, Any]]) -> dict[str, Any]:"""Choose the midpoint of the separating interval; reject mismatched or failed protocols."""by_label = {report["label"]: report for report in reports}- if len(reports) != 3 or set(by_label) != {"ultra5", "i7", "max395"}:+ backends = {report.get("backend", "dml") for report in reports}+ if len(backends) != 1 or not backends <= {"dml", "webgpu"}:+ raise ValueError("Do not mix DML and WebGPU calibration reports")+ backend = next(iter(backends))+ if backend == "dml" and (len(reports) != 3 or set(by_label) != {"ultra5", "i7", "max395"}):raise ValueError("Exactly one ultra5, i7 and max395 report is required")+ if backend == "webgpu" and (len(reports) < 3 or len(by_label) != len(reports)):+ raise ValueError("WebGPU derivation needs at least three distinctly labeled hosts")signatures = set()ratios: dict[str, list[float]] = {}+ expected = {}for label, report in by_label.items():+ expected[label] = report.get("expected") or (("cpu" if label == "i7" else "gpu") if backend == "dml" else None)+ if expected[label] not in {"cpu", "gpu"}:+ raise ValueError("WebGPU reports must declare --expect cpu or gpu from independent performance evidence")if report.get("errors") or len(report["runs"]) != 5 or report.get("threshold") is not None:raise ValueError(f"{label} needs five successful calibration processes")ratios[label] = []for run in report["runs"]:- if not run.get("outputs_match") or run["gpu"]["providers"][0] != "DmlExecutionProvider":- raise ValueError("Missing DirectML execution evidence")+ provider = WEBGPU_PROVIDER if backend == "webgpu" else "DmlExecutionProvider"+ if run.get("backend", "dml") != backend or not run.get("outputs_match") or run["gpu"]["providers"][0] != provider:+ raise ValueError("Missing matching backend execution evidence")signatures.add((run["input"]["manifest_sha256"],run["input"]["input_sha256"],run["model_sha256"],+ run.get("output_format", "full-probabilities"),run["ort_version"],run["intra_threads"],run["inter_threads"],+ backend,+ run.get("ep_version", run["ort_version"]),+ json.dumps(run.get("webgpu_options"), sort_keys=True),))ratio = run["gpu_elapsed"] / run["cpu_elapsed"]choose_gpu(run["cpu_elapsed"], run["gpu_elapsed"], 0.5) # Validate finite positive timings only.ratios[label].append(ratio)if len(signatures) != 1:- raise ValueError("Samples, weights, ORT or thread settings differ across calibration runs")- lower = max(*ratios["ultra5"], *ratios["max395"])- upper = min(*ratios["i7"], 1.0)+ raise ValueError("Samples, weights, ORT, EP version/options or thread settings differ across calibration runs")+ if set(expected.values()) != {"cpu", "gpu"}:+ raise ValueError("Threshold derivation requires both CPU-winning and GPU-winning hosts")+ lower = max(value for label, values in ratios.items() if expected[label] == "gpu" for value in values)+ upper = min(1.0, min(value for label, values in ratios.items() if expected[label] == "cpu" for value in values))if lower >= upper:raise ValueError(f"No separating interval: L={lower}, U={upper}; revise samples and rerun")return {+ "backend": backend,"threshold": (lower + upper) / 2,"lower": lower,"upper": upper,@@ -98,14 +117,22 @@ def derive_threshold(reports: list[dict[str, Any]]) -> dict[str, Any]:def run(args: argparse.Namespace) -> None:- adapters = discover_gpus()- if args.luid:- adapter = next((gpu for gpu in adapters if gpu.luid == args.luid), None)+ backend = "webgpu" if sys.platform == "linux" else "dml"+ if backend == "webgpu":+ if args.luid:+ raise ValueError("--luid is only supported by DirectML; WebGPU uses the default high-performance adapter")+ adapters = discover_webgpu_devices()+ adapter = adapters[0] if adapters else None+ if args.threshold is not None and args.expect is None:+ raise ValueError("WebGPU independent validation requires --expect cpu or gpu")else:- adapter = preferred_gpu(adapters)+ if args.label not in {"ultra5", "i7", "max395"}:+ raise ValueError("DirectML calibration labels are ultra5, i7 and max395")+ adapters = discover_gpus()+ adapter = next((gpu for gpu in adapters if gpu.luid == args.luid), None) if args.luid else preferred_gpu(adapters)if adapter is None:raise ValueError("Requested hardware GPU was not found")- if adapter.uma is not True:+ if backend == "dml" and adapter.uma is not True:raise ValueError("Threshold calibration must explicitly target a confirmed integrated GPU")if args.model is None:from mineru.model.registry import MINERU_4_MODELS_ONNX@@ -117,6 +144,8 @@ def run(args: argparse.Namespace) -> None:args.output.mkdir(parents=True, exist_ok=False)report: dict[str, Any] = {"label": args.label,+ "backend": backend,+ "expected": args.expect or (("cpu" if args.label == "i7" else "gpu") if backend == "dml" else None),"environment": environment(),"adapter": asdict(adapter),"threshold": args.threshold,@@ -148,7 +177,7 @@ def run(args: argparse.Namespace) -> None:print(f"{args.label} {index + 1}/{count}: {error}", flush=True)write_json(target, report)if args.threshold is not None:- expected = "cpu" if args.label == "i7" else "gpu"+ expected = report["expected"]report["validation_passed"] = not report["errors"] and all(row["selected"] == expected for row in report["runs"])write_json(target, report)if report["errors"] or report.get("validation_passed") is False:@@ -160,17 +189,19 @@ def main() -> None:commands = parser.add_subparsers(dest="command", required=True)commands.add_parser("devices")measure = commands.add_parser("run")- measure.add_argument("--label", choices=("ultra5", "i7", "max395"), required=True)+ measure.add_argument("--label", required=True, help="Host label; DML uses ultra5/i7/max395, WebGPU accepts any label")+ measure.add_argument("--expect", choices=("cpu", "gpu"), help="Expected device for independent validation")measure.add_argument("--luid", help="Benchmark-only adapter identity; use devices to find the iGPU")measure.add_argument("--model", type=Path)measure.add_argument("--output", type=Path, required=True)measure.add_argument("--threshold", type=float, help="Run three independent validation processes")derive = commands.add_parser("derive")- derive.add_argument("reports", type=Path, nargs=3)+ derive.add_argument("reports", type=Path, nargs="+")derive.add_argument("--output", type=Path, required=True)args = parser.parse_args()if args.command == "devices":- print(json.dumps([asdict(gpu) for gpu in discover_gpus()], ensure_ascii=False, indent=2))+ devices = discover_webgpu_devices() if sys.platform == "linux" else discover_gpus()+ print(json.dumps([asdict(gpu) for gpu in devices], ensure_ascii=False, indent=2))elif args.command == "run":run(args)else:diff --git a/tests/unittest/test_directml_runtime.py b/tests/unittest/test_directml_runtime.pyindex 294a1f27..a755afc0 100644--- a/tests/unittest/test_directml_runtime.py+++ b/tests/unittest/test_directml_runtime.py@@ -44,6 +44,7 @@ def test_vendor_presets_and_actual_index(monkeypatch: pytest.MonkeyPatch, vendordef test_cpu_models_do_not_select_gpu(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setattr(onnx, "sys", SimpleNamespace(platform="win32"))select = Mock(side_effect=AssertionError("selection"))factory = Mock(return_value=SimpleNamespace(get_providers=lambda: ["CPUExecutionProvider"]))monkeypatch.setattr(onnx, "resolve_onnx_device", select)@@ -74,7 +75,7 @@ def test_production_fallback_does_not_save_gpu(monkeypatch: pytest.MonkeyPatch,opts.profile_file_prefix = "unet-profile"assert (onnx.ort_session(- "test.onnx", model_kind=kind, selection=OnnxSelection("gpu", adapter, "discrete_gpu"), sess_options=opts+ "test.onnx", model_kind=kind, selection=OnnxSelection("gpu", adapter, "discrete_gpu", "dml"), sess_options=opts)is cpu)@@ -137,12 +138,15 @@ def test_mfr_only_actual_batch_eight_uses_gpu(monkeypatch: pytest.MonkeyPatch, cmodel.session = SimpleNamespace(run=lambda names, feeds: run(gpu_calls, feeds))factory = Mock(return_value=SimpleNamespace(run=lambda names, feeds: run(cpu_calls, feeds)))- monkeypatch.setattr(mfr, "ort_session", factory)+ monkeypatch.setattr(mfr, "create_ort_session", factory)monkeypatch.setattr(model, "_preprocess", lambda crop: np.full((1, 1, 2, 2), crop[0, 0, 0]) if crop.size else None)monkeypatch.setattr(model, "_decode_tokens", lambda tokens: str(tokens[0]))crops = [np.full((value, value, 3), value, dtype=np.uint8) for value in range(count, 0, -1)]+ crops.insert(1, np.empty((0, 0, 3), dtype=np.uint8))+ expected = [str(value) for value in range(count, 0, -1)]+ expected.insert(1, "")for _ in range(2):- assert model._infer_batch(crops, batch_size=batch) == [str(value) for value in range(count, 0, -1)]+ assert model._infer_batch(crops, batch_size=batch) == expectedassert all(size == 8 for size in gpu_calls)assert all(0 < size < 8 for size in cpu_calls)assert sum(gpu_calls + cpu_calls) == count * 2diff --git a/tests/unittest/test_model_backend_selection.py b/tests/unittest/test_model_backend_selection.pyindex 0e32bcaa..965a7890 100644--- a/tests/unittest/test_model_backend_selection.py+++ b/tests/unittest/test_model_backend_selection.py@@ -232,17 +232,18 @@ def _resolved_direct_dependencies(platform: str, machine: str, extra: str, pytho@pytest.mark.parametrize("platform,machine",- [("darwin", "arm64"), ("darwin", "x86_64"), ("linux", "x86_64"), ("win32", "AMD64"),+ [("darwin", "arm64"), ("darwin", "x86_64"), ("linux", "x86_64"), ("linux", "aarch64"), ("win32", "AMD64"),("win32", "amd64"), ("win32", "x86_64"), ("win32", "ARM64")],)@pytest.mark.parametrize("extra", ["", "torch", "full", "all"])[email protected]("python", ["3.10", "3.11", "3.12", "3.13", "3.14"])[email protected]("python", ["3.11", "3.12", "3.13", "3.14"])def test_package_markers(platform: str, machine: str, extra: str, python: str) -> None:"""ARM 基础包引入完整 Torch 依赖,其他平台基础包保持 ONNX 与 llama。"""names = _resolved_direct_dependencies(platform, machine, extra, python)runtime = "onnxruntime-directml" if platform == "win32" and machine in {"AMD64", "amd64", "x86_64"} else "onnxruntime"assert {"docvortex", "gradio", runtime, "mineru-llama-cpp"} <= namesassert not {"onnxruntime", "onnxruntime-directml"} <= names+ assert ("onnxruntime-ep-webgpu" in names) == (platform == "linux" and machine == "x86_64")# lxml 由 DocVortex 声明,不在 MinerU 运行时 extras 中重复维护。assert "lxml" not in namesexpected_torch = (platform, machine) == ("darwin", "arm64") or extra in {"torch", "full", "all"}diff --git a/tests/unittest/test_onnx_calibration.py b/tests/unittest/test_onnx_calibration.pyindex c6ad6aa6..adc50d00 100644--- a/tests/unittest/test_onnx_calibration.py+++ b/tests/unittest/test_onnx_calibration.py@@ -14,6 +14,8 @@ import pytestfrom mineru.model.runtime import onnx_calibration as calibrationfrom mineru.model.runtime.windows_gpu import GpuAdapter+from mineru.model.runtime.webgpu import WEBGPU_PROVIDER, WebGPUDevice+from mineru.model.runtime.onnx_rec import CompactRecModel, REC_OUTPUT_FORMATfrom scripts.benchmark_onnx_selection import derive_threshold@@ -21,6 +23,12 @@ def gpu() -> GpuAdapter:return GpuAdapter(2, "id", "test", 0x8086, 1, True, 0)[email protected](autouse=True)+def fake_rec_preparation(monkeypatch: pytest.MonkeyPatch) -> None:+ """Protocol tests mock sessions; real graph conversion is covered separately."""+ monkeypatch.setattr(calibration, "prepare_rec_model", lambda path: CompactRecModel("compact.onnx", 4, "raw", "compact"))++def test_installed_samples_are_fixed_and_complete() -> None:first, metadata = calibration.prepare_input()second, repeated = calibration.prepare_input()@@ -62,7 +70,8 @@ def test_measurement_reuses_input_and_excludes_warmup(tmp_path: Path, monkeypatcdef run(names: object, feed: dict[str, np.ndarray]) -> list[np.ndarray]:feeds.append(feed["image"])- return [np.ones((6, 3, 4), dtype=np.float32)]+ assert names == ["mineru_rec_ids", "mineru_rec_scores"]+ return [np.ones((6, 3), dtype=np.int64), np.ones((6, 3), dtype=np.float32)]return SimpleNamespace(get_inputs=lambda: [SimpleNamespace(name="image")], run=run, get_providers=lambda: [provider])@@ -74,6 +83,8 @@ def test_measurement_reuses_input_and_excludes_warmup(tmp_path: Path, monkeypatcassert result["gpu_elapsed"] == 3assert result["cpu"]["first_s"] == result["gpu"]["first_s"] == 100assert result["cpu"]["init_s"] == 4 and result["gpu"]["init_s"] == 8+ assert result["output_format"] == REC_OUTPUT_FORMAT+ assert result["model_sha256"] == "compact" and result["source_model_sha256"] == "raw"def test_worker_timeout_is_bounded(monkeypatch: pytest.MonkeyPatch) -> None:@@ -90,7 +101,7 @@ def test_worker_silent_fallback_is_not_a_measurement(monkeypatch: pytest.MonkeyPmonkeypatch.setattr(calibration.subprocess, "run", Mock(return_value=SimpleNamespace(returncode=0, stdout=json.dumps(output))))- with pytest.raises(ValueError, match="DirectML"):+ with pytest.raises(ValueError, match="DmlExecutionProvider"):calibration.run_calibration("rec.onnx", gpu())@@ -144,3 +155,61 @@ def test_invalid_calibration_set_is_rejected(failure: str) -> None:data[0]["runs"][0]["gpu_elapsed"] = 1.5with pytest.raises(ValueError):derive_threshold(data)++[email protected]("failure", [None, "fallback", "wrong_backend", "wrong_output_format", "nan", "mismatch"])+def test_webgpu_worker_protocol(monkeypatch: pytest.MonkeyPatch, failure: str | None) -> None:+ output = {+ "backend": "webgpu",+ "output_format": REC_OUTPUT_FORMAT,+ "cpu_elapsed": 1,+ "gpu_elapsed": 0.4,+ "outputs_match": True,+ "gpu": {"providers": [WEBGPU_PROVIDER, "CPUExecutionProvider"]},+ "cpu": {"providers": ["CPUExecutionProvider"]},+ }+ if failure == "fallback":+ output["gpu"]["providers"] = ["CPUExecutionProvider"]+ elif failure == "wrong_backend":+ output["backend"] = "dml"+ elif failure == "wrong_output_format":+ output["output_format"] = "full-probabilities"+ elif failure == "nan":+ output["gpu_elapsed"] = float("nan")+ elif failure == "mismatch":+ output["outputs_match"] = False+ runner = Mock(return_value=SimpleNamespace(returncode=0, stdout=json.dumps(output)))+ monkeypatch.setattr(calibration.subprocess, "run", runner)+ descriptor = WebGPUDevice(1, 2, (("pci_bus_id", "test"),))+ if failure:+ with pytest.raises(ValueError):+ calibration.run_calibration("rec.onnx", descriptor)+ else:+ assert calibration.run_calibration("rec.onnx", descriptor)["backend"] == "webgpu"+ request = json.loads(runner.call_args.kwargs["input"])+ assert request["backend"] == "webgpu"+ assert request["adapter"]["metadata"] == [["pci_bus_id", "test"]]+++def test_threshold_derivation_rejects_mixed_backends_and_ep_options() -> None:+ data = reports()+ data[0]["backend"] = "webgpu"+ with pytest.raises(ValueError, match="mix"):+ derive_threshold(data)+ for report in data:+ report["backend"] = "webgpu"+ report["expected"] = "cpu" if report["label"] == "i7" else "gpu"+ for run in report["runs"]:+ run.update(backend="webgpu", ep_version="0.4.0", webgpu_options={"preferredLayout": "NHWC"})+ run["gpu"]["providers"] = [WEBGPU_PROVIDER]+ assert derive_threshold(data)["backend"] == "webgpu"+ data[0]["runs"][0]["webgpu_options"] = {"preferredLayout": "NCHW"}+ with pytest.raises(ValueError, match="EP version/options"):+ derive_threshold(data)+++def test_threshold_derivation_rejects_mixed_output_contracts() -> None:+ data = reports()+ data[0]["runs"][0]["output_format"] = REC_OUTPUT_FORMAT+ with pytest.raises(ValueError, match="differ"):+ derive_threshold(data)diff --git a/tests/unittest/test_onnx_device_selection.py b/tests/unittest/test_onnx_device_selection.pyindex c72d3fc4..6e10a722 100644--- a/tests/unittest/test_onnx_device_selection.py+++ b/tests/unittest/test_onnx_device_selection.py@@ -14,6 +14,7 @@ from pydantic import ValidationErrorfrom mineru.config import Configfrom mineru.model.runtime import onnx_device as policyfrom mineru.model.runtime.windows_gpu import GpuAdapter, preferred_gpu+from mineru.model.runtime.webgpu import WebGPUDevicesettings = importlib.import_module("mineru.config")@@ -31,7 +32,7 @@ def config_file(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:monkeypatch.setattr(settings.config.model, "onnx_device", "auto")monkeypatch.setattr(policy, "sys", SimpleNamespace(platform="win32"))monkeypatch.setattr(policy, "_selections", {})- monkeypatch.setattr(policy, "ONNX_GPU_TIME_RATIO_THRESHOLD", 0.75)+ monkeypatch.setattr(policy, "ONNX_GPU_TIME_RATIO_THRESHOLDS", {"dml": 0.75, "webgpu": 0.8})monkeypatch.setattr(policy, "discover_gpus", lambda **_: (adapter(),))import onnxruntimefrom mineru.model import registry@@ -176,9 +177,9 @@ def test_discovery_and_unavailable_states(config_file: Path, monkeypatch: pytestelif mode == "discovery_error":monkeypatch.setattr(policy, "discover_gpus", Mock(side_effect=OSError("DXGI error")))elif mode == "pending":- monkeypatch.setattr(policy, "ONNX_GPU_TIME_RATIO_THRESHOLD", None)+ monkeypatch.setitem(policy.ONNX_GPU_TIME_RATIO_THRESHOLDS, "dml", None)else:- monkeypatch.setattr(policy, "sys", SimpleNamespace(platform="linux"))+ monkeypatch.setattr(policy, "sys", SimpleNamespace(platform="darwin"))assert policy.resolve_onnx_device().device == "cpu"assert yaml.safe_load(config_file.read_text())["model"]["onnx_device"] == ("cpu" if mode == "no_gpu" else "auto")@@ -273,3 +274,87 @@ def test_unet_first_model_shares_selection_and_persistence(if expected == "gpu":assert factory.call_args_list[0].kwargs["providers"][0][1] == {"device_id": 7, "disable_metacommands": "true"}assert yaml.safe_load(config_file.read_text())["model"] == {"onnx_device": expected, "source": "local"}+++def linux_config(config_file: Path, monkeypatch: pytest.MonkeyPatch) -> Path:+ monkeypatch.setattr(policy, "sys", SimpleNamespace(platform="linux"))+ monkeypatch.setattr(policy, "discover_webgpu_devices", Mock(return_value=(WebGPUDevice(0x1002, 1),)))+ return config_file++[email protected](("gpu_time", "expected"), [(1.5, "gpu"), (1.6, "gpu"), (1.601, "cpu")])[email protected]("hardware", ["integrated", "discrete"])+def test_linux_calibrates_both_gpu_types_and_uses_own_threshold(+ linux_config: Path, monkeypatch: pytest.MonkeyPatch, gpu_time: float, expected: str, hardware: str+) -> None:+ from mineru.model.runtime import onnx_calibration++ descriptor = WebGPUDevice(0x1002, 1, (("type", hardware),))+ monkeypatch.setattr(policy, "discover_webgpu_devices", Mock(return_value=(descriptor,)))+ measure = Mock(return_value={"cpu_elapsed": 2.0, "gpu_elapsed": gpu_time})+ monkeypatch.setattr(onnx_calibration, "run_calibration", measure)+ selection = policy.resolve_onnx_device()+ assert selection.device == expected and selection.backend == "webgpu"+ assert policy.resolve_onnx_device() is selection+ measure.assert_called_once_with("rec.onnx", descriptor, intra_op_num_threads=0, inter_op_num_threads=0)+ assert yaml.safe_load(linux_config.read_text())["model"] == {"onnx_device": expected, "source": "local"}++[email protected]("requested", ["cpu", "gpu"])+def test_linux_explicit_modes_do_not_calibrate(linux_config: Path, monkeypatch: pytest.MonkeyPatch, requested: str) -> None:+ from mineru.model.runtime import onnx_calibration++ monkeypatch.setattr(settings.config.model, "onnx_device", requested)+ assert policy.resolve_onnx_device().device == requested+ onnx_calibration.run_calibration.assert_not_called()+ assert policy.discover_webgpu_devices.call_count == (0 if requested == "cpu" else 1)++[email protected]("error", [ImportError("plugin"), OSError("Vulkan"), TimeoutError("worker"), ValueError("outputs")])+def test_linux_failures_preserve_auto_and_do_not_retry(+ linux_config: Path, monkeypatch: pytest.MonkeyPatch, error: Exception+) -> None:+ from mineru.model.runtime import onnx_calibration++ failing = Mock(side_effect=error)+ if isinstance(error, (ImportError, OSError)) and not isinstance(error, TimeoutError):+ monkeypatch.setattr(policy, "discover_webgpu_devices", failing)+ else:+ monkeypatch.setattr(onnx_calibration, "run_calibration", failing)+ for _ in range(2):+ assert policy.resolve_onnx_device().device == "cpu"+ assert failing.call_count == 1+ assert yaml.safe_load(linux_config.read_text())["model"]["onnx_device"] == "auto"+++def test_linux_environment_override_never_persists(linux_config: Path, monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setattr(settings, "get_config_source", lambda key: "env")+ assert policy.resolve_onnx_device().device == "gpu"+ assert yaml.safe_load(linux_config.read_text())["model"]["onnx_device"] == "auto"++[email protected]("state", ["no_devices", "pending_threshold"])+def test_linux_unavailable_state_preserves_auto(linux_config: Path, monkeypatch: pytest.MonkeyPatch, state: str) -> None:+ from mineru.model.runtime import onnx_calibration++ if state == "no_devices":+ monkeypatch.setattr(policy, "discover_webgpu_devices", Mock(return_value=()))+ else:+ monkeypatch.setitem(policy.ONNX_GPU_TIME_RATIO_THRESHOLDS, "webgpu", None)+ assert policy.resolve_onnx_device().device == "cpu"+ onnx_calibration.run_calibration.assert_not_called()+ assert yaml.safe_load(linux_config.read_text())["model"]["onnx_device"] == "auto"+++def test_linux_waiting_process_rechecks_saved_choice(linux_config: Path, monkeypatch: pytest.MonkeyPatch) -> None:+ class OtherProcessSaved:+ def acquire(self) -> None:+ linux_config.write_text("model:\n onnx_device: cpu\n")++ def release(self) -> None:+ pass++ monkeypatch.setattr(policy, "FileLock", lambda *args, **kwargs: OtherProcessSaved())+ assert policy.resolve_onnx_device().device == "cpu"+ policy.discover_webgpu_devices.assert_not_called()diff --git a/tests/unittest/test_onnx_model_stack.py b/tests/unittest/test_onnx_model_stack.pyindex 9100635c..50b4f470 100644--- a/tests/unittest/test_onnx_model_stack.py+++ b/tests/unittest/test_onnx_model_stack.py@@ -100,6 +100,7 @@ def test_ort_providers_remain_cpu_on_accelerated_hosts(monkeypatch: pytest.Monkedef test_ocr_txt_dictionary_ctc_space_and_shape_validation(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:"""字符顺序、CTC blank 和尾部空格不得重复或错位。"""from mineru.model.ocr import pp_ocr_v6_onnx as ocr+ from mineru.model.runtime.onnx_rec import CompactRecModeldictionary = tmp_path / "dict.txt"dictionary.write_text("中\nA\n", encoding="utf-8")@@ -108,11 +109,10 @@ def test_ocr_txt_dictionary_ctc_space_and_shape_validation(monkeypatch: pytest.Mget_outputs=lambda: [SimpleNamespace(shape=["batch", "width", 4])],)monkeypatch.setattr(ocr, "ort_session", lambda *args, **kwargs: session)+ monkeypatch.setattr(ocr, "prepare_rec_model", lambda path: CompactRecModel("compact.onnx", 4, "source", "compact"))recognizer = ocr.TextRecognizerONNX("rec.onnx", str(dictionary))logits = np.eye(4, dtype=np.float32)[[1, 1, 0, 2, 3, 3, 0, 1]]- assert recognizer._decode(logits) == ("中A 中", 1.0)- with pytest.raises(ValueError, match="CTC output shape"):- recognizer._decode(np.zeros((3, 5)))+ assert recognizer._decode(logits.argmax(-1), logits.max(-1)) == ("中A 中", 1.0)dictionary.write_text("中\nA\nB\n", encoding="utf-8")with pytest.raises(ValueError, match="dictionary requires"):ocr.TextRecognizerONNX("rec.onnx", str(dictionary))diff --git a/tests/unittest/test_onnx_rec_compact.py b/tests/unittest/test_onnx_rec_compact.pynew file mode 100644index 00000000..9433e271--- /dev/null+++ b/tests/unittest/test_onnx_rec_compact.py@@ -0,0 +1,156 @@+"""Real graph conversion, cache publication and shared ONNX rec output semantics."""++from __future__ import annotations++from concurrent.futures import ThreadPoolExecutor+import hashlib+import json+from pathlib import Path+from types import SimpleNamespace++import numpy as np+import onnx+from onnx import TensorProto, helper+import onnxruntime as ort+import pytest++from mineru.model.runtime.onnx_rec import REC_IDS_OUTPUT, REC_SCORES_OUTPUT, prepare_rec_model, validate_rec_outputs+++def make_model(path: Path, opset: int = 11, classes: int = 5) -> Path:+ graph = helper.make_graph(+ [helper.make_node("Softmax", ["x"], ["probs"], axis=2)],+ "rec",+ [helper.make_tensor_value_info("x", TensorProto.FLOAT, ["B", "T", classes])],+ [helper.make_tensor_value_info("probs", TensorProto.FLOAT, ["B", "T", classes])],+ )+ model = helper.make_model(graph, opset_imports=[helper.make_opsetid("", opset)], ir_version=8)+ onnx.save(model, path)+ return path+++def cpu_session(path: str) -> ort.InferenceSession:+ options = ort.SessionOptions()+ options.intra_op_num_threads = 4+ options.inter_op_num_threads = 1+ return ort.InferenceSession(path, sess_options=options, providers=["CPUExecutionProvider"])++[email protected]("opset", [11, 13, 18, 20])+def test_compact_graph_matches_dense_probabilities(tmp_path: Path, opset: int) -> None:+ source = make_model(tmp_path / "original.onnx", opset)+ original_bytes = source.read_bytes()+ compact = prepare_rec_model(str(source), cache_dir=tmp_path / "cache")+ dense, reduced = cpu_session(str(source)), cpu_session(compact.path)+ assert [out.name for out in reduced.get_outputs()] == [REC_IDS_OUTPUT, REC_SCORES_OUTPUT]+ for batch, steps in [(1, 1), (6, 40), (2, 127), (7, 5)]:+ values = np.random.default_rng(42).normal(size=(batch, steps, 5)).astype(np.float32)+ values[0, 0] = 0 # Ties must select the first class, matching NumPy CTC decoding.+ expected = dense.run(None, {"x": values})[0]+ ids, scores = validate_rec_outputs(reduced.run(None, {"x": values}), batch_size=batch, num_classes=5)+ np.testing.assert_array_equal(ids, expected.argmax(-1))+ np.testing.assert_array_equal(scores, expected.max(-1))+ assert source.read_bytes() == original_bytes+ assert compact.source_sha256 == hashlib.sha256(original_bytes).hexdigest()+++def test_cache_reuse_invalidation_and_corruption_repair(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:+ from mineru.model.runtime import onnx_rec++ source = make_model(tmp_path / "original.onnx")+ cache = tmp_path / "cache"+ first = prepare_rec_model(str(source), cache_dir=cache)+ with monkeypatch.context() as context:+ context.setattr(onnx_rec, "_reduce_graph", lambda _: pytest.fail("warm cache must not convert again"))+ assert prepare_rec_model(str(source), cache_dir=cache) == first+ target = Path(first.path)+ for damaged in [target, target.with_suffix(".json")]:+ damaged.write_bytes(b"broken")+ assert prepare_rec_model(str(source), cache_dir=cache) == first+ make_model(source, classes=6)+ second = prepare_rec_model(str(source), cache_dir=cache)+ assert second.path != first.path and second.num_classes == 6+ assert target.is_file()+++def test_concurrent_preparation_publishes_one_complete_model(tmp_path: Path) -> None:+ source = make_model(tmp_path / "original.onnx")+ cache = tmp_path / "cache"+ with ThreadPoolExecutor(max_workers=4) as pool:+ values = list(pool.map(lambda _: prepare_rec_model(str(source), cache_dir=cache), range(8)))+ assert len(set(values)) == 1+ onnx.checker.check_model(values[0].path)+ assert len(list(cache.glob("*.onnx"))) == len(list(cache.glob("*.json"))) == 1+ assert not list(cache.glob(".*"))+++def test_external_weights_are_rejected_without_publishing_cache(tmp_path: Path) -> None:+ source = make_model(tmp_path / "original.onnx")+ model = onnx.load(source)+ weight = helper.make_tensor("external", TensorProto.FLOAT, [1], [1.0])+ weight.data_location = TensorProto.EXTERNAL+ weight.external_data.add(key="location", value="weights.bin")+ model.graph.initializer.append(weight)+ source.write_bytes(model.SerializeToString())+ with pytest.raises(ValueError, match="embedded weights"):+ prepare_rec_model(str(source), cache_dir=tmp_path / "cache")+ assert not list((tmp_path / "cache").glob("*.onnx"))++[email protected]("bad", ["nan", "negative", "too_large", "id_bounds", "id_dtype", "shape", "empty", "count"])+def test_invalid_compact_outputs_are_rejected(bad: str) -> None:+ ids = np.zeros((2, 3), dtype=np.int64)+ scores = np.ones((2, 3), dtype=np.float32)+ if bad == "nan":+ scores[0, 0] = np.nan+ elif bad == "negative":+ scores[0, 0] = -0.1+ elif bad == "too_large":+ scores[0, 0] = 2+ elif bad == "id_bounds":+ ids[0, 0] = 5+ elif bad == "id_dtype":+ ids = ids.astype(np.float32)+ elif bad == "shape":+ scores = scores[:, :1]+ elif bad == "empty":+ ids, scores = ids[:, :0], scores[:, :0]+ outputs = [ids] if bad == "count" else [ids, scores]+ with pytest.raises(ValueError):+ validate_rec_outputs(outputs, batch_size=2, num_classes=5)++[email protected]("provider", ["CPUExecutionProvider", "DmlExecutionProvider", "WebGpuExecutionProvider"])+def test_all_backends_use_compact_graph_and_restore_input_order(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, provider: str+) -> None:+ from mineru.model.ocr import pp_ocr_v6_onnx as ocr++ source = make_model(tmp_path / "original.onnx")+ compact = prepare_rec_model(str(source), cache_dir=tmp_path / "cache")+ monkeypatch.setattr(ocr, "prepare_rec_model", lambda path: compact)+ dictionary = tmp_path / "dict.txt"+ dictionary.write_text("A\nB\nC\n", encoding="utf-8")+ batches = []++ def run(names: list[str], feeds: dict[str, np.ndarray]) -> list[np.ndarray]:+ assert names == [REC_IDS_OUTPUT, REC_SCORES_OUTPUT]+ labels = feeds["x"][:, 0, 0, 0].astype(np.int64)+ batches.append(len(labels))+ ids = np.stack([labels, labels, np.zeros_like(labels), labels], axis=1)+ return [ids, np.full(ids.shape, 0.75, dtype=np.float32)]++ def session(path: str, *args: object, **kwargs: object) -> SimpleNamespace:+ assert path == compact.path+ return SimpleNamespace(get_inputs=lambda: [SimpleNamespace(name="x")], run=run, get_providers=lambda: [provider])++ monkeypatch.setattr(ocr, "ort_session", session)+ rec = ocr.TextRecognizerONNX(str(source), str(dictionary), rec_batch_num=2)+ monkeypatch.setattr(rec, "_resize_norm_img", lambda image, ratio: np.full((3, 48, 320), image[0, 0, 0], dtype=np.float32))+ inputs = [np.full((10, width, 3), label, dtype=np.uint8) for width, label in [(30, 1), (10, 2), (20, 3), (40, 0), (50, 1)]]+ result, _ = rec(inputs)+ assert result == [("AA", 0.75), ("BB", 0.75), ("CC", 0.75), ("", 1.0), ("AA", 0.75)]+ assert batches == [2, 2, 1]+ assert rec([]) == ([], 0.0)+ manifest = json.loads(Path(compact.path).with_suffix(".json").read_text())+ assert manifest["num_classes"] == len(rec.character)diff --git a/tests/unittest/test_table_onnx_runtime.py b/tests/unittest/test_table_onnx_runtime.pyindex 6e238a37..f403639f 100644--- a/tests/unittest/test_table_onnx_runtime.py+++ b/tests/unittest/test_table_onnx_runtime.py@@ -227,7 +227,7 @@ def test_onnx_unet_preserves_options_profile_and_dml_lock(monkeypatch: pytest.Momonkeypatch.setenv("MINERU_TABLE_DEVICE", "cpu")monkeypatch.setenv("MINERU_ONNX_PROFILE_DIR", str(tmp_path / "profiles"))monkeypatch.setenv("MINERU_INTRA_OP_NUM_THREADS", "8")- select = Mock(return_value=OnnxSelection("gpu", GpuAdapter(7, "id", "intel", 0x8086, 1, True, 0)))+ select = Mock(return_value=OnnxSelection("gpu", GpuAdapter(7, "id", "intel", 0x8086, 1, True, 0), backend="dml"))monkeypatch.setattr(onnx, "resolve_onnx_device", select)monkeypatch.setattr(onnx.ort, "get_available_providers", lambda: ["DmlExecutionProvider", "CPUExecutionProvider"])native = SimpleNamespace(get_providers=lambda: ["DmlExecutionProvider", "CPUExecutionProvider"], disable_fallback=Mock())@@ -235,7 +235,7 @@ def test_onnx_unet_preserves_options_profile_and_dml_lock(monkeypatch: pytest.Momonkeypatch.setattr(onnx.ort, "InferenceSession", factory)model = TSRUnet({"model_path": "unet.onnx", "small_backend": "onnx", "intra_op_num_threads": 2})assert isinstance(model.session.session, onnx.DirectMLSession)- select.assert_called_once_with(intra_op_num_threads=2)+ select.assert_called_once_with(intra_op_num_threads=2, inter_op_num_threads=1)opts = factory.call_args.kwargs["sess_options"]assert (opts.intra_op_num_threads, opts.inter_op_num_threads) == (2, 1)assert not opts.enable_cpu_mem_arena and not opts.enable_mem_patterndiff --git a/tests/unittest/test_webgpu_runtime.py b/tests/unittest/test_webgpu_runtime.pynew file mode 100644index 00000000..9a8252ca--- /dev/null+++ b/tests/unittest/test_webgpu_runtime.py@@ -0,0 +1,193 @@+"""Linux plugin lifecycle, real-provider checks, model routing and shared locking."""++from concurrent.futures import ThreadPoolExecutor+from dataclasses import asdict+import json+from pathlib import Path+import sys+import threading+import time+from types import SimpleNamespace+from unittest.mock import Mock++import pytest++from mineru.model.runtime import onnx, webgpu+from mineru.model.runtime.onnx_device import OnnxSelection+++def device() -> webgpu.WebGPUDevice:+ return webgpu.WebGPUDevice(0x1002, 1, (("pci_bus_id", "0000:00:01.0"),))+++def test_registration_is_lazy_once_per_process_and_filters_virtual_devices(monkeypatch: pytest.MonkeyPatch) -> None:+ plugin = SimpleNamespace(get_library_path=lambda: "webgpu.so")+ monkeypatch.setitem(sys.modules, "onnxruntime_ep_webgpu", plugin)+ monkeypatch.setattr(webgpu, "_registered_pid", None)+ register = Mock()+ monkeypatch.setattr(onnx.ort, "register_execution_provider_library", register)+ gpu = SimpleNamespace(type=onnx.ort.OrtHardwareDeviceType.GPU, vendor_id=0x1002, device_id=1, metadata={})+ virtual = SimpleNamespace(type=onnx.ort.OrtHardwareDeviceType.GPU, vendor_id=0, device_id=0, metadata={})+ cpu = SimpleNamespace(type=onnx.ort.OrtHardwareDeviceType.CPU, vendor_id=1, device_id=0, metadata={})+ monkeypatch.setattr(+ onnx.ort,+ "get_ep_devices",+ lambda: [SimpleNamespace(ep_name=webgpu.WEBGPU_PROVIDER, device=item) for item in (virtual, cpu, gpu)],+ )+ # CPU creation never imports/registers the plugin or enumerates hardware.+ monkeypatch.setattr(onnx.ort, "InferenceSession", Mock(return_value=object()))+ onnx.create_ort_session("cpu.onnx")+ register.assert_not_called()+ with ThreadPoolExecutor(max_workers=4) as pool:+ results = list(pool.map(lambda _: webgpu.discover_webgpu_devices(), range(8)))+ assert register.call_count == 1+ assert all(row == (webgpu.WebGPUDevice(0x1002, 1),) for row in results)+ assert len({OnnxSelection("gpu", row[0], backend="webgpu") for row in results}) == 1+ monkeypatch.setattr(webgpu, "_registered_pid", -1)+ webgpu.discover_webgpu_devices()+ assert register.call_count == 2+++def test_production_options_resolve_native_handles_locally(monkeypatch: pytest.MonkeyPatch) -> None:+ ep = SimpleNamespace(device=SimpleNamespace(vendor_id=0x1002, device_id=1, metadata=dict(device().metadata)))+ monkeypatch.setattr(webgpu, "_devices", lambda: [ep])+ options = SimpleNamespace(add_provider_for_devices=Mock())+ webgpu.attach_webgpu_provider(options, device())+ options.add_provider_for_devices.assert_called_once_with([ep], webgpu.WEBGPU_OPTIONS)+ descriptor = json.loads(json.dumps(asdict(device())))+ assert descriptor == {"vendor_id": 0x1002, "device_id": 1, "metadata": [["pci_bus_id", "0000:00:01.0"]]}+ with pytest.raises(RuntimeError, match="no longer available"):+ webgpu.attach_webgpu_provider(options, webgpu.WebGPUDevice(1, 2))+++def test_registration_failure_can_be_retried(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setattr(webgpu, "_registered_pid", None)+ monkeypatch.setitem(sys.modules, "onnxruntime_ep_webgpu", SimpleNamespace(get_library_path=lambda: "missing.so"))+ register = Mock(side_effect=OSError("libvulkan missing"))+ monkeypatch.setattr(onnx.ort, "register_execution_provider_library", register)+ for _ in range(2):+ with pytest.raises(OSError, match="libvulkan"):+ webgpu.discover_webgpu_devices()+ assert register.call_count == 2 and webgpu._registered_pid is None+++def test_factory_uses_attached_plugin_without_provider_override(monkeypatch: pytest.MonkeyPatch) -> None:+ attach = Mock()+ native = SimpleNamespace(get_providers=lambda: [webgpu.WEBGPU_PROVIDER, "CPUExecutionProvider"], disable_fallback=Mock())+ factory = Mock(return_value=native)+ monkeypatch.setattr(onnx, "attach_webgpu_provider", attach)+ monkeypatch.setattr(onnx.ort, "InferenceSession", factory)+ session = onnx.create_ort_session("rec.onnx", adapter=device(), model_kind="ocr_rec")+ assert isinstance(session, onnx.WebGPUSession)+ assert "providers" not in factory.call_args.kwargs+ assert attach.call_args.args[1] == device()+ native.disable_fallback.assert_called_once()++[email protected]("failure", ["plugin_missing", "vulkan_error", "exception", "silent"])+def test_gpu_failure_uses_clean_cpu_options(monkeypatch: pytest.MonkeyPatch, failure: str) -> None:+ def attach(opts: object, descriptor: object) -> None:+ opts.add_session_config_entry("test.plugin", "attached")+ if failure == "plugin_missing":+ raise ImportError("missing plugin")+ if failure == "vulkan_error":+ raise OSError("libvulkan missing")++ cpu = SimpleNamespace(get_providers=lambda: ["CPUExecutionProvider"])+ factory = Mock(side_effect=[RuntimeError("device lost"), cpu] if failure == "exception" else None, return_value=cpu)+ monkeypatch.setattr(onnx, "attach_webgpu_provider", attach)+ monkeypatch.setattr(onnx.ort, "InferenceSession", factory)+ save = Mock()+ monkeypatch.setattr(onnx, "confirm_gpu_session", save)+ options = onnx.ort.SessionOptions()+ options.intra_op_num_threads = 2+ options.inter_op_num_threads = 3+ options.enable_cpu_mem_arena = False+ options.enable_profiling = True+ options.profile_file_prefix = "unet-profile"+ assert (+ onnx.ort_session(+ "unet.onnx", model_kind="unet", selection=OnnxSelection("gpu", device(), backend="webgpu"), sess_options=options+ )+ is cpu+ )+ fallback = factory.call_args.kwargs["sess_options"]+ assert fallback is not options+ assert (fallback.intra_op_num_threads, fallback.inter_op_num_threads) == (2, 3)+ assert not fallback.enable_cpu_mem_arena+ assert fallback.enable_profiling and fallback.profile_file_prefix == "unet-profile"+ with pytest.raises(RuntimeError):+ fallback.get_session_config_entry("test.plugin")+ save.assert_not_called()+++def test_linux_routes_only_five_models(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setattr(onnx, "sys", SimpleNamespace(platform="linux"))+ select = Mock(return_value=OnnxSelection("gpu", device(), backend="webgpu"))+ factory = Mock(return_value=SimpleNamespace(get_providers=lambda: [webgpu.WEBGPU_PROVIDER]))+ monkeypatch.setattr(onnx, "resolve_onnx_device", select)+ monkeypatch.setattr(onnx, "create_ort_session", factory)+ for kind in ("layout", "mfr", "ocr_det", "ocr_rec", "unet"):+ onnx.ort_session("test.onnx", model_kind=kind)+ assert factory.call_args.kwargs["adapter"] == device()+ assert select.call_count == 5+ for kind in ("", "seal_det", "table", "slanet_plus", "mfr_tail_cpu"):+ onnx.ort_session("test.onnx", model_kind=kind)+ assert factory.call_args.kwargs["adapter"] is None+ assert select.call_count == 5+++def test_webgpu_creation_and_two_session_runs_share_lock(monkeypatch: pytest.MonkeyPatch) -> None:+ active = 0+ maximum = 0+ counter_lock = threading.Lock()++ def work(*args: object, **kwargs: object) -> list[int]:+ nonlocal active, maximum+ with counter_lock:+ active += 1+ maximum = max(maximum, active)+ time.sleep(0.01)+ with counter_lock:+ active -= 1+ return [1]++ native = SimpleNamespace(run=work, get_providers=lambda: [webgpu.WEBGPU_PROVIDER], disable_fallback=Mock())+ sessions = [onnx.WebGPUSession(native), onnx.WebGPUSession(native)]+ monkeypatch.setattr(onnx, "attach_webgpu_provider", work)+ monkeypatch.setattr(onnx.ort, "InferenceSession", lambda *args, **kwargs: native)++ def task(index: int) -> None:+ if index % 3 == 0:+ onnx.create_ort_session("rec.onnx", adapter=device())+ else:+ assert sessions[index % 2].run(None, {}) == [1]++ with ThreadPoolExecutor(max_workers=4) as pool:+ list(pool.map(task, range(12)))+ assert maximum == 1++[email protected]("provider", ["CPUExecutionProvider", "DmlExecutionProvider", webgpu.WEBGPU_PROVIDER])+def test_mfr_tail_policy_uses_actual_provider(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, provider: str) -> None:+ from mineru.model.mfr import pp_formulanet_plus_m_onnx as mfr++ config = tmp_path / "model.yml"+ config.write_text("PostProcess:\n character_dict: []\n")+ session = SimpleNamespace(get_providers=lambda: [provider], get_inputs=lambda: [SimpleNamespace(name="image")])+ monkeypatch.setattr(mfr, "ort_session", Mock(return_value=session))+ for name in ("UniMERNetDecode", "UniMERNetImgDecode", "UniMERNetTestTransform", "LatexImageFormat"):+ monkeypatch.setattr(mfr, name, Mock())+ model = mfr.PPFormulaNetPlusMONNX("mfr.onnx", str(config))+ assert model.cpu_tail_enabled is (provider != "CPUExecutionProvider")++[email protected](("lang", "kind"), [("ch", "ocr_det"), ("seal", "seal_det")])+def test_normal_and_seal_detectors_are_explicit(monkeypatch: pytest.MonkeyPatch, lang: str, kind: str) -> None:+ from mineru.model.ocr import pp_ocr_v6_onnx as ocr++ detector = Mock()+ monkeypatch.setattr(ocr, "TextDetectorONNX", detector)+ monkeypatch.setattr(ocr, "TextRecognizerONNX", Mock())+ ocr.PPOCRv6ONNX("det.onnx", "rec.onnx", "dict.txt", lang=lang)+ assert detector.call_args.kwargs["model_kind"] == kind