Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 1 | // |
Matthew Sloyan | 6dd0702 | 2023-01-25 12:40:11 +0000 | [diff] [blame] | 2 | // Copyright © 2020 Samsung Electronics Co Ltd and Contributors. All rights reserved. |
Declan-ARM | 7c75e33 | 2024-03-12 16:40:25 +0000 | [diff] [blame^] | 3 | // Copyright © 2023-2024 Arm Ltd and Contributors. All rights reserved. |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 4 | // SPDX-License-Identifier: MIT |
| 5 | // |
| 6 | |
| 7 | #include "ReduceLayer.hpp" |
| 8 | #include "LayerCloneBase.hpp" |
| 9 | |
| 10 | #include <armnn/TypesUtils.hpp> |
| 11 | |
Colm Donelan | 0c47974 | 2021-12-10 12:43:54 +0000 | [diff] [blame] | 12 | #include <armnn/backends/WorkloadData.hpp> |
| 13 | #include <armnn/backends/WorkloadFactory.hpp> |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 14 | |
| 15 | namespace armnn |
| 16 | { |
| 17 | |
| 18 | ReduceLayer::ReduceLayer(const ReduceDescriptor& param, const char* name) |
| 19 | : LayerWithParameters(1, 1, LayerType::Reduce, param, name) |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | std::unique_ptr<IWorkload> ReduceLayer::CreateWorkload(const IWorkloadFactory& factory) const |
| 24 | { |
| 25 | ReduceQueueDescriptor descriptor; |
Sadik Armagan | 58fb0ee | 2021-03-12 09:15:35 +0000 | [diff] [blame] | 26 | descriptor.m_Parameters.m_vAxis = m_Param.m_vAxis; |
| 27 | descriptor.m_Parameters.m_KeepDims = m_Param.m_KeepDims; |
| 28 | descriptor.m_Parameters.m_ReduceOperation = m_Param.m_ReduceOperation; |
| 29 | SetAdditionalInfo(descriptor); |
| 30 | |
Teresa Charlin | 611c7fb | 2022-01-07 09:47:29 +0000 | [diff] [blame] | 31 | return factory.CreateWorkload(LayerType::Reduce, descriptor, PrepInfoAndDesc(descriptor)); |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | ReduceLayer* ReduceLayer::Clone(Graph& graph) const |
| 35 | { |
Sadik Armagan | 58fb0ee | 2021-03-12 09:15:35 +0000 | [diff] [blame] | 36 | auto layer = CloneBase<ReduceLayer>(graph, m_Param, GetName()); |
| 37 | layer->m_Param.m_vAxis = m_Param.m_vAxis; |
| 38 | layer->m_Param.m_KeepDims = m_Param.m_KeepDims; |
| 39 | layer->m_Param.m_ReduceOperation = m_Param.m_ReduceOperation; |
| 40 | |
| 41 | return std::move(layer); |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | void ReduceLayer::ValidateTensorShapesFromInputs() |
| 45 | { |
| 46 | VerifyLayerConnections(1, CHECK_LOCATION()); |
| 47 | |
| 48 | const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape(); |
| 49 | |
| 50 | VerifyShapeInferenceType(outputShape, m_ShapeInferenceMethod); |
| 51 | |
Mike Kelly | a9ac6ba | 2023-06-30 15:18:26 +0100 | [diff] [blame] | 52 | const TensorInfo& input = GetInputSlot(0).GetTensorInfo(); |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 53 | |
Declan-ARM | 7c75e33 | 2024-03-12 16:40:25 +0000 | [diff] [blame^] | 54 | if (auto inputDims = input.GetNumDimensions(); inputDims != std::clamp(inputDims, 1u, 4u)) |
| 55 | { |
| 56 | throw armnn::LayerValidationException("ReduceLayer: Reduce supports up to 4D input."); |
| 57 | } |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 58 | |
Mike Kelly | 377fb21 | 2023-01-10 15:55:28 +0000 | [diff] [blame] | 59 | std::vector<TensorShape> inferredShapes = InferOutputShapes( {input.GetShape() }); |
| 60 | |
| 61 | ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, "ReduceLayer"); |
| 62 | } |
| 63 | |
| 64 | std::vector<TensorShape> ReduceLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const |
| 65 | { |
Declan-ARM | 7c75e33 | 2024-03-12 16:40:25 +0000 | [diff] [blame^] | 66 | if (inputShapes.size() != 1) |
| 67 | { |
| 68 | throw armnn::Exception("inputShapes' size is \"" + std::to_string(inputShapes.size()) + |
| 69 | "\" - should be \"1\"."); |
| 70 | } |
| 71 | |
Mike Kelly | 377fb21 | 2023-01-10 15:55:28 +0000 | [diff] [blame] | 72 | const TensorShape& input = inputShapes[0]; |
| 73 | |
Declan-ARM | 7c75e33 | 2024-03-12 16:40:25 +0000 | [diff] [blame^] | 74 | if (auto inputDims = input.GetNumDimensions(); inputDims != std::clamp(inputDims, 1u, 4u)) |
| 75 | { |
| 76 | throw armnn::Exception("ReduceLayer: Reduce supports up to 4D input."); |
| 77 | } |
Mike Kelly | 377fb21 | 2023-01-10 15:55:28 +0000 | [diff] [blame] | 78 | |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 79 | unsigned int rank = input.GetNumDimensions(); |
| 80 | unsigned int outputRank = 0; |
| 81 | |
| 82 | // Calculate output dimension |
| 83 | if (m_Param.m_KeepDims) |
| 84 | { |
| 85 | outputRank = rank; |
| 86 | } |
| 87 | else if (m_Param.m_vAxis.empty()) |
| 88 | { |
| 89 | outputRank = 1; |
| 90 | } |
| 91 | else if (m_Param.m_vAxis.size() > input.GetNumDimensions()) |
| 92 | { |
| 93 | throw LayerValidationException("ReduceLayer: Dimensions to reduce can not be bigger than input dimensions"); |
| 94 | } |
| 95 | else |
| 96 | { |
| 97 | outputRank = input.GetNumDimensions() - armnn::numeric_cast<unsigned int>(m_Param.m_vAxis.size()); |
| 98 | if (outputRank == 0) |
| 99 | { |
| 100 | outputRank = 1; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | std::vector<unsigned int> dimSizes(outputRank, 1); |
| 105 | if (!m_Param.m_vAxis.empty()) |
| 106 | { |
| 107 | // Skip the dimension that has been reduced unless keepDims is true. |
| 108 | unsigned int outputIndex = 0; |
| 109 | for (unsigned int i = 0; i < input.GetNumDimensions(); ++i) |
| 110 | { |
| 111 | if (std::find(m_Param.m_vAxis.begin(), m_Param.m_vAxis.end(), i) == m_Param.m_vAxis.end()) |
| 112 | { |
Mike Kelly | 377fb21 | 2023-01-10 15:55:28 +0000 | [diff] [blame] | 113 | dimSizes[outputIndex] = armnn::numeric_cast<unsigned int>(input[i]); |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 114 | ++outputIndex; |
| 115 | } |
| 116 | else if (m_Param.m_KeepDims) |
| 117 | { |
| 118 | dimSizes[outputIndex] = 1; |
| 119 | ++outputIndex; |
| 120 | } |
| 121 | } |
| 122 | } |
Mike Kelly | 377fb21 | 2023-01-10 15:55:28 +0000 | [diff] [blame] | 123 | return std::vector<TensorShape>({ TensorShape(outputRank, dimSizes.data()) }); |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Nikhil Raj | 4d2eec0 | 2022-05-30 11:08:52 +0100 | [diff] [blame] | 126 | void ReduceLayer::ExecuteStrategy(IStrategy& strategy) const |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 127 | { |
Nikhil Raj | 4d2eec0 | 2022-05-30 11:08:52 +0100 | [diff] [blame] | 128 | strategy.ExecuteStrategy(this, GetParameters(), {}, GetName()); |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 129 | } |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 130 | |
| 131 | } // namespace armnn |