-
Notifications
You must be signed in to change notification settings - Fork 24.1k
Adding MPS support for 3D convolutions #99246
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
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/99246
Note: Links to docs will display an error until the docs builds have been completed. ❌ 2 New FailuresAs of commit 5ec4aa5: NEW FAILURES - The following jobs have failed:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
@kulinseth gentle reminder to respond to the review request or to help me re-assign it to someone else |
descriptor:conv2dDescriptor_ | ||
name:nil]; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In line 319 below you want to update the biasPlaceholder
to support 3d convolutions. I think something like this should work:
if (is3DConv) {
biasPlaceholder =
native_mps::Placeholder(cachedGraph->biasTensor_, (bias_opt.value()).view({1, bias_shape[0], 1, 1, 1}));
} else {
biasPlaceholder =
native_mps::Placeholder(cachedGraph->biasTensor_, (bias_opt.value()).view({1, bias_shape[0], 1, 1}));
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Berzeg Thanks a lot for your feedback. Yes you are absolutely right, the bias shape was wrong for 3D and is now fixed
name:nil]; | ||
|
||
} else { | ||
if (bias_defined) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This conditional statement and its block, which contains the biasTensor assignment should be moved outside the conditional block that contains it. Otherwise, biasTensor
will always be nil
for 3d convolutions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can move it right below where you set MPSGraphTensor* biasTensor = nil;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I moved the block accordingly so that it is called regardless of isConv3d or isDepthwiseConv. I amended my tests to include biases and it now works as expected. I created a set of tests at https://gist.github.com/mattiaspaul/b63cd65c9afa4290b316d9297e19ca03 (maybe some could be added to the official test_mps.py at some point)
needed to merge the intermediate changes to Convolution.mm and can hopefully reopen the corrected pull request |
#if !defined(__MAC_13_0) && \ | ||
(!defined(MAC_OS_X_VERSION_13_0) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_13_0)) | ||
|
||
@compatibility_alias MPSGraphConvolution3DOpDescriptor unsupported_MPSGraphConvolution3DOpDescriptor; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move this down to Line 41
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mattiaspaul , can you please move this down to Line 41
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried moving it, but if it appears under the MPSGraph interface the build fails. I found the suggestion for compatibility_alias at nshipster to solve the challenge that the build is done on MacOS12 where the Descriptor is undefined and runs on MacOS13.2 where it becomes available, but am happy to learn about cleaner alternatives.
#if !defined(__MAC_13_0) && \ | ||
(!defined(MAC_OS_X_VERSION_13_0) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_13_0)) | ||
|
||
@implementation MPSGraphConvolution3DOpDescriptor |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can be included in the MPSGraphVenturaOps.h file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion I tried this but got "duplicate symbol" errors from Upsample.mm and Unique.mm for MPSGraphConvolution3DOpDescriptor so left it in the .mm file for now
forwardConvolutionDescriptor:(MPSGraphConvolution3DOpDescriptor * _Nonnull) forwardConvolutionDescriptor | ||
name:(NSString * _Nullable) name; | ||
|
||
- (MPSGraphTensor * _Nonnull) convolution3DWeightsGradientWithIncomingGradientTensor:(MPSGraphTensor * _Nonnull) incomingGradient |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These need to be defined outside the macro.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot @kulinseth this was exactly the problem that caused the build failure. It is now fixed and passes the basic tests.
@mattiaspaul , thanks for the PR. I provided few comments. There is also a build failure:
This issue is related to the declaration of Conv3D methods to be outside the macro. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, works well!
@kulinseth could you please check the revised version again that I posted three weeks ago? |
Hey mattias, Thank you very much for your work. I wanted to train an nnUNet network for image segmentation, but I need to be able to do conv_transpose3d using mps. Do you have any news on that front? Thank you very much again. Best regards, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now looks good.
@pytorchbot merge |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
Merge failedReason: This PR is too stale; the last push date was more than 3 days ago. Please rebase and try again. You can rebase and merge by leaving the following comment on this PR: Details for Dev Infra teamRaised by workflow job |
You don't have permissions to rebase this PR since you are a first time contributor. If you think this is a mistake, please contact PyTorch Dev Infra. |
Merge failedReason: Comment with id 1573298960 not found Details for Dev Infra teamRaised by workflow job |
You are not authorized to force merges to this repository. Please use the regular |
MacOS 12 is still failing with build:
|
@pytorchbot rebase |
@pytorchbot started a rebase job onto refs/remotes/origin/viable/strict. Check the current status here |
Successfully rebased |
Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as |
Any news on this? Seems like a lot of work went into it and it might be close. |
@mattiaspaul , can you please rebase and fix the conflicts ? We don't have MacOS12 support in CI anymore and shouldn't hit previous issues. |
|
||
namespace at::native { | ||
//Create 3D convolution descriptor | ||
void fill_conv3d_desc(MPSGraphConvolution3DOpDescriptor* descriptor_, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you need to add the 'static' keyword on this function.
Hey @mattiaspaul, would you like to continue on this yourself? I think the tests for MacOS 12 need some fixes. If not, I'll have a look at them later. |
Fixes #77818 I saw that PR #99246 was approved, but no one fixed the rebase conflicts, so I am bringing this up again to be merged. I am leveraging @mattiaspaul work. Quoting the description here: > * this pull request enables 3D convolutions (forward/backward) for MPS (Apple Silicon) within the same Convolution.mm file as conv2d. > * does not support channel_last (since pytorch doesn't implement channel_last for 3D tensors) > * does not support conv3d_transpose and treats depth-separable convolutions not as normal case (there are no MPS kernels available for either of those so far) > * requires MacOS >=13.2 (Ventura) Please, let me know if there are any other changes needed and I'll be happy to implement them. Pull Request resolved: #114183 Approved by: https://github.com/malfet
Fixes pytorch#77818 I saw that PR pytorch#99246 was approved, but no one fixed the rebase conflicts, so I am bringing this up again to be merged. I am leveraging @mattiaspaul work. Quoting the description here: > * this pull request enables 3D convolutions (forward/backward) for MPS (Apple Silicon) within the same Convolution.mm file as conv2d. > * does not support channel_last (since pytorch doesn't implement channel_last for 3D tensors) > * does not support conv3d_transpose and treats depth-separable convolutions not as normal case (there are no MPS kernels available for either of those so far) > * requires MacOS >=13.2 (Ventura) Please, let me know if there are any other changes needed and I'll be happy to implement them. Pull Request resolved: pytorch#114183 Approved by: https://github.com/malfet
Fixes pytorch#77818 I saw that PR pytorch#99246 was approved, but no one fixed the rebase conflicts, so I am bringing this up again to be merged. I am leveraging @mattiaspaul work. Quoting the description here: > * this pull request enables 3D convolutions (forward/backward) for MPS (Apple Silicon) within the same Convolution.mm file as conv2d. > * does not support channel_last (since pytorch doesn't implement channel_last for 3D tensors) > * does not support conv3d_transpose and treats depth-separable convolutions not as normal case (there are no MPS kernels available for either of those so far) > * requires MacOS >=13.2 (Ventura) Please, let me know if there are any other changes needed and I'll be happy to implement them. Pull Request resolved: pytorch#114183 Approved by: https://github.com/malfet
Fixes #77818
@kulinseth @albanD could you please check whether this is implemented as intended by you and whether we would require additional tests in test_mps.py?