Skip to content
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

Create kernel on-device for transforms.functional.gaussian_blur #8426

Merged
merged 4 commits into from
May 29, 2024

Conversation

drhead
Copy link
Contributor

@drhead drhead commented May 17, 2024

The current implementation of gaussian blur creates the kernels on CPU and then moves them to the device and dtype of the image. This goes against best practices for pytorch optimization, since it induces a forced device sync.

To fix it, I have passed the device and dtype parameters over to the linspace operation that creates the initial tensor. This should remove the forced device sync. It is worth noting that this may result in it being calculated in a lower precision than normal. This would of course involve an extra call for the cast to get it to the correct precision.

There's probably room for more optimization in this, especially when GaussianBlur is used as a module -- really, I think it would be more appropriate to create the kernels one time and reuse them instead of constructing new ones. This PR solves the more critical issue as is, though.

Closes #8401

cc @vfdev-5

Copy link

pytorch-bot bot commented May 17, 2024

🔗 Helpful Links

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

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

❌ 8 New Failures, 14 Unrelated Failures

As of commit ad00447 with merge base a5f531a (image):

NEW FAILURES - The following jobs have failed:

FLAKY - The following jobs failed but were likely due to flakiness present on trunk:

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

Copy link
Member

@NicolasHug NicolasHug left a comment

Choose a reason for hiding this comment

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

Thank you for the PR @drhead , I'll merge after the CI passes.

Note that the V2 version in torchvision.transforms.v2.functional.gaussian_blur would already create the kernel on-device, so perhaps you can migrate to the V2 version already.

@NicolasHug NicolasHug changed the title Create gaussian kernels on-device Create kernel on-device for transforms.functional.gaussian_blur May 29, 2024
@NicolasHug NicolasHug merged commit 45e053b into pytorch:main May 29, 2024
43 of 65 checks passed
facebook-github-bot pushed a commit that referenced this pull request Jun 7, 2024
…blur` (#8426)

Reviewed By: vmoens

Differential Revision: D58283858

fbshipit-source-id: a4df173fcafe9bce4b35478a7eab5f66f2579180

Co-authored-by: Nicolas Hug <nh.nicolas.hug@gmail.com>
Co-authored-by: Nicolas Hug <nicolashug@fb.com>
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.

Can't use gaussian_blur if sigma is a tensor on gpu
3 participants