Skip to content

perf(pdf): vectorize aggregate_embedded_text_by_block's IoU and drop redundant slicing - #4681

Open
KRRT7 wants to merge 2 commits into
Unstructured-IO:mainfrom
KRRT7:perf/vectorize-embedded-text-aggregation
Open

KRRT7 wants to merge 2 commits into
Unstructured-IO:mainfrom
KRRT7:perf/vectorize-embedded-text-aggregation

Conversation

@KRRT7

@KRRT7 KRRT7 commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Summary

aggregate_embedded_text_by_block() runs once per layout region that needs text, from merge_out_layout_with_ocr_layout() (OCR) and merge_inferred_with_extracted_layout() (hi_res). Per call it:

  • tested if sum(mask): — builtin sum iterates a numpy bool array element by element; now mask.any();
  • sliced source_regions with the same mask three times; now once;
  • computed _aggregated_iou with a Python loop over the selected boxes; now vectorized with numpy.

Numerical equivalence

_aggregated_iou accumulates with np.cumsum, which adds left to right like the old loop (np.sum's pairwise summation could differ in the last bit), and seeds the sums with the coordinates' own dtype, so float32 input still accumulates in float32 as the loop did. Results are bit-identical, with the same return dtype, for both float32 and float64.

test_aggregated_iou_matches_scalar_loop_bit_for_bit pins this: it compares against the old scalar loop over 500 random cases per dtype, including zero-size and inverted boxes, and requires exact equality. (In production, TextRegions converts coordinates to float64, so the float32 case isn't currently reachable; the test keeps the claim unconditional.)

Performance

Walltime, pytest-codspeed run locally (Apple Silicon), merge_out_layout_with_ocr_layout with 60 regions and 1,500 OCR words on one page:

before after
per page 2.7 ms 0.39 ms

Validation

  • test_unstructured/partition/pdf_image/test_pdfminer_processing.py, test_ocr.py, test_pdf.py and test_merge_elements.py pass.

@cragwolfe cragwolfe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the PDF partitioning changes at e79d99a. I did not establish an actionable Critical, High, or Medium defect in the reviewed scope.

One qualification to the bit-identical claim: with float32 coordinates under NumPy 2, the new intersection accumulation can use a different precision from the previous scalar loop. The available source does not establish whether a supported production path passes such coordinates or whether a resulting threshold difference has a user-visible effect. A dtype and threshold-boundary equivalence test would help establish that contract.

This is a scoped source review; it does not cover metrics or staging behavior, uv.lock, or CHANGELOG.md, and it is not a statement about CI or merge readiness.

(authored by codex)

Seed the cumulative sums with the input dtype so float32 coordinates accumulate
in float32 like the scalar loop did, and pin bit-for-bit equality against that
loop for float32 and float64.

This branch has not been deployed

No deployments
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.

2 participants