From 563494c2f447e201e88e6d7133a41e12971777eb Mon Sep 17 00:00:00 2001 From: Giorgio Arena Date: Mon, 30 Apr 2018 17:29:41 +0100 Subject: COMPMID-1084 Rework the way validation is performed for NHWC data layout Change-Id: I00b95f560548da76718298b642c8166f92421097 Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/129520 Tested-by: Jenkins Reviewed-by: Michele DiGiorgio Reviewed-by: Anthony Barbier --- .../reference/BatchNormalizationLayer.cpp | 20 ++++------- .../reference/DepthwiseConvolutionLayer.cpp | 39 ++++++---------------- tests/validation/reference/PoolingLayer.cpp | 36 ++++---------------- 3 files changed, 23 insertions(+), 72 deletions(-) (limited to 'tests/validation/reference') diff --git a/tests/validation/reference/BatchNormalizationLayer.cpp b/tests/validation/reference/BatchNormalizationLayer.cpp index ae309d9093..c8badacc79 100644 --- a/tests/validation/reference/BatchNormalizationLayer.cpp +++ b/tests/validation/reference/BatchNormalizationLayer.cpp @@ -27,7 +27,6 @@ #include "tests/validation/FixedPoint.h" #include "tests/validation/Helpers.h" -#include "tests/validation/reference/Permute.h" namespace arm_compute { @@ -42,7 +41,6 @@ template ::value, int>:: SimpleTensor batch_normalization_layer(const SimpleTensor &src, const SimpleTensor &mean, const SimpleTensor &var, const SimpleTensor &beta, const SimpleTensor &gamma, float epsilon, ActivationLayerInfo act_info, int fixed_point_position) { - ARM_COMPUTE_ERROR_ON_MSG(src.data_layout() == DataLayout::NHWC, "Unsupported NHWC format"); ARM_COMPUTE_UNUSED(act_info); SimpleTensor result(src.shape(), src.data_type()); @@ -88,14 +86,12 @@ SimpleTensor batch_normalization_layer(const SimpleTensor &src, const Simp { ARM_COMPUTE_UNUSED(fixed_point_position); - const bool is_nhwc = src.data_layout() == DataLayout::NHWC; - const SimpleTensor perm_src = (is_nhwc) ? permute(src, PermutationVector(1U, 2U, 0U)) : src; - SimpleTensor result(perm_src.shape(), perm_src.data_type()); + SimpleTensor result(src.shape(), src.data_type()); - const auto cols = static_cast(perm_src.shape()[0]); - const auto rows = static_cast(perm_src.shape()[1]); - const auto depth = static_cast(perm_src.shape()[2]); - const int upper_dims = perm_src.shape().total_size() / (cols * rows * depth); + const auto cols = static_cast(src.shape()[0]); + const auto rows = static_cast(src.shape()[1]); + const auto depth = static_cast(src.shape()[2]); + const int upper_dims = src.shape().total_size() / (cols * rows * depth); for(int r = 0; r < upper_dims; ++r) { @@ -107,7 +103,7 @@ SimpleTensor batch_normalization_layer(const SimpleTensor &src, const Simp { const int pos = l + k * cols + i * rows * cols + r * cols * rows * depth; const float denominator = sqrt(var[i] + epsilon); - const float numerator = perm_src[pos] - mean[i]; + const float numerator = src[pos] - mean[i]; const float x_bar = numerator / denominator; result[pos] = beta[i] + x_bar * gamma[i]; } @@ -120,10 +116,6 @@ SimpleTensor batch_normalization_layer(const SimpleTensor &src, const Simp result = activation_layer(result, act_info); } - if(is_nhwc) - { - result = permute(result, PermutationVector(2U, 0U, 1U)); - } return result; } template SimpleTensor batch_normalization_layer(const SimpleTensor &src, const SimpleTensor &mean, const SimpleTensor &var, const SimpleTensor &beta, diff --git a/tests/validation/reference/DepthwiseConvolutionLayer.cpp b/tests/validation/reference/DepthwiseConvolutionLayer.cpp index 207e5fc45c..10c617e953 100644 --- a/tests/validation/reference/DepthwiseConvolutionLayer.cpp +++ b/tests/validation/reference/DepthwiseConvolutionLayer.cpp @@ -24,7 +24,6 @@ #include "DepthwiseConvolutionLayer.h" #include "ConvolutionLayer.h" -#include "Permute.h" #include "Utils.h" #include "tests/validation/FixedPoint.h" @@ -51,9 +50,11 @@ namespace reference * */ template -void depthwise_convolution_nchw(const SimpleTensor &src, const SimpleTensor &weights, const SimpleTensor &biases, SimpleTensor &dst, const PadStrideInfo &conv_info, - unsigned int depth_multiplier) +SimpleTensor depthwise_convolution(const SimpleTensor &src, const SimpleTensor &weights, const SimpleTensor &biases, const TensorShape &dst_shape, const PadStrideInfo &conv_info, + unsigned int depth_multiplier) { + SimpleTensor dst{ dst_shape, src.data_type(), 1, src.fixed_point_position() }; + // Compute reference const int filter_width = weights.shape().x(); const int filter_height = weights.shape().y(); @@ -113,11 +114,16 @@ void depthwise_convolution_nchw(const SimpleTensor &src, const SimpleTensor &src, const SimpleTensor &weights, const SimpleTensor &biases, SimpleTensor &dst, const PadStrideInfo &conv_info, - unsigned int depth_multiplier) +template <> +SimpleTensor depthwise_convolution(const SimpleTensor &src, const SimpleTensor &weights, const SimpleTensor &biases, const TensorShape &dst_shape, + const PadStrideInfo &conv_info, unsigned int depth_multiplier) { + SimpleTensor dst{ dst_shape, src.data_type(), 1, src.fixed_point_position(), src.quantization_info() }; + // Create reference const int input_offset = -src.quantization_info().offset; const float input_scale = src.quantization_info().scale; @@ -196,33 +202,10 @@ void depthwise_convolution_nchw(const SimpleTensor &src, const SimpleTe } } } -} - -template -SimpleTensor depthwise_convolution(const SimpleTensor &src, const SimpleTensor &weights, const SimpleTensor &biases, const TensorShape &dst_shape, const PadStrideInfo &conv_info, - unsigned int depth_multiplier) -{ - SimpleTensor dst{ dst_shape, src.data_type(), 1, src.fixed_point_position(), src.quantization_info() }; - - if(src.data_layout() == DataLayout::NHWC) - { - SimpleTensor src_nchw = reference::permute(src, PermutationVector(1U, 2U, 0U)); - SimpleTensor weights_nchw = reference::permute(weights, PermutationVector(1U, 2U, 0U)); - SimpleTensor dst_nchw = reference::permute(dst, PermutationVector(1U, 2U, 0U)); - - depthwise_convolution_nchw(src_nchw, weights_nchw, biases, dst_nchw, conv_info, depth_multiplier); - - return reference::permute(dst_nchw, PermutationVector(2U, 0U, 1U)); - } - - depthwise_convolution_nchw(src, weights, biases, dst, conv_info, depth_multiplier); return dst; } -template SimpleTensor depthwise_convolution(const SimpleTensor &src, const SimpleTensor &weights, const SimpleTensor &biases, const TensorShape &dst_shape, - const PadStrideInfo &conv_info, unsigned int depth_multiplier); - template SimpleTensor depthwise_convolution(const SimpleTensor &src, const SimpleTensor &weights, const SimpleTensor &biases, const TensorShape &dst_shape, const PadStrideInfo &conv_info, unsigned int depth_multiplier); diff --git a/tests/validation/reference/PoolingLayer.cpp b/tests/validation/reference/PoolingLayer.cpp index f3f456b26e..69734545c9 100644 --- a/tests/validation/reference/PoolingLayer.cpp +++ b/tests/validation/reference/PoolingLayer.cpp @@ -23,7 +23,6 @@ */ #include "PoolingLayer.h" -#include "Permute.h" #include "arm_compute/core/Types.h" #include "arm_compute/core/utils/misc/ShapeCalculator.h" #include "tests/validation/FixedPoint.h" @@ -39,11 +38,14 @@ namespace reference { using namespace arm_compute::misc::shape_calculator; -template -SimpleTensor pooling_layer_nchw(const SimpleTensor &src, SimpleTensor &dst, const PoolingLayerInfo &info) +template ::value, int>::type> +SimpleTensor pooling_layer(const SimpleTensor &src, const PoolingLayerInfo &info) { ARM_COMPUTE_ERROR_ON(info.is_global_pooling() && (src.shape().x() != src.shape().y())); + // Create reference + SimpleTensor dst{ compute_pool_shape(TensorInfo(src.shape(), 1, src.data_type(), src.fixed_point_position()), info), src.data_type(), 1, src.fixed_point_position() }; + const int pool_size_x = info.is_global_pooling() ? src.shape().x() : info.pool_size().width; const int pool_size_y = info.is_global_pooling() ? src.shape().y() : info.pool_size().height; PoolingType type = info.pool_type(); @@ -171,10 +173,7 @@ SimpleTensor pooling_layer(const SimpleTensor &src, const PoolingLayerInfo bool exclude_padding = info.exclude_padding(); // Create reference - TensorInfo src_info(src.shape(), 1, src.data_type(), src.fixed_point_position()); - src_info.set_data_layout(src.data_layout()); - - SimpleTensor dst{ compute_pool_shape(src_info, info), src.data_type(), 1, src.fixed_point_position() }; + SimpleTensor dst{ compute_pool_shape(TensorInfo(src.shape(), 1, src.data_type(), src.fixed_point_position()), info), src.data_type(), 1, src.fixed_point_position() }; const auto w_dst = static_cast(dst.shape()[0]); const auto h_dst = static_cast(dst.shape()[1]); @@ -284,29 +283,6 @@ SimpleTensor pooling_layer(const SimpleTensor &src, c return dst; } -template ::value, int>::type> -SimpleTensor pooling_layer(const SimpleTensor &src, const PoolingLayerInfo &info) -{ - TensorInfo src_info(src.shape(), 1, src.data_type(), src.fixed_point_position()); - src_info.set_data_layout(src.data_layout()); - - SimpleTensor dst{ compute_pool_shape(src_info, info), src.data_type(), 1, src.fixed_point_position() }; - - if(src.data_layout() == DataLayout::NHWC) - { - SimpleTensor src_nchw = reference::permute(src, PermutationVector(1U, 2U, 0U)); - SimpleTensor dst_nchw = reference::permute(dst, PermutationVector(1U, 2U, 0U)); - - pooling_layer_nchw(src_nchw, dst_nchw, info); - - return reference::permute(dst_nchw, PermutationVector(2U, 0U, 1U)); - } - else - { - return pooling_layer_nchw(src, dst, info); - } -} - template SimpleTensor pooling_layer(const SimpleTensor &src, const PoolingLayerInfo &info); template SimpleTensor pooling_layer(const SimpleTensor &src, const PoolingLayerInfo &info); template SimpleTensor pooling_layer(const SimpleTensor &src, const PoolingLayerInfo &info); -- cgit v1.2.1