Skip to content

Fix int64 bounding box truncation in V2 geometry transforms#9545

Open
Sainava wants to merge 1 commit into
pytorch:mainfrom
Sainava:fix/bbox-int64-type-safety
Open

Fix int64 bounding box truncation in V2 geometry transforms#9545
Sainava wants to merge 1 commit into
pytorch:mainfrom
Sainava:fix/bbox-int64-type-safety

Conversation

@Sainava

@Sainava Sainava commented Jul 8, 2026

Copy link
Copy Markdown

Resolves the documented TODOs in torchvision/transforms/v2/functional/_geometry.py regarding integer bounding box casting.

The Issue

When users pass bounding boxes as torch.int64, subsequent format conversions (e.g., XYXY to CXCYWH) perform division operations. Without casting to a floating-point type first, this causes unsafe integer division/truncation, severely degrading the bounding box coordinates.

The Fix

  • Added a targeted cast if bounding_boxes.dtype == torch.int64: bounding_boxes = bounding_boxes.float() prior to convert_bounding_box_format inside both perspective_bounding_boxes and elastic_bounding_boxes.
  • Bypassed the cast for native floating-point tensors to prevent unnecessary memory allocations and to preserve float64 precision.
  • Cached original_dtype = bounding_boxes.dtype at the start of the functions to ensure the returned bounding boxes are correctly cast back to the user's original requested dtype, preventing silent type mutations.

Testing

Verified mathematically via local test suite execution:

  • pytest test/test_transforms_v2.py
    Specifically validated against TestElastic and TestPerspective to ensure all format edge cases maintain correctness across the int64 -> float32 -> int64 round trip.

@pytorch-bot

pytorch-bot Bot commented Jul 8, 2026

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/vision/9545

Note: Links to docs will display an error until the docs builds have been completed.

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla

meta-cla Bot commented Jul 8, 2026

Copy link
Copy Markdown

Hi @Sainava!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla meta-cla Bot added the cla signed label Jul 8, 2026
@meta-cla

meta-cla Bot commented Jul 8, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant