Skip to content

Render each PDF page once in pdf_extraction - #2828

Closed
jioffe502 wants to merge 17 commits into
mainfrom
devin/1793748111-single-page-render
Closed

jioffe502 wants to merge 17 commits into
mainfrom
devin/1793748111-single-page-render

Conversation

@jioffe502

@jioffe502 jioffe502 commented Nov 4, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Since #2841, crops are skipped by default; this PR makes the crop path render once when it does run (explicit extract_images=True, caption, image store, explicit dedup). The default text and ViDoRe paths do not exercise it.

pdf_extraction rendered every page twice when extract_images=True: once at the model scale for the page-elements/OCR page_image, and again in extract_image_like_objects_from_pdfium_page at scale 1 just to cut image/shape/form crops. This PR renders once and cuts the crops from that same raster.

before: render(model scale) -> page_image          render(scale 1, rotation) -> crop
after:  render(model scale) -> page_image
                            -> resize to page points, rot90(page rotation) -> crop
  • _render_page_to_base64 is split into _render_page_rgb + _encode_page_image so pdf_extraction can keep the RGB array; the wrapper stays for image_store.
  • extract_image_like_objects_from_pdfium_page(page, page_image) takes that array instead of rendering. The rotation it applies is the same one the old second render applied.

Results: pps flat, PDF-extraction CPU −5.7% when crops run

main vs this PR, bo10k (213,538 pages), 8 GPUs, BF16, same image, explicit extract_images: true:

pages/s R@10 PDF-extraction CPU per page
main b1c42dba 331.0 0.7468 70.9 ms
this PR 9541c41c¹ 329.4 0.7483 66.9 ms (−5.7%)

Rows are unchanged: 2 vs 4 unique (source, page, type) rows, which is OCR run-to-run noise.

¹ The eval ran on 9541c41c. Later commits only merge in #2841's review fixes and main, which don't touch the crop path.

Earlier measurements (older main, 2 GPUs)

PR vs main, measured with extract_images=True, 2 runs each, A/B/B/A, same config, cold compile caches, 2 GPUs

These runs predate #2841, when extract_images=True was the default, so they measure the crop path this PR now applies to.

Dataset (pages) Arm pages/s GPU util, whole run GPU util, steady recall@5 recall@10 PDF extraction CPU per page
bo767 (54,730) main 71.8–72.5 88.2–88.9% 94.3–94.6% 0.882–0.884 0.909–0.911 73.5–76.3 ms
this PR 72.1–72.4 (±0%) 89.0–89.1% 94.9–95.0% 0.886–0.887 0.912 68.1–70.7 ms (−7%)
bo10k (213,538) main 94.3–94.4 90.3–90.8% 92.8–93.2% 0.707 0.737–0.738 73.7–73.9 ms
this PR 94.5 (+0.1%) 90.4–90.7% 92.8–93.3% 0.709–0.710 0.738–0.739 67.2–67.3 ms (−9%)

Ranges are min–max over 2 runs per arm, run order main, PR, PR, main per dataset. PDF extraction CPU is Ray Data's pdf_extraction worker time divided by pages.

  • Throughput: unchanged. Both datasets are GPU-bound in this setup, so the CPU saved in PDF extraction does not show up as pages/s.
  • Recall: PR runs are not below main on either dataset. On recall@5 they are 2–4 queries above main's two-run band. The model input does not change (see below), so this is run-to-run OCR noise, not an effect of this PR.
  • Element counts (text/table/chart per page) differ between PR and main on 0 bo767 pages and 3–8 bo10k pages, the same as main vs main (1 and 7 pages).
  • Tail: on the slowest bo10k document (42x30 in drawings), extraction CPU for its three heaviest pages drops from 427/193/119 s to 244/105/64 s.

Why it is safe

  • Unchanged: the model input. page_image is encoded from the same render as before, so page elements and OCR see the same bytes. Crop boxes still come from PDF object positions.
  • Changed: crop pixels are resampled from the model-scale render instead of rendered natively at scale 1 (median difference ~3/255 per pixel; on the ~4% of pages larger than the 1024 px fit the crops are upsampled, so softer). In the default text pipeline crops carry no text and are dropped before embedding.
  • Per-page offline diff, main vs PR, over every page of both datasets (268k pages): same crop count on every page; page image, text and crop boxes match on all but 7 pages. Rerun in isolation, 6 of those 7 match (PDFium state in long-lived workers); one bo10k page keeps a form crop box 1 pt narrower.
  • Scope: PDF crop path only. Main's existing crop placement on 90°/270° pages is kept as is.

Tests

Link to Devin session: https://nvidia-cloud.devinenterprise.com/sessions/57f5368b39844a038449e009f6bdd631
Open in Devin Desktop: https://nvidia-cloud.devinenterprise.com/desktop/session/57f5368b39844a038449e009f6bdd631?variant=devin
Requested by: @jioffe502


👀 1 finding for you to triage · Follow Devin's work

Devin Review

Image/shape/form crops were cut from a second full-page PDFium render at
scale 1. Derive that raster from the page render already made for the
page-elements/OCR input instead (resize to page points, then the same
rotation as before). The model input is unchanged.

Signed-off-by: Jacob Ioffe <jioffe@nvidia.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@devin-ai-integration devin-ai-integration Bot 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.

Note

Newer findings are available below. Devin Review posted a newer report on this PR, in addition to the findings presented here.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

@greptile-apps

greptile-apps Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no new actionable issue was found.

Summary

The PR reuses the page raster for PDF image crops instead of rendering each page twice.

  • PDF page crops now reuse the page render instead of rendering again.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart LR
    A[PDF page] --> B[Render once]
    B --> C[RGB array]
    C --> D[Encode page image]
    C --> E[Resize to page points]
    E --> F[Cut image crops]
Loading

Reviews (14) · Last reviewed commit: "Merge main (#2845) into devin/1793748111..." · Reviewed by Greptile

greptile-apps[bot]

This comment was marked as resolved.

…raphIngestor

Signed-off-by: Jacob Ioffe <jioffe@nvidia.com>
…e-page-render

Signed-off-by: Jacob Ioffe <jioffe@nvidia.com>
@jioffe502
jioffe502 changed the base branch from main to jioffe/skip-unused-image-extraction November 9, 2026 17:01
greptile-apps[bot]

This comment was marked as resolved.

@greptile-apps

This comment has been minimized.

…made

Signed-off-by: Jacob Ioffe <jioffe@nvidia.com>
…111-single-page-render

Signed-off-by: Jacob Ioffe <jioffe@nvidia.com>
greptile-apps[bot]

This comment was marked as resolved.

With extract_images resolved off, native-text pages under pdfium_hybrid
lost page_image, but page elements and OCR still read it on every page.
Render whenever those stages run; only the crops stay skipped.

Signed-off-by: Jacob Ioffe <jioffe@nvidia.com>
…111-single-page-render

Signed-off-by: Jacob Ioffe <jioffe@nvidia.com>
Signed-off-by: Jacob Ioffe <jioffe@nvidia.com>
…111-single-page-render

Signed-off-by: Jacob Ioffe <jioffe@nvidia.com>
@jioffe502
jioffe502 changed the base branch from jioffe/skip-unused-image-extraction to main November 10, 2026 19:43
Signed-off-by: Jacob Ioffe <jioffe@nvidia.com>
extract_image_like_objects_from_pdfium_page takes page_image as an optional
argument again; without it the page is rendered at scale 1 as before.
Document its parameters and return value.

Signed-off-by: Jacob Ioffe <jioffe@nvidia.com>

@devin-ai-integration devin-ai-integration Bot 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.

Note

Newer findings are available below. Devin Review posted a newer report on this PR, in addition to the findings presented here.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

List the image, shape, and form options actually consumed by the box helpers.
@devin-ai-integration

Copy link
Copy Markdown
Contributor

Superseded by #2861, which batches the release fixes into one PR, one commit per original PR. This change is commit-for-commit identical there.

@devin-ai-integration

Copy link
Copy Markdown
Contributor

Reopened: keeping the release fixes as individual PRs for review. #2861 is closed.

@devin-ai-integration

Copy link
Copy Markdown
Contributor

Closing in favor of #2845, which includes this render-once change and also fixes crop placement on rotated and offset PDF pages.

@devin-ai-integration

Copy link
Copy Markdown
Contributor

Reopening: #2845's current head keeps a separate crop render, so it does not include the render-once change from this PR. Keeping this open alongside #2845.

page_image is now keyword-only, so existing calls like
extract_image_like_objects_from_pdfium_page(page, False) still bind merge.

Signed-off-by: Jacob Ioffe <jioffe@nvidia.com>
else:
points_size = (math.ceil(page_width), math.ceil(page_height))
page_image_pts = np.rot90(
np.asarray(Image.fromarray(page_image).resize(points_size, Image.BILINEAR)), k=-(rotation // 90)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[P1] Keep the cached raster in the display frame fixed by #2845

For the requested order (#2845 first), page_image already has the PDF page rotation applied, and #2845 maps every crop box into that displayed frame. This additional np.rot90 turns the raster a second time, so the corrected boxes address the wrong pixels. I combined this actual cached-crop function with #2845's box helpers and real PDFium fixtures: the 270-degree crop is entirely blank instead of entirely image content; 90/180-degree crops are also wrong. The no-cache branch at line 628 likewise retains the extra rotation. Rebase this change onto #2845, resize the existing displayed raster without another turn, and have both paths use that same frame. Reuse #2845's correctness fixtures (including offset page boxes) in the single-render tests; their current reference renders with the old extra rotation and therefore cannot protect the earlier fix.

Crops cut from the page-element render now use #2845's displayed page frame: the render is only resized to points, with no extra rotation. The single-render tests compare against main's scale-1 render and expect a crop on every rotation.

Signed-off-by: Jacob Ioffe <jioffe@nvidia.com>
@jioffe502

Copy link
Copy Markdown
Collaborator Author

Closing in favor of a fresh PR from main. As Julio found, this branch predates #2845 and rotates the cached page raster a second time, so 90/180/270-degree crops are wrong on top of #2845. The redo will reuse #2845's displayed-frame raster without another rotation and test against #2845's rotation and offset fixtures.

@jioffe502 jioffe502 closed this Nov 24, 2026

@devin-ai-integration devin-ai-integration Bot 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.

Devin Review found 1 finding. It is left for you to triage below.

View findings in Devin Review · Follow Devin's work

Devin Review

Comment on lines +550 to +552
Crop boxes are in points in the displayed page frame. When ``page_image`` is
given, it is resized to the page size in points, so the boxes line up with a
``page.render(scale=1)`` raster without rendering the page again.

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.

🔍 PR summary describes an extra rotation

The PR summary says extract_image_like_objects_from_pdfium_page rotates the reused raster after resizing. Instead, _page_rect_to_display maps crop boxes into the orientation PDFium already rendered.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

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.

3 participants