voxelize_op.cc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <vector>
  15. #include "paddle/include/experimental/ext_all.h"
  16. template <typename T, typename T_int>
  17. bool hard_voxelize_cpu_kernel(
  18. const T *points, const float point_cloud_range_x_min,
  19. const float point_cloud_range_y_min, const float point_cloud_range_z_min,
  20. const float voxel_size_x, const float voxel_size_y,
  21. const float voxel_size_z, const int grid_size_x, const int grid_size_y,
  22. const int grid_size_z, const int64_t num_points, const int num_point_dim,
  23. const int max_num_points_in_voxel, const int max_voxels, T *voxels,
  24. T_int *coords, T_int *num_points_per_voxel, T_int *grid_idx_to_voxel_idx,
  25. T_int *num_voxels) {
  26. std::fill(voxels,
  27. voxels + max_voxels * max_num_points_in_voxel * num_point_dim,
  28. static_cast<T>(0));
  29. num_voxels[0] = 0;
  30. int voxel_idx, grid_idx, curr_num_point;
  31. int coord_x, coord_y, coord_z;
  32. for (int point_idx = 0; point_idx < num_points; ++point_idx) {
  33. coord_x = floor(
  34. (points[point_idx * num_point_dim + 0] - point_cloud_range_x_min) /
  35. voxel_size_x);
  36. coord_y = floor(
  37. (points[point_idx * num_point_dim + 1] - point_cloud_range_y_min) /
  38. voxel_size_y);
  39. coord_z = floor(
  40. (points[point_idx * num_point_dim + 2] - point_cloud_range_z_min) /
  41. voxel_size_z);
  42. if (coord_x < 0 || coord_x > grid_size_x || coord_x == grid_size_x) {
  43. continue;
  44. }
  45. if (coord_y < 0 || coord_y > grid_size_y || coord_y == grid_size_y) {
  46. continue;
  47. }
  48. if (coord_z < 0 || coord_z > grid_size_z || coord_z == grid_size_z) {
  49. continue;
  50. }
  51. grid_idx =
  52. coord_z * grid_size_y * grid_size_x + coord_y * grid_size_x + coord_x;
  53. voxel_idx = grid_idx_to_voxel_idx[grid_idx];
  54. if (voxel_idx == -1) {
  55. voxel_idx = num_voxels[0];
  56. if (num_voxels[0] == max_voxels || num_voxels[0] > max_voxels) {
  57. continue;
  58. }
  59. num_voxels[0]++;
  60. grid_idx_to_voxel_idx[grid_idx] = voxel_idx;
  61. coords[voxel_idx * 3 + 0] = coord_z;
  62. coords[voxel_idx * 3 + 1] = coord_y;
  63. coords[voxel_idx * 3 + 2] = coord_x;
  64. }
  65. curr_num_point = num_points_per_voxel[voxel_idx];
  66. if (curr_num_point < max_num_points_in_voxel) {
  67. for (int j = 0; j < num_point_dim; ++j) {
  68. voxels[voxel_idx * max_num_points_in_voxel * num_point_dim +
  69. curr_num_point * num_point_dim + j] =
  70. points[point_idx * num_point_dim + j];
  71. }
  72. num_points_per_voxel[voxel_idx] = curr_num_point + 1;
  73. }
  74. }
  75. return true;
  76. }
  77. std::vector<paddle::Tensor> hard_voxelize_cpu(
  78. const paddle::Tensor &points, const std::vector<float> &voxel_size,
  79. const std::vector<float> &point_cloud_range,
  80. const int max_num_points_in_voxel, const int max_voxels) {
  81. auto num_points = points.shape()[0];
  82. auto num_point_dim = points.shape()[1];
  83. const float voxel_size_x = voxel_size[0];
  84. const float voxel_size_y = voxel_size[1];
  85. const float voxel_size_z = voxel_size[2];
  86. const float point_cloud_range_x_min = point_cloud_range[0];
  87. const float point_cloud_range_y_min = point_cloud_range[1];
  88. const float point_cloud_range_z_min = point_cloud_range[2];
  89. int grid_size_x = static_cast<int>(
  90. round((point_cloud_range[3] - point_cloud_range[0]) / voxel_size_x));
  91. int grid_size_y = static_cast<int>(
  92. round((point_cloud_range[4] - point_cloud_range[1]) / voxel_size_y));
  93. int grid_size_z = static_cast<int>(
  94. round((point_cloud_range[5] - point_cloud_range[2]) / voxel_size_z));
  95. auto voxels =
  96. paddle::empty({max_voxels, max_num_points_in_voxel, num_point_dim},
  97. paddle::DataType::FLOAT32, paddle::CPUPlace());
  98. auto coords = paddle::full({max_voxels, 3}, 0, paddle::DataType::INT32,
  99. paddle::CPUPlace());
  100. auto *coords_data = coords.data<int>();
  101. auto num_points_per_voxel = paddle::full(
  102. {max_voxels}, 0, paddle::DataType::INT32, paddle::CPUPlace());
  103. auto *num_points_per_voxel_data = num_points_per_voxel.data<int>();
  104. std::fill(num_points_per_voxel_data,
  105. num_points_per_voxel_data + num_points_per_voxel.size(),
  106. static_cast<int>(0));
  107. auto num_voxels =
  108. paddle::full({1}, 0, paddle::DataType::INT32, paddle::CPUPlace());
  109. auto *num_voxels_data = num_voxels.data<int>();
  110. auto grid_idx_to_voxel_idx =
  111. paddle::full({grid_size_z, grid_size_y, grid_size_x}, -1,
  112. paddle::DataType::INT32, paddle::CPUPlace());
  113. auto *grid_idx_to_voxel_idx_data = grid_idx_to_voxel_idx.data<int>();
  114. PD_DISPATCH_FLOATING_TYPES(
  115. points.type(), "hard_voxelize_cpu_kernel", ([&] {
  116. hard_voxelize_cpu_kernel<data_t, int>(
  117. points.data<data_t>(), point_cloud_range_x_min,
  118. point_cloud_range_y_min, point_cloud_range_z_min, voxel_size_x,
  119. voxel_size_y, voxel_size_z, grid_size_x, grid_size_y, grid_size_z,
  120. num_points, num_point_dim, max_num_points_in_voxel, max_voxels,
  121. voxels.data<data_t>(), coords_data, num_points_per_voxel_data,
  122. grid_idx_to_voxel_idx_data, num_voxels_data);
  123. }));
  124. return {voxels, coords, num_points_per_voxel, num_voxels};
  125. }
  126. #ifdef PADDLE_WITH_CUDA
  127. std::vector<paddle::Tensor> hard_voxelize_cuda(
  128. const paddle::Tensor &points, const std::vector<float> &voxel_size,
  129. const std::vector<float> &point_cloud_range, int max_num_points_in_voxel,
  130. int max_voxels);
  131. #endif
  132. std::vector<paddle::Tensor> hard_voxelize(
  133. const paddle::Tensor &points, const std::vector<float> &voxel_size,
  134. const std::vector<float> &point_cloud_range,
  135. const int max_num_points_in_voxel, const int max_voxels) {
  136. if (points.is_cpu()) {
  137. return hard_voxelize_cpu(points, voxel_size, point_cloud_range,
  138. max_num_points_in_voxel, max_voxels);
  139. #ifdef PADDLE_WITH_CUDA
  140. } else if (points.is_gpu() || points.is_gpu_pinned()) {
  141. return hard_voxelize_cuda(points, voxel_size, point_cloud_range,
  142. max_num_points_in_voxel, max_voxels);
  143. #endif
  144. } else {
  145. PD_THROW(
  146. "Unsupported device type for hard_voxelize "
  147. "operator.");
  148. }
  149. }
  150. std::vector<std::vector<int64_t>> HardInferShape(
  151. std::vector<int64_t> points_shape, const std::vector<float> &voxel_size,
  152. const std::vector<float> &point_cloud_range,
  153. const int &max_num_points_in_voxel, const int &max_voxels) {
  154. return {{max_voxels, max_num_points_in_voxel, points_shape[1]},
  155. {max_voxels, 3},
  156. {max_voxels},
  157. {1}};
  158. }
  159. std::vector<paddle::DataType> HardInferDtype(paddle::DataType points_dtype) {
  160. return {points_dtype, paddle::DataType::INT32, paddle::DataType::INT32,
  161. paddle::DataType::INT32};
  162. }
  163. PD_BUILD_OP(hard_voxelize)
  164. .Inputs({"POINTS"})
  165. .Outputs({"VOXELS", "COORS", "NUM_POINTS_PER_VOXEL", "num_voxels"})
  166. .SetKernelFn(PD_KERNEL(hard_voxelize))
  167. .Attrs({"voxel_size: std::vector<float>",
  168. "point_cloud_range: std::vector<float>",
  169. "max_num_points_in_voxel: int", "max_voxels: int"})
  170. .SetInferShapeFn(PD_INFER_SHAPE(HardInferShape))
  171. .SetInferDtypeFn(PD_INFER_DTYPE(HardInferDtype));