voxelize_op.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright (c) 2024 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/extension.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>
  78. hard_voxelize_cpu(const paddle::Tensor &points,
  79. const std::vector<float> &voxel_size,
  80. const std::vector<float> &point_cloud_range,
  81. const int max_num_points_in_voxel, const int max_voxels) {
  82. auto num_points = points.shape()[0];
  83. auto num_point_dim = points.shape()[1];
  84. const float voxel_size_x = voxel_size[0];
  85. const float voxel_size_y = voxel_size[1];
  86. const float voxel_size_z = voxel_size[2];
  87. const float point_cloud_range_x_min = point_cloud_range[0];
  88. const float point_cloud_range_y_min = point_cloud_range[1];
  89. const float point_cloud_range_z_min = point_cloud_range[2];
  90. int grid_size_x = static_cast<int>(
  91. round((point_cloud_range[3] - point_cloud_range[0]) / voxel_size_x));
  92. int grid_size_y = static_cast<int>(
  93. round((point_cloud_range[4] - point_cloud_range[1]) / voxel_size_y));
  94. int grid_size_z = static_cast<int>(
  95. round((point_cloud_range[5] - point_cloud_range[2]) / voxel_size_z));
  96. auto voxels =
  97. paddle::empty({max_voxels, max_num_points_in_voxel, num_point_dim},
  98. paddle::DataType::FLOAT32, paddle::CPUPlace());
  99. auto coords = paddle::full({max_voxels, 3}, 0, paddle::DataType::INT32,
  100. paddle::CPUPlace());
  101. auto *coords_data = coords.data<int>();
  102. auto num_points_per_voxel = paddle::full(
  103. {max_voxels}, 0, paddle::DataType::INT32, paddle::CPUPlace());
  104. auto *num_points_per_voxel_data = num_points_per_voxel.data<int>();
  105. std::fill(num_points_per_voxel_data,
  106. num_points_per_voxel_data + num_points_per_voxel.size(),
  107. static_cast<int>(0));
  108. auto num_voxels =
  109. paddle::full({1}, 0, paddle::DataType::INT32, paddle::CPUPlace());
  110. auto *num_voxels_data = num_voxels.data<int>();
  111. auto grid_idx_to_voxel_idx =
  112. paddle::full({grid_size_z, grid_size_y, grid_size_x}, -1,
  113. paddle::DataType::INT32, paddle::CPUPlace());
  114. auto *grid_idx_to_voxel_idx_data = grid_idx_to_voxel_idx.data<int>();
  115. PD_DISPATCH_FLOATING_TYPES(
  116. points.type(), "hard_voxelize_cpu_kernel", ([&] {
  117. hard_voxelize_cpu_kernel<data_t, int>(
  118. points.data<data_t>(), point_cloud_range_x_min,
  119. point_cloud_range_y_min, point_cloud_range_z_min, voxel_size_x,
  120. voxel_size_y, voxel_size_z, grid_size_x, grid_size_y, grid_size_z,
  121. num_points, num_point_dim, max_num_points_in_voxel, max_voxels,
  122. voxels.data<data_t>(), coords_data, num_points_per_voxel_data,
  123. grid_idx_to_voxel_idx_data, num_voxels_data);
  124. }));
  125. return {voxels, coords, num_points_per_voxel, num_voxels};
  126. }
  127. #ifdef PADDLE_WITH_CUDA
  128. std::vector<paddle::Tensor>
  129. hard_voxelize_cuda(const paddle::Tensor &points,
  130. const std::vector<float> &voxel_size,
  131. const std::vector<float> &point_cloud_range,
  132. int max_num_points_in_voxel, int max_voxels);
  133. #endif
  134. std::vector<paddle::Tensor>
  135. hard_voxelize(const paddle::Tensor &points,
  136. const std::vector<float> &voxel_size,
  137. const std::vector<float> &point_cloud_range,
  138. const int max_num_points_in_voxel, const int max_voxels) {
  139. if (points.is_cpu()) {
  140. return hard_voxelize_cpu(points, voxel_size, point_cloud_range,
  141. max_num_points_in_voxel, max_voxels);
  142. #ifdef PADDLE_WITH_CUDA
  143. } else if (points.is_gpu() || points.is_gpu_pinned()) {
  144. return hard_voxelize_cuda(points, voxel_size, point_cloud_range,
  145. max_num_points_in_voxel, max_voxels);
  146. #endif
  147. } else {
  148. PD_THROW("Unsupported device type for hard_voxelize "
  149. "operator.");
  150. }
  151. }
  152. std::vector<std::vector<int64_t>>
  153. HardInferShape(std::vector<int64_t> points_shape,
  154. const std::vector<float> &voxel_size,
  155. const std::vector<float> &point_cloud_range,
  156. const int &max_num_points_in_voxel, const int &max_voxels) {
  157. return {{max_voxels, max_num_points_in_voxel, points_shape[1]},
  158. {max_voxels, 3},
  159. {max_voxels},
  160. {1}};
  161. }
  162. std::vector<paddle::DataType> HardInferDtype(paddle::DataType points_dtype) {
  163. return {points_dtype, paddle::DataType::INT32, paddle::DataType::INT32,
  164. paddle::DataType::INT32};
  165. }
  166. PD_BUILD_OP(hard_voxelize)
  167. .Inputs({"POINTS"})
  168. .Outputs({"VOXELS", "COORS", "NUM_POINTS_PER_VOXEL", "num_voxels"})
  169. .SetKernelFn(PD_KERNEL(hard_voxelize))
  170. .Attrs({"voxel_size: std::vector<float>",
  171. "point_cloud_range: std::vector<float>",
  172. "max_num_points_in_voxel: int", "max_voxels: int"})
  173. .SetInferShapeFn(PD_INFER_SHAPE(HardInferShape))
  174. .SetInferDtypeFn(PD_INFER_DTYPE(HardInferDtype));