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 | |
| 8 | #include "LayerTestResult.hpp" |
| 9 | |
| 10 | #include <ResolveType.hpp> |
| 11 | |
Matteo Martincigh | e5b8eb9 | 2019-11-28 15:45:42 +0000 | [diff] [blame] | 12 | #include <armnn/backends/IBackendInternal.hpp> |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 13 | #include <backendsCommon/WorkloadFactory.hpp> |
| 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 | |
| 43 | auto input = MakeTensor<T, InputDim>(inputTensorInfo, ConvertToDataType<ArmnnType>(inputData, inputTensorInfo)); |
| 44 | |
| 45 | LayerTestResult<T, OutputDim> result(outputTensorInfo); |
| 46 | result.outputExpected = MakeTensor<T, OutputDim>( |
| 47 | outputTensorInfo, ConvertToDataType<ArmnnType>(outputData, outputTensorInfo)); |
| 48 | |
Finn Williams | 826a543 | 2020-08-27 16:15:20 +0100 | [diff] [blame] | 49 | std::unique_ptr<armnn::ITensorHandle> inputHandle = tensorHandleFactory.CreateTensorHandle(inputTensorInfo); |
| 50 | std::unique_ptr<armnn::ITensorHandle> outputHandle = tensorHandleFactory.CreateTensorHandle(outputTensorInfo); |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 51 | |
| 52 | armnn::MeanQueueDescriptor data; |
| 53 | data.m_Parameters.m_Axis = axis; |
| 54 | data.m_Parameters.m_KeepDims = keepDims; |
| 55 | armnn::WorkloadInfo info; |
| 56 | AddInputToWorkload(data, info, inputTensorInfo, inputHandle.get()); |
| 57 | AddOutputToWorkload(data, info, outputTensorInfo, outputHandle.get()); |
| 58 | |
| 59 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateMean(data, info); |
| 60 | |
| 61 | inputHandle->Allocate(); |
| 62 | outputHandle->Allocate(); |
| 63 | |
| 64 | CopyDataToITensorHandle(inputHandle.get(), input.origin()); |
| 65 | |
| 66 | workload->PostAllocationConfigure(); |
| 67 | workload->Execute(); |
| 68 | |
| 69 | CopyDataFromITensorHandle(result.output.origin(), outputHandle.get()); |
| 70 | |
| 71 | return result; |
| 72 | } |
| 73 | |
| 74 | } // anonymous namespace |
| 75 | |
| 76 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 77 | LayerTestResult<T, 1> MeanSimpleTest( |
| 78 | armnn::IWorkloadFactory& workloadFactory, |
Finn Williams | 826a543 | 2020-08-27 16:15:20 +0100 | [diff] [blame] | 79 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 80 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 81 | { |
| 82 | const unsigned int inputShape[] = { 3, 2 }; |
| 83 | const unsigned int outputShape[] = { 1 }; |
| 84 | |
| 85 | std::vector<float> input({ 1.5f, 1.5f, 2.5f, 2.5f, 3.5f, 3.5f }); |
| 86 | std::vector<float> output({ 2.5f }); |
| 87 | |
| 88 | return MeanTestHelper<ArmnnType, T, 2, 1>( |
Finn Williams | 826a543 | 2020-08-27 16:15:20 +0100 | [diff] [blame] | 89 | workloadFactory, memoryManager, tensorHandleFactory, inputShape, input, {}, false, outputShape, output); |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 93 | LayerTestResult<T, 3> MeanSimpleAxisTest( |
| 94 | armnn::IWorkloadFactory& workloadFactory, |
Finn Williams | 826a543 | 2020-08-27 16:15:20 +0100 | [diff] [blame] | 95 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 96 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 97 | { |
| 98 | const unsigned int inputShape[] = { 2, 3, 1, 2 }; |
| 99 | const unsigned int outputShape[] = { 3, 1, 2 }; |
| 100 | |
| 101 | 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 }); |
| 102 | std::vector<float> output({ 1.5f, 2.5f, 3.5f, 4.5f, 5.5f, 6.5f }); |
| 103 | |
| 104 | return MeanTestHelper<ArmnnType, T, 4, 3>( |
Finn Williams | 826a543 | 2020-08-27 16:15:20 +0100 | [diff] [blame] | 105 | workloadFactory, memoryManager, tensorHandleFactory, inputShape, input, { 0 }, false, outputShape, output); |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 109 | LayerTestResult<T, 4> MeanKeepDimsTest( |
| 110 | armnn::IWorkloadFactory& workloadFactory, |
Finn Williams | 826a543 | 2020-08-27 16:15:20 +0100 | [diff] [blame] | 111 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 112 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 113 | { |
| 114 | const unsigned int inputShape[] = { 1, 1, 3, 2 }; |
| 115 | const unsigned int outputShape[] = { 1, 1, 1, 2 }; |
| 116 | |
| 117 | std::vector<float> input({ 1.5f, 1.5f, 2.5f, 2.5f, 3.5f, 3.5f }); |
| 118 | std::vector<float> output({ 2.5f, 2.5f }); |
| 119 | |
| 120 | return MeanTestHelper<ArmnnType, T, 4, 4>( |
Finn Williams | 826a543 | 2020-08-27 16:15:20 +0100 | [diff] [blame] | 121 | workloadFactory, memoryManager, tensorHandleFactory, inputShape, input, { 2 }, true, outputShape, output); |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 125 | LayerTestResult<T, 4> MeanMultipleDimsTest( |
| 126 | armnn::IWorkloadFactory& workloadFactory, |
Finn Williams | 826a543 | 2020-08-27 16:15:20 +0100 | [diff] [blame] | 127 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 128 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 129 | { |
| 130 | const unsigned int inputShape[] = { 2, 3, 1, 2 }; |
| 131 | const unsigned int outputShape[] = { 1, 3, 1, 1 }; |
| 132 | |
| 133 | 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 }); |
| 134 | std::vector<float> output({ 2.0f, 4.0f, 6.0f }); |
| 135 | |
| 136 | return MeanTestHelper<ArmnnType, T, 4, 4>( |
Finn Williams | 826a543 | 2020-08-27 16:15:20 +0100 | [diff] [blame] | 137 | workloadFactory, memoryManager, tensorHandleFactory, |
| 138 | inputShape, input, { 0, 3 }, true, outputShape, output); |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 142 | LayerTestResult<T, 1> MeanVts1Test( |
| 143 | armnn::IWorkloadFactory& workloadFactory, |
Finn Williams | 826a543 | 2020-08-27 16:15:20 +0100 | [diff] [blame] | 144 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 145 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 146 | { |
| 147 | const unsigned int inputShape[] = { 4, 3, 2 }; |
| 148 | const unsigned int outputShape[] = { 2 }; |
| 149 | |
| 150 | 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, |
| 151 | 15.0f, 16.0f, 17.0f, 18.0f, 19.0f, 20.0f, 21.0f, 22.0f, 23.0f, 24.0f }); |
| 152 | std::vector<float> output({ 12.0f, 13.0f }); |
| 153 | |
| 154 | return MeanTestHelper<ArmnnType, T, 3, 1>( |
Finn Williams | 826a543 | 2020-08-27 16:15:20 +0100 | [diff] [blame] | 155 | workloadFactory, memoryManager, tensorHandleFactory, |
| 156 | inputShape, input, { 0, 1 }, false, outputShape, output); |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 160 | LayerTestResult<T, 3> MeanVts2Test( |
| 161 | armnn::IWorkloadFactory& workloadFactory, |
Finn Williams | 826a543 | 2020-08-27 16:15:20 +0100 | [diff] [blame] | 162 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 163 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 164 | { |
| 165 | const unsigned int inputShape[] = { 4, 3, 2 }; |
| 166 | const unsigned int outputShape[] = { 1, 3, 1 }; |
| 167 | |
| 168 | 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, |
| 169 | 15.0f, 16.0f, 17.0f, 18.0f, 19.0f, 20.0f, 21.0f, 22.0f, 23.0f, 24.0f }); |
| 170 | std::vector<float> output({ 10.5f, 12.5f, 14.5f }); |
| 171 | |
| 172 | return MeanTestHelper<ArmnnType, T, 3, 3>( |
Finn Williams | 826a543 | 2020-08-27 16:15:20 +0100 | [diff] [blame] | 173 | workloadFactory, memoryManager, tensorHandleFactory, |
| 174 | inputShape, input, { 0, 2 }, true, outputShape, output); |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 178 | LayerTestResult<T, 3> MeanVts3Test( |
| 179 | armnn::IWorkloadFactory& workloadFactory, |
Finn Williams | 826a543 | 2020-08-27 16:15:20 +0100 | [diff] [blame] | 180 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 181 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 182 | { |
| 183 | const unsigned int inputShape[] = { 1, 2, 2, 1 }; |
| 184 | const unsigned int outputShape[] = { 1, 2, 1 }; |
| 185 | |
| 186 | std::vector<float> input({ 1.0f, 2.0f, 3.0f, 4.0f }); |
| 187 | std::vector<float> output({ 1.5f, 3.5f }); |
| 188 | |
| 189 | return MeanTestHelper<ArmnnType, T, 4, 3>( |
Finn Williams | 826a543 | 2020-08-27 16:15:20 +0100 | [diff] [blame] | 190 | workloadFactory, memoryManager, tensorHandleFactory, inputShape, input, { 2 }, false, outputShape, output); |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 191 | } |