Skip to content

Deform conv2d mps support #9026

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

Open
wants to merge 34 commits into
base: main
Choose a base branch
from

Conversation

goldfishsound
Copy link

DeformConV2 MPS Support

This PR feature a full MPS implementation of the DeformConV2 operator. 

  1. 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.

  2. 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. 

  3. 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.

  4. 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.

  5. 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.

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
Copy link

pytorch-bot bot commented Apr 20, 2025

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

There 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.

Copy link

pytorch-bot bot commented Apr 20, 2025

Didn't find following labels among repository labels: topic: not user facing

@goldfishsound
Copy link
Author

@pytorchbot label "enhancement"
@pytorchbot label "module: ops"
@pytorchbot label "module: vision"

@goldfishsound
Copy link
Author

@pytorchbot label "topic: not user facing"

Copy link

pytorch-bot bot commented Apr 21, 2025

Didn't find following labels among repository labels: topic: not user facing

@chenjingcheng
Copy link

I test it,it is very slow:
Running on cpu
Time: 0.005s
Output: torch.Size([1, 8, 128, 128])

Running on mps
Time: 0.292s
Output: torch.Size([1, 8, 128, 128])
I built it from source code.

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