ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Tanh.cpp
Go to the documentation of this file.
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#include <limits> // std::numeric_limits
21
22#include <tensorflow/lite/kernels/internal/reference/tanh.h>
23
24namespace luci_interpreter
25{
26namespace kernels
27{
28
29Tanh::Tanh(const Tensor *input, Tensor *output) : Kernel({input}, {output}) {}
30
32{
33 LUCI_INTERPRETER_CHECK(input()->element_type() == output()->element_type());
34 if (input()->element_type() == DataType::U8)
35 {
36 populateLookupTable();
37 }
38 output()->resize(input()->shape());
39}
40
41void Tanh::execute() const
42{
43 switch (input()->element_type())
44 {
45 case DataType::FLOAT32:
46 evalFloat();
47 break;
48 case DataType::U8:
49 evalQuantized();
50 break;
51 default:
52 throw std::runtime_error("luci-intp Tanh Unsupported type.");
53 }
54}
55
56void Tanh::evalFloat() const
57{
58 tflite::reference_ops::Tanh(getTensorShape(input()), getTensorData<float>(input()),
59 getTensorShape(output()), getTensorData<float>(output()));
60}
61
62void Tanh::evalQuantized() const
63{
64 const int size = tflite::MatchingFlatSize(getTensorShape(input()), getTensorShape(output()));
65 uint8_t *output_data = getTensorData<uint8_t>(output());
66 const uint8_t *input_data = getTensorData<uint8_t>(input());
67 for (int i = 0; i < size; ++i)
68 {
69 output_data[i] = getTableValue(input_data[i]);
70 }
71}
72
73void Tanh::populateLookupTable()
74{
75 const auto input_scale = static_cast<double>(input()->scale());
76 const auto input_zero_point = static_cast<int32_t>(input()->zero_point());
77 const auto output_scale = static_cast<double>(output()->scale());
78 const auto output_zero_point = static_cast<int32_t>(output()->zero_point());
79 const float inverse_scale = 1 / output_scale;
80 int32_t maxval = std::numeric_limits<uint8_t>::max();
81 int32_t minval = std::numeric_limits<uint8_t>::min();
82 for (int32_t val = minval; val <= maxval; ++val)
83 {
84 const float dequantized = input_scale * (val - input_zero_point);
85 const float transformed = std::tanh(dequantized);
86 const float rescaled = std::round(transformed * inverse_scale);
87 const int32_t quantized = static_cast<int32_t>(rescaled + output_zero_point);
88 setTableValue(static_cast<uint8_t>(std::max(std::min(maxval, quantized), minval)),
89 static_cast<uint8_t>(val));
90 }
91}
92
93} // namespace kernels
94} // namespace luci_interpreter
void resize(const Shape &new_shape)
Definition Tensor.cpp:56
float scale() const
Definition Tensor.h:109
int32_t zero_point() const
Definition Tensor.h:115
void execute() const override
Definition Tanh.cpp:41
void configure() override
Definition Tanh.cpp:31
const Tensor * input() const
Definition Tanh.h:32
Tensor * output() const
Definition Tanh.h:33
Tanh(const Tensor *input, Tensor *output)
Definition Tanh.cpp:29
#define LUCI_INTERPRETER_CHECK(cond)
Definition Utils.h:36
tflite::RuntimeShape getTensorShape(const Tensor *tensor)
Definition Utils.h:194
int32_t size[5]
Definition Slice.cpp:35