perf: Import onnx quantization and matplotlib only where used
KR
1mo ago+38+38+01234567890123456789-3-3-0123456789
Summary
Importing the layout API loaded two large libraries that most runs never use:
models/detectron2onnx.pyimportedonnxruntime.quantizationat module load, which loads all ofonnx.models/base.pyimportsdetectron2onnxfor every model, YOLOX included, but quantization only runs when a quantized Detectron2 model doesn't exist on disk yet.visualize.pyimportedmatplotlib.pyplot, andinference/layout.pyimportsvisualizefordraw_bbox. Onlyshow_plotuses 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.pyimportsinference.layout,models.base,models.detectron2onnxandvisualizein a fresh interpreter and asserts that neitheronnxnormatplotlibgets loaded. It fails onmainand passes here. - The existing
show_plottests still patchmatplotlib.pyplot.subplots/showand pass, because the module attributes are looked up at call time. make test: 383 passed, 1 skipped.ruff check/ruff format --checkclean.
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.