ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
SparseConvBackpropFilter.h
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 #include <tbb/parallel_for.h>
10 
11 #include <Eigen/Core>
12 #include <mutex>
13 
14 namespace cloudViewer {
15 namespace ml {
16 namespace impl {
17 
18 // Implementation of SparseConvBackropFilterCPU
19 template <class TFeat,
20  class TOut,
21  class TIndex,
22  class TKernelIndex,
23  bool POINT_IMPORTANCE>
24 void _SparseConvBackropFilterCPU(TOut* filter_backprop,
25  const std::vector<int>& filter_dims,
26  size_t num_out,
27  size_t num_inp,
28  const TFeat* inp_features,
29  const TFeat* inp_importance,
30  const TIndex* neighbors_index,
31  const TKernelIndex* neighbors_kernel_index,
32  const TFeat* neighbors_importance,
33  const int64_t* neighbors_row_splits,
34  const TFeat* out_features_gradient,
35  bool normalize) {
36  const bool NEIGHBOR_IMPORTANCE = neighbors_importance;
37 
38  const int in_channels = filter_dims[filter_dims.size() - 2];
39  const int out_channels = filter_dims[filter_dims.size() - 1];
40 
41  int num_kernel_elements = 1;
42  for (std::size_t i = 0; i < filter_dims.size() - 2; ++i) {
43  num_kernel_elements *= filter_dims[i];
44  }
45  const int total_filter_size =
46  num_kernel_elements * in_channels * out_channels;
47 
48  memset(filter_backprop, 0, sizeof(TOut) * total_filter_size);
49  std::mutex filter_backprop_mutex;
50 
51  tbb::parallel_for(
52  tbb::blocked_range<size_t>(0, num_out, 10032),
53  [&](const tbb::blocked_range<size_t>& r) {
54  int range_length = r.end() - r.begin();
55 
56  Eigen::Matrix<TFeat, Eigen::Dynamic, Eigen::Dynamic> B(
57  in_channels * num_kernel_elements, range_length);
58  B.setZero();
59  Eigen::Matrix<TFeat, Eigen::Dynamic, Eigen::Dynamic> C(
60  out_channels, range_length);
61 
62  Eigen::Array<TFeat, Eigen::Dynamic, 1> infeat(in_channels, 1);
63 
64  for (size_t out_idx = r.begin(); out_idx != r.end();
65  ++out_idx) {
66  const int out_col = out_idx - r.begin();
67  const size_t neighbor_start = neighbors_row_splits[out_idx];
68  const size_t neighbor_end =
69  neighbors_row_splits[out_idx + 1];
70  TFeat normalizer(0);
71 
72  for (size_t n = neighbor_start; n < neighbor_end; ++n) {
73  const size_t inp_idx = neighbors_index[n];
74  const int kernel_idx = neighbors_kernel_index[n];
75 
76  const TFeat n_importance =
77  (NEIGHBOR_IMPORTANCE ? neighbors_importance[n]
78  : TFeat(1));
79  normalizer += n_importance;
80 
81  for (int ic = 0; ic < in_channels; ++ic)
82  infeat(ic) =
83  inp_features[inp_idx * in_channels + ic];
84 
85  TFeat importance(1);
86  if (POINT_IMPORTANCE)
87  importance = inp_importance[inp_idx];
88  if (NEIGHBOR_IMPORTANCE) importance *= n_importance;
89 
90  if (POINT_IMPORTANCE || NEIGHBOR_IMPORTANCE) {
91  for (int ic = 0; ic < in_channels; ++ic)
92  infeat(ic) *= importance;
93  }
94  for (int ic = 0; ic < in_channels; ++ic) {
95  B(kernel_idx * in_channels + ic, out_col) =
96  infeat(ic);
97  }
98  }
99 
100  C.col(out_col) = Eigen::Map<
101  const Eigen::Array<TFeat, Eigen::Dynamic, 1>>(
102  out_features_gradient + out_idx * out_channels,
103  out_channels, 1);
104 
105  if (normalize && normalizer != TFeat(0))
106  C.col(out_col) /= normalizer;
107 
108  } // out_idx
109 
110  Eigen::Matrix<TFeat, Eigen::Dynamic, Eigen::Dynamic> A(
111  out_channels, num_kernel_elements * in_channels);
112 
113  A = C * B.transpose();
114 
115  {
116  std::lock_guard<std::mutex> lock(filter_backprop_mutex);
117  int linear_i = 0;
118  for (int j = 0; j < num_kernel_elements * in_channels; ++j)
119  for (int i = 0; i < out_channels; ++i, ++linear_i) {
120  filter_backprop[linear_i] += TOut(A(i, j));
121  }
122  }
123  });
124 }
125 
180 template <class TFeat, class TOut, class TIndex, class TKernelIndex>
181 void SparseConvBackpropFilterCPU(TOut* filter_backprop,
182  const std::vector<int>& filter_dims,
183  size_t num_out,
184  size_t num_inp,
185  const TFeat* inp_features,
186  const TFeat* inp_importance,
187  const TIndex* neighbors_index,
188  const TKernelIndex* neighbors_kernel_index,
189  const TFeat* neighbors_importance,
190  const int64_t* neighbors_row_splits,
191  const TFeat* out_features_gradient,
192  bool normalize) {
193  bool has_importance = inp_importance;
194 
195 #define FN_PARAMETERS \
196  filter_backprop, filter_dims, num_out, num_inp, inp_features, \
197  inp_importance, neighbors_index, neighbors_kernel_index, \
198  neighbors_importance, neighbors_row_splits, out_features_gradient, \
199  normalize
200 
201 #define CALL_TEMPLATE(HAS_IMPORTANCE) \
202  if (HAS_IMPORTANCE == has_importance) \
203  _SparseConvBackropFilterCPU<TFeat, TOut, TIndex, TKernelIndex, \
204  HAS_IMPORTANCE>(FN_PARAMETERS);
205 
206 #define CALL_TEMPLATE2 \
207  CALL_TEMPLATE(true) \
208  CALL_TEMPLATE(false)
209 
211 
212 #undef CALL_TEMPLATE
213 #undef CALL_TEMPLATE2
214 
215 #undef FN_PARAMETERS
216 }
217 
218 } // namespace impl
219 } // namespace ml
220 } // namespace cloudViewer
#define CALL_TEMPLATE2
__host__ __device__ float2 normalize(float2 v)
Definition: cutil_math.h:1179
void _SparseConvBackropFilterCPU(TOut *filter_backprop, const std::vector< int > &filter_dims, size_t num_out, size_t num_inp, const TFeat *inp_features, const TFeat *inp_importance, const TIndex *neighbors_index, const TKernelIndex *neighbors_kernel_index, const TFeat *neighbors_importance, const int64_t *neighbors_row_splits, const TFeat *out_features_gradient, bool normalize)
void SparseConvBackpropFilterCPU(TOut *filter_backprop, const std::vector< int > &filter_dims, size_t num_out, size_t num_inp, const TFeat *inp_features, const TFeat *inp_importance, const TIndex *neighbors_index, const TKernelIndex *neighbors_kernel_index, const TFeat *neighbors_importance, const int64_t *neighbors_row_splits, const TFeat *out_features_gradient, bool normalize)
Generic file read and write utility for python interface.