-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathcupy_knn_vs_pytorch3d_tests.py
189 lines (148 loc) · 6.46 KB
/
cupy_knn_vs_pytorch3d_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
from easyvolcap.utils.console_utils import *
from easyvolcap.utils.test_utils import my_tests
from easyvolcap.utils.data_utils import load_pts
from easyvolcap.utils.timer_utils import timer
import cupy
import torch
import cupy_knn
from pytorch3d.ops import knn_points
from torch.utils.dlpack import to_dlpack
from torch.utils.dlpack import from_dlpack
device = 'cuda'
pts = load_pts('assets/meshes/bunny.ply')[0] # only the vertices matter
pts = torch.as_tensor(pts, device=device)[None]
K = 10
timer.disabled = False
def test_torch_cupy_interop():
global pts
pts_cupy = cupy.asarray(pts)
print(pts_cupy)
assert pts.__cuda_array_interface__['data'][0] == pts_cupy.__cuda_array_interface__['data'][0]
pts = torch.as_tensor(pts_cupy, device=device)
assert pts.__cuda_array_interface__['data'][0] == pts_cupy.__cuda_array_interface__['data'][0]
def test_cupy_knn_results():
d2, idx, nn = knn_points(pts, pts, K=K, return_nn=False, return_sorted=True)
pts_cupy = cupy.asarray(pts)
lbvh = cupy_knn.LBVHIndex(leaf_size=32,
compact=False,
shrink_to_fit=False,
sort_queries=True)
lbvh.build(pts_cupy)
lbvh.prepare_knn_default(K, radius=None)
idx_cupy, d2_cupy, nn_cupy = lbvh.query_knn(pts_cupy)
idx_tensor = torch.as_tensor(idx_cupy.astype(cupy.int64), device=device)[None] # !: BATCH
d2_tensor = torch.as_tensor(d2_cupy, device=device)[None]
nn_tensor = torch.as_tensor(nn_cupy.astype(cupy.int64), device=device)[None]
torch.testing.assert_allclose(d2, d2_tensor)
torch.testing.assert_allclose(idx, idx_tensor)
def raw_cupy_knn_points(p1: torch.Tensor,
p2: torch.Tensor,
K: int = 1,
return_nn: bool = True,
return_sorted: bool = True,
leaf_size=32,
compact=False,
shrink_to_fit=False,
radius=None,
return_lbvh=False,
):
import cupy
import cupy_knn
# !: BATCH
p1_cupy = cupy.asarray(p1)
p2_cupy = cupy.asarray(p2)
lbvh = cupy_knn.LBVHIndex(leaf_size=leaf_size,
compact=compact,
shrink_to_fit=shrink_to_fit,
sort_queries=return_sorted)
lbvh.build(p1_cupy)
lbvh.prepare_knn_default(K, radius=radius)
idx_cupy, d2_cupy, nn_cupy = lbvh.query_knn(p2_cupy)
idx_tensor = torch.as_tensor(idx_cupy.astype(cupy.int64), device=p1.device)[None] # !: BATCH
d2_tensor = torch.as_tensor(d2_cupy, device=p1.device)[None]
nn_tensor = torch.as_tensor(nn_cupy.astype(cupy.int64), device=p1.device)[None]
if not return_lbvh:
return d2_tensor, idx_tensor, nn_tensor
else:
return d2_tensor, idx_tensor, nn_tensor, return_lbvh
def test_cupy_knn_points_results():
d2_0, idx_0, nn_0 = knn_points(pts, pts, K=K, return_nn=False, return_sorted=True)
d2_1, idx_1, nn_1 = raw_cupy_knn_points(pts, pts, K=K, return_nn=False, return_sorted=True)
torch.testing.assert_allclose(d2_0, d2_1)
torch.testing.assert_allclose(idx_0, idx_1)
repeat = 500
def caching_lbvh_constructor(
# These are required
p1: torch.Tensor,
return_sorted: bool = True,
leaf_size=32,
compact=False,
shrink_to_fit=False,
# These are dynamic
K: int = 1,
radius=None,
):
key = (p1.data_ptr(), return_sorted, leaf_size, compact, shrink_to_fit)
if key not in caching_lbvh_constructor.cache:
import cupy
import cupy_knn
p1_cupy = cupy.asarray(p1)
lbvh = cupy_knn.LBVHIndex(leaf_size=leaf_size,
compact=compact,
shrink_to_fit=shrink_to_fit,
sort_queries=return_sorted)
lbvh.build(p1_cupy)
caching_lbvh_constructor.cache[key] = lbvh
if len(caching_lbvh_constructor.cache) > caching_lbvh_constructor.maxsize:
# caching_lbvh_constructor.cache.popitem(last=False)
# Updated answer
# In the context of the question, we are dealing with pseudocode, but starting in Python 3.8, := is actually a valid operator that allows for assignment of variables within expressions:
# https://stackoverflow.com/questions/26000198/what-does-colon-equal-in-python-mean
(k := next(iter(caching_lbvh_constructor.cache)), caching_lbvh_constructor.cache.pop(k))
else:
lbvh = caching_lbvh_constructor.cache[key]
lbvh.prepare_knn_default(K, radius=radius)
return lbvh
caching_lbvh_constructor.maxsize = 128
caching_lbvh_constructor.cache = dotdict()
def cupy_knn_points(p1: torch.Tensor,
p2: torch.Tensor,
K: int = 1,
return_nn: bool = True,
return_sorted: bool = True,
leaf_size=1024,
compact=False,
shrink_to_fit=False,
radius=None,
return_lbvh=False,
):
import cupy
import cupy_knn
# !: BATCH
lbvh = caching_lbvh_constructor(p1, leaf_size, compact, shrink_to_fit, return_sorted, K, radius)
p2_cupy = cupy.asarray(p2)
idx_cupy, d2_cupy, nn_cupy = lbvh.query_knn(p2_cupy)
idx_tensor = torch.as_tensor(idx_cupy.astype(cupy.int64), device=p1.device)[None] # !: BATCH
d2_tensor = torch.as_tensor(d2_cupy, device=p1.device)[None]
nn_tensor = torch.as_tensor(nn_cupy.astype(cupy.int64), device=p1.device)[None]
if not return_lbvh:
return d2_tensor, idx_tensor, nn_tensor
else:
return d2_tensor, idx_tensor, nn_tensor, return_lbvh
def test_cupy_knn_pytorch3d_speed():
torch.cuda.synchronize()
timer.record('')
for i in range(repeat):
d2_0, idx_0, nn_0 = knn_points(pts, pts, K=K, return_nn=True, return_sorted=False)
torch.cuda.synchronize()
timer.record(f'PyTorch3D x {repeat}')
for i in range(repeat):
d2_0, idx_0, nn_0 = raw_cupy_knn_points(pts, pts, K=K, return_nn=True, return_sorted=True)
torch.cuda.synchronize()
timer.record(f'cupy-knn raw x {repeat}')
for i in range(repeat):
d2_0, idx_0, nn_0 = cupy_knn_points(pts, pts, K=K, return_nn=True, return_sorted=True)
torch.cuda.synchronize()
timer.record(f'cupy-knn cache x {repeat}')
if __name__ == '__main__':
my_tests(globals())