2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "kernels/Relu.h"
18 #include "kernels/Utils.h"
24 namespace luci_interpreter
30 Relu::Relu(const Tensor *input, Tensor *output) : Kernel({input}, {output}) {}
32 void Relu::configure()
34 LUCI_INTERPRETER_CHECK(input()->element_type() == output()->element_type());
35 if (input()->element_type() == DataType::S16)
37 LUCI_INTERPRETER_CHECK(input()->zero_point() == 0 && output()->zero_point() == 0);
40 if (input()->element_type() == DataType::U8 || input()->element_type() == DataType::S16)
42 double multiplier = input()->scale() / output()->scale();
43 quantizeMultiplier(multiplier, &_output_multiplier, &_output_shift);
45 output()->resize(input()->shape());
48 void Relu::execute() const
50 switch (input()->element_type())
52 case DataType::FLOAT32:
62 throw std::runtime_error("Unsupported type.");
66 void Relu::evalFloat() const
68 const auto input_data = getTensorData<float>(input());
69 const auto input_shape = getTensorShape(input());
70 auto output_data = getTensorData<float>(output());
71 auto output_shape = getTensorShape(output());
73 luci_interpreter_pal::Relu(input_shape, input_data, output_shape, output_data);
76 void Relu::evalQuantized() const
78 tflite::ReluParams params;
79 params.input_offset = input()->zero_point();
80 params.output_offset = output()->zero_point();
81 params.output_multiplier = _output_multiplier;
82 params.output_shift = _output_shift;
84 params.quantized_activation_min =
85 std::max(static_cast<int32_t>(std::numeric_limits<uint8_t>::min()), params.output_offset);
86 params.quantized_activation_max = static_cast<int32_t>(std::numeric_limits<uint8_t>::max());
88 luci_interpreter_pal::ReluX(params, getTensorShape(input()), getTensorData<uint8_t>(input()),
89 getTensorShape(output()), getTensorData<uint8_t>(output()));
92 void Relu::evalQuantizedS16() const
94 const auto *input_data = getTensorData<int16_t>(input());
95 auto *output_data = getTensorData<int16_t>(output());
97 constexpr int32_t output_min = 0;
98 constexpr int32_t output_max = std::numeric_limits<int16_t>::max();
100 const int32_t num_elements = input()->shape().num_elements();
102 for (int32_t i = 0; i < num_elements; ++i)
104 const int32_t input_val = input_data[i];
106 tflite::MultiplyByQuantizedMultiplier(input_val, _output_multiplier, _output_shift);
107 output_val = std::max(output_val, output_min);
108 output_val = std::min(output_val, output_max);
109 output_data[i] = static_cast<int16_t>(output_val);
113 } // namespace kernels
114 } // namespace luci_interpreter