Open3D (C++ API)  0.16.1
SparseConvBackpropFilterOpKernel.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
35template <class TIndex>
36class SparseConvBackpropFilterOpKernel : public tensorflow::OpKernel {
37public:
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",
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 const Tensor& filters = context->input(0);
55 const Tensor& inp_features = context->input(1);
56 const Tensor& inp_importance = context->input(2);
57 const Tensor& neighbors_index = context->input(3);
58 const Tensor& neighbors_kernel_index = context->input(4);
59 const Tensor& neighbors_importance = context->input(5);
60 const Tensor& neighbors_row_splits = context->input(6);
61 const Tensor& out_features_gradient = context->input(7);
62
63 Dim num_out("num_out");
64 Dim num_inp("num_inp");
65 Dim num_kernel_elements("num_kernel_elements");
66 Dim in_channels("in_channels");
67 Dim out_channels("out_channels");
68 Dim num_neighbors("num_neighbors");
69
70 CHECK_SHAPE_COMBINE_FIRST_DIMS(context, filters, num_kernel_elements,
71 in_channels, out_channels);
72 CHECK_SHAPE(context, neighbors_row_splits, num_out + 1);
73 CHECK_SHAPE(context, inp_features, num_inp, in_channels);
74 CHECK_SHAPE(context, inp_importance, 0 || num_inp);
75 CHECK_SHAPE(context, neighbors_index, num_neighbors);
76 CHECK_SHAPE(context, neighbors_kernel_index, num_neighbors);
77 CHECK_SHAPE(context, neighbors_importance, 0 || num_neighbors);
78 CHECK_SHAPE(context, out_features_gradient, num_out, out_channels);
79
80 TensorShape filter_backprop_shape(filters.shape());
81 Tensor* filter_backprop = nullptr;
82 OP_REQUIRES_OK(context,
83 context->allocate_output(0, filter_backprop_shape,
84 &filter_backprop));
85
86 std::vector<int> filter_dims;
87 for (int i = 0; i < filters.dims(); ++i) {
88 filter_dims.push_back(filters.dim_size(i));
89 }
90 bool point_importances = inp_importance.shape().dim_size(0) != 0;
91
92 bool has_neighbors_importances =
93 neighbors_importance.shape().dim_size(0) != 0;
94
95 Kernel(context, filters, inp_features, inp_importance, neighbors_index,
96 neighbors_kernel_index, neighbors_importance,
97 neighbors_row_splits, out_features_gradient, filter_dims,
98 point_importances, has_neighbors_importances, *filter_backprop);
99 }
100
101 virtual void Kernel(tensorflow::OpKernelContext* context,
102 const tensorflow::Tensor& filters,
103 const tensorflow::Tensor& inp_features,
104 const tensorflow::Tensor& inp_importance,
105 const tensorflow::Tensor& neighbors_index,
106 const tensorflow::Tensor& neighbors_kernel_index,
107 const tensorflow::Tensor& neighbors_importance,
108 const tensorflow::Tensor& neighbors_row_splits,
109 const tensorflow::Tensor& out_features_gradient,
110 const std::vector<int>& filter_dims,
111 const bool point_importances,
112 const bool has_neighbors_importances,
113 tensorflow::Tensor& filter_backprop) = 0;
114
115public:
118};
#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: SparseConvBackpropFilterOpKernel.h:36
SparseConvBackpropFilterOpKernel(tensorflow::OpKernelConstruction *construction)
Definition: SparseConvBackpropFilterOpKernel.h:38
virtual void Kernel(tensorflow::OpKernelContext *context, const tensorflow::Tensor &filters, const tensorflow::Tensor &inp_features, const tensorflow::Tensor &inp_importance, 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
int max_temp_mem_MB
Definition: SparseConvBackpropFilterOpKernel.h:117
bool normalize
Definition: SparseConvBackpropFilterOpKernel.h:116
void Compute(tensorflow::OpKernelContext *context) override
Definition: SparseConvBackpropFilterOpKernel.h:49
Class for dimensions for which the value should be inferred.
Definition: ShapeChecking.h:69
Definition: ShapeChecking.h:35