From d905decd256558bbee165e636ce4242ac3b9c917 Mon Sep 17 00:00:00 2001 From: Matthew Sloyan Date: Mon, 3 May 2021 12:22:03 +0100 Subject: MLCE-418 Reduce layer does not support multiple axes * Added backend specific optimization to chain new reduces layers for each axis to simulate behaviour of a layer with multiple axes. * Added function to calculate reduced output shape. * Added unit tests. Signed-off-by: Matthew Sloyan Change-Id: I180b0b111b7bcf3d0c283f1db0b82d5f17757682 --- src/backends/aclCommon/ArmComputeUtils.hpp | 55 ++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) (limited to 'src/backends/aclCommon/ArmComputeUtils.hpp') diff --git a/src/backends/aclCommon/ArmComputeUtils.hpp b/src/backends/aclCommon/ArmComputeUtils.hpp index d9efab288f..5bc5abcb05 100644 --- a/src/backends/aclCommon/ArmComputeUtils.hpp +++ b/src/backends/aclCommon/ArmComputeUtils.hpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -267,4 +268,58 @@ inline arm_compute::ReductionOperation ConvertReductionOperationToAcl(const Redu } } +/// Function to compute the output tensor shape based on the axes and if keepDims is set. +inline const TensorShape ComputeReductionTensorShape(const armnn::TensorInfo& input, + const std::vector& vAxis, + const bool keepDims) +{ + unsigned int rank = input.GetNumDimensions(); + unsigned int outputRank = 0; + + // Calculate output dimension + if (keepDims) + { + outputRank = rank; + } + else if (vAxis.empty()) + { + outputRank = 1; + } + else if (vAxis.size() > input.GetNumDimensions()) + { + throw LayerValidationException("ReduceLayer: Dimensions to reduce can not be bigger than input dimensions"); + } + else + { + outputRank = input.GetNumDimensions() - armnn::numeric_cast(vAxis.size()); + if (outputRank == 0) + { + outputRank = 1; + } + } + + std::vector dimSizes(outputRank, 1); + if (!vAxis.empty()) + { + // Skip the dimension that has been reduced unless keepDims is true. + unsigned int outputIndex = 0; + for (unsigned int i = 0; i < input.GetNumDimensions(); ++i) + { + if (std::find(vAxis.begin(), vAxis.end(), i) == vAxis.end()) + { + dimSizes[outputIndex] = armnn::numeric_cast(input.GetShape()[i]); + ++outputIndex; + } + else if (keepDims) + { + dimSizes[outputIndex] = 1; + ++outputIndex; + } + } + } + + const TensorShape inferredShape = TensorShape(outputRank, dimSizes.data()); + return inferredShape; +} + } // namespace armnn -- cgit v1.2.1