perf(layout): Render only the requested page when hotloading

KR
1mo ago
+53-3

Summary

PageLayout._get_image hotloads a page image for annotate() when the page has neither an image nor an image path. It rendered every page of the PDF to PNG in a temp dir and then opened one of them, so its cost grew with document length. It now passes the page to convert_pdf_to_image as first_page/last_page, so only that page is rendered.

Impact

sample-docs/pdf2image-memory-error-test-400p.pdf, one call:

BeforeAfter
Page 119.9 s0.07 s
Page 40016.3 s0.05 s
Temporary PNGs at peak~165 MiBone page

Behavior preserved

  • Page numbers below 1 index from the end of the document (image_paths[page_number - 1]); page 0 returns the last page, which test_get_image relies on. Those still render every page.
  • A page past the end raises the same ValueError.
  • Pixels, size, mode and rotation metadata (pdf_rotation, pdf_rotation_correction) match the previous output for every page number from −n−1 to n+1. Checked on rotated-page-90.pdf, loremipsum_multipage.pdf, empty-document.pdf, layout-parser-paper.pdf, form-field.pdf, and a generated 5-page document with pages rotated 0/180/90/270/90. That covers rotation corrections that are looked up by page index when only part of the document is rendered.

Verification

  • New tests in test_unstructured_inference/inference/test_layout.py:
    • only the requested page is rendered, and its pixels match a full render (fails on main)
    • page numbers 0, −1 and −9 still index from the end
    • a page past the end still raises ValueError
  • make test: 386 passed, 1 skipped. ruff check / ruff format --check clean.

No overlap with #524, which adds pre-rendered image_paths to from_file/process_file_with_model and doesn't touch _get_image. Version bumped to 1.6.17 with a CHANGELOG entry; #530–#532 also target 1.6.17, so later merges need the next patch version.