19#include "kernels/Utils.h"
22#include <tensorflow/lite/kernels/internal/reference/tanh.h>
34 if (
input()->element_type() == DataType::U8)
36 populateLookupTable();
43 switch (
input()->element_type())
45 case DataType::FLOAT32:
52 throw std::runtime_error(
"luci-intp Tanh Unsupported type.");
56void Tanh::evalFloat()
const
62void Tanh::evalQuantized()
const
65 uint8_t *output_data = getTensorData<uint8_t>(
output());
66 const uint8_t *input_data = getTensorData<uint8_t>(
input());
67 for (
int i = 0; i <
size; ++i)
69 output_data[i] = getTableValue(input_data[i]);
73void Tanh::populateLookupTable()
75 const auto input_scale =
static_cast<double>(
input()->
scale());
76 const auto input_zero_point =
static_cast<int32_t
>(
input()->
zero_point());
77 const auto output_scale =
static_cast<double>(
output()->
scale());
79 const float inverse_scale = 1 / output_scale;
80 int32_t maxval = std::numeric_limits<uint8_t>::max();
81 int32_t minval = std::numeric_limits<uint8_t>::min();
82 for (int32_t val = minval; val <= maxval; ++val)
84 const float dequantized = input_scale * (val - input_zero_point);
85 const float transformed = std::tanh(dequantized);
86 const float rescaled = std::round(transformed * inverse_scale);
87 const int32_t quantized =
static_cast<int32_t
>(rescaled + output_zero_point);
88 setTableValue(
static_cast<uint8_t
>(std::max(std::min(maxval, quantized), minval)),
89 static_cast<uint8_t
>(val));
void resize(const Shape &new_shape)
int32_t zero_point() const
void execute() const override
void configure() override
const Tensor * input() const
Tanh(const Tensor *input, Tensor *output)
#define LUCI_INTERPRETER_CHECK(cond)
tflite::RuntimeShape getTensorShape(const Tensor *tensor)