Skip to main content

Chamfer distance with Metal/MPS acceleration (macOS)

Project description

torch-chamfer-dist

torch-chamfer-dist provides a fast Chamfer distance implementation for PyTorch. On macOS with Metal/MPS it runs kd-tree nearest-neighbour queries directly on the GPU; elsewhere it falls back to an optimized CPU kd-tree. Autograd support is built in.

Installation

pip install torch-chamfer-dist

The provided wheel targets macOS 13+ (arm64 and x86_64). On other platforms the CPU backend is selected automatically.

Quick start

import torch
import chamfer

# Create two point clouds on the desired device ("mps" for Metal, "cpu" otherwise)
a = torch.rand(5_000, 3, device="mps")
b = torch.rand(5_000, 3, device="mps")

# Nearest neighbours via kd-tree
dist_idx, dist_sq = chamfer.closest_points(a, b)

# Chamfer distance with gradients
loss = chamfer.chamfer_distance(a, b)
loss.backward()

The device of the inputs determines the backend. When both tensors live on MPS the Metal kernel is used; otherwise a CPU kd-tree path runs. Gradients are computed on the same device without host roundtrips.

Benchmarks

The repository ships a benchmark script comparing brute-force, CPU kd-tree, and Metal kd-tree implementations. Example (20k points per cloud on an M2 Pro):

Method      | Forward           | Backward
------------+-------------------+------------------
Brute force | 0.885 s           | 1.829 s
KD-tree CPU | 0.139 s (6.39x)   | 0.269 s (6.79x)
KD-tree MPS | 0.008 s (115.31x) | 0.012 s (147.63x)

Run the benchmark locally:

PYTHONPATH=. python benchmarks/benchmark_chamfer.py --n 20000 --chunk 4096 --repeat 3

Set CHAMFER_PROFILE=1 to emit per-stage timings (tree build, kernel wait, etc.).

Development

  • Install dependencies: pip install torch nanobind pytest build.
  • Run tests: python -m pytest.
  • Build wheel: python -m build.

Publishing to PyPI

python -m pip install --upgrade build twine
python -m build
python -m twine upload dist/*

Remember to bump the version in pyproject.toml before tagging and uploading a release.

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

torch_chamfer_dist-0.1.1.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

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

torch_chamfer_dist-0.1.1-cp39-cp39-macosx_13_0_universal2.whl (310.2 kB view details)

Uploaded CPython 3.9macOS 13.0+ universal2 (ARM64, x86-64)

File details

Details for the file torch_chamfer_dist-0.1.1.tar.gz.

File metadata

  • Download URL: torch_chamfer_dist-0.1.1.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for torch_chamfer_dist-0.1.1.tar.gz
Algorithm Hash digest
SHA256 88d6f8cae62729bbdb9c8da3a012b0fd4b9b4c086f273976cc9853341d952f29
MD5 cf2a279d31a5cc987a318eb3144bdeac
BLAKE2b-256 f25576b003ac0fd6ce4f2de8f2e5ca8c48b5923c8982b6760404df4f4fa48ffc

See more details on using hashes here.

File details

Details for the file torch_chamfer_dist-0.1.1-cp39-cp39-macosx_13_0_universal2.whl.

File metadata

File hashes

Hashes for torch_chamfer_dist-0.1.1-cp39-cp39-macosx_13_0_universal2.whl
Algorithm Hash digest
SHA256 760af21cf0d2f8f7972903dafee2dde181d35fc80e81414368a070869f4f86b1
MD5 d740107b185fbb2934486ab74bd2e7dc
BLAKE2b-256 9b9f971fbd3b20152f407b58cc43fef1dc8598226e1bcbfedb41a35264a75737

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