Skip to content

Fix bounding box center overflow for narrow dtypes - #9595

Open
aswanth-07 wants to merge 1 commit into
pytorch:mainfrom
aswanth-07:agent/fix-bbox-center-overflow
Open

Fix bounding box center overflow for narrow dtypes#9595
aswanth-07 wants to merge 1 commit into
pytorch:mainfrom
aswanth-07:agent/fix-bbox-center-overflow

Conversation

@aswanth-07

@aswanth-07 aswanth-07 commented Aug 9, 2026

Copy link
Copy Markdown

Summary

  • avoid the overflowing x1 * 2 intermediate in XYXY to CXCYWH conversion
  • compute the equivalent center as x1 + width / 2
  • add regression coverage for uint8 and float16, in both in-place and out-of-place conversion

Why

_xyxy_to_cxcywh first stores x2 - x1 as the width, then computes the center through (x1 * 2 + width) / 2. For narrow dtypes, x1 * 2 can overflow even when the input coordinates, width, and correct center all fit in the dtype.

For example, a valid uint8 box with x coordinates 200 and 220 returns center 82 instead of 210, while a valid float16 box with x coordinates 32768 and 49152 returns infinity instead of 40960. Reordering the same expression to x1 + width / 2 avoids the unnecessary intermediate and retains integer floor rounding.

Fixes #9594.

Validation

  • full TestConvertBoundingBoxFormat: 184 passed, 30 expected failures
  • 5,000 randomized valid boxes across uint8, int8, int16, int32, and int64
  • float autograd smoke test
  • ufmt check on both changed files
  • changed lines introduce no new flake8 or mypy diagnostics; broader commands report only unchanged baseline findings
  • git diff --check

The affected conversion is pure Python. The local source checkout used the compatible installed TorchVision extension and supplied only the newly introduced qnms schema required during import.

AI disclosure: I used Codex to help audit the conversion arithmetic, search for duplicates, implement and test the focused patch, and draft this PR. I reviewed and understand the change and will personally handle maintainer follow-up.

@pytorch-bot

pytorch-bot Bot commented Aug 9, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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 Aug 9, 2026

Copy link
Copy Markdown

Hi @aswanth-07!

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 commented Aug 9, 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!

@meta-cla meta-cla Bot added the cla signed label Aug 9, 2026
@aswanth-07
aswanth-07 marked this pull request as ready for review August 9, 2026 16:41
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.

XYXY to CXCYWH conversion overflows for narrow dtypes

1 participant