Imported Upstream version 1.18.0
[platform/core/ml/nnfw.git] / compiler / luci-interpreter / src / kernels / Tanh.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/Tanh.h"
18
19 #include "kernels/Utils.h"
20
21 #include <tensorflow/lite/kernels/internal/reference/tanh.h>
22
23 namespace luci_interpreter
24 {
25 namespace kernels
26 {
27
28 Tanh::Tanh(const Tensor *input, Tensor *output) : Kernel({input}, {output}) {}
29
30 void Tanh::configure()
31 {
32   LUCI_INTERPRETER_CHECK(input()->element_type() == output()->element_type());
33   if (input()->element_type() == DataType::U8)
34   {
35     populateLookupTable();
36   }
37   output()->resize(input()->shape());
38 }
39
40 void Tanh::execute() const
41 {
42   switch (input()->element_type())
43   {
44     case DataType::FLOAT32:
45       evalFloat();
46       break;
47     case DataType::U8:
48       evalQuantized();
49       break;
50     default:
51       throw std::runtime_error("Unsupported type.");
52   }
53 }
54
55 void Tanh::evalFloat() const
56 {
57   tflite::reference_ops::Tanh(getTensorShape(input()), getTensorData<float>(input()),
58                               getTensorShape(output()), getTensorData<float>(output()));
59 }
60
61 void Tanh::evalQuantized() const
62 {
63   const int size = tflite::MatchingFlatSize(getTensorShape(input()), getTensorShape(output()));
64   uint8_t *output_data = getTensorData<uint8_t>(output());
65   const uint8_t *input_data = getTensorData<uint8_t>(input());
66   for (int i = 0; i < size; ++i)
67   {
68     output_data[i] = getTableValue(input_data[i]);
69   }
70 }
71
72 void Tanh::populateLookupTable()
73 {
74   const auto input_scale = static_cast<double>(input()->scale());
75   const auto input_zero_point = static_cast<int32_t>(input()->zero_point());
76   const auto output_scale = static_cast<double>(output()->scale());
77   const auto output_zero_point = static_cast<int32_t>(output()->zero_point());
78   const float inverse_scale = 1 / output_scale;
79   int32_t maxval = std::numeric_limits<uint8_t>::max();
80   int32_t minval = std::numeric_limits<uint8_t>::min();
81   for (int32_t val = minval; val <= maxval; ++val)
82   {
83     const float dequantized = input_scale * (val - input_zero_point);
84     const float transformed = std::tanh(dequantized);
85     const float rescaled = std::round(transformed * inverse_scale);
86     const int32_t quantized = static_cast<int32_t>(rescaled + output_zero_point);
87     setTableValue(static_cast<uint8_t>(std::max(std::min(maxval, quantized), minval)),
88                   static_cast<uint8_t>(val));
89   }
90 }
91
92 } // namespace kernels
93 } // namespace luci_interpreter