19#include "kernels/Utils.h"
21#include "PALUnidirectionalSequenceLSTM.h"
35 const float x_log2 = std::log(x) * (1.0f / std::log(2.0f));
36 const float x_log2_rounded = std::round(x_log2);
37 const float x_log2_fracpart = x_log2 - x_log2_rounded;
39 *log2_result =
static_cast<int>(x_log2_rounded);
40 return std::abs(x_log2_fracpart) < 1e-3f;
50createInterGateParams(
const float input1_scale,
const float input2_scale,
const float output_scale,
51 const DataType output_type,
const int output_zp)
54 if (output_type == DataType::S16)
59 else if (output_type == DataType::S8)
69 const double input_product_scale =
70 static_cast<double>(input1_scale) *
static_cast<double>(input2_scale);
71 double effective_scale = input_product_scale /
static_cast<double>(output_scale);
72 auto output_shift =
static_cast<int>(op_params.
output_shift);
78void createGateParams(
const circle::Tensor *input,
const circle::Tensor *input_weight,
79 const circle::Tensor *input_bias,
const circle::Tensor *hidden_state,
80 const circle::Tensor *hidden_state_weight,
81 const float nonlinear_activation_input_scale,
const DataType cell_type,
82 lstm::GateParameters *gate_params)
87 double real_multiplier = 0.0;
89 int32_t output_activation_min;
90 int32_t output_activation_max;
91 int32_t output_multiplier;
93 Tensor::scale(input), Tensor::scale(input_weight), nonlinear_activation_input_scale);
96 nonlinear_activation_input_scale, cell_type,
97 &output_activation_min, &output_activation_max);
103 input_gate_params.
input_offset = -Tensor::zero_point(input);
104 input_gate_params.
weights_offset = -Tensor::zero_point(input_weight);
107 gate_params->input_fc_params = input_gate_params;
113 double real_multiplier = 0.0;
115 int32_t output_activation_min;
116 int32_t output_activation_max;
117 int32_t output_multiplier;
119 Tensor::scale(hidden_state_weight),
120 nonlinear_activation_input_scale);
123 nonlinear_activation_input_scale, cell_type,
124 &output_activation_min, &output_activation_max);
130 recurrent_gate_params.
input_offset = -Tensor::zero_point(hidden_state);
131 recurrent_gate_params.
weights_offset = -Tensor::zero_point(hidden_state_weight);
134 gate_params->recurrent_fc_params = recurrent_gate_params;
138void prepareGateParamsInteger(lstm::LSTMStruct *lstm_struct,
139 lstm::LSTMParameters *quant_lstm_params)
141 float nonlinear_input_scale = 0.00024414062;
143 createGateParams(lstm_struct->input(), lstm_struct->input_to_forget_weights(),
144 lstm_struct->forget_gate_bias(), lstm_struct->output_state(),
145 lstm_struct->recurrent_to_forget_weights(), nonlinear_input_scale, DataType::S16,
146 &quant_lstm_params->forget_gate_parameters);
148 createGateParams(lstm_struct->input(), lstm_struct->input_to_input_weights(),
149 lstm_struct->input_gate_bias(), lstm_struct->output_state(),
150 lstm_struct->recurrent_to_input_weights(), nonlinear_input_scale, DataType::S16,
151 &quant_lstm_params->input_gate_parameters);
154 createGateParams(lstm_struct->input(), lstm_struct->input_to_cell_weights(),
155 lstm_struct->cell_gate_bias(), lstm_struct->output_state(),
156 lstm_struct->recurrent_to_cell_weights(), nonlinear_input_scale, DataType::S16,
157 &quant_lstm_params->cell_gate_parameters);
160 createGateParams(lstm_struct->input(), lstm_struct->input_to_output_weights(),
161 lstm_struct->output_gate_bias(), lstm_struct->output_state(),
162 lstm_struct->recurrent_to_output_weights(), nonlinear_input_scale, DataType::S16,
163 &quant_lstm_params->output_gate_parameters);
166 float nonlinear_output_scale = 0.00003051757;
167 float cell_state_scale =
168 Tensor::scale(lstm_struct->cell_state());
170 quant_lstm_params->inter_gate_parameters.forget_cell_mul_params = createInterGateParams(
171 nonlinear_output_scale, cell_state_scale, cell_state_scale, DataType::S16, 0);
174 quant_lstm_params->inter_gate_parameters.input_mul_params = createInterGateParams(
175 nonlinear_output_scale, nonlinear_output_scale, cell_state_scale, DataType::S16, 0);
178 quant_lstm_params->inter_gate_parameters.output_mul_params = createInterGateParams(
179 nonlinear_output_scale, nonlinear_output_scale, Tensor::scale(lstm_struct->output_state()),
180 Tensor::element_type(lstm_struct->output_state()),
181 Tensor::zero_point(lstm_struct->output_state()));
187lstm::CellStateInfo createLstmCellStateInfo(
const float cell_state_scale,
const float cell_clip)
189 lstm::CellStateInfo cell_state_info;
193 cell_state_info.cell_state_scale_power = buffer;
195 cell_state_info.cell_clip = cell_clip;
196 cell_state_info.quantized_cell_clip =
static_cast<int16_t
>(std::min(
197 std::max(
static_cast<double>(cell_clip) /
static_cast<double>(cell_state_scale), -32768.0),
199 return cell_state_info;
202void evalInt8(
const circle::Operator *cur_op,
BaseRuntimeGraph *runtime_graph,
bool)
204 lstm::LSTMStruct lstm_struct(cur_op, runtime_graph);
206 lstm::LSTMParameters quant_lstm_params;
207 prepareGateParamsInteger(&lstm_struct, &quant_lstm_params);
209 lstm::CellStateInfo cell_state_info = createLstmCellStateInfo(
210 luci_interpreter::Tensor::scale(lstm_struct.cell_state()), lstm_struct.options->cell_clip());
212 const bool time_major = lstm_struct.options->time_major();
213 const auto batch_size =
215 const auto state_dimension =
Tensor::dim(lstm_struct.output_state(), 1);
216 const auto cell_state_type_size =
getDataTypeSize(Tensor::element_type(lstm_struct.cell_state()));
218 auto scratch_0_data =
219 std::make_unique<uint8_t[]>(batch_size * state_dimension * cell_state_type_size);
220 auto scratch_1_data =
221 std::make_unique<uint8_t[]>(batch_size * state_dimension * cell_state_type_size);
222 auto scratch_2_data =
223 std::make_unique<uint8_t[]>(batch_size * state_dimension * cell_state_type_size);
224 auto scratch_3_data =
225 std::make_unique<uint8_t[]>(batch_size * state_dimension * cell_state_type_size);
228 auto output_state_data =
229 std::make_unique<int8_t[]>(Tensor::num_elements(lstm_struct.output_state()));
230 std::fill_n(output_state_data.get(), Tensor::num_elements(lstm_struct.output_state()), 0);
233 auto cell_state_data =
234 std::make_unique<int16_t[]>(Tensor::num_elements(lstm_struct.cell_state()));
235 std::fill_n(cell_state_data.get(), Tensor::num_elements(lstm_struct.cell_state()), 0);
238 &lstm_struct, &quant_lstm_params, &cell_state_info, output_state_data.get(),
239 cell_state_data.get(), kernels::getTensorData<int16_t>(scratch_0_data.get()),
240 kernels::getTensorData<int16_t>(scratch_1_data.get()),
241 kernels::getTensorData<int16_t>(scratch_2_data.get()),
242 kernels::getTensorData<int16_t>(scratch_3_data.get()), runtime_graph);
258lstm::GateParameters createGateParamsFloat()
260 lstm::GateParameters gate_params;
262 gate_params.input_fc_params = createFcParamsFloat();
263 gate_params.recurrent_fc_params = createFcParamsFloat();
268lstm::CellStateInfo createLstmCellStateInfoFloat(
const float cell_clip)
270 lstm::CellStateInfo cell_state_info;
271 cell_state_info.cell_clip = cell_clip;
272 cell_state_info.cell_state_scale_power = 0;
273 cell_state_info.quantized_cell_clip = 0;
274 return cell_state_info;
277void prepareGateParamsFloat(lstm::LSTMParameters *float_lstm_params)
280 float_lstm_params->forget_gate_parameters = createGateParamsFloat();
281 float_lstm_params->input_gate_parameters = createGateParamsFloat();
282 float_lstm_params->cell_gate_parameters = createGateParamsFloat();
283 float_lstm_params->output_gate_parameters = createGateParamsFloat();
291 float_lstm_params->inter_gate_parameters.forget_cell_mul_params = op_params;
292 float_lstm_params->inter_gate_parameters.input_mul_params = op_params;
293 float_lstm_params->inter_gate_parameters.output_mul_params = op_params;
296void evalFloat(
const circle::Operator *cur_op,
BaseRuntimeGraph *runtime_graph,
bool)
298 lstm::LSTMStruct lstm_struct(cur_op, runtime_graph);
300 lstm::CellStateInfo cell_state_info =
301 createLstmCellStateInfoFloat(lstm_struct.options->cell_clip());
303 lstm::LSTMParameters lstm_params;
304 prepareGateParamsFloat(&lstm_params);
306 const bool time_major = lstm_struct.options->time_major();
307 const auto batch_size =
309 const auto state_dimension =
Tensor::dim(lstm_struct.output_state(), 1);
310 const auto cell_state_type_size =
getDataTypeSize(Tensor::element_type(lstm_struct.cell_state()));
312 auto scratch_0_data =
313 std::make_unique<uint8_t[]>(batch_size * state_dimension * cell_state_type_size);
314 auto scratch_1_data =
315 std::make_unique<uint8_t[]>(batch_size * state_dimension * cell_state_type_size);
316 auto scratch_2_data =
317 std::make_unique<uint8_t[]>(batch_size * state_dimension * cell_state_type_size);
318 auto scratch_3_data =
319 std::make_unique<uint8_t[]>(batch_size * state_dimension * cell_state_type_size);
322 auto output_state_data =
323 std::make_unique<float[]>(Tensor::num_elements(lstm_struct.output_state()));
324 std::fill_n(output_state_data.get(), Tensor::num_elements(lstm_struct.output_state()), 0);
327 auto cell_state_data = std::make_unique<float[]>(Tensor::num_elements(lstm_struct.cell_state()));
328 std::fill_n(cell_state_data.get(), Tensor::num_elements(lstm_struct.cell_state()), 0);
330 luci_interpreter_pal::evalLSTM<float, float, float, float>(
331 &lstm_struct, &lstm_params, &cell_state_info, output_state_data.get(), cell_state_data.get(),
332 kernels::getTensorData<float>(scratch_0_data.get()),
333 kernels::getTensorData<float>(scratch_1_data.get()),
334 kernels::getTensorData<float>(scratch_2_data.get()),
335 kernels::getTensorData<float>(scratch_3_data.get()), runtime_graph);
339void validateWeightTensorSize(
const circle::Tensor *weight_tensor,
int dim1_size,
int dim2_size)
346void validateTensorsSize(lstm::LSTMStruct *lstm_struct,
const bool time_major)
348 const auto batch_size =
351 const auto input_dimension =
Tensor::dim(lstm_struct->input(), 2);
352 const auto state_dimension =
Tensor::dim(lstm_struct->output_state(), 1);
355 for (int32_t i = 1; i < 5; i++)
357 validateWeightTensorSize(lstm_struct->get_internal_tensor(i), state_dimension, input_dimension);
361 for (int32_t i = 5; i < 9; i++)
363 validateWeightTensorSize(lstm_struct->get_internal_tensor(i), state_dimension, state_dimension);
367 for (int32_t i = 12; i < 16; i++)
377 batch_size * state_dimension);
379 batch_size * state_dimension);
398 Tensor::element_type(lstm_struct.
input()) == DataType::S8);
402 const bool time_major = lstm_struct.
options->time_major();
404 validateTensorsSize(&lstm_struct, time_major);
407 for (int32_t i = 9; i < 12; ++i)
411 for (int32_t i = 16; i < 18; ++i)
415 for (int32_t i = 20; i < 24; ++i)
422 const auto input_index = cur_op->inputs()->operator[](0);
423 assert(input_index != -1);
429 switch (Tensor::element_type(input))
432 case DataType::FLOAT32:
433 evalFloat(cur_op, runtime_graph, is_inplace);
438 evalInt8(cur_op, runtime_graph, is_inplace);
442 assert(
false &&
"Unsupported type.");
const circle::Tensor * getCircleTensorByIndex(int32_t index)
bool is_inplace_op(const circle::Operator *op)
#define LUCI_INTERPRETER_CHECK(cond)
DataType
"scalar" value type
bool checkedLog2(const float x, int *log2_result)
void calculateActivationRange(Activation activation, T *activation_min, T *activation_max)
void calculateActivationRangeQuantized(Activation activation, const Tensor *output, int32_t *activation_min, int32_t *activation_max)
double getQuantizedConvolutionMultipler(float input_scale, float filter_scale, float output_scale)
void quantizeMultiplier(double double_multiplier, int32_t *quantized_multiplier, int *shift)
void evalLSTM< int8_t, int8_t, int16_t, int32_t >(luci_interpreter::lstm::LSTMStruct *lstm_struct, luci_interpreter::lstm::LSTMParameters *lstm_params, luci_interpreter::lstm::CellStateInfo *cell_state_info, int8_t *output_state_data, int16_t *cell_state_data, int16_t *scratch0, int16_t *scratch1, int16_t *scratch2, int16_t *scratch3, luci_interpreter::BaseRuntimeGraph *runtime_graph)
RuntimeGraph BaseRuntimeGraph
void configure_kernel_CircleUnidirectionalSequenceLSTM(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)
size_t getDataTypeSize(DataType data_type)
void execute_kernel_CircleUnidirectionalSequenceLSTM(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)
const loco::Dimension & dim(uint32_t axis) const
const circle::UnidirectionalSequenceLSTMOptions * options
const circle::Tensor * get_internal_tensor(int i)
const circle::Tensor * input()
void validateTensorTypes()
int32_t output_multiplier
int32_t quantized_activation_min
float float_activation_max
float float_activation_min
int32_t quantized_activation_max
int32_t quantized_activation_max
int32_t quantized_activation_min
float float_activation_min
float float_activation_max
int32_t output_multiplier