28 #include <tbb/parallel_for.h>
38 template <
class TFeat,
42 bool POINT_IMPORTANCE>
44 const std::vector<int>& filter_dims,
47 const TFeat* inp_features,
48 const TFeat* inp_importance,
49 const TIndex* neighbors_index,
50 const TKernelIndex* neighbors_kernel_index,
51 const TFeat* neighbors_importance,
52 const int64_t* neighbors_row_splits,
53 const TFeat* out_features_gradient,
55 const bool NEIGHBOR_IMPORTANCE = neighbors_importance;
57 const int in_channels = filter_dims[filter_dims.size() - 2];
58 const int out_channels = filter_dims[filter_dims.size() - 1];
60 int num_kernel_elements = 1;
61 for (
int i = 0; i < filter_dims.size() - 2; ++i)
62 num_kernel_elements *= filter_dims[i];
63 const int total_filter_size =
64 num_kernel_elements * in_channels * out_channels;
66 memset(filter_backprop, 0,
sizeof(TOut) * total_filter_size);
67 std::mutex filter_backprop_mutex;
70 tbb::blocked_range<size_t>(0, num_out, 10032),
71 [&](
const tbb::blocked_range<size_t>& r) {
72 int range_length = r.end() - r.begin();
74 Eigen::Matrix<TFeat, Eigen::Dynamic, Eigen::Dynamic> B(
75 in_channels * num_kernel_elements, range_length);
77 Eigen::Matrix<TFeat, Eigen::Dynamic, Eigen::Dynamic> C(
78 out_channels, range_length);
80 Eigen::Array<TFeat, Eigen::Dynamic, 1> infeat(in_channels, 1);
82 for (
size_t out_idx = r.begin(); out_idx != r.end();
84 const int out_col = out_idx - r.begin();
85 const size_t neighbor_start = neighbors_row_splits[out_idx];
86 const size_t neighbor_end =
87 neighbors_row_splits[out_idx + 1];
90 for (
size_t n = neighbor_start; n < neighbor_end; ++n) {
91 const size_t inp_idx = neighbors_index[n];
92 const int kernel_idx = neighbors_kernel_index[n];
94 const TFeat n_importance =
95 (NEIGHBOR_IMPORTANCE ? neighbors_importance[n]
97 normalizer += n_importance;
99 for (
int ic = 0; ic < in_channels; ++ic)
101 inp_features[inp_idx * in_channels + ic];
104 if (POINT_IMPORTANCE)
105 importance = inp_importance[inp_idx];
106 if (NEIGHBOR_IMPORTANCE) importance *= n_importance;
108 if (POINT_IMPORTANCE || NEIGHBOR_IMPORTANCE) {
109 for (
int ic = 0; ic < in_channels; ++ic)
110 infeat(ic) *= importance;
112 for (
int ic = 0; ic < in_channels; ++ic) {
113 B(kernel_idx * in_channels + ic, out_col) =
118 C.col(out_col) = Eigen::Map<
119 const Eigen::Array<TFeat, Eigen::Dynamic, 1>>(
120 out_features_gradient + out_idx * out_channels,
123 if (normalize && normalizer != TFeat(0))
124 C.col(out_col) /= normalizer;
128 Eigen::Matrix<TFeat, Eigen::Dynamic, Eigen::Dynamic> A(
129 out_channels, num_kernel_elements * in_channels);
131 A = C * B.transpose();
134 std::lock_guard<std::mutex> lock(filter_backprop_mutex);
136 for (
int j = 0; j < num_kernel_elements * in_channels; ++j)
137 for (
int i = 0; i < out_channels; ++i, ++linear_i) {
138 filter_backprop[linear_i] += TOut(A(i, j));
198 template <
class TFeat,
class TOut,
class TIndex,
class TKernelIndex>
200 const std::vector<int>& filter_dims,
203 const TFeat* inp_features,
204 const TFeat* inp_importance,
205 const TIndex* neighbors_index,
206 const TKernelIndex* neighbors_kernel_index,
207 const TFeat* neighbors_importance,
208 const int64_t* neighbors_row_splits,
209 const TFeat* out_features_gradient,
211 bool has_importance = inp_importance;
213 #define FN_PARAMETERS \
214 filter_backprop, filter_dims, num_out, num_inp, inp_features, \
215 inp_importance, neighbors_index, neighbors_kernel_index, \
216 neighbors_importance, neighbors_row_splits, out_features_gradient, \
219 #define CALL_TEMPLATE(HAS_IMPORTANCE) \
220 if (HAS_IMPORTANCE == has_importance) \
221 _SparseConvBackropFilterCPU<TFeat, TOut, TIndex, TKernelIndex, \
222 HAS_IMPORTANCE>(FN_PARAMETERS);
224 #define CALL_TEMPLATE2 \
225 CALL_TEMPLATE(true) \
231 #undef CALL_TEMPLATE2
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)
Definition: SparseConvBackpropFilter.h:43
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)
Definition: SparseConvBackpropFilter.h:199
Definition: PinholeCameraIntrinsic.cpp:35