aboutsummaryrefslogtreecommitdiff
path: root/src/backends/aclCommon/ArmComputeUtils.hpp
diff options
context:
space:
mode:
authorMatthew Sloyan <matthew.sloyan@arm.com>2021-05-03 12:22:03 +0100
committerMatthew Sloyan <matthew.sloyan@arm.com>2021-05-06 17:58:26 +0000
commitd905decd256558bbee165e636ce4242ac3b9c917 (patch)
tree86f51622399553d1741b66ff232a429de8fc43f8 /src/backends/aclCommon/ArmComputeUtils.hpp
parent1f58f03d82c482626b1b4673b6c0e25da4338fb5 (diff)
downloadarmnn-d905decd256558bbee165e636ce4242ac3b9c917.tar.gz
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 <matthew.sloyan@arm.com> Change-Id: I180b0b111b7bcf3d0c283f1db0b82d5f17757682
Diffstat (limited to 'src/backends/aclCommon/ArmComputeUtils.hpp')
-rw-r--r--src/backends/aclCommon/ArmComputeUtils.hpp55
1 files changed, 55 insertions, 0 deletions
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 <armnn/Descriptors.hpp>
#include <armnn/Tensor.hpp>
#include <armnn/utility/Assert.hpp>
+#include <armnn/utility/NumericCast.hpp>
#include <backendsCommon/WorkloadData.hpp>
#include <arm_compute/core/Types.h>
@@ -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<uint32_t>& 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<unsigned int>(vAxis.size());
+ if (outputRank == 0)
+ {
+ outputRank = 1;
+ }
+ }
+
+ std::vector<unsigned int> 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<unsigned int>(input.GetShape()[i]);
+ ++outputIndex;
+ }
+ else if (keepDims)
+ {
+ dimSizes[outputIndex] = 1;
+ ++outputIndex;
+ }
+ }
+ }
+
+ const TensorShape inferredShape = TensorShape(outputRank, dimSizes.data());
+ return inferredShape;
+}
+
} // namespace armnn