2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2019 The TensorFlow Authors. All Rights Reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "kernels/Relu6.h"
19 #include "kernels/Utils.h"
23 namespace luci_interpreter
29 Relu6::Relu6(const Tensor *input, Tensor *output) : Kernel({input}, {output}) {}
31 void Relu6::configure()
33 LUCI_INTERPRETER_CHECK(input()->element_type() == output()->element_type());
35 if (input()->element_type() == DataType::U8)
37 double multiplier = input()->scale() / output()->scale();
38 quantizeMultiplier(multiplier, &_output_multiplier, &_output_shift);
40 // TODO: enable it only if kernel with dynamic shapes
41 output()->resize(input()->shape());
44 void Relu6::execute() const
46 switch (input()->element_type())
48 case DataType::FLOAT32:
55 assert(false && "Unsupported type.");
59 void Relu6::evalFloat() const
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());
66 luci_interpreter_pal::Relu6(input_shape, input_data, output_shape, output_data);
69 void Relu6::evalQuantized() const
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;
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())));
83 luci_interpreter_pal::ReluX(params, getTensorShape(input()), getTensorData<uint8_t>(input()),
84 getTensorShape(output()), getTensorData<uint8_t>(output()));
87 } // namespace kernels
88 } // namespace luci_interpreter