| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_ADD_H_ |
| #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_ADD_H_ |
|
|
| #include <algorithm> |
| #include <limits> |
|
|
| #include "edge-impulse-sdk/tensorflow/lite/kernels/internal/common.h" |
| #include "edge-impulse-sdk/tensorflow/lite/kernels/internal/types.h" |
|
|
| namespace tflite { |
| namespace reference_integer_ops { |
|
|
| inline void CheckArithmeticParams(const ArithmeticParams& params) { |
| TFLITE_DCHECK_LE(params.quantized_activation_min, |
| params.quantized_activation_max); |
| |
| |
| TFLITE_DCHECK_GE(-params.input1_offset, std::numeric_limits<int8_t>::min()); |
| TFLITE_DCHECK_GE(-params.input2_offset, std::numeric_limits<int8_t>::min()); |
| TFLITE_DCHECK_LE(-params.input1_offset, std::numeric_limits<int8_t>::max()); |
| TFLITE_DCHECK_LE(-params.input2_offset, std::numeric_limits<int8_t>::max()); |
| } |
|
|
| inline void ElementWise( |
| int size, const ArithmeticParams& params, const int8_t* input1_data, |
| const int8_t* input2_data, int8_t* output_data, |
| void (*check_arithmetic_params)(const ArithmeticParams&), |
| int8_t (*binary_func)(int8_t, int8_t, const ArithmeticParams&)) { |
| CheckArithmeticParams(params); |
| for (int i = 0; i < size; ++i) { |
| output_data[i] = binary_func(input1_data[i], input2_data[i], params); |
| } |
| } |
|
|
| inline void BroadcastBinaryFunction4DSlow( |
| const ArithmeticParams& params, const RuntimeShape& input1_shape, |
| const int8_t* input1_data, const RuntimeShape& input2_shape, |
| const int8_t* input2_data, const RuntimeShape& output_shape, |
| int8_t* output_data, |
| void (*check_arithmetic_params)(const ArithmeticParams&), |
| int8_t (*binary_func)(int8_t, int8_t, const ArithmeticParams&)) { |
| NdArrayDesc<4> desc1; |
| NdArrayDesc<4> desc2; |
| NdArrayDescsForElementwiseBroadcast(input1_shape, input2_shape, &desc1, |
| &desc2); |
| const RuntimeShape extended_output_shape = |
| RuntimeShape::ExtendedShape(4, output_shape); |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| for (int b = 0; b < extended_output_shape.Dims(0); ++b) { |
| for (int y = 0; y < extended_output_shape.Dims(1); ++y) { |
| for (int x = 0; x < extended_output_shape.Dims(2); ++x) { |
| for (int c = 0; c < extended_output_shape.Dims(3); ++c) { |
| output_data[Offset(extended_output_shape, b, y, x, c)] = binary_func( |
| input1_data[SubscriptToIndex(desc1, b, y, x, c)], |
| input2_data[SubscriptToIndex(desc2, b, y, x, c)], params); |
| } |
| } |
| } |
| } |
| } |
|
|
| inline int8_t AddFunc(int8_t x, int8_t y, const ArithmeticParams& params) { |
| const int32_t input1_val = params.input1_offset + x; |
| const int32_t input2_val = params.input2_offset + y; |
| const int32_t shifted_input1_val = input1_val * (1 << params.left_shift); |
| const int32_t shifted_input2_val = input2_val * (1 << params.left_shift); |
| const int32_t scaled_input1_val = |
| MultiplyByQuantizedMultiplierSmallerThanOneExp( |
| shifted_input1_val, params.input1_multiplier, params.input1_shift); |
| const int32_t scaled_input2_val = |
| MultiplyByQuantizedMultiplierSmallerThanOneExp( |
| shifted_input2_val, params.input2_multiplier, params.input2_shift); |
| const int32_t raw_sum = scaled_input1_val + scaled_input2_val; |
| const int32_t raw_output = |
| MultiplyByQuantizedMultiplierSmallerThanOneExp( |
| raw_sum, params.output_multiplier, params.output_shift) + |
| params.output_offset; |
| const int32_t clamped_output = |
| std::min(params.quantized_activation_max, |
| std::max(params.quantized_activation_min, raw_output)); |
| return static_cast<int8_t>(clamped_output); |
| } |
|
|
| |
| |
| inline void AddElementwise(int size, const ArithmeticParams& params, |
| const int8_t* input1_data, const int8_t* input2_data, |
| int8_t* output_data) { |
| ElementWise(size, params, input1_data, input2_data, output_data, |
| CheckArithmeticParams, AddFunc); |
| } |
|
|
| inline void Add(const ArithmeticParams& params, |
| const RuntimeShape& input1_shape, const int8_t* input1_data, |
| const RuntimeShape& input2_shape, const int8_t* input2_data, |
| const RuntimeShape& output_shape, int8_t* output_data) { |
| CheckArithmeticParams(params); |
|
|
| const int flat_size = |
| MatchingElementsSize(input1_shape, input2_shape, output_shape); |
|
|
| AddElementwise(flat_size, params, input1_data, input2_data, output_data); |
| } |
|
|
| inline void BroadcastAdd4DSlow(const ArithmeticParams& params, |
| const RuntimeShape& input1_shape, |
| const int8_t* input1_data, |
| const RuntimeShape& input2_shape, |
| const int8_t* input2_data, |
| const RuntimeShape& output_shape, |
| int8_t* output_data) { |
| BroadcastBinaryFunction4DSlow(params, input1_shape, input1_data, input2_shape, |
| input2_data, output_shape, output_data, |
| CheckArithmeticParams, AddFunc); |
| } |
|
|
| } |
| } |
|
|
| #endif |
|
|