From 1a750b4af2f07243a5cf5d04fda2139ad4f6c089 Mon Sep 17 00:00:00 2001 From: Hyung-Kyu Choi Date: Thu, 29 Mar 2018 17:52:26 +0900 Subject: [PATCH] Enable Pooling of CpuExecutor - Enable Poolinig of CpuExecutor - Enable OperationType::AVERAGE_POOL_2D of CpuExecutor Signed-off-by: Hyung-Kyu Choi --- src/runtime/ref/nn/common/CMakeLists.txt | 1 + src/runtime/ref/nn/common/CpuExecutor.cpp | 2 + src/runtime/ref/nn/common/operations/Pooling.cpp | 162 +++++++++++++++++++++++ 3 files changed, 165 insertions(+) create mode 100644 src/runtime/ref/nn/common/operations/Pooling.cpp diff --git a/src/runtime/ref/nn/common/CMakeLists.txt b/src/runtime/ref/nn/common/CMakeLists.txt index 1346b9b..285c850 100644 --- a/src/runtime/ref/nn/common/CMakeLists.txt +++ b/src/runtime/ref/nn/common/CMakeLists.txt @@ -14,6 +14,7 @@ SET (CUR_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/OperationsUtils.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Utils.cpp ${CMAKE_CURRENT_SOURCE_DIR}/operations/Activation.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/operations/Pooling.cpp ${CMAKE_CURRENT_SOURCE_DIR}/operations/SimpleMath.cpp ) SET (SRCS diff --git a/src/runtime/ref/nn/common/CpuExecutor.cpp b/src/runtime/ref/nn/common/CpuExecutor.cpp index e6d8625..9f3473b 100644 --- a/src/runtime/ref/nn/common/CpuExecutor.cpp +++ b/src/runtime/ref/nn/common/CpuExecutor.cpp @@ -569,6 +569,7 @@ int CpuExecutor::executeOperation(const Operation& operation) { outShape); } } break; +#endif // REF-ANN case OperationType::AVERAGE_POOL_2D: { const size_t inCount = ins.size(); if ((inCount != 10 && inCount != 7) || @@ -649,6 +650,7 @@ int CpuExecutor::executeOperation(const Operation& operation) { outShape); } } break; +#if 0 // REF-ANN case OperationType::L2_POOL_2D: { const size_t inCount = ins.size(); if ((inCount != 10 && inCount != 7) || diff --git a/src/runtime/ref/nn/common/operations/Pooling.cpp b/src/runtime/ref/nn/common/operations/Pooling.cpp new file mode 100644 index 0000000..19bf134 --- /dev/null +++ b/src/runtime/ref/nn/common/operations/Pooling.cpp @@ -0,0 +1,162 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * 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. + */ + +#include "Operations.h" +#include "OperationsUtils.h" + +#include "internal/optimized/optimized_ops.h" + +namespace android { +namespace nn { + +#define ANDROID_NN_POOLING_PARAMETERS \ + uint32_t height = getSizeOfDimension(inputShape, 1); \ + uint32_t width = getSizeOfDimension(inputShape, 2); \ + uint32_t outHeight = getSizeOfDimension(outputShape, 1); \ + uint32_t outWidth = getSizeOfDimension(outputShape, 2); \ + \ + uint32_t paddingHeight = (uint32_t)padding_top; \ + uint32_t paddingWidth = (uint32_t)padding_left; + +bool averagePoolFloat32(const float* inputData, const Shape& inputShape, + int32_t padding_left, int32_t padding_right, + int32_t padding_top, int32_t padding_bottom, + int32_t stride_width, int32_t stride_height, + int32_t filter_width, int32_t filter_height, int32_t activation, + float* outputData, const Shape& outputShape) { + + ANDROID_NN_POOLING_PARAMETERS + + #define ANDROID_NN_AVERAGE_POOL(activation) \ + optimized_ops::AveragePool( \ + inputData, convertShapeToDims(inputShape), \ + stride_width, stride_height, paddingWidth, paddingHeight, \ + filter_width, filter_height, \ + outputData, convertShapeToDims(outputShape)) + + ANDROID_NN_MACRO_DISPATCH(ANDROID_NN_AVERAGE_POOL) + #undef ANDROID_NN_AVERAGE_POOL + + return true; +} + +bool averagePoolQuant8(const uint8_t* inputData, const Shape& inputShape, + int32_t padding_left, int32_t padding_right, + int32_t padding_top, int32_t padding_bottom, + int32_t stride_width, int32_t stride_height, + int32_t filter_width, int32_t filter_height, int32_t activation, + uint8_t* outputData, const Shape& outputShape) { + + ANDROID_NN_POOLING_PARAMETERS + + int32_t output_activation_min = 0; + int32_t output_activation_max = 0; + + CalculateActivationRangeUint8(activation, outputShape, + &output_activation_min, + &output_activation_max); + + #define ANDROID_NN_AVERAGE_POOL(activation) \ + optimized_ops::AveragePool( \ + inputData, convertShapeToDims(inputShape), \ + stride_width, stride_height, paddingWidth, paddingHeight, \ + filter_width, filter_height, \ + output_activation_min, output_activation_max, \ + outputData, convertShapeToDims(outputShape)) + + ANDROID_NN_MACRO_DISPATCH(ANDROID_NN_AVERAGE_POOL) + #undef ANDROID_NN_AVERAGE_POOL + + return true; +} + +bool l2PoolFloat32(const float* inputData, const Shape& inputShape, + int32_t padding_left, int32_t padding_right, + int32_t padding_top, int32_t padding_bottom, + int32_t stride_width, int32_t stride_height, + int32_t filter_width, int32_t filter_height, int32_t activation, + float* outputData, const Shape& outputShape) { + + ANDROID_NN_POOLING_PARAMETERS + + #define ANDROID_NN_L2_POOL(activation) \ + optimized_ops::L2Pool( \ + inputData, convertShapeToDims(inputShape), \ + stride_width, stride_height, paddingWidth, paddingHeight, \ + filter_width, filter_height, \ + outputData, convertShapeToDims(outputShape)) + + ANDROID_NN_MACRO_DISPATCH(ANDROID_NN_L2_POOL) + #undef ANDROID_NN_L2_POOL + + return true; +} + +bool maxPoolFloat32(const float* inputData, const Shape& inputShape, + int32_t padding_left, int32_t padding_right, + int32_t padding_top, int32_t padding_bottom, + int32_t stride_width, int32_t stride_height, + int32_t filter_width, int32_t filter_height, int32_t activation, + float* outputData, const Shape& outputShape) { + + ANDROID_NN_POOLING_PARAMETERS + + #define ANDROID_NN_MAX_POOL(activation) \ + optimized_ops::MaxPool( \ + inputData, convertShapeToDims(inputShape), \ + stride_width, stride_height, paddingWidth, paddingHeight, \ + filter_width, filter_height, \ + outputData, convertShapeToDims(outputShape)) + + ANDROID_NN_MACRO_DISPATCH(ANDROID_NN_MAX_POOL) + #undef ANDROID_NN_MAX_POOL + + return true; +} + +bool maxPoolQuant8(const uint8_t* inputData, const Shape& inputShape, + int32_t padding_left, int32_t padding_right, + int32_t padding_top, int32_t padding_bottom, + int32_t stride_width, int32_t stride_height, + int32_t filter_width, int32_t filter_height, int32_t activation, + uint8_t* outputData, const Shape& outputShape) { + + ANDROID_NN_POOLING_PARAMETERS + + int32_t output_activation_min = 0; + int32_t output_activation_max = 0; + + CalculateActivationRangeUint8(activation, outputShape, + &output_activation_min, + &output_activation_max); + + #define ANDROID_NN_MAX_POOL(activation) \ + optimized_ops::MaxPool( \ + inputData, convertShapeToDims(inputShape), \ + stride_width, stride_height, paddingWidth, paddingHeight, \ + filter_width, filter_height, \ + output_activation_min, output_activation_max, \ + outputData, convertShapeToDims(outputShape)) + + ANDROID_NN_MACRO_DISPATCH(ANDROID_NN_MAX_POOL) + #undef ANDROID_NN_MAX_POOL + + return true; +} + +#undef ANDROID_NN_POOLING_PARAMETERS +} // namespace nn +} // namespace android -- 2.7.4