38 : _input(nullptr), _output(nullptr), _kernel()
49 const float inverse_scale = 1 / output_scale;
50 int32_t maxval = std::numeric_limits<uint8_t>::max();
51 int32_t minval = std::numeric_limits<uint8_t>::min();
52 for (int32_t val = minval; val <= maxval; ++val)
54 const float dequantized = input_scale * (val - input_zero_point);
55 float transformed = 0.f;
58 transformed = std::tanh(dequantized);
62 transformed = 1.0f / (1.0f + std::exp(-dequantized));
66 throw std::runtime_error(
"ElementwiseActivationLayer : unsupported activation type");
68 const float rescaled = std::round(transformed * inverse_scale);
69 const int32_t quantized =
static_cast<int32_t
>(rescaled + output_zero_point);
70 _table[val] =
static_cast<uint8_t
>(std::max(std::min(maxval, quantized), minval));
78 const uint8_t *input_data = getBuffer<uint8_t>(input);
79 uint8_t *output_data = getBuffer<uint8_t>(output);
81 for (
int i = 0; i <
size; ++i)
83 output_data[i] =
_table[input_data[i]];
88 float alpha,
float beta,
97 if (input->data_type() == OperandType::FLOAT32)
101 getBuffer<float>(output));
106 throw std::runtime_error{
"ElementwiseActivationLayer(Elu): unsupported data type"};
114 std::placeholders::_1, std::placeholders::_2);
120 getBuffer<float>(output));
125 throw std::runtime_error{
"ElementwiseActivationLayer(Logistic): unsupported data type"};
131 if (alpha == std::numeric_limits<float>::infinity() && beta == 0.f)
135 getBuffer<float>(output));
138 else if (alpha == 6.f && beta == 0.f)
142 getBuffer<float>(output));
147 throw std::runtime_error(
148 "ElementwiseActivationLayer : This layer suppports only ReLU(0-inf) and ReLU6(0-6)");
153 throw std::runtime_error{
"ElementwiseActivationLayer(ReLU): unsupported data type"};
161 std::placeholders::_1, std::placeholders::_2);
167 getBuffer<float>(output));
172 throw std::runtime_error{
"ElementwiseActivationLayer(Tanh): unsupported data type"};
180 getBuffer<float>(input),
getShape(output),
181 getBuffer<float>(output));
186 throw std::runtime_error{
"ElementwiseActivationLayer(LeakyReLU): unsupported data type"};
190 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, 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 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