Skip to main content

MPS Deformable Convolution

Deformable Convolution 2D for PyTorch on Apple Silicon (M1/M2/M3/M4).

Drop-in replacement for torchvision.ops.deform_conv2d that actually works on MPS.

Why?

Deformable convolutions are used everywhere:

  • Detection: DETR, Deformable DETR, mmdetection models
  • Video: BasicVSR++, EDVR, optical flow models
  • Segmentation: Mask R-CNN with DCN backbones

But torchvision's implementation is CUDA-only. On Mac you get:

NotImplementedError: deform_conv2d not implemented for MPS

This package provides a native Metal implementation.

Installation

pip install mps-deform-conv

Or from source:

git clone https://github.com/mpsops/mps-deform-conv
cd mps-deform-conv
pip install -e .

Quick Start

Basic Usage

import torch
from mps_deform_conv import deform_conv2d

# Input: (batch, channels, height, width)
input = torch.randn(1, 64, 32, 32, device='mps')

# Weight: (out_channels, in_channels, kernel_h, kernel_w)
weight = torch.randn(64, 64, 3, 3, device='mps')

# Offset: (batch, 2 * kernel_h * kernel_w, out_h, out_w)
# 2 values (dy, dx) for each position in the 3x3 kernel
offset = torch.randn(1, 2*9, 32, 32, device='mps')

# Run deformable convolution
output = deform_conv2d(input, offset, weight, padding=(1, 1))

DeformConv2d Module

from mps_deform_conv import DeformConv2d

# Create layer
conv = DeformConv2d(
    in_channels=64,
    out_channels=128,
    kernel_size=3,
    padding=1
).to('mps')

# Forward pass requires input and offset
x = torch.randn(1, 64, 32, 32, device='mps')
offset = torch.randn(1, 2*9, 32, 32, device='mps')
output = conv(x, offset)

ModulatedDeformConv2d (DCNv2)

Includes the offset predictor - a complete conv layer replacement:

from mps_deform_conv import ModulatedDeformConv2d

# DCNv2 with internal offset/mask prediction
conv = ModulatedDeformConv2d(
    in_channels=64,
    out_channels=128,
    kernel_size=3,
    padding=1
).to('mps')

# Just pass input - offsets learned internally
x = torch.randn(1, 64, 32, 32, device='mps')
output = conv(x)

API Reference

deform_conv2d(input, offset, weight, bias, stride, padding, dilation, mask)

Functional interface matching torchvision.ops.deform_conv2d.

Parameter Type Description
input Tensor Input tensor (N, C_in, H, W)
offset Tensor Offset tensor (N, 2*K*K*groups, H_out, W_out)
weight Tensor Weight tensor (C_out, C_in/groups, K, K)
bias Tensor Optional bias (C_out,)
stride tuple Convolution stride (default: (1, 1))
padding tuple Input padding (default: (0, 0))
dilation tuple Kernel dilation (default: (1, 1))
mask Tensor Optional DCNv2 mask (N, K*K*groups, H_out, W_out)

DeformConv2d

Module wrapping deform_conv2d. Takes input and offset in forward.

ModulatedDeformConv2d

Self-contained DCNv2 module with internal offset prediction. Takes only input in forward.

How It Works

Standard convolution samples on a fixed grid:

[•] [•] [•]
[•] [x] [•]
[•] [•] [•]

Deformable convolution learns offsets to sample from arbitrary positions:

    [•]
[•]     [•]
    [x]     [•]
[•]     [•]
    [•]

This lets the network adapt its receptive field to the input content - useful for detecting objects at different scales, handling geometric transformations, etc.

Compatibility

  • PyTorch: 2.0+
  • macOS: 12.0+ (Monterey)
  • Hardware: Apple Silicon (M1/M2/M3/M4)

Features

  • Full forward and backward pass (training supported)
  • Gradients verified against torchvision (< 0.00001 error)
  • fp32 and fp16 supported
  • Grouped convolutions supported

Credits

License

MIT

Metadata

Release files for mps-deform-conv 0.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mps-deform-conv 0.2.2
File Size Uploaded Provenance Actions
mps_deform_conv-0.2.2.tar.gz 24.0 kB Without provenance Details for mps_deform_conv-0.2.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mps-deform-conv 0.2.2
File Interpreter ABI Platform Actions
mps_deform_conv-0.2.2-cp314-cp314-macosx_15_0_arm64.whl CPython 3.14 CPython 3.14 macOS 15.0+ ARM64 Details for mps_deform_conv-0.2.2-cp314-cp314-macosx_15_0_arm64.whl

Total release size: 133.6 kB

Release files / mps_deform_conv-0.2.2.tar.gz

Download URL mps_deform_conv-0.2.2.tar.gz
Size 24.0 kB
Tags Source
SHA-256 checksum
How to use checksums
560659ba50f62f708c710a468174faccf88444fd7f5879c9390a354e054cd1d6
BLAKE2b-256 checksum
How to use checksums
2b6ba7a0d4d90a9bde62d321a32ff383d2c288df85ccf58715062c4aa89bc2d4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.0

Release files / mps_deform_conv-0.2.2-cp314-cp314-macosx_15_0_arm64.whl

Download URL mps_deform_conv-0.2.2-cp314-cp314-macosx_15_0_arm64.whl
Size 109.5 kB
Tags CPython 3.14 macOS 15.0+ ARM64
SHA-256 checksum
How to use checksums
aac92b51e46d63669d40b2cbbc6e498ff023994ed7dae9e50a7e43a30114b235
BLAKE2b-256 checksum
How to use checksums
14f035b6294921ae5c1777d3f4255884c2bdee4c1a33a9e38f060ad81ca429d8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.0

Security for mps-deform-conv 0.2.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page