ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ContinuousConv.cuh
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #define EIGEN_USE_GPU
11 
12 #include <Helper.h>
13 #include <cutlass/gemm/gemm.h>
14 #include <cutlass/gemm/sgemm_traits.h>
15 
16 #include "ml/impl/continuous_conv/ContinuousConvCUDAKernels.h"
17 #include "ml/impl/misc/MemoryAllocation.h"
18 
19 using cloudViewer::utility::DivUp;
20 
21 namespace cloudViewer {
22 namespace ml {
23 namespace impl {
24 
25 /// Computes the output features of a continuous convolution.
26 ///
27 /// All pointer arguments point to device memory unless stated otherwise.
28 ///
29 /// \tparam TFeat Type for the features and weights
30 /// \tparam TOut Type for the output features
31 /// \tparam TReal Type for point positions and extents
32 /// \tparam TIndex Type for neighbor indexing
33 ///
34 /// \param temp Pointer to temporary memory. If nullptr then the required
35 /// size of temporary memory will be written to \p temp_size and no
36 /// work is done. This function can make use of more memory and
37 /// returns the maximum size that can be used in max_temp_size.
38 ///
39 /// \param temp_size The size of the temporary memory in bytes. This is
40 /// used as an output if temp is nullptr and returns the minimum temp
41 /// size required.
42 ///
43 /// \param max_temp_size This is used as an output if temp is nullptr and
44 /// returns the maximum temp size that can be used.
45 ///
46 /// \param texture_alignment The texture alignment in bytes. This is used
47 /// for allocating segments within the temporary memory.
48 ///
49 /// \param out_features Output array for the computed features with shape
50 /// [num_out, out channels]
51 ///
52 /// \param filter_dims The sizes of the filter dimensions. The size of
53 /// filter_dims must be 5. The order is
54 /// [depth, height, width, inp channels, out channels].
55 ///
56 /// \param filter Pointer to the filter values.
57 ///
58 /// \param num_out The number of output points.
59 ///
60 /// \param out_positions The positions of the output points. The shape is
61 /// [num_out, 3].
62 ///
63 /// \param num_inp The number of input points.
64 ///
65 /// \param inp_positions The positions of the input points. The shape is
66 /// [num_inp, 3].
67 ///
68 /// \param inp_features The input features with shape
69 /// [num_inp, in_channels].
70 ///
71 /// \param inp_importance Optional importance for each input point with
72 /// shape [num_inp]. Set to null to disable.
73 ///
74 /// \param neighbors_index_size The size of the neighbors_index array.
75 ///
76 /// \param neighbors_index The array with lists of neighbors for each
77 /// output point. The start and end of each sublist is defined by
78 /// \p neighbors_row_splits.
79 ///
80 /// \param neighbors_importance Optional importance for each entry in
81 /// \p neighbors_index. Set to null to disable.
82 ///
83 /// \param neighbors_row_splits The prefix sum which defines the start
84 /// and end of the sublists in \p neighbors_index. The size of the
85 /// array is \p num_out + 1.
86 ///
87 /// \param extents The spatial extents of the filter in coordinate units.
88 /// extents can be a scalar or a 1D array of shape [num_out] or a
89 /// 2D array of shape [num_out,3]. The shape depends on
90 /// \p individual_extent and \p isotropic_extent.
91 ///
92 /// \param offsets A single 3D vector used in the filter coordinate
93 /// computation. The shape is [3].
94 ///
95 /// \param interpolation The interpolation mode. Either LINEAR or
96 /// NEAREST_NEIGHBOR.
97 ///
98 /// \param coordinate_mapping The coordinate mapping function. One of
99 /// IDENTITY, BALL_TO_CUBE_RADIAL, BALL_TO_CUBE_VOLUME_PRESERVING.
100 ///
101 /// \param align_corners If true then the voxel centers of the outer voxels
102 /// of the filter array are mapped to the boundary of the filter shape.
103 /// If false then the boundary of the filter array is mapped to the
104 /// boundary of the filter shape.
105 ///
106 /// \param individual_extent If true each output point has an individual
107 /// extent.
108 ///
109 /// \param isotropic_extent If true each then the extent is isotropic for
110 /// each output point.
111 ///
112 /// \param normalize If true then the result is normalized either by the
113 /// number of points (neighbors_importance is null) or by the sum of
114 /// the respective values in neighbors_importance.
115 ///
116 template <class TFeat, class TOut, class TReal, class TIndex>
117 void CConvComputeFeaturesCUDA(const cudaStream_t& stream,
118  void* temp,
119  size_t& temp_size,
120  size_t& max_temp_size,
121  int texture_alignment,
122  TOut* out_features,
123  const std::vector<int>& filter_dims,
124  const TFeat* filter,
125  TIndex num_out,
126  const TReal* out_positions,
127  TIndex num_inp,
128  const TReal* inp_positions,
129  const TFeat* inp_features,
130  const TFeat* inp_importance,
131  size_t neighbors_index_size,
132  const TIndex* neighbors_index,
133  const TFeat* neighbors_importance,
134  const int64_t* neighbors_row_splits,
135  const TReal* extents,
136  const TReal* offsets,
137  InterpolationMode interpolation,
138  CoordinateMapping coordinate_mapping,
139  bool align_corners,
140  bool individual_extent,
141  bool isotropic_extent,
142  bool normalize) {
143  const bool get_temp_size = !temp;
144 
145  if (get_temp_size) {
146  temp = (char*)1; // worst case alignment
147  temp_size = std::numeric_limits<int64_t>::max();
148  }
149 
150  MemoryAllocation mem_temp(temp, temp_size, texture_alignment);
151 
152  const int in_channels = filter_dims[filter_dims.size() - 2];
153  const int out_channels = filter_dims[filter_dims.size() - 1];
154 
155  int spatial_filter_size = 1;
156  for (int i = 0; i < 3; ++i) spatial_filter_size *= filter_dims[i];
157 
158  // this defines how much temporary storage we need at least.
159  // we want to allocate memory for at least 32 output points.
160  const size_t min_num_cols_per_run = std::min(size_t(num_out), size_t(32));
161  const size_t max_num_cols_per_run = num_out;
162  const size_t bytes_per_column =
163  sizeof(TFeat) * (spatial_filter_size * in_channels);
164  const size_t min_temp_size_bytes = min_num_cols_per_run * bytes_per_column;
165  const size_t max_temp_size_bytes = max_num_cols_per_run * bytes_per_column;
166 
167  if (get_temp_size) {
168  std::pair<char*, size_t> tmp =
169  mem_temp.Alloc<char>(min_temp_size_bytes);
170  temp_size = mem_temp.MaxUsed();
171  mem_temp.Free(tmp);
172  mem_temp.Alloc<char>(max_temp_size_bytes);
173  max_temp_size = mem_temp.MaxUsed();
174  return;
175  }
176 
177  // Request segment using all of the temporary memory
178  std::pair<void*, size_t> mem_columns = mem_temp.AllocLargestSegment();
179 
180  if (mem_columns.second < min_temp_size_bytes) {
181  std::stringstream ss;
182  ss << "temp is too small " << mem_columns.second
183  << " bytes. Expected at least " << min_temp_size_bytes << " bytes\n";
184  throw std::runtime_error(ss.str());
185  }
186 
187  // init output
188  cudaMemsetAsync(out_features, 0, sizeof(TOut) * num_out * out_channels,
189  stream);
190 
191  size_t num_cols_per_run =
192  std::min(mem_columns.second / bytes_per_column, size_t(num_out));
193 
194  typedef cutlass::gemm::SgemmTraits<
195  cutlass::MatrixLayout::kColumnMajor, // layout of A matrix (filter)
196  cutlass::MatrixLayout::kColumnMajor, // layout of B matrix
197  // (columns)
198  cutlass::Shape<8, 64, 64> // threadblock tile size
199  >
200  GemmTraits;
201 
202  typedef cutlass::gemm::Gemm<GemmTraits> Gemm;
203 
204  // this is the pointer to the patch matrix
205  TFeat* columns = (TFeat*)mem_columns.first;
206 
207  // if we cannot process all data at once we need multiple runs
208  const size_t num_runs = DivUp(num_out, num_cols_per_run);
209  for (size_t run_i = 0; run_i < num_runs; ++run_i) {
210  const TIndex begin_idx = run_i * num_cols_per_run;
211  const TIndex end_idx =
212  std::min(size_t(num_out), (run_i + 1) * num_cols_per_run);
213  const size_t num_cols_this_run = end_idx - begin_idx;
214 
215  // compute the patch matrix
216  FillColumn<TFeat, TReal, TIndex>(
217  stream, columns, in_channels, begin_idx, end_idx, num_out,
218  out_positions, num_inp, inp_positions, inp_features,
219  inp_importance, neighbors_index_size, neighbors_index,
220  neighbors_importance, neighbors_row_splits, extents, offsets,
221  filter_dims, interpolation, coordinate_mapping, align_corners,
222  individual_extent, isotropic_extent, normalize);
223 
224  // C is MxN
225  // B is KxN
226  // A is MxK
227  int m = out_channels;
228  int k = spatial_filter_size * in_channels;
229  int n = num_cols_this_run;
230  float alpha = 1;
231  const float* const A = filter;
232  int lda = m;
233  const float* const B = columns;
234  int ldb = k;
235  float beta = 1;
236  float* C = out_features + (run_i * num_cols_per_run * out_channels);
237  int ldc = m;
238 
239  typename Gemm::Params params;
240  int result = params.initialize(m, // GEMM M dimension
241  n, // GEMM N dimension
242  k, // GEMM K dimension
243  alpha, // scalar alpha
244  A, // matrix A operand
245  lda,
246  B, // matrix B operand
247  ldb,
248  beta, // scalar beta
249  C, // source matrix C
250  ldc,
251  C, // destination matrix C
252  ldc);
253 
254  if (result) {
255  throw std::runtime_error(
256  "Failed to initialize CUTLASS Gemm::Params object.");
257  }
258 
259  Gemm::launch(params, stream);
260  }
261 }
262 
263 } // namespace impl
264 } // namespace ml
265 } // namespace cloudViewer