-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSparseConvTransposeOps.cpp
More file actions
224 lines (205 loc) · 11.2 KB
/
SparseConvTransposeOps.cpp
File metadata and controls
224 lines (205 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// ----------------------------------------------------------------------------
// - Open3D: www.open3d.org -
// ----------------------------------------------------------------------------
// Copyright (c) 2018-2024 www.open3d.org
// SPDX-License-Identifier: MIT
// ----------------------------------------------------------------------------
//
#include <vector>
#include "open3d/ml/paddle/PaddleHelper.h"
#include "open3d/ml/paddle/misc/InvertNeighborsListOps.h"
#include "open3d/ml/paddle/misc/ReduceSubarraysSumOps.h"
#include "open3d/ml/paddle/sparse_conv/SparseConvOpKernel.h"
#include "open3d/ml/paddle/sparse_conv/SparseConvTransposeBackpropFilterOpKernel.h"
#include "open3d/ml/paddle/sparse_conv/SparseConvTransposeOpKernel.h"
std::vector<paddle::Tensor> SparseConvTransposeForward(
paddle::Tensor& filters,
paddle::Tensor& out_importance,
paddle::Tensor& inp_features,
paddle::Tensor& inp_neighbors_index,
paddle::Tensor& inp_neighbors_importance_sum,
paddle::Tensor& inp_neighbors_row_splits,
paddle::Tensor& neighbors_index,
paddle::Tensor& neighbors_kernel_index,
paddle::Tensor& neighbors_importance,
paddle::Tensor& neighbors_row_splits,
const bool normalize,
const int64_t max_temp_mem_MB) {
CHECK_TYPE(neighbors_row_splits, paddle::DataType::INT64);
CHECK_TYPE(inp_neighbors_row_splits, paddle::DataType::INT64);
CHECK_SAME_DTYPE(neighbors_index, inp_neighbors_index);
CHECK_SAME_DTYPE(filters, inp_features, out_importance,
neighbors_importance);
CHECK_SAME_DEVICE_TYPE(filters, inp_features, out_importance);
// check input shapes
using namespace open3d::ml::op_util;
Dim num_out("num_out");
Dim num_inp("num_inp");
Dim num_kernel_elements("num_kernel_elements");
Dim in_channels("in_channels");
Dim out_channels("out_channels");
Dim num_neighbors("num_neighbors");
CHECK_SHAPE_COMBINE_FIRST_DIMS(filters, num_kernel_elements, in_channels,
out_channels);
CHECK_SHAPE(neighbors_row_splits, num_out + 1);
CHECK_SHAPE(out_importance, 0 || num_out);
CHECK_SHAPE(inp_features, num_inp, in_channels);
CHECK_SHAPE(inp_neighbors_index, num_neighbors);
CHECK_SHAPE(inp_neighbors_importance_sum, 0 || num_inp);
CHECK_SHAPE(inp_neighbors_row_splits, num_inp + 1);
CHECK_SHAPE(neighbors_index, num_neighbors);
CHECK_SHAPE(neighbors_kernel_index, num_neighbors);
CHECK_SHAPE(neighbors_importance, 0 || num_neighbors);
// make sure that these are on the same place as the filters and feats
auto place = inp_features.place();
neighbors_index = neighbors_index.copy_to(place, false);
neighbors_kernel_index = neighbors_kernel_index.copy_to(place, false);
neighbors_importance = neighbors_importance.copy_to(place, false);
neighbors_row_splits = neighbors_row_splits.copy_to(place, false);
inp_neighbors_index = inp_neighbors_index.copy_to(place, false);
inp_neighbors_importance_sum =
inp_neighbors_importance_sum.copy_to(place, false);
inp_neighbors_row_splits = inp_neighbors_row_splits.copy_to(place, false);
const auto& feat_dtype = filters.dtype();
const auto& index_dtype = neighbors_index.dtype();
const auto& kernel_index_dtype = neighbors_kernel_index.dtype();
paddle::Tensor out_features = paddle::empty(
{num_out.value(), out_channels.value()}, feat_dtype, place);
#define FN_PARAMETERS \
filters, out_importance, inp_features, inp_neighbors_importance_sum, \
inp_neighbors_row_splits, neighbors_index, neighbors_kernel_index, \
neighbors_importance, neighbors_row_splits, normalize, \
max_temp_mem_MB, out_features
#define CALL(feat_t, out_t, index_t, kernel_index_t, fn) \
if (ComparePaddleDtype<feat_t>(feat_dtype) && \
ComparePaddleDtype<index_t>(index_dtype) && \
ComparePaddleDtype<kernel_index_t>(kernel_index_dtype)) { \
fn<feat_t, out_t, index_t, kernel_index_t>(FN_PARAMETERS); \
return {out_features}; \
}
if (inp_features.is_gpu() || inp_features.is_custom_device()) {
#ifdef BUILD_CUDA_MODULE
CALL(float, float, int32_t, uint8_t, ::SparseConvTransposeCUDA)
#else
PD_CHECK(false,
"SparseConvTranspose was not compiled with CUDA "
"support");
#endif
} else {
CALL(float, float, int32_t, uint8_t, ::SparseConvTransposeCPU)
}
#undef FN_PARAMETERS
#undef CALL
PD_CHECK(false, "SparseConv does not support " +
phi::DataTypeToString(inp_features.dtype()) +
" as input for inp_features and " +
phi::DataTypeToString(neighbors_index.dtype()) +
" as input for neighbors_index");
return {paddle::Tensor()};
}
std::vector<paddle::Tensor> SparseConvTransposeBackward(
paddle::Tensor& filters,
paddle::Tensor& out_importance,
paddle::Tensor& inp_features,
paddle::Tensor& inp_neighbors_importance_sum,
paddle::Tensor& inp_neighbors_row_splits,
paddle::Tensor& neighbors_index,
paddle::Tensor& neighbors_kernel_index,
paddle::Tensor& neighbors_importance,
paddle::Tensor& neighbors_row_splits,
paddle::Tensor& out_features_gradient,
const bool normalize,
const int64_t max_temp_mem_MB) {
auto place = inp_features.place();
const auto& feat_dtype = filters.dtype();
const auto& index_dtype = neighbors_index.dtype();
const auto& kernel_index_dtype = neighbors_kernel_index.dtype();
CHECK_SAME_DTYPE(out_features_gradient, inp_features, filters);
CHECK_SAME_DEVICE_TYPE(out_features_gradient, inp_features, filters);
// output vars
paddle::Tensor filters_backprop;
paddle::Tensor inp_features_backprop;
#define CALL(feat_t, out_t, index_t, kernel_index_t, fn_suffix) \
if (ComparePaddleDtype<feat_t>(feat_dtype) && \
ComparePaddleDtype<index_t>(index_dtype) && \
ComparePaddleDtype<kernel_index_t>(kernel_index_dtype)) { \
filters_backprop = paddle::empty(filters.shape(), feat_dtype, place); \
SparseConvTransposeBackpropFilter##fn_suffix<feat_t, out_t, index_t, \
kernel_index_t>( \
filters, out_importance, inp_features, \
inp_neighbors_importance_sum, inp_neighbors_row_splits, \
neighbors_index, neighbors_kernel_index, neighbors_importance, \
neighbors_row_splits, out_features_gradient, normalize, \
max_temp_mem_MB, filters_backprop); \
\
paddle::Tensor inv_neighbors_index, _inv_neighbors_row_splits, \
inv_neighbors_importance, inv_arange; \
paddle::Tensor arange = Arange(neighbors_index.shape()[0], place); \
auto inv = InvertNeighborsList(neighbors_index, neighbors_row_splits, \
arange, inp_features.shape()[0]); \
inv_neighbors_index = inv[0]; \
_inv_neighbors_row_splits = inv[1]; \
inv_arange = inv[2]; \
paddle::Tensor inv_neighbors_kernel_index = \
paddle::experimental::gather(neighbors_kernel_index, \
inv_arange); \
if (neighbors_importance.shape()[0] > 0) { \
inv_neighbors_importance = paddle::experimental::gather( \
neighbors_importance, inv_arange); \
} else { \
inv_neighbors_importance = paddle::empty({0}, feat_dtype, place); \
} \
inp_features_backprop = \
paddle::ones(inp_features.shape(), feat_dtype, place); \
auto filters_transposed = Transpose(filters, -1, -2).contiguous(); \
\
SparseConv##fn_suffix<feat_t, out_t, index_t, kernel_index_t>( \
filters_transposed, out_features_gradient, out_importance, \
inv_neighbors_index, inv_neighbors_kernel_index, \
inv_neighbors_importance, inp_neighbors_row_splits, normalize, \
max_temp_mem_MB, inp_features_backprop); \
dispatch_success = true; \
}
bool dispatch_success = false;
if (inp_features.is_gpu() || inp_features.is_custom_device()) {
#ifdef BUILD_CUDA_MODULE
CALL(float, float, int32_t, uint8_t, CUDA)
#else
PD_CHECK(false,
"SparseConvTranspose backward was not compiled "
"with CUDA support");
#endif
} else {
CALL(float, float, int32_t, uint8_t, CPU)
}
PD_CHECK(dispatch_success,
"SparseConvTranspose backward does not support " +
phi::DataTypeToString(inp_features.dtype()) +
" as input for inp_features and " +
phi::DataTypeToString(neighbors_index.dtype()) +
" as input for neighbors_index");
return {filters_backprop, inp_features_backprop};
}
std::vector<paddle::DataType> SparseConvTransposeInferDtype(
paddle::DataType inp_positions_dtype) {
return {inp_positions_dtype};
}
PD_BUILD_OP(open3d_sparse_conv_transpose)
.Inputs({"filters", "out_importance", "inp_features",
"inp_neighbors_index", "inp_neighbors_importance_sum",
"inp_neighbors_row_splits", "neighbors_index",
"neighbors_kernel_index", "neighbors_importance",
"neighbors_row_splits"})
.Outputs({"out_features"})
.Attrs({"normalize:bool", "max_temp_mem_MB:int64_t"})
.SetKernelFn(PD_KERNEL(SparseConvTransposeForward))
.SetInferDtypeFn(PD_INFER_DTYPE(SparseConvTransposeInferDtype));
PD_BUILD_GRAD_OP(open3d_sparse_conv_transpose)
.Inputs({"filters", "out_importance", "inp_features",
"inp_neighbors_importance_sum", "inp_neighbors_row_splits",
"neighbors_index", "neighbors_kernel_index",
"neighbors_importance", "neighbors_row_splits",
paddle::Grad("out_features")})
.Outputs({paddle::Grad("filters"), paddle::Grad("inp_features")})
.Attrs({"normalize:bool", "max_temp_mem_MB:int64_t"})
.SetKernelFn(PD_KERNEL(SparseConvTransposeBackward));