From e4beb2dce77b660d0067d551020b8c65ccd5a6a8 Mon Sep 17 00:00:00 2001 From: Kevin Turcios Date: Mon, 9 Nov 2026 03:17:08 -0500 Subject: [PATCH 1/2] perf(pdf): vectorize aggregate_embedded_text_by_block's IoU and drop redundant slicing --- .../pdf_image/pdfminer_processing.py | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/unstructured/partition/pdf_image/pdfminer_processing.py b/unstructured/partition/pdf_image/pdfminer_processing.py index 580ad5f243..2a2c3d8de2 100644 --- a/unstructured/partition/pdf_image/pdfminer_processing.py +++ b/unstructured/partition/pdf_image/pdfminer_processing.py @@ -901,12 +901,16 @@ def remove_duplicate_elements( def _aggregated_iou(box1s, box2): - intersection = 0.0 - sum_areas = calculate_bbox_area(box2) + x1, y1, x2, y2 = box1s[:, 0], box1s[:, 1], box1s[:, 2], box1s[:, 3] + inter_w = np.minimum(x2, box2[2]) - np.maximum(x1, box2[0]) + inter_h = np.minimum(y2, box2[3]) - np.maximum(y1, box2[1]) + inter_areas = np.where((inter_w > 0) & (inter_h > 0), inter_w * inter_h, 0.0) + areas = (x2 - x1) * (y2 - y1) - for i in range(box1s.shape[0]): - intersection += calculate_intersection_area(box1s[i, :], box2) - sum_areas += calculate_bbox_area(box1s[i, :]) + # -- cumsum accumulates left to right like the scalar loop this replaced, so the float + # -- result is bit-identical (np.sum's pairwise summation would not be). -- + intersection = np.cumsum(np.concatenate(([0.0], inter_areas)))[-1] + sum_areas = np.cumsum(np.concatenate(([calculate_bbox_area(box2)], areas)))[-1] union = sum_areas - intersection @@ -937,16 +941,14 @@ def aggregate_embedded_text_by_block( .astype(bool) ) - text = " ".join([text for text in source_regions.slice(mask).texts if text]) + selected = source_regions.slice(mask) + text = " ".join([text for text in selected.texts if text]) - if sum(mask): - source_bboxes = source_regions.slice(mask).element_coords - target_bboxes = target_region.element_coords - - iou = _aggregated_iou(source_bboxes, target_bboxes[0, :]) + if mask.any(): + iou = _aggregated_iou(selected.element_coords, target_region.element_coords[0, :]) fully_filled = ( - all(flag == IsExtracted.TRUE for flag in source_regions.slice(mask).is_extracted_array) + all(flag == IsExtracted.TRUE for flag in selected.is_extracted_array) and iou > text_coverage_threshold ) is_extracted = IsExtracted.TRUE if fully_filled else IsExtracted.PARTIAL From ac92ac915ae6ddc18d64d8a4cdabc2c2ab9403c6 Mon Sep 17 00:00:00 2001 From: Kevin Turcios Date: Sat, 21 Nov 2026 03:51:48 -0500 Subject: [PATCH 2/2] perf(pdf): keep the coordinates' dtype in _aggregated_iou's accumulation 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. --- .../pdf_image/test_pdfminer_processing.py | 33 +++++++++++++++++++ .../pdf_image/pdfminer_processing.py | 7 ++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/test_unstructured/partition/pdf_image/test_pdfminer_processing.py b/test_unstructured/partition/pdf_image/test_pdfminer_processing.py index 97dc773f0d..1c80ce7f63 100644 --- a/test_unstructured/partition/pdf_image/test_pdfminer_processing.py +++ b/test_unstructured/partition/pdf_image/test_pdfminer_processing.py @@ -20,12 +20,15 @@ from test_unstructured.unit_utils import example_doc_path from unstructured.partition.auto import partition from unstructured.partition.pdf_image.pdfminer_processing import ( + _aggregated_iou, _deduplicate_ltchars, _rotate_bboxes, _validate_bbox, aggregate_embedded_text_by_block, bboxes1_is_almost_subregion_of_bboxes2, boxes_self_iou, + calculate_bbox_area, + calculate_intersection_area, clean_pdfminer_inner_elements, get_widget_text_from_annots, process_file_with_pdfminer, @@ -238,6 +241,36 @@ def test_aggregate_not_filling_target(): assert extracted.value == "false" +def _scalar_aggregated_iou(box1s, box2): + """Reference: the per-box loop `_aggregated_iou` used before it was vectorized.""" + intersection = 0.0 + sum_areas = calculate_bbox_area(box2) + for i in range(box1s.shape[0]): + intersection += calculate_intersection_area(box1s[i, :], box2) + sum_areas += calculate_bbox_area(box1s[i, :]) + union = sum_areas - intersection + return 1.0 if union == 0 else intersection / union + + +@pytest.mark.parametrize("dtype", [np.float32, np.float64]) +def test_aggregated_iou_matches_scalar_loop_bit_for_bit(dtype): + # -- the result is compared against a coverage threshold, so a last-bit difference could + # -- flip `is_extracted` at the boundary; require exact equality and the same dtype -- + rng = np.random.default_rng(0) + for _ in range(500): + n = int(rng.integers(1, 40)) + xy = rng.uniform(0, 2000, (n, 2)) + wh = rng.uniform(-5, 300, (n, 2)) # -- includes zero-size and inverted boxes -- + box1s = np.hstack([xy, xy + wh]).astype(dtype) + box2 = np.array([100, 100, 900, 1500], dtype=dtype) + + expected = _scalar_aggregated_iou(box1s, box2) + actual = _aggregated_iou(box1s, box2) + + assert actual == expected + assert np.asarray(actual).dtype == np.asarray(expected).dtype + + @pytest.mark.parametrize( ("coords1", "coords2", "expected"), [ diff --git a/unstructured/partition/pdf_image/pdfminer_processing.py b/unstructured/partition/pdf_image/pdfminer_processing.py index 2a2c3d8de2..ba20b0a588 100644 --- a/unstructured/partition/pdf_image/pdfminer_processing.py +++ b/unstructured/partition/pdf_image/pdfminer_processing.py @@ -908,9 +908,10 @@ def _aggregated_iou(box1s, box2): areas = (x2 - x1) * (y2 - y1) # -- cumsum accumulates left to right like the scalar loop this replaced, so the float - # -- result is bit-identical (np.sum's pairwise summation would not be). -- - intersection = np.cumsum(np.concatenate(([0.0], inter_areas)))[-1] - sum_areas = np.cumsum(np.concatenate(([calculate_bbox_area(box2)], areas)))[-1] + # -- result is bit-identical (np.sum's pairwise summation would not be). The seeds keep the + # -- coordinates' dtype so float32 input still accumulates in float32, as the loop did. -- + intersection = np.cumsum(np.concatenate((np.zeros(1, inter_areas.dtype), inter_areas)))[-1] + sum_areas = np.cumsum(np.concatenate((np.atleast_1d(calculate_bbox_area(box2)), areas)))[-1] union = sum_areas - intersection