Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / compiler / luci-interpreter / src / core / Kernel.h
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef LUCI_INTERPRETER_CORE_KERNEL_H
18 #define LUCI_INTERPRETER_CORE_KERNEL_H
19
20 #include "luci_interpreter/core/Tensor.h"
21
22 #include <vector>
23
24 namespace luci_interpreter
25 {
26
27 // Base class for all kernels.
28 class Kernel
29 {
30 protected:
31   Kernel(std::vector<const Tensor *> inputs, std::vector<Tensor *> outputs)
32       : _inputs(std::move(inputs)), _outputs(std::move(outputs))
33   {
34   }
35
36 public:
37   virtual ~Kernel() = default;
38
39   std::vector<const Tensor *> getInputTensors() const { return _inputs; }
40   std::vector<Tensor *> getOutputTensors() const { return _outputs; }
41
42   // Configures the kernel.
43   // This function is currently called once for each kernel during interpreter construction,
44   // which makes it a convenient place for preparing (resizing) output tensors.
45   virtual void configure() = 0;
46
47   // Executes the kernel.
48   virtual void execute() const = 0;
49
50 protected:
51   // NOTE Prefer not to use these in derived classes.
52   const std::vector<const Tensor *> _inputs;
53   const std::vector<Tensor *> _outputs;
54 };
55
56 // Base class for kernels with parameters.
57 template <typename Params> class KernelWithParams : public Kernel
58 {
59 protected:
60   KernelWithParams(std::vector<const Tensor *> inputs, std::vector<Tensor *> outputs,
61                    const Params &params)
62       : Kernel(std::move(inputs), std::move(outputs)), _params(params)
63   {
64   }
65
66 public:
67   const Params &params() const { return _params; }
68
69 protected:
70   const Params _params;
71 };
72
73 } // namespace luci_interpreter
74
75 #endif // LUCI_INTERPRETER_CORE_KERNEL_H