0449708f3237b196ff3f277b7179510975660641
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / src / kernels / Relu6.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  * Copyright 2019 The TensorFlow Authors. All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *    http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include "kernels/Relu6.h"
19 #include "kernels/Utils.h"
20
21 #include "PALRelu6.h"
22
23 namespace luci_interpreter
24 {
25
26 namespace kernels
27 {
28
29 Relu6::Relu6(const Tensor *input, Tensor *output) : Kernel({input}, {output}) {}
30
31 void Relu6::configure()
32 {
33   LUCI_INTERPRETER_CHECK(input()->element_type() == output()->element_type());
34
35   if (input()->element_type() == DataType::U8)
36   {
37     double multiplier = input()->scale() / output()->scale();
38     quantizeMultiplier(multiplier, &_output_multiplier, &_output_shift);
39   }
40   // TODO: enable it only if kernel with dynamic shapes
41   output()->resize(input()->shape());
42 }
43
44 void Relu6::execute() const
45 {
46   switch (input()->element_type())
47   {
48     case DataType::FLOAT32:
49       evalFloat();
50       break;
51     case DataType::U8:
52       evalQuantized();
53       break;
54     default:
55       assert(false && "Unsupported type.");
56   }
57 }
58
59 void Relu6::evalFloat() const
60 {
61   const auto input_data = getTensorData<float>(input());
62   const auto input_shape = getTensorShape(input());
63   auto output_data = getTensorData<float>(output());
64   auto output_shape = getTensorShape(output());
65
66   luci_interpreter_pal::Relu6(input_shape, input_data, output_shape, output_data);
67 }
68
69 void Relu6::evalQuantized() const
70 {
71   tflite::ReluParams params;
72   params.input_offset = input()->zero_point();
73   params.output_offset = output()->zero_point();
74   params.output_multiplier = _output_multiplier;
75   params.output_shift = _output_shift;
76
77   params.quantized_activation_min =
78     std::max(static_cast<int32_t>(std::numeric_limits<uint8_t>::min()), params.output_offset);
79   params.quantized_activation_max =
80     std::min(static_cast<int32_t>(std::numeric_limits<uint8_t>::max()),
81              params.output_offset + static_cast<int32>(roundf(6.f / output()->scale())));
82
83   luci_interpreter_pal::ReluX(params, getTensorShape(input()), getTensorData<uint8_t>(input()),
84                               getTensorShape(output()), getTensorData<uint8_t>(output()));
85 }
86
87 } // namespace kernels
88 } // namespace luci_interpreter