Skip to content

Add NemotronParseBBox.to_original_coordinates for raw nemotron-parse output#1335

Merged
jamesbraza merged 1 commit into
mainfrom
fix/nemotron-to-original-coordinates
Jun 5, 2026
Merged

Add NemotronParseBBox.to_original_coordinates for raw nemotron-parse output#1335
jamesbraza merged 1 commit into
mainfrom
fix/nemotron-to-original-coordinates

Conversation

@jamesbraza

Copy link
Copy Markdown
Member

Why

nemotron-parse predicts bounding boxes in the space of its preprocessed input: each page is resized (aspect-preserving) and centered onto a target 2048x1648 (HxW) canvas with white padding, so the boxes are normalized to that padded canvas rather than to the page.

NVIDIA's hosted NIM undoes that and returns input-image coordinates, which is why PaperQA's current decode is correct against NIM. A consumer of the model's raw output, though, e.g. a self-hosted vLLM or HuggingFace transformers endpoint, receives canvas-space coordinates and draws every box shifted inward and compressed, worst at low DPI where the padding dominates. This adds a first-class way for such consumers to recover original-image coordinates, mirroring what NVIDIA's postprocessing.transform_bbox_to_original and example.py do.

Notes

  • Opt-in, and intentionally not used on the existing NIM path. NIM already returns input-image coordinates, so applying this there would double-transform. to_original_coordinates sits alongside to_page_coordinates for the raw-output case only.
  • Relation to Upsizing PDF pages to match nemotron-parse training data #1271: that (closed) PR addressed the same mismatch from the input side, upsizing/pre-fitting pages to the training aspect ratio before sending. This is the complementary output side, and leaves the request path untouched.
  • Tests assert parity against NVIDIA's reference across the no-resize, downsized, and width-resized regimes, plus round-trip recovery and a low-DPI heavy-padding case.

🤖 Generated with Claude Code

Copilot AI review requested due to automatic review settings June 5, 2026 01:20
@jamesbraza jamesbraza added the bug Something isn't working label Jun 5, 2026
@jamesbraza jamesbraza self-assigned this Jun 5, 2026
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. enhancement New feature or request labels Jun 5, 2026

Copilot AI 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.

Pull request overview

Adds a first-class conversion utility for consumers of raw nemotron-parse outputs by inverting the model’s resize + centered-padding preprocessing, so canvas-normalized bounding boxes can be mapped back into original image pixel coordinates (matching NVIDIA’s reference behavior). This complements the existing to_page_coordinates path (used for NIM, which already returns input-image coordinates).

Changes:

  • Add NEMOTRON_PARSE_TARGET_HEIGHT/WIDTH constants for Nemotron-Parse’s native input canvas size.
  • Add NemotronParseBBox.to_original_coordinates() to convert padded-canvas normalized bboxes to original-image pixel coordinates.
  • Add tests asserting parity with NVIDIA’s reference transform across multiple resize/padding regimes and a round-trip recovery check.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
packages/paper-qa-nemotron/src/paperqa_nemotron/api.py Introduces target canvas constants and to_original_coordinates() bbox back-projection.
packages/paper-qa-nemotron/tests/test_api.py Adds parity and round-trip tests for the new coordinate transform.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/paper-qa-nemotron/src/paperqa_nemotron/api.py
Comment thread packages/paper-qa-nemotron/src/paperqa_nemotron/api.py
@jamesbraza
jamesbraza force-pushed the fix/nemotron-to-original-coordinates branch from 45d07cb to a80f1d8 Compare June 5, 2026 02:03
…e output

nemotron-parse emits bounding boxes normalized to its target 2048x1648 (HxW) canvas,
which it builds by an aspect-preserving resize followed by centered white padding.
NVIDIA's hosted NIM maps coordinates back to the input image before returning, so
PaperQA's existing decode is correct against NIM. Consumers of the model's raw output
(e.g. a self-hosted vLLM or HuggingFace transformers endpoint) instead receive
canvas-space coordinates and must invert that mapping themselves, as NVIDIA's
example.py does.

Add NemotronParseBBox.to_original_coordinates (companion to to_page_coordinates) plus
the NEMOTRON_PARSE_TARGET_HEIGHT/WIDTH constants: a faithful port of NVIDIA's
postprocessing.transform_bbox_to_original, validated against that reference with
parity, round-trip, and low-DPI tests.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@jamesbraza
jamesbraza force-pushed the fix/nemotron-to-original-coordinates branch from a80f1d8 to 59dd96d Compare June 5, 2026 22:14
@jamesbraza
jamesbraza merged commit d7675d7 into main Jun 5, 2026
5 of 7 checks passed
@jamesbraza
jamesbraza deleted the fix/nemotron-to-original-coordinates branch June 5, 2026 22:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working enhancement New feature or request size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants