Skip to content

mem: Disable ONNX memory pattern on layout model sessions - #532

Open
KRRT7 wants to merge 1 commit into
Unstructured-IO:mainfrom
KRRT7:mem/disable-onnx-mem-pattern-only
Open

KRRT7 wants to merge 1 commit into
Unstructured-IO:mainfrom
KRRT7:mem/disable-onnx-mem-pattern-only

Conversation

@KRRT7

@KRRT7 KRRT7 commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

ONNX Runtime's memory pattern (SessionOptions.enable_mem_pattern) plans buffers from the first run's allocations and keeps them resident for reuse. For the YoloX layout model this keeps about 120 MiB more RSS per session without making later runs faster. This disables it for the YoloX and Detectron2 sessions and keeps the CPU memory arena on.

Supersedes #495, which disabled both enable_mem_pattern and enable_cpu_mem_arena behind an ONNX_DISABLE_MEMORY_ARENA env var. Measured separately, the arena doesn't save memory here, and the pattern is safe to turn off by default, so this drops the arena change and the env var. #495 also could no longer be updated in place, and its tip had an unresolved conflict marker in CHANGELOG.md.

Impact

Single YoloX session, 1×3×1024×768 input, warm runs (macOS, CPU provider):

Options Peak RSS Per run
default ~700 MiB 0.85–0.90 s
enable_mem_pattern=False (this PR) 580 MiB 0.83–0.88 s
both disabled (#495 with env var) 641 MiB 0.86 s
enable_cpu_mem_arena=False only 758 MiB 1.21 s

End to end, process_file_with_model on sample-docs/layout-parser-paper.pdf (16 pages, YoloX), two paired runs:

Peak RSS Output hash
main 1008 / 1013 MiB 34aee4cf5cdb
this PR 893 / 892 MiB 34aee4cf5cdb

The hash covers every page's element coordinates, probabilities and class ids. Wall time was no slower; it was noisy on a busy machine.

Verification

  • New tests assert that both models create their session with enable_mem_pattern=False and enable_cpu_mem_arena=True. They fail on main and pass here.
  • make test: 381 passed, 1 skipped. ruff check / ruff format --check clean.

Measured only with the CPU execution provider; enable_mem_pattern is a CPU-side planning option, but a CUDA/TensorRT check before release would confirm.

Version bumped to 1.6.17 with a CHANGELOG entry. #530 and #531 also target 1.6.17, so later merges need the next patch version.

View guided diff

ONNX Runtime's memory pattern plans buffers from the first run's
allocations and keeps them resident for reuse. For the YoloX layout
model this costs about 120 MiB of RSS per session without making later
runs faster. Disable it for YoloX and Detectron2 sessions, and keep the
CPU memory arena, which saves no further memory here.

A 16-page YoloX run's peak RSS drops from about 1010 MiB to 892 MiB
with identical output across two paired runs.

Supersedes Unstructured-IO#495, which disabled both options behind the
ONNX_DISABLE_MEMORY_ARENA env var.

Co-authored-by: wtfyd-agent[bot] <328989958+wtfyd-agent[bot]@users.noreply.github.com>
@KRRT7

KRRT7 commented Oct 24, 2026

Copy link
Copy Markdown
Contributor Author

Combined effect of #530, #531, #532 and #533 merged together onto main (9a227e7), measured against main on the same machine with alternating runs:

main all four
Peak RSS, layout-parser-paper.pdf (16 pages) through YoloX 1014–1015 MiB 666–674 MiB
RSS after importing inference.layout + models.base 369–417 MiB 148–149 MiB
structure_to_cells, synthetic 120×12 table, 8k tokens 28.3 s 0.33 s
PageLayout._get_image, page 200 of the 400-page sample 24.5 s 0.11 s
  • The 16-page YoloX output hash (coordinates, probabilities, class ids per page) is identical to main.
  • make test passes on the combined branch: 415 passed, 1 skipped.
  • The code changes merge without conflicts. All four PRs bump to 1.6.17, so each one merged after the first needs the next patch version.
  • Timings came from a busy machine and read about 2× high in absolute terms; the memory numbers were stable across runs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant