| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_DEPTHWISECONV_UINT8_H_ |
| #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_DEPTHWISECONV_UINT8_H_ |
|
|
| #include <algorithm> |
|
|
| #include "edge-impulse-sdk/third_party/gemmlowp/fixedpoint/fixedpoint.h" |
| #include "edge-impulse-sdk/tensorflow/lite/kernels/internal/common.h" |
| #include "edge-impulse-sdk/tensorflow/lite/kernels/internal/compatibility.h" |
| #include "edge-impulse-sdk/tensorflow/lite/kernels/internal/types.h" |
|
|
| namespace tflite { |
|
|
| |
| |
| |
| enum class DepthwiseConvImplementation { |
| |
| |
| |
| kNone = 0, |
| |
| kUseGenericKernel, |
| kUseNeon3x3, |
| kUseNeon3x3DotProduct, |
| |
| kUseCModel3x3DotProduct, |
| |
| kUseUnwound3x3DotProduct, |
| |
| kUseIntrinsics3x3DotProduct, |
| }; |
|
|
| |
| enum class DepthwiseConvOutputRounding { |
| kNone = 0, |
| kAwayFromZero, |
| kUpward, |
| |
| }; |
|
|
| |
| enum class DepthwiseConvDepthMultiplication { |
| kNoMultiplication = 0, |
| kUnitInputDepth, |
| }; |
|
|
| namespace reference_ops { |
| namespace depthwise_conv { |
|
|
| template <DepthwiseConvOutputRounding output_rounding> |
| inline int32_t DepthwiseConvRound(int32_t x, int32_t quantized_multiplier, |
| int shift) { |
| TFLITE_DCHECK_NE(output_rounding, DepthwiseConvOutputRounding::kNone); |
| return MultiplyByQuantizedMultiplier(x, quantized_multiplier, shift); |
| } |
|
|
| |
| #if TFLITE_SINGLE_ROUNDING |
| template <> |
| inline int32_t DepthwiseConvRound<DepthwiseConvOutputRounding::kAwayFromZero>( |
| int32_t x, int32_t quantized_multiplier, int shift) { |
| using gemmlowp::RoundingDivideByPOT; |
| using gemmlowp::SaturatingRoundingDoublingHighMul; |
| int left_shift = shift > 0 ? shift : 0; |
| int right_shift = shift > 0 ? 0 : -shift; |
| return RoundingDivideByPOT(SaturatingRoundingDoublingHighMul( |
| x * (1 << left_shift), quantized_multiplier), |
| right_shift); |
| } |
|
|
| template <> |
| inline int32_t DepthwiseConvRound<DepthwiseConvOutputRounding::kUpward>( |
| int32_t x, int32_t quantized_multiplier, int shift) { |
| return MultiplyByQuantizedMultiplier(x, quantized_multiplier, shift); |
| } |
| |
| #else |
| template <> |
| inline int32_t DepthwiseConvRound<DepthwiseConvOutputRounding::kAwayFromZero>( |
| int32_t x, int32_t quantized_multiplier, int shift) { |
| return MultiplyByQuantizedMultiplier(x, quantized_multiplier, shift); |
| } |
|
|
| template <> |
| inline int32_t DepthwiseConvRound<DepthwiseConvOutputRounding::kUpward>( |
| int32_t x, int32_t quantized_multiplier, int shift) { |
| using gemmlowp::SaturatingRoundingDoublingHighMul; |
| const int left_shift = shift > 0 ? shift : 0; |
| const int right_shift = shift > 0 ? 0 : -shift; |
| const int rounding_offset = right_shift > 0 ? 1 << (right_shift - 1) : 0; |
| return (SaturatingRoundingDoublingHighMul(x * (1 << left_shift), |
| quantized_multiplier) + |
| rounding_offset) >> |
| right_shift; |
| } |
| #endif |
|
|
| template <DepthwiseConvOutputRounding output_rounding> |
| struct DepthwiseConvBasicKernel { |
| static inline void Run( |
| const DepthwiseParams& params, const RuntimeShape& input_shape, |
| const uint8_t* input_data, const RuntimeShape& filter_shape, |
| const uint8_t* filter_data, const RuntimeShape& bias_shape, |
| const int32_t* bias_data, const RuntimeShape& output_shape, |
| uint8_t* output_data) { |
| const int stride_width = params.stride_width; |
| const int stride_height = params.stride_height; |
| const int dilation_width_factor = params.dilation_width_factor; |
| const int dilation_height_factor = params.dilation_height_factor; |
| const int pad_width = params.padding_values.width; |
| const int pad_height = params.padding_values.height; |
| const int depth_multiplier = params.depth_multiplier; |
| const int32_t output_activation_min = params.quantized_activation_min; |
| const int32_t output_activation_max = params.quantized_activation_max; |
| const int32_t input_offset = params.input_offset; |
| const int32_t filter_offset = params.weights_offset; |
| const int32_t output_offset = params.output_offset; |
| const int32_t output_multiplier = params.output_multiplier; |
| const int output_shift = params.output_shift; |
| TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4); |
| TFLITE_DCHECK_EQ(filter_shape.DimensionsCount(), 4); |
| TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4); |
|
|
| TFLITE_DCHECK_LE(output_activation_min, output_activation_max); |
| const int batches = MatchingDim(input_shape, 0, output_shape, 0); |
| const int output_depth = MatchingDim(filter_shape, 3, output_shape, 3); |
| const int input_height = input_shape.Dims(1); |
| const int input_width = input_shape.Dims(2); |
| const int input_depth = input_shape.Dims(3); |
| const int filter_height = filter_shape.Dims(1); |
| const int filter_width = filter_shape.Dims(2); |
| const int output_height = output_shape.Dims(1); |
| const int output_width = output_shape.Dims(2); |
| TFLITE_DCHECK_EQ(output_depth, input_depth * depth_multiplier); |
| TFLITE_DCHECK_EQ(bias_shape.FlatSize(), output_depth); |
|
|
| for (int b = 0; b < batches; ++b) { |
| for (int out_y = 0; out_y < output_height; ++out_y) { |
| for (int out_x = 0; out_x < output_width; ++out_x) { |
| for (int ic = 0; ic < input_depth; ++ic) { |
| for (int m = 0; m < depth_multiplier; m++) { |
| const int oc = m + ic * depth_multiplier; |
| const int in_x_origin = (out_x * stride_width) - pad_width; |
| const int in_y_origin = (out_y * stride_height) - pad_height; |
| int32_t acc = 0; |
| for (int filter_y = 0; filter_y < filter_height; ++filter_y) { |
| for (int filter_x = 0; filter_x < filter_width; ++filter_x) { |
| const int in_x = |
| in_x_origin + dilation_width_factor * filter_x; |
| const int in_y = |
| in_y_origin + dilation_height_factor * filter_y; |
| |
| |
| if ((in_x >= 0) && (in_x < input_width) && (in_y >= 0) && |
| (in_y < input_height)) { |
| int32_t input_val = |
| input_data[Offset(input_shape, b, in_y, in_x, ic)]; |
| int32_t filter_val = filter_data[Offset( |
| filter_shape, 0, filter_y, filter_x, oc)]; |
| acc += (filter_val + filter_offset) * |
| (input_val + input_offset); |
| } |
| } |
| } |
| if (bias_data) { |
| acc += bias_data[oc]; |
| } |
| acc = DepthwiseConvRound<output_rounding>(acc, output_multiplier, |
| output_shift); |
| acc += output_offset; |
| acc = std::max(acc, output_activation_min); |
| acc = std::min(acc, output_activation_max); |
| output_data[Offset(output_shape, b, out_y, out_x, oc)] = |
| static_cast<uint8_t>(acc); |
| } |
| } |
| } |
| } |
| } |
| } |
|
|
| |
| |
| static inline void RunPerChannel( |
| const DepthwiseParams& params, const RuntimeShape& input_shape, |
| const int8_t* input_data, const RuntimeShape& filter_shape, |
| const int8_t* filter_data, const RuntimeShape& bias_shape, |
| const int32_t* bias_data, const RuntimeShape& output_shape, |
| int8_t* output_data) { |
| |
| |
| const int stride_width = params.stride_width; |
| const int stride_height = params.stride_height; |
| const int dilation_width_factor = params.dilation_width_factor; |
| const int dilation_height_factor = params.dilation_height_factor; |
| const int pad_width = params.padding_values.width; |
| const int pad_height = params.padding_values.height; |
| const int depth_multiplier = params.depth_multiplier; |
| const int32_t input_offset = params.input_offset; |
| const int32_t output_offset = params.output_offset; |
| const int32_t output_activation_min = params.quantized_activation_min; |
| const int32_t output_activation_max = params.quantized_activation_max; |
| const int32_t* output_multiplier = params.output_multiplier_per_channel; |
| const int32_t* output_shift = params.output_shift_per_channel; |
|
|
| |
| TFLITE_DCHECK_EQ(input_shape.DimensionsCount(), 4); |
| TFLITE_DCHECK_EQ(filter_shape.DimensionsCount(), 4); |
| TFLITE_DCHECK_EQ(output_shape.DimensionsCount(), 4); |
|
|
| TFLITE_DCHECK_LE(output_activation_min, output_activation_max); |
| const int batches = MatchingDim(input_shape, 0, output_shape, 0); |
| const int output_depth = MatchingDim(filter_shape, 3, output_shape, 3); |
| const int input_height = input_shape.Dims(1); |
| const int input_width = input_shape.Dims(2); |
| const int input_depth = input_shape.Dims(3); |
| const int filter_height = filter_shape.Dims(1); |
| const int filter_width = filter_shape.Dims(2); |
| const int output_height = output_shape.Dims(1); |
| const int output_width = output_shape.Dims(2); |
| TFLITE_DCHECK_EQ(output_depth, input_depth * depth_multiplier); |
| TFLITE_DCHECK_EQ(bias_shape.FlatSize(), output_depth); |
|
|
| for (int batch = 0; batch < batches; ++batch) { |
| for (int out_y = 0; out_y < output_height; ++out_y) { |
| for (int out_x = 0; out_x < output_width; ++out_x) { |
| for (int in_channel = 0; in_channel < input_depth; ++in_channel) { |
| for (int m = 0; m < depth_multiplier; ++m) { |
| const int output_channel = m + in_channel * depth_multiplier; |
| const int in_x_origin = (out_x * stride_width) - pad_width; |
| const int in_y_origin = (out_y * stride_height) - pad_height; |
| int32_t acc = 0; |
| for (int filter_y = 0; filter_y < filter_height; ++filter_y) { |
| for (int filter_x = 0; filter_x < filter_width; ++filter_x) { |
| const int in_x = |
| in_x_origin + dilation_width_factor * filter_x; |
| const int in_y = |
| in_y_origin + dilation_height_factor * filter_y; |
| |
| const bool is_point_inside_image = |
| (in_x >= 0) && (in_x < input_width) && (in_y >= 0) && |
| (in_y < input_height); |
| if (is_point_inside_image) { |
| int32_t input_val = input_data[Offset( |
| input_shape, batch, in_y, in_x, in_channel)]; |
| int32_t filter_val = filter_data[Offset( |
| filter_shape, 0, filter_y, filter_x, output_channel)]; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| acc += filter_val * (input_val + input_offset); |
| } |
| } |
| } |
| if (bias_data) { |
| acc += bias_data[output_channel]; |
| } |
| acc = DepthwiseConvRound<output_rounding>( |
| acc, output_multiplier[output_channel], |
| output_shift[output_channel]); |
| acc += output_offset; |
| acc = std::max(acc, output_activation_min); |
| acc = std::min(acc, output_activation_max); |
| output_data[Offset(output_shape, batch, out_y, out_x, |
| output_channel)] = static_cast<int8_t>(acc); |
| } |
| } |
| } |
| } |
| } |
| } |
| }; |
|
|
| } |
|
|
| inline void DepthwiseConv( |
| const DepthwiseParams& params, const RuntimeShape& input_shape, |
| const uint8_t* input_data, const RuntimeShape& filter_shape, |
| const uint8_t* filter_data, const RuntimeShape& bias_shape, |
| const int32_t* bias_data, const RuntimeShape& output_shape, |
| uint8_t* output_data) { |
| return depthwise_conv::DepthwiseConvBasicKernel< |
| DepthwiseConvOutputRounding::kAwayFromZero>::Run(params, input_shape, |
| input_data, filter_shape, |
| filter_data, bias_shape, |
| bias_data, output_shape, |
| output_data); |
| } |
|
|
| } |
| } |
|
|
| #endif |
|
|