ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ElementwiseActivationLayer.cc
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
18
19#include "OperationUtils.h"
20
21#include <cker/operation/ELU.h>
24#include <cker/operation/ReLU.h>
26#include <cker/operation/Tanh.h>
27#include <cker/operation/GELU.h>
28
30{
31
33 : _input(nullptr), _output(nullptr), _kernel()
34{
35 // DO NOTHING
36}
37
39{
40 const auto input_scale = static_cast<double>(_input->data_scale());
41 const auto input_zero_point = static_cast<int32_t>(_input->data_zero_point());
42 const auto output_scale = static_cast<double>(_output->data_scale());
43 const auto output_zero_point = static_cast<int32_t>(_output->data_zero_point());
44 const float inverse_scale = 1 / output_scale;
45 int32_t maxval = std::numeric_limits<uint8_t>::max();
46 int32_t minval = std::numeric_limits<uint8_t>::min();
47 for (int32_t val = minval; val <= maxval; ++val)
48 {
49 const float dequantized = input_scale * (val - input_zero_point);
50 float transformed = 0.f;
52 {
53 transformed = std::tanh(dequantized);
54 }
55 else if (op_type == ElementwiseActivationType::kLogistic)
56 {
57 transformed = 1.0f / (1.0f + std::exp(-dequantized));
58 }
59 else
60 {
61 throw std::runtime_error("ElementwiseActivationLayer : unsupported activation type");
62 }
63 const float rescaled = std::round(transformed * inverse_scale);
64 const int32_t quantized = static_cast<int32_t>(rescaled + output_zero_point);
65 _table[val] = static_cast<uint8_t>(std::max(std::min(maxval, quantized), minval));
66 }
67}
68
70 IPortableTensor *output)
71{
72 const int size = MatchingFlatSize(getShape(input), getShape(output));
73 const uint8_t *input_data = getBuffer<uint8_t>(input);
74 uint8_t *output_data = getBuffer<uint8_t>(output);
75
76 for (int i = 0; i < size; ++i)
77 {
78 output_data[i] = _table[input_data[i]];
79 }
80}
81
83 float alpha, float beta, bool approximate,
85{
86 _input = input;
87 _output = output;
88
89 switch (op_type)
90 {
92 if (input->data_type() == OperandType::FLOAT32)
93 {
94 _kernel = [](const IPortableTensor *input, IPortableTensor *output) {
95 nnfw::cker::ELU(getShape(input), getBuffer<float>(input), getShape(output),
96 getBuffer<float>(output));
97 };
98 }
99 else
100 {
101 throw std::runtime_error{"ElementwiseActivationLayer(Elu): unsupported data type"};
102 }
103 break;
105 if (_input->data_type() == OperandType::QUANT_UINT8_ASYMM)
106 {
107 PopulateLookupTable(op_type);
109 std::placeholders::_1, std::placeholders::_2);
110 }
111 else if (_input->data_type() == OperandType::FLOAT32)
112 {
113 _kernel = [](const IPortableTensor *input, IPortableTensor *output) {
114 nnfw::cker::Logistic(getShape(input), getBuffer<float>(input), getShape(output),
115 getBuffer<float>(output));
116 };
117 }
118 else
119 {
120 throw std::runtime_error{"ElementwiseActivationLayer(Logistic): unsupported data type"};
121 }
122 break;
124 if (_input->data_type() == OperandType::FLOAT32)
125 {
126 if (alpha == std::numeric_limits<float>::infinity() && beta == 0.f)
127 {
128 _kernel = [](const IPortableTensor *input, IPortableTensor *output) {
129 nnfw::cker::ReLU(getShape(input), getBuffer<float>(input), getShape(output),
130 getBuffer<float>(output));
131 };
132 }
133 else if (alpha == 6.f && beta == 0.f)
134 {
135 _kernel = [](const IPortableTensor *input, IPortableTensor *output) {
136 nnfw::cker::ReLU6(getShape(input), getBuffer<float>(input), getShape(output),
137 getBuffer<float>(output));
138 };
139 }
140 else
141 {
142 throw std::runtime_error(
143 "ElementwiseActivationLayer : This layer suppports only ReLU(0-inf) and ReLU6(0-6)");
144 }
145 }
146 else
147 {
148 throw std::runtime_error{"ElementwiseActivationLayer(ReLU): unsupported data type"};
149 }
150 break;
152 if (_input->data_type() == OperandType::QUANT_UINT8_ASYMM)
153 {
154 PopulateLookupTable(op_type);
156 std::placeholders::_1, std::placeholders::_2);
157 }
158 else if (_input->data_type() == OperandType::FLOAT32)
159 {
160 _kernel = [](const IPortableTensor *input, IPortableTensor *output) {
161 nnfw::cker::Tanh(getShape(input), getBuffer<float>(input), getShape(output),
162 getBuffer<float>(output));
163 };
164 }
165 else
166 {
167 throw std::runtime_error{"ElementwiseActivationLayer(Tanh): unsupported data type"};
168 }
169 break;
171 if (_input->data_type() == OperandType::FLOAT32)
172 {
173 _kernel = [alpha](const IPortableTensor *input, IPortableTensor *output) {
175 getBuffer<float>(input), getShape(output),
176 getBuffer<float>(output));
177 };
178 }
179 else
180 {
181 throw std::runtime_error{"ElementwiseActivationLayer(LeakyReLU): unsupported data type"};
182 }
183 break;
185 if (_input->data_type() == OperandType::FLOAT32)
186 {
187 _kernel = [approximate](const IPortableTensor *input, IPortableTensor *output) {
189 getBuffer<float>(input), getShape(output), getBuffer<float>(output));
190 };
191 }
192 else
193 {
194 throw std::runtime_error{"ElementwiseActivationLayer(GELU): unsupported data type"};
195 }
196 break;
197 default:
198 throw std::runtime_error("ElementwiseActivationLayer: unsupported op type");
199 }
200}
201
203
204} // namespace onert::backend::cpu::ops
int MatchingFlatSize(const Dims< N > &dims, const Dims< N > &check_dims_0)
Definition Dims.h:108
A tensor class that is portable for other backends.
float data_scale() const override final
int32_t data_zero_point() const override final
ir::DataType data_type() const override final
void PopulateLookupTable(const ElementwiseActivationType op_type)
std::function< void(const IPortableTensor *input, IPortableTensor *output)> _kernel
void configure(const IPortableTensor *input, IPortableTensor *output, float alpha, float beta, bool approximate, const ElementwiseActivationType op_type)
void EvalUsingLookupTable(const IPortableTensor *input, IPortableTensor *output)
void LeakyReLU(const LeakyReluParams &params, const Shape &input_shape, const float *input_data, const Shape &output_shape, float *output_data)
Definition LeakyReLU.h:31
void Logistic(const Shape &input_shape, const float *input_data, const Shape &output_shape, float *output_data)
Definition Logistic.h:32
void Tanh(const Shape &input_shape, const float *input_data, const Shape &output_shape, float *output_data)
Definition Tanh.h:31
void GELU(const GELUParams &params, const Shape &input_shape, const float *input_data, const Shape &output_shape, float *output_data)
Definition GELU.h:40
void ReLU6(const Shape &input_shape, const float *input_data, const Shape &output_shape, float *output_data)
Definition ReLU6.h:32
void ELU(const Shape &input_shape, const float *input_data, const Shape &output_shape, float *output_data)
Definition ELU.h:30
void ReLU(const Shape &input_shape, const float *input_data, const Shape &output_shape, float *output_data)
Definition ReLU.h:32
nnfw::cker::Shape getShape(const IPortableTensor *tensor)
int32_t size[5]
Definition Slice.cpp:35