From d3de696de855d0293b477ddcf12d02764166d16a Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EC=98=A4=ED=98=95=EC=84=9D/On-Device=20Lab=28SR=29/Staff?= =?utf8?q?=20Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Fri, 29 Mar 2019 15:16:23 +0900 Subject: [PATCH] Introduce float convolution kernel (#4892) Introduce float convolution kernel from tflite Use kernel in neurun cpu backend Signed-off-by: Hyeongseok Oh --- libs/cker/include/cker/operation/Conv.h | 132 +++++++++++++++++++++ .../neurun/backend/cpu/kernel/ConvolutionLayer.cc | 37 +++--- 2 files changed, 148 insertions(+), 21 deletions(-) create mode 100644 libs/cker/include/cker/operation/Conv.h diff --git a/libs/cker/include/cker/operation/Conv.h b/libs/cker/include/cker/operation/Conv.h new file mode 100644 index 0000000..b14af01 --- /dev/null +++ b/libs/cker/include/cker/operation/Conv.h @@ -0,0 +1,132 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved + * Copyright 2017 The TensorFlow Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __NNFW_CKER_CONV_H__ +#define __NNFW_CKER_CONV_H__ + +#include "cker/Types.h" +#include "cker/Shape.h" +#include "cker/Utils.h" + +namespace nnfw +{ +namespace cker +{ + +struct ConvParams +{ + PaddingType padding_type; + PaddingValues padding_values; + // TODO(starka): This was just "stride", so check that width+height is OK. + int16_t stride_width; + int16_t stride_height; + int16_t dilation_width_factor; + int16_t dilation_height_factor; + // uint8_t inference params. + // TODO(b/65838351): Use smaller types if appropriate. + int32_t input_offset; + int32_t weights_offset; + int32_t output_offset; + int32_t output_multiplier; + int output_shift; + // uint8_t, etc, activation params. + int32_t quantized_activation_min; + int32_t quantized_activation_max; + // float activation params. + float float_activation_min; + float float_activation_max; +}; + +inline void Conv(const ConvParams ¶ms, const Shape &input_shape, const float *input_data, + const Shape &filter_shape, const float *filter_data, const Shape &bias_shape, + const float *bias_data, const Shape &output_shape, float *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 float output_activation_min = params.float_activation_min; + const float output_activation_max = params.float_activation_max; + assert(input_shape.DimensionsCount() == 4); + assert(filter_shape.DimensionsCount() == 4); + assert(output_shape.DimensionsCount() == 4); + + const int batches = MatchingDim(input_shape, 0, output_shape, 0); + const int input_depth = MatchingDim(input_shape, 3, filter_shape, 3); + const int output_depth = MatchingDim(filter_shape, 0, output_shape, 3); + if (bias_data) + { + assert(bias_shape.FlatSize() == output_depth); + } + const int input_height = input_shape.Dims(1); + const int input_width = input_shape.Dims(2); + 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); + 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 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; + float total = 0.f; + for (int filter_y = 0; filter_y < filter_height; ++filter_y) + { + 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_y = in_y_origin + dilation_height_factor * filter_y; + // If the location is outside the bounds of the input image, + // use zero as a default value. + if ((in_x >= 0) && (in_x < input_width) && (in_y >= 0) && (in_y < input_height)) + { + 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); + } + } + } + } + float bias_value = 0.0f; + if (bias_data) + { + bias_value = bias_data[out_channel]; + } + output_data[Offset(output_shape, batch, out_y, out_x, out_channel)] = + ActivationFunctionWithMinMax(total + bias_value, output_activation_min, + output_activation_max); + } + } + } + } +} + +} // namespace cker +} // namespace nnfw + +#endif // __NNFW_CKER_CONCATENATION_H_ diff --git a/runtimes/neurun/backend/cpu/kernel/ConvolutionLayer.cc b/runtimes/neurun/backend/cpu/kernel/ConvolutionLayer.cc index f8cfe1d..675e05e 100644 --- a/runtimes/neurun/backend/cpu/kernel/ConvolutionLayer.cc +++ b/runtimes/neurun/backend/cpu/kernel/ConvolutionLayer.cc @@ -16,6 +16,8 @@ #include "ConvolutionLayer.h" +#include + // TODO : Discard legacy methods #include "tensorflow/contrib/lite/kernels/internal/optimized/legacy_optimized_ops.h" #include "OperationUtils.h" @@ -99,30 +101,23 @@ ConvolutionLayer::ConvolutionLayer() bool ConvolutionLayer::convFloat32() { - ANDROID_NN_CONV_PARAMETERS(float) - - const ::tflite::Dims<4> &kernel_dim = convertShapeToDims(_kernelShape); - const int kernel_width = ArraySize(kernel_dim, 1); - const int kernel_height = ArraySize(kernel_dim, 2); - const bool need_im2col = - _strideWidth != 1 || _strideHeight != 1 || kernel_width != 1 || kernel_height != 1; - - float *im2colDataToPass = nullptr; - if (need_im2col) - { - im2colDataToPass = im2colData; - } - float output_activation_min, output_activation_max; CalculateActivationRangeFloat(_activation, &output_activation_min, &output_activation_max); - int32_t dilationWidthFactor = 1, dilationHeightFactor = 1; - tflite::optimized_ops::Conv( - _inputData.f, convertShapeToDims(_inputShape), _kernelData.f, - convertShapeToDims(_kernelShape), _biasData.f, convertShapeToDims(_biasShape), _strideWidth, - _strideHeight, dilationWidthFactor, dilationHeightFactor, paddingWidth, paddingHeight, - output_activation_min, output_activation_max, _outputData.f, convertShapeToDims(_outputShape), - im2colDataToPass, im2colDim); + nnfw::cker::ConvParams op_params; + op_params.padding_values.width = _paddingLeft; + op_params.padding_values.height = _paddingTop; + op_params.stride_width = _strideWidth; + op_params.stride_height = _strideHeight; + op_params.dilation_width_factor = 1; + op_params.dilation_height_factor = 1; + op_params.float_activation_min = output_activation_min; + op_params.float_activation_max = output_activation_max; + + nnfw::cker::Conv(op_params, convertShapeToCkerShape(_inputShape), _inputData.f, + convertShapeToCkerShape(_kernelShape), _kernelData.f, + convertShapeToCkerShape(_biasShape), _biasData.f, + convertShapeToCkerShape(_outputShape), _outputData.f); return true; } -- 2.7.4