ONE - On-device Neural Engine
Loading...
Searching...
No Matches
LeakyRelu.cpp
Go to the documentation of this file.
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/leaky_relu.h>
22
23#include "PALLeakyRelu.h"
24
25#include <stdexcept>
26
27namespace luci_interpreter
28{
29
30namespace kernels
31{
32
33LeakyRelu::LeakyRelu(const Tensor *input, Tensor *output, const LeakyReluParams &params)
34 : KernelWithParams<LeakyReluParams>({input}, {output}, params)
35{
36}
37
39{
40 LUCI_INTERPRETER_CHECK(input()->element_type() == output()->element_type());
41 if (input()->element_type() == DataType::U8)
42 {
43 double alpha_multiplier = input()->scale() * params().alpha / output()->scale();
44 quantizeMultiplier(alpha_multiplier, &_output_multiplier_alpha, &_output_shift_alpha);
45 double identity_multiplier = input()->scale() / output()->scale();
46 quantizeMultiplier(identity_multiplier, &_output_multiplier_identity, &_output_shift_identity);
47 }
48 output()->resize(input()->shape());
49}
50
52{
53 switch (input()->element_type())
54 {
55 case DataType::FLOAT32:
56 evalFloat();
57 break;
58 case DataType::U8:
59 evalQuantized();
60 break;
61 default:
62 throw std::runtime_error("luci-intp LeakyRelu Unsupported type.");
63 }
64}
65
66void LeakyRelu::evalFloat() const
67{
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()));
72}
73
74void LeakyRelu::evalQuantized() const
75{
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;
83
84 tflite::reference_ops::QuantizeLeakyRelu(
85 op_params, getTensorShape(input()), getTensorData<uint8_t>(input()), getTensorShape(output()),
86 getTensorData<uint8_t>(output()));
87}
88
89} // namespace kernels
90} // namespace luci_interpreter
const LeakyReluParams & params() const
Definition Kernel.h:67
void resize(const Shape &new_shape)
Definition Tensor.cpp:56
float scale() const
Definition Tensor.h:109
int32_t zero_point() const
Definition Tensor.h:115
const Tensor * input() const
Definition LeakyRelu.h:33
LeakyRelu(const Tensor *input, Tensor *output, const LeakyReluParams &params)
Definition LeakyRelu.cpp:33
#define LUCI_INTERPRETER_CHECK(cond)
Definition Utils.h:36
tflite::RuntimeShape getTensorShape(const Tensor *tensor)
Definition Utils.h:194
void quantizeMultiplier(double double_multiplier, int32_t *quantized_multiplier, int *shift)
Definition Utils.cpp:157