fce01a605b20c89b742acf1243ac614a17c6b0c9
[platform/core/ml/nnfw.git] / compiler / luci-interpreter / src / kernels / LeakyRelu.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/LeakyRelu.h"
18
19 #include "kernels/Utils.h"
20
21 #include <tensorflow/lite/kernels/internal/reference/reference_ops.h>
22 #include <tensorflow/lite/kernels/internal/optimized/optimized_ops.h>
23
24 #include <stdexcept>
25
26 namespace luci_interpreter
27 {
28
29 namespace kernels
30 {
31
32 LeakyRelu::LeakyRelu(const Tensor *input, Tensor *output, const LeakyReluParams &params)
33     : KernelWithParams<LeakyReluParams>({input}, {output}, params)
34 {
35 }
36
37 void LeakyRelu::configure()
38 {
39   assert(input()->element_type() == output()->element_type());
40   if (input()->element_type() == DataType::U8)
41   {
42     _q_alpha = static_cast<uint8_t>(std::max<float>(
43         std::numeric_limits<uint8_t>::min(),
44         std::min<float>(std::numeric_limits<uint8_t>::max(),
45                         std::round(input()->zero_point() + (params().alpha / input()->scale())))));
46     double real_multiplier = input()->scale() * input()->scale() / output()->scale();
47     quantizeMultiplierSmallerThanOneExp(real_multiplier, &_output_multiplier, &_output_shift);
48   }
49   output()->resize(input()->shape());
50 }
51
52 void LeakyRelu::execute() const
53 {
54   switch (input()->element_type())
55   {
56     case DataType::FLOAT32:
57       evalFloat();
58       break;
59     case DataType::U8:
60       evalQuantized();
61       break;
62     default:
63       throw std::runtime_error("Unsupported type.");
64   }
65 }
66
67 void LeakyRelu::evalFloat() const
68 {
69   tflite::LeakyReluParams op_params{};
70   op_params.alpha = params().alpha;
71   tflite::optimized_ops::LeakyRelu(op_params, getTensorShape(input()),
72                                    getTensorData<float>(input()), getTensorShape(output()),
73                                    getTensorData<float>(output()));
74 }
75
76 void LeakyRelu::evalQuantized() const
77 {
78   tflite::LeakyReluParams op_params{};
79   op_params.input_offset = input()->zero_point();
80   op_params.alpha_offset = input()->zero_point();
81   op_params.output_offset = output()->zero_point();
82
83   op_params.output_multiplier = _output_multiplier;
84   op_params.output_shift = _output_shift;
85
86   tflite::reference_ops::QuantizeLeakyRelu(
87       op_params, _q_alpha, getTensorShape(input()), getTensorData<uint8_t>(input()),
88       getTensorShape(output()), getTensorData<uint8_t>(output()));
89 }
90
91 } // namespace kernels
92 } // namespace luci_interpreter