mirror of
https://github.com/facebookresearch/pytorch3d.git
synced 2025-12-19 22:00:35 +08:00
remove nearest_neighbors
Summary: knn is more general and faster than the nearest_neighbor code, so remove the latter. Reviewed By: gkioxari Differential Revision: D20816424 fbshipit-source-id: 75d6c44d17180752d0c9859814bbdf7892558158
This commit is contained in:
committed by
Facebook GitHub Bot
parent
790eb8c402
commit
3794f6753f
@@ -1,42 +0,0 @@
|
||||
# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
|
||||
|
||||
from itertools import product
|
||||
|
||||
import torch
|
||||
from fvcore.common.benchmark import benchmark
|
||||
from test_nearest_neighbor_points import TestNearestNeighborPoints
|
||||
|
||||
|
||||
def bm_nn_points() -> None:
|
||||
kwargs_list = []
|
||||
|
||||
N = [1, 4, 32]
|
||||
D = [3, 4]
|
||||
P1 = [1, 128]
|
||||
P2 = [32, 128]
|
||||
test_cases = product(N, D, P1, P2)
|
||||
for case in test_cases:
|
||||
n, d, p1, p2 = case
|
||||
kwargs_list.append({"N": n, "D": d, "P1": p1, "P2": p2})
|
||||
|
||||
benchmark(
|
||||
TestNearestNeighborPoints.bm_nn_points_python_with_init,
|
||||
"NN_PYTHON",
|
||||
kwargs_list,
|
||||
warmup_iters=1,
|
||||
)
|
||||
|
||||
benchmark(
|
||||
TestNearestNeighborPoints.bm_nn_points_cpu_with_init,
|
||||
"NN_CPU",
|
||||
kwargs_list,
|
||||
warmup_iters=1,
|
||||
)
|
||||
|
||||
if torch.cuda.is_available():
|
||||
benchmark(
|
||||
TestNearestNeighborPoints.bm_nn_points_cuda_with_init,
|
||||
"NN_CUDA",
|
||||
kwargs_list,
|
||||
warmup_iters=1,
|
||||
)
|
||||
@@ -1,91 +0,0 @@
|
||||
# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
|
||||
|
||||
import unittest
|
||||
|
||||
import torch
|
||||
from pytorch3d import _C
|
||||
|
||||
|
||||
class TestNearestNeighborPoints(unittest.TestCase):
|
||||
@staticmethod
|
||||
def nn_points_idx_naive(x, y):
|
||||
"""
|
||||
PyTorch implementation of nn_points_idx function.
|
||||
"""
|
||||
N, P1, D = x.shape
|
||||
_N, P2, _D = y.shape
|
||||
assert N == _N and D == _D
|
||||
diffs = x.view(N, P1, 1, D) - y.view(N, 1, P2, D)
|
||||
dists2 = (diffs * diffs).sum(3)
|
||||
idx = dists2.argmin(2)
|
||||
return idx
|
||||
|
||||
def _test_nn_helper(self, device):
|
||||
for D in [3, 4]:
|
||||
for N in [1, 4]:
|
||||
for P1 in [1, 8, 64, 128]:
|
||||
for P2 in [32, 128]:
|
||||
x = torch.randn(N, P1, D, device=device)
|
||||
y = torch.randn(N, P2, D, device=device)
|
||||
|
||||
# _C.nn_points_idx should dispatch
|
||||
# to the cpp or cuda versions of the function
|
||||
# depending on the input type.
|
||||
idx1 = _C.nn_points_idx(x, y)
|
||||
idx2 = TestNearestNeighborPoints.nn_points_idx_naive(x, y)
|
||||
self.assertTrue(idx1.size(1) == P1)
|
||||
self.assertTrue(torch.all(idx1 == idx2))
|
||||
|
||||
def test_nn_cuda(self):
|
||||
"""
|
||||
Test cuda output vs naive python implementation.
|
||||
"""
|
||||
device = torch.device("cuda:0")
|
||||
self._test_nn_helper(device)
|
||||
|
||||
def test_nn_cpu(self):
|
||||
"""
|
||||
Test cpu output vs naive python implementation
|
||||
"""
|
||||
device = torch.device("cpu")
|
||||
self._test_nn_helper(device)
|
||||
|
||||
@staticmethod
|
||||
def bm_nn_points_cpu_with_init(
|
||||
N: int = 4, D: int = 4, P1: int = 128, P2: int = 128
|
||||
):
|
||||
device = torch.device("cpu")
|
||||
x = torch.randn(N, P1, D, device=device)
|
||||
y = torch.randn(N, P2, D, device=device)
|
||||
|
||||
def nn_cpu():
|
||||
_C.nn_points_idx(x.contiguous(), y.contiguous())
|
||||
|
||||
return nn_cpu
|
||||
|
||||
@staticmethod
|
||||
def bm_nn_points_cuda_with_init(
|
||||
N: int = 4, D: int = 4, P1: int = 128, P2: int = 128
|
||||
):
|
||||
device = torch.device("cuda:0")
|
||||
x = torch.randn(N, P1, D, device=device)
|
||||
y = torch.randn(N, P2, D, device=device)
|
||||
torch.cuda.synchronize()
|
||||
|
||||
def nn_cpp():
|
||||
_C.nn_points_idx(x.contiguous(), y.contiguous())
|
||||
torch.cuda.synchronize()
|
||||
|
||||
return nn_cpp
|
||||
|
||||
@staticmethod
|
||||
def bm_nn_points_python_with_init(
|
||||
N: int = 4, D: int = 4, P1: int = 128, P2: int = 128
|
||||
):
|
||||
x = torch.randn(N, P1, D)
|
||||
y = torch.randn(N, P2, D)
|
||||
|
||||
def nn_python():
|
||||
TestNearestNeighborPoints.nn_points_idx_naive(x, y)
|
||||
|
||||
return nn_python
|
||||
Reference in New Issue
Block a user