10 #include <tbb/parallel_for.h>
20 template <
class TFeat,
27 bool INDIVIDUAL_EXTENT,
28 bool ISOTROPIC_EXTENT,
29 bool POINT_IMPORTANCE>
31 const std::vector<int>& filter_dims,
34 const TReal* out_positions,
36 const TReal* inp_positions,
37 const TFeat* inp_features,
38 const TFeat* inp_importance,
39 size_t neighbors_index_size,
40 const TIndex* neighbors_index,
41 const TFeat* neighbors_importance,
42 const int64_t* neighbors_row_splits,
46 const bool NEIGHBORS_IMPORTANCE = neighbors_importance !=
nullptr;
49 typedef Eigen::Array<TReal, VECSIZE, 1> Vec_t;
51 InterpolationVec_t interpolation;
53 const int in_channels = filter_dims[filter_dims.size() - 2];
54 const int out_channels = filter_dims[filter_dims.size() - 1];
56 int spatial_filter_size = 1;
57 for (
int i = 0; i < 3; ++i) spatial_filter_size *= filter_dims[i];
58 Eigen::Array<int, 3, 1> filter_size_xyz(filter_dims[2], filter_dims[1],
61 memset(out_features, 0,
sizeof(TOut) * num_out * out_channels);
63 typedef Eigen::Array<TFeat, VECSIZE, Eigen::Dynamic> Matrix;
64 typedef Eigen::Array<TReal, VECSIZE, 3> Matrix3C;
67 tbb::blocked_range<size_t>(0, num_out, 32),
68 [&](
const tbb::blocked_range<size_t>& r) {
69 int range_length = r.end() - r.begin();
71 Eigen::Matrix<TOut, Eigen::Dynamic, 1> normalizers(range_length,
73 normalizers.setZero();
75 Eigen::Matrix<TFeat, Eigen::Dynamic, Eigen::Dynamic> B(
76 in_channels * spatial_filter_size, range_length);
79 Matrix infeat(
VECSIZE, in_channels);
81 Eigen::Array<TReal, 3, 1> offsets_(offsets[0], offsets[1],
85 if (INDIVIDUAL_EXTENT ==
false) {
86 if (ISOTROPIC_EXTENT) {
87 inv_extents = 1 / extents[0];
89 inv_extents.col(0) = 1 / extents[0];
90 inv_extents.col(1) = 1 / extents[1];
91 inv_extents.col(2) = 1 / extents[2];
95 for (
size_t out_idx = r.begin(); out_idx != r.end();
97 const int out_col = out_idx - r.begin();
98 const size_t neighbor_start = neighbors_row_splits[out_idx];
99 const size_t neighbor_end =
100 neighbors_row_splits[out_idx + 1];
102 if (INDIVIDUAL_EXTENT) {
103 if (ISOTROPIC_EXTENT) {
104 inv_extents = 1 / extents[out_idx];
106 inv_extents.col(0) = 1 / extents[3 * out_idx + 0];
107 inv_extents.col(1) = 1 / extents[3 * out_idx + 1];
108 inv_extents.col(2) = 1 / extents[3 * out_idx + 2];
112 typename InterpolationVec_t::Weight_t interp_weights;
113 typename InterpolationVec_t::Idx_t interp_indices;
115 int vec_valid_count = 0;
123 for (
size_t n = neighbor_start; n < neighbor_end; ++n) {
124 const size_t inp_idx = neighbors_index[n];
125 const int i = vec_valid_count;
126 x(i) = inp_positions[inp_idx * 3 + 0] -
127 out_positions[out_idx * 3 + 0];
128 y(i) = inp_positions[inp_idx * 3 + 1] -
129 out_positions[out_idx * 3 + 1];
130 z(i) = inp_positions[inp_idx * 3 + 2] -
131 out_positions[out_idx * 3 + 2];
133 const TFeat n_importance =
134 (NEIGHBORS_IMPORTANCE ? neighbors_importance[n]
136 normalizers(out_col) += TOut(n_importance);
138 for (
int ic = 0; ic < in_channels; ++ic)
140 inp_features[inp_idx * in_channels + ic];
142 TFeat importance(1.0);
143 if (POINT_IMPORTANCE)
144 importance = inp_importance[inp_idx];
145 if (NEIGHBORS_IMPORTANCE) importance *= n_importance;
147 if (POINT_IMPORTANCE || NEIGHBORS_IMPORTANCE) {
148 for (
int ic = 0; ic < in_channels; ++ic)
149 infeat(i, ic) *= importance;
153 if (vec_valid_count ==
VECSIZE) {
156 x, y, z, filter_size_xyz, inv_extents,
158 interpolation.Interpolate(
159 interp_weights, interp_indices, x, y, z,
160 filter_size_xyz, in_channels);
161 for (
int k = 0; k <
VECSIZE; ++k)
162 for (
int j = 0; j < InterpolationVec_t::Size();
164 for (
int ic = 0; ic < in_channels; ++ic)
165 B(interp_indices(j, k) + ic, out_col) +=
166 TFeat(interp_weights(j, k)) *
172 if (vec_valid_count) {
175 x, y, z, filter_size_xyz, inv_extents,
177 interpolation.Interpolate(interp_weights,
178 interp_indices, x, y, z,
179 filter_size_xyz, in_channels);
180 for (
int k = 0; k < vec_valid_count; ++k)
181 for (
int j = 0; j < InterpolationVec_t::Size();
183 for (
int ic = 0; ic < in_channels; ++ic)
184 B(interp_indices(j, k) + ic, out_col) +=
185 TFeat(interp_weights(j, k)) *
192 Eigen::Map<
const Eigen::Matrix<TFeat, Eigen::Dynamic,
194 A(filter, out_channels,
195 spatial_filter_size * in_channels);
196 Eigen::Map<Eigen::Matrix<TOut, Eigen::Dynamic, Eigen::Dynamic>>
197 C(out_features + (r.begin() * out_channels),
198 out_channels, range_length);
200 C = (A * B).
template cast<TOut>();
202 for (
int i = 0; i < range_length; ++i) {
203 if (normalizers(i) != TOut(0))
204 C.col(i) /= normalizers(i);
285 template <
class TFeat,
class TOut,
class TReal,
class TIndex>
287 const std::vector<int>& filter_dims,
290 const TReal* out_positions,
292 const TReal* inp_positions,
293 const TFeat* inp_features,
294 const TFeat* inp_importance,
295 size_t neighbors_index_size,
296 const TIndex* neighbors_index,
297 const TFeat* neighbors_importance,
298 const int64_t* neighbors_row_splits,
299 const TReal* extents,
300 const TReal* offsets,
304 bool individual_extent,
305 bool isotropic_extent,
308 bool has_importance = inp_importance;
310 #define FN_PARAMETERS \
311 out_features, filter_dims, filter, num_out, out_positions, num_inp, \
312 inp_positions, inp_features, inp_importance, neighbors_index_size, \
313 neighbors_index, neighbors_importance, neighbors_row_splits, \
314 extents, offsets, normalize
316 #define CALL_TEMPLATE(INTERPOLATION, MAPPING, ALIGN_CORNERS, \
317 INDIVIDUAL_EXTENT, ISOTROPIC_EXTENT, HAS_IMPORTANCE) \
318 if (INTERPOLATION == interpolation && MAPPING == coordinate_mapping && \
319 ALIGN_CORNERS == align_corners && \
320 INDIVIDUAL_EXTENT == individual_extent && \
321 ISOTROPIC_EXTENT == isotropic_extent && \
322 HAS_IMPORTANCE == has_importance) \
323 _CConvComputeFeaturesCPU<TFeat, TOut, TReal, TIndex, INTERPOLATION, \
324 MAPPING, ALIGN_CORNERS, INDIVIDUAL_EXTENT, \
325 ISOTROPIC_EXTENT, HAS_IMPORTANCE>( \
328 #define CALL_TEMPLATE2(INTERPOLATION, MAPPING) \
329 CALL_TEMPLATE(INTERPOLATION, MAPPING, true, true, true, true) \
330 CALL_TEMPLATE(INTERPOLATION, MAPPING, true, true, true, false) \
331 CALL_TEMPLATE(INTERPOLATION, MAPPING, true, true, false, true) \
332 CALL_TEMPLATE(INTERPOLATION, MAPPING, true, true, false, false) \
333 CALL_TEMPLATE(INTERPOLATION, MAPPING, true, false, true, true) \
334 CALL_TEMPLATE(INTERPOLATION, MAPPING, true, false, true, false) \
335 CALL_TEMPLATE(INTERPOLATION, MAPPING, true, false, false, true) \
336 CALL_TEMPLATE(INTERPOLATION, MAPPING, true, false, false, false) \
337 CALL_TEMPLATE(INTERPOLATION, MAPPING, false, true, true, true) \
338 CALL_TEMPLATE(INTERPOLATION, MAPPING, false, true, true, false) \
339 CALL_TEMPLATE(INTERPOLATION, MAPPING, false, true, false, true) \
340 CALL_TEMPLATE(INTERPOLATION, MAPPING, false, true, false, false) \
341 CALL_TEMPLATE(INTERPOLATION, MAPPING, false, false, true, true) \
342 CALL_TEMPLATE(INTERPOLATION, MAPPING, false, false, true, false) \
343 CALL_TEMPLATE(INTERPOLATION, MAPPING, false, false, false, true) \
344 CALL_TEMPLATE(INTERPOLATION, MAPPING, false, false, false, false)
346 #define CALL_TEMPLATE3(INTERPOLATION) \
347 CALL_TEMPLATE2(INTERPOLATION, CoordinateMapping::BALL_TO_CUBE_RADIAL) \
348 CALL_TEMPLATE2(INTERPOLATION, \
349 CoordinateMapping::BALL_TO_CUBE_VOLUME_PRESERVING) \
350 CALL_TEMPLATE2(INTERPOLATION, CoordinateMapping::IDENTITY)
352 #define CALL_TEMPLATE4 \
353 CALL_TEMPLATE3(InterpolationMode::LINEAR) \
354 CALL_TEMPLATE3(InterpolationMode::LINEAR_BORDER) \
355 CALL_TEMPLATE3(InterpolationMode::NEAREST_NEIGHBOR)
360 #undef CALL_TEMPLATE2
361 #undef CALL_TEMPLATE3
362 #undef CALL_TEMPLATE4
__host__ __device__ float2 normalize(float2 v)
void CConvComputeFeaturesCPU(TOut *out_features, const std::vector< int > &filter_dims, const TFeat *filter, size_t num_out, const TReal *out_positions, size_t num_inp, const TReal *inp_positions, const TFeat *inp_features, const TFeat *inp_importance, size_t neighbors_index_size, const TIndex *neighbors_index, const TFeat *neighbors_importance, const int64_t *neighbors_row_splits, const TReal *extents, const TReal *offsets, InterpolationMode interpolation, CoordinateMapping coordinate_mapping, bool align_corners, bool individual_extent, bool isotropic_extent, bool normalize)
void _CConvComputeFeaturesCPU(TOut *out_features, const std::vector< int > &filter_dims, const TFeat *filter, size_t num_out, const TReal *out_positions, size_t num_inp, const TReal *inp_positions, const TFeat *inp_features, const TFeat *inp_importance, size_t neighbors_index_size, const TIndex *neighbors_index, const TFeat *neighbors_importance, const int64_t *neighbors_row_splits, const TReal *extents, const TReal *offsets, bool normalize)
void ComputeFilterCoordinates(Eigen::Array< T, VECSIZE, 1 > &x, Eigen::Array< T, VECSIZE, 1 > &y, Eigen::Array< T, VECSIZE, 1 > &z, const Eigen::Array< int, 3, 1 > &filter_size, const Eigen::Array< T, VECSIZE, 3 > &inv_extents, const Eigen::Array< T, 3, 1 > &offset)
Generic file read and write utility for python interface.
Class for computing interpolation weights.