Open3D (C++ API)
ReduceSubarraysSumOpKernel.h
Go to the documentation of this file.
1 // The MIT License (MIT)
2 //
3 // Copyright (c) 2020 www.open3d.org
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 // IN THE SOFTWARE.
22 // ----------------------------------------------------------------------------
23 #pragma once
24 
25 #include "tensorflow/core/framework/op.h"
26 #include "tensorflow/core/framework/op_kernel.h"
27 #include "tensorflow/core/lib/core/errors.h"
28 
30 // namespace for code that is common for all kernels
32 
33 // Base class with common code for the OpKernel implementations
34 class ReduceSubarraysSumOpKernel : public tensorflow::OpKernel {
35 public:
36  explicit ReduceSubarraysSumOpKernel(
37  tensorflow::OpKernelConstruction* construction)
38  : OpKernel(construction) {}
39 
40  void Compute(tensorflow::OpKernelContext* context) override {
41  using namespace tensorflow;
42  static_assert(sizeof(int64) == sizeof(int64_t),
43  "int64 type is not compatible");
44 
45  const Tensor& values = context->input(0);
46  OP_REQUIRES(context, values.shape().dims() == 1,
47  errors::InvalidArgument("values must be a rank 1 tensor"));
48 
49  const Tensor& row_splits = context->input(1);
50  OP_REQUIRES(
51  context, row_splits.shape().dims() == 1,
52  errors::InvalidArgument("row_splits must be a rank 1 tensor"));
53 
54  // special treatment for empty values vector
55  if (values.shape().dim_size(0) == 0) {
56  Tensor* sums_tensor = 0;
57  OP_REQUIRES_OK(context, context->allocate_output(0, values.shape(),
58  &sums_tensor));
59  return;
60  }
61 
62  Tensor* sums_tensor = 0;
63  TensorShape sums_shape({row_splits.shape().dim_size(0) - 1});
64  OP_REQUIRES_OK(context,
65  context->allocate_output(0, sums_shape, &sums_tensor));
66 
67  Kernel(context, values, row_splits, *sums_tensor);
68  }
69 
70  // Function with the device specific code
71  virtual void Kernel(tensorflow::OpKernelContext* context,
72  const tensorflow::Tensor& values,
73  const tensorflow::Tensor& row_splits,
74  tensorflow::Tensor& sums) = 0;
75 
76 private:
77 };
78 
79 } // namespace reduce_subarrays_sum_opkernel