Skip to content

Commit 153ea08

Browse files
author
humanpose1
committed
fix segfault cpu
1 parent edb7176 commit 153ea08

File tree

4 files changed

+47
-12
lines changed

4 files changed

+47
-12
lines changed

cpu/include/cloud.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ struct PointCloud
5757
pts = temp;
5858
}
5959
void set_batch(std::vector<scalar_t> new_pts, int begin, int size){
60+
6061
std::vector<PointXYZ> temp(size);
6162
for(int i=0; i < size; i++){
6263
PointXYZ point;

cpu/src/neighbors.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,24 +190,30 @@ int batch_nanoflann_neighbors (vector<scalar_t>& queries,
190190
search_params.sorted = true;
191191
for (auto& p0 : query_pcd.pts){
192192
// Check if we changed batch
193-
194-
if (i0 == sum_qb + q_batches[b]){
193+
if (i0 == sum_qb + q_batches[b] && b < s_batches.size()){
195194
sum_qb += q_batches[b];
196195
sum_sb += s_batches[b];
196+
197197
b++;
198198

199199
// Change the points
200200
current_cloud.pts.clear();
201201
current_cloud.set_batch(supports, sum_sb, s_batches[b]);
202202
// Build KDTree of the current element of the batch
203203
delete index;
204+
204205
index = new my_kd_tree_t(3, current_cloud, tree_params);
205206
index->buildIndex();
206207
}
207208
// Initial guess of neighbors size
209+
210+
208211
all_inds_dists[i0].reserve(max_count);
209212
// Find neighbors
213+
//std::cerr << p0.x << p0.y << p0.z<<std::endl;
210214
scalar_t query_pt[3] = { p0.x, p0.y, p0.z};
215+
216+
211217
size_t nMatches = index->radiusSearch(query_pt, r2, all_inds_dists[i0], search_params);
212218
// Update max count
213219

@@ -221,8 +227,10 @@ int batch_nanoflann_neighbors (vector<scalar_t>& queries,
221227
max_count = max_num;
222228
}
223229
// Reserve the memory
230+
224231
if(mode == 0){
225232
neighbors_indices.resize(query_pcd.pts.size() * max_count);
233+
226234
dists.resize(query_pcd.pts.size() * max_count);
227235
i0 = 0;
228236
sum_sb = 0;
@@ -231,6 +239,7 @@ int batch_nanoflann_neighbors (vector<scalar_t>& queries,
231239

232240
for (auto& inds_dists : all_inds_dists){// Check if we changed batch
233241

242+
234243
if (i0 == sum_qb + q_batches[b]){
235244
sum_qb += q_batches[b];
236245
sum_sb += s_batches[b];

cpu/src/torch_nearest_neighbors.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,22 @@ std::pair<at::Tensor, at::Tensor> ball_query(at::Tensor query,
6161

6262
void cumsum(const vector<long>& batch, vector<long>& res){
6363

64-
res.resize(batch[batch.size()-1]-batch[0]+1, 0);
64+
res.resize(batch[batch.size()-1]-batch[0]+2, 0);
6565
long ind = batch[0];
6666
long incr = 1;
67-
for(int i=1; i < batch.size(); i++){
68-
69-
if(batch[i] == ind)
70-
incr++;
71-
else{
72-
res[ind-batch[0]] = incr;
73-
incr =1;
74-
ind = batch[i];
67+
if(res.size() > 1){
68+
for(int i=1; i < batch.size(); i++){
69+
if(batch[i] == ind)
70+
incr++;
71+
else{
72+
res[ind-batch[0]+1] = incr;
73+
incr =1;
74+
ind = batch[i];
75+
}
7576
}
77+
7678
}
77-
res[ind-batch[0]] = incr;
79+
res[ind-batch[0]+1] = incr;
7880
}
7981

8082
std::pair<at::Tensor, at::Tensor> batch_ball_query(at::Tensor query,
@@ -89,9 +91,11 @@ std::pair<at::Tensor, at::Tensor> batch_ball_query(at::Tensor query,
8991
std::vector<long> query_batch_stl = std::vector<long>(data_qb, data_qb+query_batch.size(0));
9092
std::vector<long> cumsum_query_batch_stl;
9193
cumsum(query_batch_stl, cumsum_query_batch_stl);
94+
9295
std::vector<long> support_batch_stl = std::vector<long>(data_sb, data_sb+support_batch.size(0));
9396
std::vector<long> cumsum_support_batch_stl;
9497
cumsum(support_batch_stl, cumsum_support_batch_stl);
98+
9599
std::vector<long> neighbors_indices;
96100

97101
auto options = torch::TensorOptions().dtype(torch::kLong).device(torch::kCPU);
@@ -107,6 +111,7 @@ std::pair<at::Tensor, at::Tensor> batch_ball_query(at::Tensor query,
107111
std::vector<scalar_t> supports_stl = std::vector<scalar_t>(data_s,
108112
data_s + support.size(0)*support.size(1));
109113

114+
110115
max_count = batch_nanoflann_neighbors<scalar_t>(queries_stl,
111116
supports_stl,
112117
cumsum_query_batch_stl,
@@ -117,6 +122,7 @@ std::pair<at::Tensor, at::Tensor> batch_ball_query(at::Tensor query,
117122
max_num,
118123
mode);
119124
});
125+
120126
long* neighbors_indices_ptr = neighbors_indices.data();
121127
auto neighbors_dists_ptr = neighbors_dists.data();
122128

test/test_ballquerry_partial.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import unittest
22
import torch
33
from torch_points import ball_query
4+
from torch_points.points_cpu import ball_query as cpu_ball_query
45
from torch_cluster import radius_cuda
56
import numpy.testing as npt
67
import numpy as np
@@ -26,6 +27,7 @@ def test_simple_gpu(self):
2627
npt.assert_array_almost_equal(idx, idx_answer)
2728
npt.assert_array_almost_equal(dist2, dist2_answer)
2829

30+
2931
def test_simple_cpu(self):
3032
x = torch.tensor([[10, 0, 0], [0.1, 0, 0], [10, 0, 0], [0.1, 0, 0]]).to(torch.float)
3133
y = torch.tensor([[0, 0, 0]]).to(torch.float)
@@ -46,5 +48,22 @@ def test_simple_cpu(self):
4648
npt.assert_array_almost_equal(idx, idx_answer)
4749
npt.assert_array_almost_equal(dist2, dist2_answer)
4850

51+
def test_random_cpu(self):
52+
a = torch.randn(1000, 3).to(torch.float)
53+
b = torch.randn(1500, 3).to(torch.float)
54+
batch_a = torch.randint(1, (1000,)).sort(0)[0].long()
55+
batch_b = torch.randint(1, (1500,)).sort(0)[0].long()
56+
idx, dist2 = ball_query(1.0, 12, a, b, batch_a, batch_b, mode="PARTIAL_DENSE")
57+
idx, dist2 = ball_query(1.0, 12, b, a, batch_b, batch_a, mode="PARTIAL_DENSE")
58+
idx = idx.detach().cpu().numpy()
59+
dist2 = dist2.detach().cpu().numpy()
60+
idx2, _ = cpu_ball_query(a, b, 1.0, 12)
61+
print(idx[5], print(idx2[5]))
62+
63+
64+
65+
66+
67+
4968
if __name__ == "__main__":
5069
unittest.main()

0 commit comments

Comments
 (0)