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