Nikhil Raj | 68c2c90 | 2019-09-19 11:21:11 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2019 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "ArgMinMaxTestImpl.hpp" |
| 7 | |
| 8 | #include <armnn/ArmNN.hpp> |
| 9 | |
| 10 | #include <backendsCommon/test/DataTypeUtils.hpp> |
| 11 | #include <backendsCommon/test/TensorCopyUtils.hpp> |
| 12 | #include <backendsCommon/test/WorkloadTestUtils.hpp> |
| 13 | |
| 14 | #include <test/TensorHelpers.hpp> |
| 15 | |
| 16 | namespace |
| 17 | { |
| 18 | |
| 19 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 20 | LayerTestResult<int32_t, 3> ArgMinMaxTestCommon( |
| 21 | armnn::IWorkloadFactory& workloadFactory, |
| 22 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 23 | armnn::ArgMinMaxFunction argMinMaxFunction, |
| 24 | const armnn::TensorInfo inputTensorInfo, |
| 25 | const armnn::TensorInfo outputTensorInfo, |
| 26 | const std::vector<float>& inputData, |
| 27 | const std::vector<int32_t>& outputData, |
| 28 | int axis = 3) |
| 29 | { |
| 30 | auto inputTensor = MakeTensor<T, 4>(inputTensorInfo, ConvertToDataType<ArmnnType>(inputData, inputTensorInfo)); |
| 31 | |
| 32 | LayerTestResult<int32_t, 3> result(outputTensorInfo); |
Nikhil Raj | 68c2c90 | 2019-09-19 11:21:11 +0100 | [diff] [blame] | 33 | result.outputExpected = MakeTensor<int32_t, 3>(outputTensorInfo, outputData); |
| 34 | |
| 35 | std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo); |
| 36 | std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo); |
| 37 | |
| 38 | armnn::ArgMinMaxQueueDescriptor descriptor; |
| 39 | descriptor.m_Parameters.m_Function = argMinMaxFunction; |
| 40 | descriptor.m_Parameters.m_Axis = axis; |
| 41 | armnn::WorkloadInfo info; |
| 42 | |
| 43 | AddInputToWorkload(descriptor, info, inputTensorInfo, inputHandle.get()); |
| 44 | AddOutputToWorkload(descriptor, info, outputTensorInfo, outputHandle.get()); |
| 45 | |
| 46 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateArgMinMax(descriptor, info); |
| 47 | |
| 48 | inputHandle->Allocate(); |
| 49 | outputHandle->Allocate(); |
| 50 | |
| 51 | CopyDataToITensorHandle(inputHandle.get(), &inputTensor[0][0][0][0]); |
| 52 | |
| 53 | workload->PostAllocationConfigure(); |
| 54 | workload->Execute(); |
| 55 | |
| 56 | CopyDataFromITensorHandle(&result.output[0][0][0], outputHandle.get()); |
| 57 | |
| 58 | return result; |
Nikhil Raj | 68c2c90 | 2019-09-19 11:21:11 +0100 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | } // namespace |
| 62 | |
| 63 | template<armnn::DataType ArmnnType, typename T> |
| 64 | LayerTestResult<int32_t, 3> ArgMaxSimpleTest( |
| 65 | armnn::IWorkloadFactory& workloadFactory, |
| 66 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 67 | { |
| 68 | const armnn::TensorShape inputShape{ 1, 1, 1, 5 }; |
| 69 | const armnn::TensorShape outputShape{ 1, 1, 1 }; |
| 70 | |
| 71 | armnn::TensorInfo inputTensorInfo(inputShape, ArmnnType); |
| 72 | |
| 73 | if(armnn::IsQuantizedType<T>()) |
| 74 | { |
| 75 | inputTensorInfo.SetQuantizationScale(1.0f); |
| 76 | inputTensorInfo.SetQuantizationOffset(0); |
| 77 | } |
| 78 | |
| 79 | armnn::TensorInfo outputTensorInfo(outputShape, armnn::DataType::Signed32); |
| 80 | |
| 81 | std::vector<float> inputValues({ 5.0f, 2.0f, 8.0f, 10.0f, 9.0f }); |
| 82 | std::vector<int32_t> outputValues({ 3 }); |
| 83 | |
| 84 | return ArgMinMaxTestCommon<ArmnnType>(workloadFactory, memoryManager, |
| 85 | armnn::ArgMinMaxFunction::Max, |
| 86 | inputTensorInfo, outputTensorInfo, |
James Conroy | d47a064 | 2019-09-17 14:22:06 +0100 | [diff] [blame] | 87 | inputValues, outputValues, -1); // axis -1 === 3 |
Nikhil Raj | 68c2c90 | 2019-09-19 11:21:11 +0100 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | template<armnn::DataType ArmnnType, typename T> |
| 91 | LayerTestResult<int32_t, 3> ArgMinSimpleTest( |
| 92 | armnn::IWorkloadFactory& workloadFactory, |
| 93 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 94 | { |
| 95 | const armnn::TensorShape inputShape{ 1, 1, 1, 5 }; |
| 96 | const armnn::TensorShape outputShape{ 1, 1, 1 }; |
| 97 | |
| 98 | armnn::TensorInfo inputTensorInfo(inputShape, ArmnnType); |
| 99 | |
| 100 | if(armnn::IsQuantizedType<T>()) |
| 101 | { |
| 102 | inputTensorInfo.SetQuantizationScale(1.0f); |
| 103 | inputTensorInfo.SetQuantizationOffset(0); |
| 104 | } |
| 105 | |
| 106 | armnn::TensorInfo outputTensorInfo(outputShape, armnn::DataType::Signed32); |
| 107 | |
| 108 | std::vector<float> inputValues({ 5.0f, 2.0f, 8.0f, 10.0f, 9.0f }); |
| 109 | std::vector<int32_t> outputValues({ 1 }); |
| 110 | |
| 111 | return ArgMinMaxTestCommon<ArmnnType>(workloadFactory, memoryManager, |
| 112 | armnn::ArgMinMaxFunction::Min, |
| 113 | inputTensorInfo, outputTensorInfo, |
| 114 | inputValues, outputValues, 3); |
| 115 | } |
| 116 | |
| 117 | template<armnn::DataType ArmnnType, typename T> |
James Conroy | d47a064 | 2019-09-17 14:22:06 +0100 | [diff] [blame] | 118 | LayerTestResult<int32_t, 3> ArgMinChannelTest( |
Nikhil Raj | 68c2c90 | 2019-09-19 11:21:11 +0100 | [diff] [blame] | 119 | armnn::IWorkloadFactory& workloadFactory, |
| 120 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 121 | { |
| 122 | const armnn::TensorShape inputShape{ 1, 3, 2, 4}; |
| 123 | const armnn::TensorShape outputShape{ 1, 2, 4 }; |
| 124 | |
| 125 | armnn::TensorInfo inputTensorInfo(inputShape, ArmnnType); |
| 126 | |
| 127 | if(armnn::IsQuantizedType<T>()) |
| 128 | { |
| 129 | inputTensorInfo.SetQuantizationScale(1.0f); |
| 130 | inputTensorInfo.SetQuantizationOffset(0); |
| 131 | } |
| 132 | |
| 133 | armnn::TensorInfo outputTensorInfo(outputShape, armnn::DataType::Signed32); |
| 134 | |
James Conroy | d47a064 | 2019-09-17 14:22:06 +0100 | [diff] [blame] | 135 | std::vector<float> inputValues({ 1.0f, 2.0f, 3.0f, 4.0f, |
| 136 | 5.0f, 6.0f, 7.0f, 8.0f, |
| 137 | |
| 138 | 10.0f, 20.0f, 30.0f, 40.0f, |
| 139 | 50.0f, 60.0f, 70.0f, 80.0f, |
| 140 | |
| 141 | 100.0f, 200.0f, 300.0f, 400.0f, |
| 142 | 500.0f, 600.0f, 700.0f, 800.0f }); |
| 143 | std::vector<int32_t> outputValues({ 0, 0, 0, 0, |
| 144 | 0, 0, 0, 0 }); |
| 145 | |
| 146 | return ArgMinMaxTestCommon<ArmnnType>(workloadFactory, memoryManager, |
| 147 | armnn::ArgMinMaxFunction::Min, |
| 148 | inputTensorInfo, outputTensorInfo, |
| 149 | inputValues, outputValues, 1); |
| 150 | } |
| 151 | |
| 152 | template<armnn::DataType ArmnnType, typename T> |
| 153 | LayerTestResult<int32_t, 3> ArgMaxChannelTest( |
| 154 | armnn::IWorkloadFactory& workloadFactory, |
| 155 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 156 | { |
| 157 | const armnn::TensorShape inputShape{ 1, 3, 2, 4}; |
| 158 | const armnn::TensorShape outputShape{ 1, 2, 4 }; |
| 159 | |
| 160 | armnn::TensorInfo inputTensorInfo(inputShape, ArmnnType); |
| 161 | |
| 162 | if(armnn::IsQuantizedType<T>()) |
| 163 | { |
| 164 | inputTensorInfo.SetQuantizationScale(1.0f); |
| 165 | inputTensorInfo.SetQuantizationOffset(0); |
| 166 | } |
| 167 | |
| 168 | armnn::TensorInfo outputTensorInfo(outputShape, armnn::DataType::Signed32); |
| 169 | |
| 170 | std::vector<float> inputValues({ 1.0f, 2.0f, 3.0f, 4.0f, |
| 171 | 5.0f, 6.0f, 7.0f, 8.0f, |
Nikhil Raj | 68c2c90 | 2019-09-19 11:21:11 +0100 | [diff] [blame] | 172 | |
| 173 | 10.0f, 20.0f, 30.0f, 40.0f, |
| 174 | 50.0f, 60.0f, 70.0f, 80.0f, |
| 175 | |
James Conroy | d47a064 | 2019-09-17 14:22:06 +0100 | [diff] [blame] | 176 | 100.0f, 200.0f, 300.0f, 400.0f, |
| 177 | 500.0f, 600.0f, 700.0f, 800.0f }); |
Nikhil Raj | 68c2c90 | 2019-09-19 11:21:11 +0100 | [diff] [blame] | 178 | std::vector<int32_t> outputValues({ 2, 2, 2, 2, |
| 179 | 2, 2, 2, 2 }); |
| 180 | |
| 181 | return ArgMinMaxTestCommon<ArmnnType>(workloadFactory, memoryManager, |
| 182 | armnn::ArgMinMaxFunction::Max, |
| 183 | inputTensorInfo, outputTensorInfo, |
| 184 | inputValues, outputValues, 1); |
| 185 | } |
| 186 | |
James Conroy | d47a064 | 2019-09-17 14:22:06 +0100 | [diff] [blame] | 187 | template<armnn::DataType ArmnnType, typename T> |
| 188 | LayerTestResult<int32_t, 3> ArgMaxHeightTest( |
| 189 | armnn::IWorkloadFactory& workloadFactory, |
| 190 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 191 | { |
| 192 | const armnn::TensorShape inputShape{ 1, 3, 2, 4}; |
James Conroy | c8724c7 | 2019-10-08 15:41:34 +0100 | [diff] [blame] | 193 | const armnn::TensorShape outputShape{ 1, 3, 4 }; |
James Conroy | d47a064 | 2019-09-17 14:22:06 +0100 | [diff] [blame] | 194 | |
| 195 | armnn::TensorInfo inputTensorInfo(inputShape, ArmnnType); |
| 196 | armnn::TensorInfo outputTensorInfo(outputShape, armnn::DataType::Signed32); |
| 197 | |
| 198 | std::vector<float> inputValues({ 1.0f, 2.0f, 3.0f, 4.0f, |
| 199 | 5.0f, 6.0f, 7.0f, 8.0f, |
| 200 | |
| 201 | 10.0f, 20.0f, 30.0f, 40.0f, |
| 202 | 50.0f, 60.0f, 70.0f, 80.0f, |
| 203 | |
| 204 | 100.0f, 200.0f, 300.0f, 400.0f, |
| 205 | 500.0f, 600.0f, 700.0f, 800.0f }); |
| 206 | std::vector<int32_t> outputValues({ 1, 1, 1, 1, |
| 207 | 1, 1, 1, 1, |
| 208 | 1, 1, 1, 1 }); |
| 209 | |
| 210 | return ArgMinMaxTestCommon<ArmnnType>(workloadFactory, memoryManager, |
| 211 | armnn::ArgMinMaxFunction::Max, |
| 212 | inputTensorInfo, outputTensorInfo, |
| 213 | inputValues, outputValues, 2); |
| 214 | } |
| 215 | |
| 216 | template<armnn::DataType ArmnnType, typename T> |
| 217 | LayerTestResult<int32_t, 3> ArgMinWidthTest( |
| 218 | armnn::IWorkloadFactory& workloadFactory, |
| 219 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 220 | { |
| 221 | const armnn::TensorShape inputShape{ 1, 3, 2, 4}; |
James Conroy | c8724c7 | 2019-10-08 15:41:34 +0100 | [diff] [blame] | 222 | const armnn::TensorShape outputShape{ 1, 3, 2 }; |
James Conroy | d47a064 | 2019-09-17 14:22:06 +0100 | [diff] [blame] | 223 | |
| 224 | armnn::TensorInfo inputTensorInfo(inputShape, ArmnnType); |
| 225 | armnn::TensorInfo outputTensorInfo(outputShape, armnn::DataType::Signed32); |
| 226 | |
| 227 | std::vector<float> inputValues({ 1.0f, 2.0f, 3.0f, 4.0f, |
| 228 | 5.0f, 6.0f, 7.0f, 8.0f, |
| 229 | |
| 230 | 10.0f, 20.0f, 30.0f, 40.0f, |
| 231 | 50.0f, 60.0f, 70.0f, 80.0f, |
| 232 | |
| 233 | 100.0f, 200.0f, 300.0f, 400.0f, |
| 234 | 500.0f, 600.0f, 700.0f, 800.0f }); |
| 235 | std::vector<int32_t> outputValues({ 0, 0, |
| 236 | 0, 0, |
| 237 | 0, 0 }); |
| 238 | |
| 239 | return ArgMinMaxTestCommon<ArmnnType>(workloadFactory, memoryManager, |
| 240 | armnn::ArgMinMaxFunction::Min, |
| 241 | inputTensorInfo, outputTensorInfo, |
| 242 | inputValues, outputValues, 3); |
| 243 | } |
| 244 | |
Nikhil Raj | 68c2c90 | 2019-09-19 11:21:11 +0100 | [diff] [blame] | 245 | |
| 246 | // Explicit template specializations |
| 247 | |
| 248 | template LayerTestResult<int32_t, 3> |
| 249 | ArgMaxSimpleTest<armnn::DataType::Float32>( |
| 250 | armnn::IWorkloadFactory& workloadFactory, |
| 251 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager); |
| 252 | |
| 253 | template LayerTestResult<int32_t, 3> |
Nikhil Raj | 68c2c90 | 2019-09-19 11:21:11 +0100 | [diff] [blame] | 254 | ArgMaxSimpleTest<armnn::DataType::QuantisedAsymm8>( |
| 255 | armnn::IWorkloadFactory& workloadFactory, |
| 256 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager); |
| 257 | |
| 258 | template LayerTestResult<int32_t, 3> |
Nikhil Raj | 68c2c90 | 2019-09-19 11:21:11 +0100 | [diff] [blame] | 259 | ArgMaxSimpleTest<armnn::DataType::QuantisedSymm16>( |
| 260 | armnn::IWorkloadFactory& workloadFactory, |
| 261 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager); |
| 262 | |
| 263 | template LayerTestResult<int32_t, 3> |
James Conroy | d47a064 | 2019-09-17 14:22:06 +0100 | [diff] [blame] | 264 | ArgMinSimpleTest<armnn::DataType::Float32>( |
| 265 | armnn::IWorkloadFactory& workloadFactory, |
| 266 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager); |
| 267 | |
| 268 | template LayerTestResult<int32_t, 3> |
| 269 | ArgMinSimpleTest<armnn::DataType::QuantisedAsymm8>( |
| 270 | armnn::IWorkloadFactory& workloadFactory, |
| 271 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager); |
| 272 | |
| 273 | template LayerTestResult<int32_t, 3> |
Nikhil Raj | 68c2c90 | 2019-09-19 11:21:11 +0100 | [diff] [blame] | 274 | ArgMinSimpleTest<armnn::DataType::QuantisedSymm16>( |
| 275 | armnn::IWorkloadFactory& workloadFactory, |
| 276 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager); |
| 277 | |
| 278 | template LayerTestResult<int32_t, 3> |
James Conroy | d47a064 | 2019-09-17 14:22:06 +0100 | [diff] [blame] | 279 | ArgMinChannelTest<armnn::DataType::Float32>( |
Nikhil Raj | 68c2c90 | 2019-09-19 11:21:11 +0100 | [diff] [blame] | 280 | armnn::IWorkloadFactory& workloadFactory, |
| 281 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager); |
| 282 | |
| 283 | template LayerTestResult<int32_t, 3> |
James Conroy | d47a064 | 2019-09-17 14:22:06 +0100 | [diff] [blame] | 284 | ArgMinChannelTest<armnn::DataType::QuantisedAsymm8>( |
Nikhil Raj | 68c2c90 | 2019-09-19 11:21:11 +0100 | [diff] [blame] | 285 | armnn::IWorkloadFactory& workloadFactory, |
| 286 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager); |
| 287 | |
| 288 | template LayerTestResult<int32_t, 3> |
James Conroy | d47a064 | 2019-09-17 14:22:06 +0100 | [diff] [blame] | 289 | ArgMinChannelTest<armnn::DataType::QuantisedSymm16>( |
Nikhil Raj | 68c2c90 | 2019-09-19 11:21:11 +0100 | [diff] [blame] | 290 | armnn::IWorkloadFactory& workloadFactory, |
| 291 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager); |
| 292 | |
| 293 | template LayerTestResult<int32_t, 3> |
James Conroy | d47a064 | 2019-09-17 14:22:06 +0100 | [diff] [blame] | 294 | ArgMaxChannelTest<armnn::DataType::Float32>( |
Nikhil Raj | 68c2c90 | 2019-09-19 11:21:11 +0100 | [diff] [blame] | 295 | armnn::IWorkloadFactory& workloadFactory, |
| 296 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager); |
| 297 | |
| 298 | template LayerTestResult<int32_t, 3> |
James Conroy | d47a064 | 2019-09-17 14:22:06 +0100 | [diff] [blame] | 299 | ArgMaxChannelTest<armnn::DataType::QuantisedAsymm8>( |
Nikhil Raj | 68c2c90 | 2019-09-19 11:21:11 +0100 | [diff] [blame] | 300 | armnn::IWorkloadFactory& workloadFactory, |
| 301 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager); |
| 302 | |
| 303 | template LayerTestResult<int32_t, 3> |
James Conroy | d47a064 | 2019-09-17 14:22:06 +0100 | [diff] [blame] | 304 | ArgMaxChannelTest<armnn::DataType::QuantisedSymm16>( |
Nikhil Raj | 68c2c90 | 2019-09-19 11:21:11 +0100 | [diff] [blame] | 305 | armnn::IWorkloadFactory& workloadFactory, |
James Conroy | d47a064 | 2019-09-17 14:22:06 +0100 | [diff] [blame] | 306 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager); |
| 307 | |
| 308 | template LayerTestResult<int32_t, 3> |
| 309 | ArgMaxHeightTest<armnn::DataType::Float32>( |
| 310 | armnn::IWorkloadFactory& workloadFactory, |
| 311 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager); |
| 312 | |
| 313 | template LayerTestResult<int32_t, 3> |
| 314 | ArgMinWidthTest<armnn::DataType::Float32>( |
| 315 | armnn::IWorkloadFactory& workloadFactory, |
| 316 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager); |