TFLite: reduced redundant calculation in uint8/float conv.h

This commit is contained in:
danielyou0230 2020-08-18 14:27:12 -07:00
parent c979f5a424
commit c06408c37d

View File

@ -59,28 +59,31 @@ inline void Conv(const ConvParams& params, const RuntimeShape& input_shape,
const int output_width = output_shape.Dims(2); const int output_width = output_shape.Dims(2);
for (int batch = 0; batch < batches; ++batch) { for (int batch = 0; batch < batches; ++batch) {
for (int out_y = 0; out_y < output_height; ++out_y) { for (int out_y = 0; out_y < output_height; ++out_y) {
for (int out_x = 0; out_x < output_width; ++out_x) {
for (int out_channel = 0; out_channel < output_depth; ++out_channel) {
const int in_x_origin = (out_x * stride_width) - pad_width;
const int in_y_origin = (out_y * stride_height) - pad_height; const int in_y_origin = (out_y * stride_height) - pad_height;
for (int out_x = 0; out_x < output_width; ++out_x) {
const int in_x_origin = (out_x * stride_width) - pad_width;
for (int out_channel = 0; out_channel < output_depth; ++out_channel) {
float total = 0.f; float total = 0.f;
for (int filter_y = 0; filter_y < filter_height; ++filter_y) { for (int filter_y = 0; filter_y < filter_height; ++filter_y) {
const int in_y = in_y_origin + dilation_height_factor * filter_y;
for (int filter_x = 0; filter_x < filter_width; ++filter_x) { for (int filter_x = 0; filter_x < filter_width; ++filter_x) {
for (int in_channel = 0; in_channel < input_depth; ++in_channel) {
const int in_x = in_x_origin + dilation_width_factor * 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; // Zero padding by omitting the areas outside the image.
// If the location is outside the bounds of the input image, const bool is_point_inside_image =
// use zero as a default value. (in_x >= 0) && (in_x < input_width) && (in_y >= 0) &&
if ((in_x >= 0) && (in_x < input_width) && (in_y >= 0) && (in_y < input_height);
(in_y < input_height)) {
float input_value = input_data[Offset( if (!is_point_inside_image) {
input_shape, batch, in_y, in_x, in_channel)]; continue;
float filter_value =
filter_data[Offset(filter_shape, out_channel, filter_y,
filter_x, in_channel)];
total += (input_value * filter_value);
} }
for (int in_channel = 0; in_channel < input_depth; ++in_channel) {
float input_value = input_data[Offset(input_shape, batch, in_y,
in_x, in_channel)];
float filter_value = filter_data[Offset(
filter_shape, out_channel, filter_y, filter_x, in_channel)];
total += (input_value * filter_value);
} }
} }
} }
@ -139,32 +142,35 @@ inline void Conv(const ConvParams& params, const RuntimeShape& input_shape,
const int output_width = output_shape.Dims(2); const int output_width = output_shape.Dims(2);
for (int batch = 0; batch < batches; ++batch) { for (int batch = 0; batch < batches; ++batch) {
for (int out_y = 0; out_y < output_height; ++out_y) { for (int out_y = 0; out_y < output_height; ++out_y) {
for (int out_x = 0; out_x < output_width; ++out_x) {
for (int out_channel = 0; out_channel < output_depth; ++out_channel) {
const int in_x_origin = (out_x * stride_width) - pad_width;
const int in_y_origin = (out_y * stride_height) - pad_height; const int in_y_origin = (out_y * stride_height) - pad_height;
for (int out_x = 0; out_x < output_width; ++out_x) {
const int in_x_origin = (out_x * stride_width) - pad_width;
for (int out_channel = 0; out_channel < output_depth; ++out_channel) {
int32_t acc = 0; int32_t acc = 0;
for (int filter_y = 0; filter_y < filter_height; ++filter_y) { for (int filter_y = 0; filter_y < filter_height; ++filter_y) {
const int in_y = in_y_origin + dilation_height_factor * filter_y;
for (int filter_x = 0; filter_x < filter_width; ++filter_x) { for (int filter_x = 0; filter_x < filter_width; ++filter_x) {
for (int in_channel = 0; in_channel < input_depth; ++in_channel) {
const int in_x = in_x_origin + dilation_width_factor * 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; // Zero padding by omitting the areas outside the image.
// If the location is outside the bounds of the input image, const bool is_point_inside_image =
// use zero as a default value. (in_x >= 0) && (in_x < input_width) && (in_y >= 0) &&
if ((in_x >= 0) && (in_x < input_width) && (in_y >= 0) && (in_y < input_height);
(in_y < input_height)) {
int32_t input_val = input_data[Offset( if (!is_point_inside_image) {
input_shape, batch, in_y, in_x, in_channel)]; continue;
int32_t filter_val = }
filter_data[Offset(filter_shape, out_channel, filter_y,
filter_x, in_channel)]; for (int in_channel = 0; in_channel < input_depth; ++in_channel) {
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, out_channel, filter_y, filter_x, in_channel)];
acc += acc +=
(filter_val + filter_offset) * (input_val + input_offset); (filter_val + filter_offset) * (input_val + input_offset);
} }
} }
} }
}
if (bias_data) { if (bias_data) {
acc += bias_data[out_channel]; acc += bias_data[out_channel];
} }
@ -258,5 +264,4 @@ inline void HybridConvPerChannel(
} // namespace reference_ops } // namespace reference_ops
} // namespace tflite } // namespace tflite
#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_CONV_H_ #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_CONV_H_