Skip to content

perf: Import onnx quantization and matplotlib only where used - #531

Open
KRRT7 wants to merge 1 commit into
Unstructured-IO:mainfrom
KRRT7:perf/lazy-heavy-imports
Open

KRRT7 wants to merge 1 commit into
Unstructured-IO:mainfrom
KRRT7:perf/lazy-heavy-imports

Conversation

@KRRT7

@KRRT7 KRRT7 commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Importing the layout API loaded two large libraries that most runs never use:

  • models/detectron2onnx.py imported onnxruntime.quantization at module load, which loads all of onnx. models/base.py imports detectron2onnx for every model, YOLOX included, but quantization only runs when a quantized Detectron2 model doesn't exist on disk yet.
  • visualize.py imported matplotlib.pyplot, and inference/layout.py imports visualize for draw_bbox. Only show_plot uses matplotlib.

Each import now happens at its single call site.

Impact

Measured on macOS, Python 3.13:

Before After
RSS after importing inference.layout + models.base 367 MiB 149 MiB
Import time (warm) ~1.2–1.5 s ~0.5–0.6 s
Peak RSS, 16-page PDF through YOLOX (process_file_with_model) 1013 MiB 792 MiB

The YOLOX run produced identical output (161 elements) on both sides.

Verification

  • New test_unstructured_inference/test_imports.py imports inference.layout, models.base, models.detectron2onnx and visualize in a fresh interpreter and asserts that neither onnx nor matplotlib gets loaded. It fails on main and passes here.
  • The existing show_plot tests still patch matplotlib.pyplot.subplots/show and pass, because the module attributes are looked up at call time.
  • make test: 383 passed, 1 skipped. ruff check / ruff format --check clean.

Version bumped to 1.6.17 with a CHANGELOG entry. #530 also targets 1.6.17, so whichever merges second needs the next patch version.

View guided diff

detectron2onnx imported onnxruntime.quantization at module load, which
loads all of onnx, and base.py imports detectron2onnx for every model,
including YOLOX. visualize imported matplotlib.pyplot, and layout.py
imports visualize for draw_bbox. Neither library is needed unless a
quantized Detectron2 model is created or show_plot is called, so import
each at its single call site.

Importing the layout and model modules drops from 367 MiB to 149 MiB
RSS. A 16-page YOLOX run's peak RSS drops from 1013 MiB to 792 MiB with
identical output.

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