Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / src / kernels / Pow.cpp
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 #include "kernels/Pow.h"
18 #include "kernels/Utils.h"
19
20 #include <tensorflow/lite/kernels/internal/reference/reference_ops.h>
21
22 namespace luci_interpreter
23 {
24 namespace kernels
25 {
26
27 Pow::Pow(const Tensor *input1, const Tensor *input2, Tensor *output)
28   : Kernel({input1, input2}, {output})
29 {
30 }
31
32 void Pow::configure()
33 {
34   LUCI_INTERPRETER_CHECK(input1()->element_type() == input2()->element_type());
35   LUCI_INTERPRETER_CHECK(input1()->element_type() == output()->element_type());
36   // TODO: enable it only if kernel with dynamic shapes
37   output()->resize(calculateShapeForBroadcast(input1()->shape(), input2()->shape()));
38 }
39
40 void Pow::execute() const
41 {
42   switch (input1()->element_type())
43   {
44     case DataType::FLOAT32:
45       eval<float>();
46       break;
47     case DataType::S32:
48       eval<int32_t>();
49       break;
50     default:
51       assert(false && "Unsupported type.");
52   }
53 }
54
55 template <typename T> void Pow::eval() const
56 {
57   tflite::ArithmeticParams params{};
58
59   const bool need_broadcast = tflite::reference_ops::ProcessBroadcastShapes(
60     getTensorShape(input1()), getTensorShape(input2()), &params);
61
62   if (need_broadcast)
63   {
64     tflite::reference_ops::BroadcastPow4DSlow(getTensorShape(input1()), getTensorData<T>(input1()),
65                                               getTensorShape(input2()), getTensorData<T>(input2()),
66                                               getTensorShape(output()), getTensorData<T>(output()));
67   }
68   else
69   {
70     tflite::reference_ops::Pow(getTensorShape(input1()), getTensorData<T>(input1()),
71                                getTensorShape(input2()), getTensorData<T>(input2()),
72                                getTensorShape(output()), getTensorData<T>(output()));
73   }
74 }
75
76 } // namespace kernels
77 } // namespace luci_interpreter