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/LeakyRelu.h"
20 #include "kernels/Utils.h"
22 #include <tensorflow/lite/kernels/internal/reference/leaky_relu.h>
24 #include "PALLeakyRelu.h"
26 namespace luci_interpreter
32 LeakyRelu::LeakyRelu(const Tensor *input, Tensor *output, const LeakyReluParams ¶ms)
33 : KernelWithParams<LeakyReluParams>({input}, {output}, params)
37 void LeakyRelu::configure()
39 LUCI_INTERPRETER_CHECK(input()->element_type() == output()->element_type());
40 if (input()->element_type() == DataType::U8)
42 double alpha_multiplier = input()->scale() * params().alpha / output()->scale();
43 quantizeMultiplier(alpha_multiplier, &_output_multiplier_alpha, &_output_shift_alpha);
44 double identity_multiplier = input()->scale() / output()->scale();
45 quantizeMultiplier(identity_multiplier, &_output_multiplier_identity, &_output_shift_identity);
47 // TODO: enable it only if kernel with dynamic shapes
48 output()->resize(input()->shape());
51 void LeakyRelu::execute() const
53 switch (input()->element_type())
55 case DataType::FLOAT32:
62 assert(false && "Unsupported type.");
66 void LeakyRelu::evalFloat() const
68 tflite::LeakyReluParams op_params{};
69 op_params.alpha = params().alpha;
70 luci_interpreter_pal::LeakyRelu(op_params, getTensorShape(input()), getTensorData<float>(input()),
71 getTensorShape(output()), getTensorData<float>(output()));
74 void LeakyRelu::evalQuantized() const
76 tflite::LeakyReluParams op_params{};
77 op_params.input_offset = input()->zero_point();
78 op_params.output_offset = output()->zero_point();
79 op_params.output_multiplier_alpha = _output_multiplier_alpha;
80 op_params.output_shift_alpha = _output_shift_alpha;
81 op_params.output_multiplier_identity = _output_multiplier_identity;
82 op_params.output_shift_identity = _output_shift_identity;
84 tflite::reference_ops::QuantizeLeakyRelu(
85 op_params, getTensorShape(input()), getTensorData<uint8_t>(input()), getTensorShape(output()),
86 getTensorData<uint8_t>(output()));
89 } // namespace kernels
90 } // namespace luci_interpreter