Teresa Charlin | 4e3e831 | 2021-08-05 12:34:37 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2021 Arm Ltd and Contributors. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "ReduceProdTestImpl.hpp" |
| 7 | |
| 8 | #include <backendsCommon/test/DataTypeUtils.hpp> |
| 9 | #include <backendsCommon/test/TensorCopyUtils.hpp> |
| 10 | #include <backendsCommon/test/WorkloadTestUtils.hpp> |
| 11 | |
| 12 | #include <test/TensorHelpers.hpp> |
| 13 | |
| 14 | namespace |
| 15 | { |
| 16 | |
| 17 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 18 | LayerTestResult<float, 4> ReduceTestCommon( |
| 19 | armnn::IWorkloadFactory& workloadFactory, |
| 20 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 21 | const armnn::ITensorHandleFactory& tensorHandleFactory, |
| 22 | const armnn::TensorInfo inputTensorInfo, |
| 23 | const armnn::TensorInfo outputTensorInfo, |
| 24 | const std::vector<float>& inputData, |
| 25 | const std::vector<float>& outputData, |
| 26 | const std::vector<int32_t> vAxis, |
| 27 | const armnn::ReduceOperation reduceOperation, |
| 28 | bool keepDims = false) |
| 29 | { |
| 30 | IgnoreUnused(memoryManager); |
| 31 | auto inputTensor = ConvertToDataType<ArmnnType>(inputData, inputTensorInfo); |
| 32 | |
| 33 | std::vector<float> actualOutput(outputTensorInfo.GetNumElements()); |
| 34 | |
| 35 | std::unique_ptr<armnn::ITensorHandle> inputHandle = tensorHandleFactory.CreateTensorHandle(inputTensorInfo); |
| 36 | std::unique_ptr<armnn::ITensorHandle> outputHandle = tensorHandleFactory.CreateTensorHandle(outputTensorInfo); |
| 37 | |
| 38 | armnn::ReduceQueueDescriptor descriptor; |
| 39 | std::vector<uint32_t> updated_idx; |
| 40 | uint32_t resolvedAxis = 0; |
| 41 | for (uint32_t i = 0; i < vAxis.size(); ++i) |
| 42 | { |
| 43 | if (vAxis[i] < 0) |
| 44 | { |
| 45 | resolvedAxis = inputTensorInfo.GetNumDimensions() + static_cast<uint32_t>(vAxis[i]); |
| 46 | } else |
| 47 | { |
| 48 | resolvedAxis = static_cast<uint32_t>(vAxis[i]); |
| 49 | } |
| 50 | |
| 51 | updated_idx.push_back(resolvedAxis); |
| 52 | } |
| 53 | |
| 54 | descriptor.m_Parameters.m_vAxis = updated_idx; |
| 55 | descriptor.m_Parameters.m_ReduceOperation = reduceOperation; |
| 56 | descriptor.m_Parameters.m_KeepDims = keepDims; |
| 57 | armnn::WorkloadInfo info; |
| 58 | |
| 59 | AddInputToWorkload(descriptor, info, inputTensorInfo, inputHandle.get()); |
| 60 | AddOutputToWorkload(descriptor, info, outputTensorInfo, outputHandle.get()); |
| 61 | |
| 62 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateReduce(descriptor, info); |
| 63 | |
| 64 | inputHandle->Allocate(); |
| 65 | outputHandle->Allocate(); |
| 66 | |
| 67 | CopyDataToITensorHandle(inputHandle.get(), inputTensor.data()); |
| 68 | |
| 69 | workload->Execute(); |
| 70 | |
| 71 | CopyDataFromITensorHandle(actualOutput.data(), outputHandle.get()); |
| 72 | |
| 73 | return LayerTestResult<float, 4>(actualOutput, |
| 74 | outputData, |
| 75 | outputHandle->GetShape(), |
| 76 | outputTensorInfo.GetShape()); |
| 77 | } |
| 78 | |
| 79 | } // namespace |
| 80 | |
| 81 | template<armnn::DataType ArmnnType, typename T> |
| 82 | LayerTestResult<float, 4> ReduceProdSimpleTest( |
| 83 | armnn::IWorkloadFactory& workloadFactory, |
| 84 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 85 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
| 86 | { |
| 87 | const armnn::TensorShape inputShape{ 1, 1, 1, 5 }; |
| 88 | const armnn::TensorShape outputShape{ 1, 1, 1, 1 }; |
| 89 | |
| 90 | armnn::TensorInfo inputTensorInfo(inputShape, ArmnnType); |
| 91 | |
| 92 | if (armnn::IsQuantizedType<T>()) |
| 93 | { |
| 94 | inputTensorInfo.SetQuantizationScale(1.0f); |
| 95 | inputTensorInfo.SetQuantizationOffset(0); |
| 96 | } |
| 97 | |
| 98 | armnn::TensorInfo outputTensorInfo(outputShape, armnn::DataType::Float32); |
| 99 | |
| 100 | std::vector<float> inputValues({ 5.0f, 2.0f, 8.0f, 10.0f, 9.0f }); |
| 101 | std::vector<float> outputValues({ 7200.0f }); |
| 102 | |
| 103 | return ReduceTestCommon<ArmnnType>(workloadFactory, |
| 104 | memoryManager, |
| 105 | tensorHandleFactory, |
| 106 | inputTensorInfo, |
| 107 | outputTensorInfo, |
| 108 | inputValues, |
| 109 | outputValues, |
| 110 | { -1 }, |
| 111 | armnn::ReduceOperation::Prod); |
| 112 | } |
| 113 | |
| 114 | template<armnn::DataType ArmnnType, typename T> |
| 115 | LayerTestResult<float, 4> ReduceProdSingleAxisTest1( |
| 116 | armnn::IWorkloadFactory& workloadFactory, |
| 117 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 118 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
| 119 | { |
| 120 | const armnn::TensorShape inputShape{ 1, 3, 2, 4 }; |
| 121 | const armnn::TensorShape outputShape{ 1, 1, 2, 4 }; |
| 122 | |
| 123 | armnn::TensorInfo inputTensorInfo(inputShape, ArmnnType); |
| 124 | |
| 125 | if (armnn::IsQuantizedType<T>()) |
| 126 | { |
| 127 | inputTensorInfo.SetQuantizationScale(1.0f); |
| 128 | inputTensorInfo.SetQuantizationOffset(0); |
| 129 | } |
| 130 | |
| 131 | armnn::TensorInfo outputTensorInfo(outputShape, armnn::DataType::Float32); |
| 132 | |
| 133 | std::vector<float> inputValues({ 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, |
| 134 | 10.0f, 20.0f, 30.0f, 40.0f, 50.0f, 60.0f, 70.0f, 80.0f, |
| 135 | 100.0f, 200.0f, 300.0f, 400.0f, 500.0f, 600.0f, 700.0f, 800.0f |
| 136 | }); |
| 137 | std::vector<float> outputValues({ 1000.0f, 8000.0f, 27000.0f, 64000.0f, 125000.0f, 216000.0f, 343000.0f, 512000.0f |
| 138 | }); |
| 139 | |
| 140 | return ReduceTestCommon<ArmnnType>(workloadFactory, |
| 141 | memoryManager, |
| 142 | tensorHandleFactory, |
| 143 | inputTensorInfo, |
| 144 | outputTensorInfo, |
| 145 | inputValues, |
| 146 | outputValues, |
| 147 | { 1 }, |
| 148 | armnn::ReduceOperation::Prod); |
| 149 | } |
| 150 | |
| 151 | template<armnn::DataType ArmnnType, typename T> |
| 152 | LayerTestResult<float, 4> ReduceProdSingleAxisTest2( |
| 153 | armnn::IWorkloadFactory& workloadFactory, |
| 154 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 155 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
| 156 | { |
| 157 | const armnn::TensorShape inputShape{ 1, 6, 3, 4 }; |
| 158 | const armnn::TensorShape outputShape{ 1, 1, 3, 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::Float32); |
| 169 | |
| 170 | std::vector<float> inputValues( {7, 8, 6, 1, |
| 171 | 1, 1, 8, 7, |
| 172 | 3, 7, 7, 7, |
| 173 | |
| 174 | 6, 8, 4, 7, |
| 175 | 3, 8, 7, 3, |
| 176 | 5, 8, 8, 8, |
| 177 | |
| 178 | |
| 179 | 7, 8, 2, 7, |
| 180 | 3, 8, 5, 6, |
| 181 | 8, 4, 2, 7, |
| 182 | |
| 183 | 1, 6, 7, 2, |
| 184 | 8, 3, 3, 1, |
| 185 | 7, 6, 2, 6, |
| 186 | |
| 187 | |
| 188 | 5, 3, 4, 8, |
| 189 | 7, 8, 2, 4, |
| 190 | 6, 6, 2, 8, |
| 191 | |
| 192 | 2, 2, 7, 2, |
| 193 | 5, 3, 6, 3, |
| 194 | 6, 1, 8, 8}); |
| 195 | std::vector<float> outputValues({ 2940.f, 18432.f, 9408.f, 1568.f, |
| 196 | 2520.f, 4608.f, 10080.f, 1512.f, |
| 197 | 30240.f, 8064.f, 3584.f, 150528.f }); |
| 198 | |
| 199 | return ReduceTestCommon<ArmnnType>(workloadFactory, |
| 200 | memoryManager, |
| 201 | tensorHandleFactory, |
| 202 | inputTensorInfo, |
| 203 | outputTensorInfo, |
| 204 | inputValues, |
| 205 | outputValues, |
| 206 | { 1 }, |
| 207 | armnn::ReduceOperation::Prod); |
| 208 | } |
| 209 | |
| 210 | template<armnn::DataType ArmnnType, typename T> |
| 211 | LayerTestResult<float, 4> ReduceProdSingleAxisTest3( |
| 212 | armnn::IWorkloadFactory& workloadFactory, |
| 213 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 214 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
| 215 | { |
| 216 | const armnn::TensorShape inputShape{ 1, 6, 3, 4 }; |
| 217 | const armnn::TensorShape outputShape{ 1, 6, 3, 1 }; |
| 218 | |
| 219 | armnn::TensorInfo inputTensorInfo(inputShape, ArmnnType); |
| 220 | |
| 221 | if (armnn::IsQuantizedType<T>()) |
| 222 | { |
| 223 | inputTensorInfo.SetQuantizationScale(1.0f); |
| 224 | inputTensorInfo.SetQuantizationOffset(0); |
| 225 | } |
| 226 | |
| 227 | armnn::TensorInfo outputTensorInfo(outputShape, armnn::DataType::Float32); |
| 228 | |
| 229 | std::vector<float> inputValues({ 7, 8, 6, 1, |
| 230 | 1, 1, 8, 7, |
| 231 | 3, 7, 7, 7, |
| 232 | |
| 233 | 6, 8, 4, 7, |
| 234 | 3, 8, 7, 3, |
| 235 | 5, 8, 8, 8, |
| 236 | |
| 237 | |
| 238 | 7, 8, 2, 7, |
| 239 | 3, 8, 5, 6, |
| 240 | 8, 4, 2, 7, |
| 241 | |
| 242 | 1, 6, 7, 2, |
| 243 | 8, 3, 3, 1, |
| 244 | 7, 6, 2, 6, |
| 245 | |
| 246 | |
| 247 | 5, 3, 4, 8, |
| 248 | 7, 8, 2, 4, |
| 249 | 6, 6, 2, 8, |
| 250 | |
| 251 | 2, 2, 7, 2, |
| 252 | 5, 3, 6, 3, |
| 253 | 6, 1, 8, 8 }); |
| 254 | std::vector<float> outputValues({ 336.f, 56.f, 1029.f, |
| 255 | 1344.f, 504.f, 2560.f, |
| 256 | |
| 257 | 784.f, 720.f, 448.f, |
| 258 | 84.f, 72.f, 504.f, |
| 259 | |
| 260 | 480.f, 448.f, 576.f, |
| 261 | 56.f, 270.f, 384.f }); |
| 262 | |
| 263 | return ReduceTestCommon<ArmnnType>(workloadFactory, |
| 264 | memoryManager, |
| 265 | tensorHandleFactory, |
| 266 | inputTensorInfo, |
| 267 | outputTensorInfo, |
| 268 | inputValues, |
| 269 | outputValues, |
| 270 | { 3 }, |
| 271 | armnn::ReduceOperation::Prod, |
| 272 | true); |
| 273 | } |
| 274 | |
| 275 | template<armnn::DataType ArmnnType, typename T> |
| 276 | LayerTestResult<float, 4> ReduceProdMultipleAxisTest( |
| 277 | armnn::IWorkloadFactory& workloadFactory, |
| 278 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 279 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
| 280 | { |
| 281 | const armnn::TensorShape inputShape{ 1, 3, 2, 4 }; |
| 282 | const armnn::TensorShape outputShape{ 1, 1, 1, 4 }; |
| 283 | |
| 284 | armnn::TensorInfo inputTensorInfo(inputShape, ArmnnType); |
| 285 | |
| 286 | if (armnn::IsQuantizedType<T>()) |
| 287 | { |
| 288 | inputTensorInfo.SetQuantizationScale(1.0f); |
| 289 | inputTensorInfo.SetQuantizationOffset(0); |
| 290 | } |
| 291 | |
| 292 | armnn::TensorInfo outputTensorInfo(outputShape, armnn::DataType::Float32); |
| 293 | |
| 294 | std::vector<float> inputValues({ 1.0f, 2.0f, 3.0f, 4.0f, |
| 295 | 5.0f, 6.0f, 7.0f, 8.0f, |
| 296 | |
| 297 | 10.0f, 20.0f, 30.0f, 40.0f, |
| 298 | 50.0f, 60.0f, 70.0f, 80.0f, |
| 299 | |
| 300 | 11.0f, 22.0f, 33.0f, 44.0f, |
| 301 | 55.0f, 66.0f, 77.0f, 88.0f }); |
| 302 | std::vector<float> outputValues({ 1512500.f, 20908800.f, 112058100.f, 396492800.f }); |
| 303 | |
| 304 | return ReduceTestCommon<ArmnnType>(workloadFactory, |
| 305 | memoryManager, |
| 306 | tensorHandleFactory, |
| 307 | inputTensorInfo, |
| 308 | outputTensorInfo, |
| 309 | inputValues, |
| 310 | outputValues, |
| 311 | { 1, 2 }, |
| 312 | armnn::ReduceOperation::Prod); |
| 313 | } |
| 314 | |
| 315 | // Explicit template specializations |
| 316 | |
| 317 | template LayerTestResult<float, 4> |
| 318 | ReduceProdSimpleTest<armnn::DataType::Float32>( |
| 319 | armnn::IWorkloadFactory& workloadFactory, |
| 320 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 321 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
| 322 | |
| 323 | template LayerTestResult<float, 4> |
| 324 | ReduceProdSingleAxisTest1<armnn::DataType::Float32>( |
| 325 | armnn::IWorkloadFactory& workloadFactory, |
| 326 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 327 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
| 328 | |
| 329 | template LayerTestResult<float, 4> |
| 330 | ReduceProdSingleAxisTest2<armnn::DataType::Float32>( |
| 331 | armnn::IWorkloadFactory& workloadFactory, |
| 332 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 333 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
| 334 | |
| 335 | template LayerTestResult<float, 4> |
| 336 | ReduceProdSingleAxisTest3<armnn::DataType::Float32>( |
| 337 | armnn::IWorkloadFactory& workloadFactory, |
| 338 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 339 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
| 340 | |
| 341 | template LayerTestResult<float, 4> |
| 342 | ReduceProdMultipleAxisTest<armnn::DataType::Float32>( |
| 343 | armnn::IWorkloadFactory& workloadFactory, |
| 344 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 345 | const armnn::ITensorHandleFactory& tensorHandleFactory); |