33 : _input(nullptr), _output(nullptr), _kernel()
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)
49 const float dequantized = input_scale * (val - input_zero_point);
50 float transformed = 0.f;
53 transformed = std::tanh(dequantized);
57 transformed = 1.0f / (1.0f + std::exp(-dequantized));
61 throw std::runtime_error(
"ElementwiseActivationLayer : unsupported activation type");
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));
73 const uint8_t *input_data = getBuffer<uint8_t>(input);
74 uint8_t *output_data = getBuffer<uint8_t>(output);
76 for (
int i = 0; i <
size; ++i)
78 output_data[i] =
_table[input_data[i]];
83 float alpha,
float beta,
bool approximate,
92 if (input->data_type() == OperandType::FLOAT32)
96 getBuffer<float>(output));
101 throw std::runtime_error{
"ElementwiseActivationLayer(Elu): unsupported data type"};
109 std::placeholders::_1, std::placeholders::_2);
115 getBuffer<float>(output));
120 throw std::runtime_error{
"ElementwiseActivationLayer(Logistic): unsupported data type"};
126 if (alpha == std::numeric_limits<float>::infinity() && beta == 0.f)
130 getBuffer<float>(output));
133 else if (alpha == 6.f && beta == 0.f)
137 getBuffer<float>(output));
142 throw std::runtime_error(
143 "ElementwiseActivationLayer : This layer suppports only ReLU(0-inf) and ReLU6(0-6)");
148 throw std::runtime_error{
"ElementwiseActivationLayer(ReLU): unsupported data type"};
156 std::placeholders::_1, std::placeholders::_2);
162 getBuffer<float>(output));
167 throw std::runtime_error{
"ElementwiseActivationLayer(Tanh): unsupported data type"};
175 getBuffer<float>(input),
getShape(output),
176 getBuffer<float>(output));
181 throw std::runtime_error{
"ElementwiseActivationLayer(LeakyReLU): unsupported data type"};
189 getBuffer<float>(input),
getShape(output), getBuffer<float>(output));
194 throw std::runtime_error{
"ElementwiseActivationLayer(GELU): unsupported data type"};
198 throw std::runtime_error(
"ElementwiseActivationLayer: unsupported op type");
int MatchingFlatSize(const Dims< N > &dims, const Dims< N > &check_dims_0)
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)
ElementwiseActivationLayer()
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)
const IPortableTensor * _input
IPortableTensor * _output
void LeakyReLU(const LeakyReluParams ¶ms, const Shape &input_shape, const float *input_data, const Shape &output_shape, float *output_data)
void Logistic(const Shape &input_shape, const float *input_data, const Shape &output_shape, float *output_data)
void Tanh(const Shape &input_shape, const float *input_data, const Shape &output_shape, float *output_data)
void GELU(const GELUParams ¶ms, const Shape &input_shape, const float *input_data, const Shape &output_shape, float *output_data)
void ReLU6(const Shape &input_shape, const float *input_data, const Shape &output_shape, float *output_data)
void ELU(const Shape &input_shape, const float *input_data, const Shape &output_shape, float *output_data)
void ReLU(const Shape &input_shape, const float *input_data, const Shape &output_shape, float *output_data)
nnfw::cker::Shape getShape(const IPortableTensor *tensor)
ElementwiseActivationType