[MPS] Fix overaccumulation in ROI ops - #9563
Merged
NicolasHug merged 4 commits intoJul 27, 2026
Merged
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/vision/9563
Note: Links to docs will display an error until the docs builds have been completed. ⏳ No Failures, 7 PendingAs of commit eb9f66f with merge base 3f87802 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
Member
|
CC @malfet if you have some cycle I'd love a review from you |
malfet
reviewed
Jul 21, 2026
|
|
||
| MTLSize threadGroupSize = MTLSizeMake(tgSize, 1, 1); | ||
| [computeEncoder dispatchThreadgroups:threadgroupsPerGrid threadsPerThreadgroup:threadGroupSize]; | ||
| mtl_setArgs(computeEncoder, |
Collaborator
There was a problem hiding this comment.
This is a good change, but purely BE one and iMO should be done in separate PR if this is OK with you
malfet
requested changes
Jul 21, 2026
malfet
left a comment
Collaborator
There was a problem hiding this comment.
You are combining good BE change (use of mtl_setArgs) with a functional fix, which makes it somewhat hard to review. Can you submit a non-functional change for mtl_setArgs migration as one PR and then a bug fix as another?
malfet
approved these changes
Jul 27, 2026
Member
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
MPS ROI kernels combined excess threadgroups with grid-stride loops, repeating outputs and gradients. Dispatch one thread per output for ROI Pool, PS ROI Pool and PS ROI.
Thanks to @jveitchmichaelis for comment on the below PR with the bug:
#9510
Main change is in the kernel and everything else is just shifting the sizes we input to the kernel (thus the huge diff):
MPS 1D loop used
n_tgs=1, which caused multiple threadgroups to traverse overlapping indices. In backward ROI kernels, repeatedatomic_addcalls over-accumulated gradients.This switches to
dispatchThreadswith one thread per pooled output element, ensuring each gradient contribution is processed exactly once.