Skip to content

Fix: Guard NMS fake op registration for CPU-only torch builds #9086

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from

Conversation

KAVYANSHTYAGI
Copy link

@KAVYANSHTYAGI KAVYANSHTYAGI commented May 26, 2025

Summary

This PR fixes a bug where importing torchvision (or libraries depending on torchvision, such as torchmetrics) on CPU-only torch builds (torch==2.7.0+cpu) results in a RuntimeError: operator torchvision::nms does not exist.
The issue is caused by unconditional registration of the fake "torchvision::nms" operator, which may not exist in CPU-only builds.

What does this PR do?

Wraps the @torch.library.register_fake("torchvision::nms") registration in a try-except block.

If the operator does not exist (as is the case for CPU-only builds), the registration is safely skipped.

No change to the logic or API for CUDA/default builds; the fake registration still functions as before where supported.

Why is this needed?

Fixes downstream errors in projects such as [torchmetrics#3098](https://github.com/Lightning-AI/torchmetrics/issues/3098) that rely on torchvision and break in CPU-only torch environments.

Makes torchvision robust to missing ops, aligning with PyTorch ecosystem best practices.

How was this tested?

Verified import of torchvision and torchmetrics works as expected with:

    torch==2.7.0+cpu torchvision==0.22.0 torchmetrics==1.7.1

    torch==2.7.0 torchvision==0.22.0 torchmetrics==1.7.1

No errors raised when the operator is absent; normal registration and functionality in standard builds.

Related Issues

#9085
[torchmetrics#3098]Lightning-AI/torchmetrics#3098

Notes

This fix is minimal and isolated to the fake operator registration.

No changes to the actual implementation of meta_nms or other meta ops.

Thank you for reviewing! Please let me know if any additional information or changes are needed.

Copy link

pytorch-bot bot commented May 26, 2025

🔗 Helpful Links

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

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.

@facebook-github-bot
Copy link

Hi @KAVYANSHTYAGI!

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 [email protected]. Thanks!

@facebook-github-bot
Copy link

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

@NicolasHug
Copy link
Member

Thanks for the PR @KAVYANSHTYAGI , I doubt this will be the right fix for #9085. This may silence the error but it doesn't fix the underlying problem (if any)

@NicolasHug NicolasHug closed this May 29, 2025
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.

3 participants