Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 1 | // |
Teresa Charlin | fbf0e5b | 2020-08-17 01:01:06 +0100 | [diff] [blame] | 2 | // Copyright © 2017 Arm Ltd and Contributors. All rights reserved. |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
Sadik Armagan | a097d2a | 2021-11-24 15:47:28 +0000 | [diff] [blame] | 8 | #include <armnnTestUtils/LayerTestResult.hpp> |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 9 | |
| 10 | #include <ResolveType.hpp> |
| 11 | |
Matteo Martincigh | e5b8eb9 | 2019-11-28 15:45:42 +0000 | [diff] [blame] | 12 | #include <armnn/backends/IBackendInternal.hpp> |
Colm Donelan | 0c47974 | 2021-12-10 12:43:54 +0000 | [diff] [blame] | 13 | #include <armnn/backends/WorkloadFactory.hpp> |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 14 | |
| 15 | namespace |
| 16 | { |
| 17 | |
| 18 | template<armnn::DataType ArmnnType, typename T, std::size_t InputDim, std::size_t OutputDim> |
| 19 | LayerTestResult<T, OutputDim> MeanTestHelper( |
| 20 | armnn::IWorkloadFactory& workloadFactory, |
| 21 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
Finn Williams | 826a543 | 2020-08-27 16:15:20 +0100 | [diff] [blame] | 22 | const armnn::ITensorHandleFactory& tensorHandleFactory, |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 23 | const unsigned int* inputShape, |
| 24 | const std::vector<float>& inputData, |
| 25 | const std::vector<unsigned int>& axis, |
| 26 | bool keepDims, |
| 27 | const unsigned int* outputShape, |
| 28 | const std::vector<float>& outputData, |
| 29 | float scale = 1.0f, |
| 30 | int32_t offset = 0) |
| 31 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 32 | IgnoreUnused(memoryManager); |
Derek Lamberti | c374ff0 | 2019-12-10 21:57:35 +0000 | [diff] [blame] | 33 | |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 34 | armnn::TensorInfo inputTensorInfo(InputDim, inputShape, ArmnnType); |
| 35 | armnn::TensorInfo outputTensorInfo(OutputDim, outputShape, ArmnnType); |
| 36 | |
| 37 | inputTensorInfo.SetQuantizationScale(scale); |
| 38 | inputTensorInfo.SetQuantizationOffset(offset); |
| 39 | |
| 40 | outputTensorInfo.SetQuantizationScale(scale); |
| 41 | outputTensorInfo.SetQuantizationOffset(offset); |
| 42 | |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 43 | auto input = ConvertToDataType<ArmnnType>(inputData, inputTensorInfo); |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 44 | |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 45 | std::vector<T> actualOutput(outputTensorInfo.GetNumElements()); |
| 46 | std::vector<T> expectedOutput = ConvertToDataType<ArmnnType>(outputData, outputTensorInfo); |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 47 | |
Finn Williams | 826a543 | 2020-08-27 16:15:20 +0100 | [diff] [blame] | 48 | std::unique_ptr<armnn::ITensorHandle> inputHandle = tensorHandleFactory.CreateTensorHandle(inputTensorInfo); |
| 49 | std::unique_ptr<armnn::ITensorHandle> outputHandle = tensorHandleFactory.CreateTensorHandle(outputTensorInfo); |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 50 | |
| 51 | armnn::MeanQueueDescriptor data; |
| 52 | data.m_Parameters.m_Axis = axis; |
| 53 | data.m_Parameters.m_KeepDims = keepDims; |
| 54 | armnn::WorkloadInfo info; |
| 55 | AddInputToWorkload(data, info, inputTensorInfo, inputHandle.get()); |
| 56 | AddOutputToWorkload(data, info, outputTensorInfo, outputHandle.get()); |
| 57 | |
| 58 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateMean(data, info); |
| 59 | |
| 60 | inputHandle->Allocate(); |
| 61 | outputHandle->Allocate(); |
| 62 | |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 63 | CopyDataToITensorHandle(inputHandle.get(), input.data()); |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 64 | |
| 65 | workload->PostAllocationConfigure(); |
| 66 | workload->Execute(); |
| 67 | |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 68 | CopyDataFromITensorHandle(actualOutput.data(), outputHandle.get()); |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 69 | |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 70 | return LayerTestResult<T, OutputDim>(actualOutput, |
| 71 | expectedOutput, |
| 72 | outputHandle->GetShape(), |
| 73 | outputTensorInfo.GetShape()); |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | } // anonymous namespace |
| 77 | |
| 78 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 79 | LayerTestResult<T, 1> MeanSimpleTest( |
| 80 | armnn::IWorkloadFactory& workloadFactory, |
Finn Williams | 826a543 | 2020-08-27 16:15:20 +0100 | [diff] [blame] | 81 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 82 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 83 | { |
| 84 | const unsigned int inputShape[] = { 3, 2 }; |
| 85 | const unsigned int outputShape[] = { 1 }; |
| 86 | |
| 87 | std::vector<float> input({ 1.5f, 1.5f, 2.5f, 2.5f, 3.5f, 3.5f }); |
| 88 | std::vector<float> output({ 2.5f }); |
| 89 | |
| 90 | return MeanTestHelper<ArmnnType, T, 2, 1>( |
Finn Williams | 826a543 | 2020-08-27 16:15:20 +0100 | [diff] [blame] | 91 | workloadFactory, memoryManager, tensorHandleFactory, inputShape, input, {}, false, outputShape, output); |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 95 | LayerTestResult<T, 3> MeanSimpleAxisTest( |
| 96 | armnn::IWorkloadFactory& workloadFactory, |
Finn Williams | 826a543 | 2020-08-27 16:15:20 +0100 | [diff] [blame] | 97 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 98 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 99 | { |
| 100 | const unsigned int inputShape[] = { 2, 3, 1, 2 }; |
| 101 | const unsigned int outputShape[] = { 3, 1, 2 }; |
| 102 | |
| 103 | std::vector<float> input({ 1.5f, 2.5f, 3.5f, 4.5f, 5.5f, 6.5f, 1.5f, 2.5f, 3.5f, 4.5f, 5.5f, 6.5f }); |
| 104 | std::vector<float> output({ 1.5f, 2.5f, 3.5f, 4.5f, 5.5f, 6.5f }); |
| 105 | |
| 106 | return MeanTestHelper<ArmnnType, T, 4, 3>( |
Finn Williams | 826a543 | 2020-08-27 16:15:20 +0100 | [diff] [blame] | 107 | workloadFactory, memoryManager, tensorHandleFactory, inputShape, input, { 0 }, false, outputShape, output); |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 111 | LayerTestResult<T, 4> MeanKeepDimsTest( |
| 112 | armnn::IWorkloadFactory& workloadFactory, |
Finn Williams | 826a543 | 2020-08-27 16:15:20 +0100 | [diff] [blame] | 113 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 114 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 115 | { |
| 116 | const unsigned int inputShape[] = { 1, 1, 3, 2 }; |
| 117 | const unsigned int outputShape[] = { 1, 1, 1, 2 }; |
| 118 | |
| 119 | std::vector<float> input({ 1.5f, 1.5f, 2.5f, 2.5f, 3.5f, 3.5f }); |
| 120 | std::vector<float> output({ 2.5f, 2.5f }); |
| 121 | |
| 122 | return MeanTestHelper<ArmnnType, T, 4, 4>( |
Finn Williams | 826a543 | 2020-08-27 16:15:20 +0100 | [diff] [blame] | 123 | workloadFactory, memoryManager, tensorHandleFactory, inputShape, input, { 2 }, true, outputShape, output); |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 127 | LayerTestResult<T, 4> MeanMultipleDimsTest( |
| 128 | armnn::IWorkloadFactory& workloadFactory, |
Finn Williams | 826a543 | 2020-08-27 16:15:20 +0100 | [diff] [blame] | 129 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 130 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 131 | { |
| 132 | const unsigned int inputShape[] = { 2, 3, 1, 2 }; |
| 133 | const unsigned int outputShape[] = { 1, 3, 1, 1 }; |
| 134 | |
| 135 | std::vector<float> input({ 1.5f, 2.5f, 3.5f, 4.5f, 5.5f, 6.5f, 1.5f, 2.5f, 3.5f, 4.5f, 5.5f, 6.5 }); |
| 136 | std::vector<float> output({ 2.0f, 4.0f, 6.0f }); |
| 137 | |
| 138 | return MeanTestHelper<ArmnnType, T, 4, 4>( |
Finn Williams | 826a543 | 2020-08-27 16:15:20 +0100 | [diff] [blame] | 139 | workloadFactory, memoryManager, tensorHandleFactory, |
| 140 | inputShape, input, { 0, 3 }, true, outputShape, output); |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 144 | LayerTestResult<T, 1> MeanVts1Test( |
| 145 | armnn::IWorkloadFactory& workloadFactory, |
Finn Williams | 826a543 | 2020-08-27 16:15:20 +0100 | [diff] [blame] | 146 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 147 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 148 | { |
| 149 | const unsigned int inputShape[] = { 4, 3, 2 }; |
| 150 | const unsigned int outputShape[] = { 2 }; |
| 151 | |
| 152 | std::vector<float> input({ 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f, 11.0f, 12.0f, 13.0f, 14.0f, |
| 153 | 15.0f, 16.0f, 17.0f, 18.0f, 19.0f, 20.0f, 21.0f, 22.0f, 23.0f, 24.0f }); |
| 154 | std::vector<float> output({ 12.0f, 13.0f }); |
| 155 | |
| 156 | return MeanTestHelper<ArmnnType, T, 3, 1>( |
Finn Williams | 826a543 | 2020-08-27 16:15:20 +0100 | [diff] [blame] | 157 | workloadFactory, memoryManager, tensorHandleFactory, |
| 158 | inputShape, input, { 0, 1 }, false, outputShape, output); |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 162 | LayerTestResult<T, 3> MeanVts2Test( |
| 163 | armnn::IWorkloadFactory& workloadFactory, |
Finn Williams | 826a543 | 2020-08-27 16:15:20 +0100 | [diff] [blame] | 164 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 165 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 166 | { |
| 167 | const unsigned int inputShape[] = { 4, 3, 2 }; |
| 168 | const unsigned int outputShape[] = { 1, 3, 1 }; |
| 169 | |
| 170 | std::vector<float> input({ 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f, 11.0f, 12.0f, 13.0f, 14.0f, |
| 171 | 15.0f, 16.0f, 17.0f, 18.0f, 19.0f, 20.0f, 21.0f, 22.0f, 23.0f, 24.0f }); |
| 172 | std::vector<float> output({ 10.5f, 12.5f, 14.5f }); |
| 173 | |
| 174 | return MeanTestHelper<ArmnnType, T, 3, 3>( |
Finn Williams | 826a543 | 2020-08-27 16:15:20 +0100 | [diff] [blame] | 175 | workloadFactory, memoryManager, tensorHandleFactory, |
| 176 | inputShape, input, { 0, 2 }, true, outputShape, output); |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 180 | LayerTestResult<T, 3> MeanVts3Test( |
| 181 | armnn::IWorkloadFactory& workloadFactory, |
Finn Williams | 826a543 | 2020-08-27 16:15:20 +0100 | [diff] [blame] | 182 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 183 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 184 | { |
| 185 | const unsigned int inputShape[] = { 1, 2, 2, 1 }; |
| 186 | const unsigned int outputShape[] = { 1, 2, 1 }; |
| 187 | |
| 188 | std::vector<float> input({ 1.0f, 2.0f, 3.0f, 4.0f }); |
| 189 | std::vector<float> output({ 1.5f, 3.5f }); |
| 190 | |
| 191 | return MeanTestHelper<ArmnnType, T, 4, 3>( |
Finn Williams | 826a543 | 2020-08-27 16:15:20 +0100 | [diff] [blame] | 192 | workloadFactory, memoryManager, tensorHandleFactory, inputShape, input, { 2 }, false, outputShape, output); |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 193 | } |