Open3D (C++ API)  0.18.0+252c867
SparseConvOpKernel.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2023 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <tensorflow/core/framework/op.h>
11 #include <tensorflow/core/framework/op_kernel.h>
12 #include <tensorflow/core/lib/core/errors.h>
13 
15 
16 template <class TIndex>
17 class SparseConvOpKernel : public tensorflow::OpKernel {
18 public:
19  explicit SparseConvOpKernel(tensorflow::OpKernelConstruction* construction)
20  : OpKernel(construction) {
21  using namespace tensorflow;
22 
23  OP_REQUIRES_OK(construction,
24  construction->GetAttr("normalize", &normalize));
25 
26  OP_REQUIRES_OK(construction, construction->GetAttr("max_temp_mem_MB",
27  &max_temp_mem_MB));
28  }
29 
30  void Compute(tensorflow::OpKernelContext* context) override {
31  using namespace tensorflow;
32  using namespace open3d::ml::op_util;
33  static_assert(sizeof(int64) == sizeof(int64_t),
34  "int64 type is not compatible");
35  const Tensor& filters = context->input(0);
36  const Tensor& inp_features = context->input(1);
37  const Tensor& inp_importance = context->input(2);
38  const Tensor& neighbors_index = context->input(3);
39  const Tensor& neighbors_kernel_index = context->input(4);
40  const Tensor& neighbors_importance = context->input(5);
41  const Tensor& neighbors_row_splits = context->input(6);
42 
43  Dim num_out("num_out");
44  Dim num_inp("num_inp");
45  Dim num_kernel_elements("num_kernel_elements");
46  Dim in_channels("in_channels");
47  Dim out_channels("out_channels");
48  Dim num_neighbors("num_neighbors");
49 
50  CHECK_SHAPE_COMBINE_FIRST_DIMS(context, filters, num_kernel_elements,
51  in_channels, out_channels);
52  CHECK_SHAPE(context, neighbors_row_splits, num_out + 1);
53  CHECK_SHAPE(context, inp_features, num_inp, in_channels);
54  CHECK_SHAPE(context, inp_importance, 0 || num_inp);
55  CHECK_SHAPE(context, neighbors_index, num_neighbors);
56  CHECK_SHAPE(context, neighbors_kernel_index, num_neighbors);
57  CHECK_SHAPE(context, neighbors_importance, 0 || num_neighbors);
58 
59  TensorShape out_features_shape({num_out.value(), out_channels.value()});
60  Tensor* out_features = nullptr;
61  OP_REQUIRES_OK(context, context->allocate_output(0, out_features_shape,
62  &out_features));
63 
64  std::vector<int> filter_dims;
65  for (int i = 0; i < filters.dims(); ++i) {
66  filter_dims.push_back(filters.dim_size(i));
67  }
68 
69  bool point_importances = inp_importance.shape().dim_size(0) != 0;
70 
71  bool has_neighbors_importances =
72  neighbors_importance.shape().dim_size(0) != 0;
73 
74  Kernel(context, filters, inp_features, inp_importance, neighbors_index,
75  neighbors_kernel_index, neighbors_importance,
76  neighbors_row_splits, filter_dims, point_importances,
77  has_neighbors_importances, *out_features);
78  }
79 
80  virtual void Kernel(tensorflow::OpKernelContext* context,
81  const tensorflow::Tensor& filters,
82  const tensorflow::Tensor& inp_features,
83  const tensorflow::Tensor& inp_importance,
84  const tensorflow::Tensor& neighbors_index,
85  const tensorflow::Tensor& neighbors_kernel_index,
86  const tensorflow::Tensor& neighbors_importance,
87  const tensorflow::Tensor& neighbors_row_splits,
88  const std::vector<int>& filter_dims,
89  const bool point_importances,
90  const bool has_neighbors_importances,
91  tensorflow::Tensor& out_features) = 0;
92 
93 public:
94  bool normalize;
96 };
#define CHECK_SHAPE_COMBINE_FIRST_DIMS(tensor,...)
Definition: TorchHelper.h:195
#define CHECK_SHAPE(tensor,...)
Definition: TorchHelper.h:186
ImGuiContext * context
Definition: Window.cpp:76
Definition: SparseConvOpKernel.h:17
int max_temp_mem_MB
Definition: SparseConvOpKernel.h:95
void Compute(tensorflow::OpKernelContext *context) override
Definition: SparseConvOpKernel.h:30
bool normalize
Definition: SparseConvOpKernel.h:94
SparseConvOpKernel(tensorflow::OpKernelConstruction *construction)
Definition: SparseConvOpKernel.h:19
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:50
int64_t & value()
Definition: ShapeChecking.h:70
Definition: ShapeChecking.h:16