Skip to content

[MPS] Fix overaccumulation in ROI ops - #9563

Merged
NicolasHug merged 4 commits into
pytorch:mainfrom
Isalia20:fix-mps-roi-pool-overaccumulation
Jul 27, 2026
Merged

[MPS] Fix overaccumulation in ROI ops#9563
NicolasHug merged 4 commits into
pytorch:mainfrom
Isalia20:fix-mps-roi-pool-overaccumulation

Conversation

@Isalia20

@Isalia20 Isalia20 commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

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, repeated atomic_add calls over-accumulated gradients.
This switches to dispatchThreads with one thread per pooled output element, ensuring each gradient contribution is processed exactly once.

@pytorch-bot

pytorch-bot Bot commented Jul 21, 2026

Copy link
Copy Markdown

🔗 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 Pending

As of commit eb9f66f with merge base 3f87802 (image):
💚 Looks good so far! There are no failures yet. 💚

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

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

Copy link
Copy Markdown
Member

CC @malfet if you have some cycle I'd love a review from you


MTLSize threadGroupSize = MTLSizeMake(tgSize, 1, 1);
[computeEncoder dispatchThreadgroups:threadgroupsPerGrid threadsPerThreadgroup:threadGroupSize];
mtl_setArgs(computeEncoder,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is a good change, but purely BE one and iMO should be done in separate PR if this is OK with you

@malfet malfet left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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?

@NicolasHug

Copy link
Copy Markdown
Member

Thanks @Isalia20 for the PR and @malfet for the review

@NicolasHug
NicolasHug merged commit 0b798b3 into pytorch:main Jul 27, 2026
43 checks passed
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