Deform conv2d mps support#9026
Open
goldfishsound wants to merge 34 commits intopytorch:mainfrom
Open
Conversation
Removed the product which is not part of the repo.
…sed for cpp and cuda implementation of deform_conv2d, and the implementation used in the optest. kernel deformable_im2col: Using threadgroups_per_grid as the n_tgs in the MPS_1D_KERNEL_LOOP to prevent multiple index values generated by the macro, when threadgroups_per_grid is larger than 1.
…m_coord kernels Modified the MPS_1D_KERNEL_LOOP to use the new threadgroups_per_grid
Substitutes addmm_ with addmm in the forward pass because addmm_ failes for weight groups > 1 (see comment in the code)
test_forward is passing test_backward is failing
Getting rid of redundant contiguous conversions of tensors.
Skipping backward test for batch_sz == 0
Removed temporary debug functions Removed redundant comments Temporary substitution of .addmm op with addop. Clean-up of debug std::cout statements
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/vision/9026
Note: Links to docs will display an error until the docs builds have been completed. ❗ 1 Active SEVsThere are 1 currently active SEVs. If your PR is affected, please view them below: This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
Didn't find following labels among repository labels: topic: not user facing |
Author
|
@pytorchbot label "enhancement" |
Author
|
@pytorchbot label "topic: not user facing" |
|
Didn't find following labels among repository labels: topic: not user facing |
|
I test it,it is very slow: Running on mps |
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.
DeformConV2 MPS Support
This PR feature a full MPS implementation of the DeformConV2 operator.
Generel notes
For consistency and maintainability, this implementation is in many ways similar to the CPU and CUDA implementations, with the obvious exceptions relating to the difference in their frameworks. The Metal part of the implementation is likewise similar to the implementations for the “ROI” related kernel implementations.
Tests
The implementation passes all tests in test_ops.py:TestDeformConv except for the two optests.generate_opcheck_test:
"test_autograd_registration"
and
"test_aot_dispatch_dynamic"
"test_autograd_registration" fails due to the missing MPS dispatch key in torch/testing/_internal/optests/autograd_registration.py
I have addressed this issue in a separate PR:
torch.testing._internal.optests - MPS Support #151758
test_aot_dispatch_dynamic fails due to issues that are not yet clear to me.
Issues - To be fixed
The CPU implementation: deform_conv2d_kernel.cpp is using the in-placed torch operator: .addmm.
However deform_conv2d_kernel.mm.
However, for reasons unknown to me, using .addmm in the MPS implementation, returns zero-value tensors after the first iteration in the convolution loop.
As a temporary solution, I have chosen to use the out-of-place version: addmm instead. This is not ideal and should be fixed.
MSL implementation - mps_kernels.h and mps_helpers.h implementations.
The implementation of the bilinear_interpolate function used by the “ROI”-related kernels is different from that used by the CPU and CUDA implementations of the deform_conv2 operator.
Currently, I have chosen to keep both implementations in mps_kernels.h and named the function: bilinear_interpolate_deform_conv2.
Suggestions
However, I suggest that mps_kernels.h be split into separate kernels, one for each ”ROI”-related operator, and one for the “deform_conv2” operator. Future implementations of ops should have their own separate kernel files.
This will not only be in keeping with the implementation design in Pytorch but also lead to safer and more maintainable code in the long run.
I also suggest that any common utility functions and constants found in mps_kernels.h be moved into mps_helpers.h in the future. Also, maybe consider renaming mps_helpers.h to a more generic name.