perf(tables): Vectorize slot_into_containers token-to-cell scoring

KR
1mo ago
+120-28

Summary

slot_into_containers scored every token against every cell in pure Python, building a new Rect and a dict for each pair and fully sorting each token's scores just to find the best one. structure_to_cells calls it twice per table, and refine_rows/refine_columns call it again through nms_by_containment. On large tables it accounts for ~99% of table postprocessing time.

This computes the overlaps with numpy in chunks of at most ~1M token-cell pairs, so temporary memory stays proportional to the number of cells rather than tokens × cells, the same way #521/#522 bound memory.

Overlaps with #527, which rewrites the same function. #527 keeps the per-pair Python loop and picks the first maximum in one pass instead of sorting. This PR vectorizes the pair scoring itself. Only one of the two should merge.

Performance

structure_to_cells on synthetic tables:

TableBeforeAfter
20×6, 500 tokens0.06 s<0.01 s
60×10, 3k tokens1.84 s0.03 s
120×12, 8k tokens11.9 s0.15 s

Behavior preserved

For Python int/float and NumPy float64/int64 coordinates the output is unchanged: the same assignments, scores and score order. That includes the edge-case behavior of Rect.intersect / Rect.get_area:

  • A zero-area container takes in every package with score 1.0 (Rect.intersect replaces a zero-area self with the other rect).
  • Packages with no positive area are skipped and get no score.
  • Ties go to the first container (argmax matches the stable descending sort).
  • Scores are returned as Python floats.

Known difference: NumPy float32 coordinates are now scored in float64 and returned as Python floats instead of np.float32. In 2,000 random float32 cases no assignment changed, but a score that lands exactly on overlap_threshold could in principle resolve differently.

Verification

  • Randomized differential check against the original implementation: 6,000 cases identical, including integer-grid boxes (forcing ties and zero-area/inverted boxes) and chunk sizes 1, 3, 7 and the default.
  • New tests in test_unstructured_inference/models/test_slot_into_containers.py:
    • a differential test against a pure-Rect reference across chunk sizes
    • explicit tests for zero-area containers, zero-area packages and tie-breaking (these also pass on the old implementation)
  • make test: 402 passed, 1 skipped. ruff check / ruff format --check clean.

Version bumped to 1.6.17 with a CHANGELOG entry.