Native PyTorch farthest point sampling for point cloud workloads
Project description
torch-fps
Optimized standard farthest point sampling (FPS) for PyTorch written in C++.
pip install torch-fps
Note: Ensure gcc > 9 and < 14. Install might take a while since its building from source (to be fixed in future).
Usage
import torch
from torch_fps import farthest_point_sampling, farthest_point_sampling_with_knn
# Create example inputs
points = torch.randn(4, 1000, 3) # [B, N, D] - batch of point clouds
mask = torch.ones(4, 1000, dtype=torch.bool) # [B, N] - valid point mask
K = 512 # Number of samples per batch (must be <= number of valid points)
# Perform farthest point sampling
idx = farthest_point_sampling(points, mask, K) # [B, K] - selected point indices
# Use indices to gather sampled points
sampled_points = points.gather(1, idx.unsqueeze(-1).expand(-1, -1, 3)) # [B, K, D]
# Fused FPS + kNN: get centroids and their k nearest neighbors in one pass
centroid_idx, neighbor_idx = farthest_point_sampling_with_knn(
points, mask, K=512, k_neighbors=32
) # centroid_idx: [B, K], neighbor_idx: [B, K, k_neighbors]
Performance
Benchmarked on AMD Threadripper 7970X and NVIDIA RTX 5090. Values show CPU / CUDA measurements. By default uses float32; override with precision= parameter.
Numbers below come from the in-repo benchmark script (python tests/profile.py) against the local extension build.
FPS:
| B | N | K | Baseline (ms) | Optimized (ms) | Speedup |
|---|---|---|---|---|---|
| 4 | 100 | 20 | 0.45 / 1.38 | 0.05 / 0.10 | 9.50x / 13.70x |
| 8 | 512 | 64 | 2.88 / 4.05 | 0.11 / 0.17 | 25.36x / 23.89x |
| 16 | 1024 | 128 | 29.92 / 7.81 | 0.39 / 0.31 | 77.57x / 25.42x |
| 32 | 2048 | 256 | 154.44 / 15.59 | 1.52 / 0.74 | 101.92x / 21.16x |
FPS+kNN:
| B | N | K | k | Baseline (ms) | Optimized (ms) | Speedup |
|---|---|---|---|---|---|---|
| 4 | 100 | 16 | 8 | 0.50 / 1.21 | 0.05 / 0.21 | 9.98x / 5.80x |
| 8 | 512 | 64 | 16 | 4.90 / 4.11 | 0.21 / 1.13 | 23.56x / 3.65x |
| 16 | 1024 | 128 | 16 | 37.60 / 8.08 | 0.80 / 2.24 | 46.88x / 3.60x |
| 32 | 2048 | 256 | 16 | 180.33 / 16.86 | 2.57 / 4.84 | 70.24x / 3.48x |
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_fps-0.3.tar.gz
(19.3 kB
view details)
File details
Details for the file torch_fps-0.3.tar.gz.
File metadata
- Download URL: torch_fps-0.3.tar.gz
- Upload date:
- Size: 19.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7a4343e1f3951f1138b221b54bf5527e93cb1e863673fbebfb58634120eab362
|
|
| MD5 |
3a4c4f37c7bf0cc1de465281c3791a94
|
|
| BLAKE2b-256 |
7f71ea1786b527e58715f085a0d156bea1ba791ed151a2fc944a303b6eb9bac4
|