Open3D (C++ API)  0.15.1
SparseConvTransposeBackpropFilterOpKernel.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018-2021 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #pragma once
28 
29 #include <tensorflow/core/framework/op.h>
30 #include <tensorflow/core/framework/op_kernel.h>
31 #include <tensorflow/core/lib/core/errors.h>
32 
34 
35 template <class TIndex>
36 class SparseConvTransposeBackpropFilterOpKernel : public tensorflow::OpKernel {
37 public:
39  tensorflow::OpKernelConstruction* construction)
40  : OpKernel(construction) {
41  using namespace tensorflow;
42  OP_REQUIRES_OK(construction,
43  construction->GetAttr("normalize", &normalize));
44 
45  OP_REQUIRES_OK(construction, construction->GetAttr("max_temp_mem_MB",
46  &max_temp_mem_MB));
47  }
48 
49  void Compute(tensorflow::OpKernelContext* context) override {
50  using namespace tensorflow;
51  using namespace open3d::ml::op_util;
52  static_assert(sizeof(int64) == sizeof(int64_t),
53  "int64 type is not compatible");
54 
55  const Tensor& filters = context->input(0);
56  const Tensor& out_importance = context->input(1);
57  const Tensor& inp_features = context->input(2);
58  const Tensor& inp_neighbors_importance_sum = context->input(3);
59  const Tensor& inp_neighbors_row_splits = context->input(4);
60  const Tensor& neighbors_index = context->input(5);
61  const Tensor& neighbors_kernel_index = context->input(6);
62  const Tensor& neighbors_importance = context->input(7);
63  const Tensor& neighbors_row_splits = context->input(8);
64  const Tensor& out_features_gradient = context->input(9);
65 
66  Dim num_out("num_out");
67  Dim num_inp("num_inp");
68  Dim num_kernel_elements("num_kernel_elements");
69  Dim in_channels("in_channels");
70  Dim out_channels("out_channels");
71  Dim num_neighbors("num_neighbors");
72 
73  CHECK_SHAPE_COMBINE_FIRST_DIMS(context, filters, num_kernel_elements,
74  in_channels, out_channels);
75  CHECK_SHAPE(context, neighbors_row_splits, num_out + 1);
76  CHECK_SHAPE(context, out_importance, 0 || num_out);
77  CHECK_SHAPE(context, inp_features, num_inp, in_channels);
78  CHECK_SHAPE(context, inp_neighbors_importance_sum, 0 || num_inp);
79  CHECK_SHAPE(context, inp_neighbors_row_splits, num_inp + 1);
80  CHECK_SHAPE(context, neighbors_index, num_neighbors);
81  CHECK_SHAPE(context, neighbors_kernel_index, num_neighbors);
82  CHECK_SHAPE(context, neighbors_importance, 0 || num_neighbors);
83  CHECK_SHAPE(context, out_features_gradient, num_out, out_channels);
84 
85  TensorShape filter_backprop_shape(filters.shape());
86  Tensor* filter_backprop = nullptr;
87  OP_REQUIRES_OK(context,
88  context->allocate_output(0, filter_backprop_shape,
89  &filter_backprop));
90 
91  std::vector<int> filter_dims;
92  for (int i = 0; i < filters.dims(); ++i) {
93  filter_dims.push_back(filters.dim_size(i));
94  }
95 
96  bool point_importances = out_importance.shape().dim_size(0) != 0;
97 
98  bool has_neighbors_importances =
99  neighbors_importance.shape().dim_size(0) != 0;
100 
101  Kernel(context, filters, out_importance, inp_features,
102  inp_neighbors_importance_sum, inp_neighbors_row_splits,
103  neighbors_index, neighbors_kernel_index, neighbors_importance,
104  neighbors_row_splits, out_features_gradient, filter_dims,
105  point_importances, has_neighbors_importances, *filter_backprop);
106  }
107 
108  virtual void Kernel(tensorflow::OpKernelContext* context,
109  const tensorflow::Tensor& filter,
110  const tensorflow::Tensor& out_importance,
111  const tensorflow::Tensor& inp_features,
112  const tensorflow::Tensor& inp_neighbors_importance_sum,
113  const tensorflow::Tensor& inp_neighbors_row_splits,
114  const tensorflow::Tensor& neighbors_index,
115  const tensorflow::Tensor& neighbors_kernel_index,
116  const tensorflow::Tensor& neighbors_importance,
117  const tensorflow::Tensor& neighbors_row_splits,
118  const tensorflow::Tensor& out_features_gradient,
119  const std::vector<int>& filter_dims,
120  const bool point_importances,
121  const bool has_neighbors_importances,
122  tensorflow::Tensor& filter_backprop) = 0;
123 
124 public:
125  bool normalize;
127 };
#define CHECK_SHAPE_COMBINE_FIRST_DIMS(tensor,...)
Definition: TorchHelper.h:214
#define CHECK_SHAPE(tensor,...)
Definition: TorchHelper.h:205
ImGuiContext * context
Definition: Window.cpp:95
Definition: SparseConvTransposeBackpropFilterOpKernel.h:36
SparseConvTransposeBackpropFilterOpKernel(tensorflow::OpKernelConstruction *construction)
Definition: SparseConvTransposeBackpropFilterOpKernel.h:38
bool normalize
Definition: SparseConvTransposeBackpropFilterOpKernel.h:125
virtual void Kernel(tensorflow::OpKernelContext *context, const tensorflow::Tensor &filter, const tensorflow::Tensor &out_importance, const tensorflow::Tensor &inp_features, const tensorflow::Tensor &inp_neighbors_importance_sum, const tensorflow::Tensor &inp_neighbors_row_splits, const tensorflow::Tensor &neighbors_index, const tensorflow::Tensor &neighbors_kernel_index, const tensorflow::Tensor &neighbors_importance, const tensorflow::Tensor &neighbors_row_splits, const tensorflow::Tensor &out_features_gradient, const std::vector< int > &filter_dims, const bool point_importances, const bool has_neighbors_importances, tensorflow::Tensor &filter_backprop)=0
void Compute(tensorflow::OpKernelContext *context) override
Definition: SparseConvTransposeBackpropFilterOpKernel.h:49
int max_temp_mem_MB
Definition: SparseConvTransposeBackpropFilterOpKernel.h:126
Class for dimensions for which the value should be inferred.
Definition: ShapeChecking.h:69
Definition: ShapeChecking.h:35