Neighbor Functionals#

KNN#

physicsnemo.nn.functional.knn(
points: Float[Tensor, 'num_points dim'],
queries: Float[Tensor, 'num_queries dim'],
k: int,
) tuple[Int[Tensor, 'num_queries k'], Float[Tensor, 'num_queries k']]#

Perform a k-nearest neighbor search on torch tensors. Can be done with torch directly, or leverage RAPIDS cuML algorithm.

Auto-dispatch selects the optimal version for the input tensor device.

Parameters:
  • points (torch.Tensor) – Tensor of shape (N, D) containing the points to search from.

  • queries (torch.Tensor) – Tensor of shape (M, D) containing the points to search for.

  • k (int) – Number of nearest neighbors to return for each query point.

  • implementation ({"cuml", "torch", "scipy"} or None) – Implementation to use for the search. When None, the preferred implementation for the input device is selected and falls back to torch when unavailable.

Returns:

  • indices (torch.Tensor) – Tensor of shape (M, k) containing the indices of the k nearest neighbors for each query point.

  • distances (torch.Tensor) – Tensor of shape (M, k) containing the distances to the k nearest neighbors for each query point.