Skip to main content

CARAFE content-aware upsampling for Apple Silicon (MPS)

Project description

MPS CARAFE

CARAFE (Content-Aware ReAssembly of FEatures) for Apple Silicon (M1/M2/M3/M4).

Drop-in replacement for mmcv's CARAFE op.

Why?

CARAFE is a learnable upsampling operator used in:

  • Mask R-CNN: Instance segmentation
  • FPN: Feature Pyramid Networks
  • YOLACT: Real-time instance segmentation

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

NotImplementedError: carafe not implemented for MPS

This package provides a native Metal implementation.

Installation

pip install mps-carafe

Or from source:

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

Quick Start

Basic CARAFE Operation

import torch
from mps_carafe import carafe

# Input features (N, C, H, W)
features = torch.randn(1, 64, 32, 32, device='mps')

# Reassembly masks (N, group_size * k^2, H*scale, W*scale)
kernel_size = 5
group_size = 1
scale_factor = 2
masks = torch.softmax(
    torch.randn(1, group_size * kernel_size**2, 64, 64, device='mps'),
    dim=1
)

# Upsample with CARAFE
output = carafe(features, masks, kernel_size, group_size, scale_factor)
# Output: (1, 64, 64, 64)

CARAFE Module

from mps_carafe import CARAFE

carafe_layer = CARAFE(kernel_size=5, group_size=1, scale_factor=2)
output = carafe_layer(features, masks)

CARAFEPack (with mask predictor)

from mps_carafe import CARAFEPack

# Complete upsampling block with built-in mask predictor
upsample = CARAFEPack(
    channels=64,
    kernel_size=5,
    group_size=1,
    scale_factor=2
).to('mps')

output = upsample(features)  # No need to provide masks

API Reference

carafe(features, masks, kernel_size, group_size, scale_factor)

Parameter Type Description
features Tensor Input features (N, C, H, W)
masks Tensor Reassembly kernels (N, group_size * k^2, Hscale, Wscale)
kernel_size int Size of reassembly kernel (typically 5)
group_size int Number of channel groups (typically 1)
scale_factor int Upsampling factor (typically 2)

CARAFEPack

Complete CARAFE block with mask prediction convolutions.

How It Works

CARAFE upsamples by:

  1. For each output pixel, identify the corresponding input neighborhood
  2. Use learned reassembly kernels (masks) to weight input pixels
  3. Sum the weighted inputs to produce the output

Unlike bilinear upsampling which uses fixed weights, CARAFE learns content-aware weights that adapt to the image content.

Compatibility

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

Features

  • Full forward and backward pass (training supported)
  • fp32 and fp16 supported
  • Compatible with mmcv CARAFE API

Credits

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mps_carafe-0.1.3.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mps_carafe-0.1.3-cp314-cp314-macosx_15_0_arm64.whl (85.1 kB view details)

Uploaded CPython 3.14macOS 15.0+ ARM64

File details

Details for the file mps_carafe-0.1.3.tar.gz.

File metadata

  • Download URL: mps_carafe-0.1.3.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for mps_carafe-0.1.3.tar.gz
Algorithm Hash digest
SHA256 6dda40e33e009afce0baff01e6a4c1b16cfd38d5e0cc2aae0c94d923854e1916
MD5 52bee99e382a404bffb0a0c873173f4f
BLAKE2b-256 d07e1e25f91d1477150a10ed11b0e3b4677bd8120cdad16c830a061a470f6a5a

See more details on using hashes here.

File details

Details for the file mps_carafe-0.1.3-cp314-cp314-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for mps_carafe-0.1.3-cp314-cp314-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 bff00b1dd64aabc7f9aa4901fa0c52f9ac5a0f5c2f66384d6a802bea2a774611
MD5 1eb95da9ac214d05ee2d05083bfc52cf
BLAKE2b-256 c04b7e31c3f89a2aef5b552ec350d08f3493fb0ce60c280653dead2888c22bba

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page