Keith Davis | 3ae3f97 | 2021-05-21 16:33:48 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2021 Arm Ltd and Contributors. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "ShapeTestImpl.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 | template<typename T, std::size_t n> |
| 15 | LayerTestResult<int32_t, 1> ShapeTest( |
| 16 | armnn::TensorInfo inputTensorInfo, |
| 17 | std::vector<T> input, |
| 18 | armnn::TensorInfo outputTensorInfo, |
| 19 | std::vector<int32_t> expectedOutputData, |
| 20 | armnn::IWorkloadFactory& workloadFactory, |
| 21 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 22 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
| 23 | { |
| 24 | IgnoreUnused(memoryManager); |
| 25 | |
| 26 | std::vector<int32_t> actualOutput(outputTensorInfo.GetNumElements()); |
| 27 | |
| 28 | std::unique_ptr<armnn::ITensorHandle> inputHandle = tensorHandleFactory.CreateTensorHandle(inputTensorInfo); |
| 29 | std::unique_ptr<armnn::ITensorHandle> outputHandle = tensorHandleFactory.CreateTensorHandle(outputTensorInfo); |
| 30 | |
| 31 | armnn::ShapeQueueDescriptor data; |
| 32 | armnn::WorkloadInfo info; |
| 33 | AddInputToWorkload(data, info, inputTensorInfo, inputHandle.get()); |
| 34 | AddOutputToWorkload(data, info, outputTensorInfo, outputHandle.get()); |
| 35 | |
| 36 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateShape(data, info); |
| 37 | |
| 38 | inputHandle->Allocate(); |
| 39 | outputHandle->Allocate(); |
| 40 | |
| 41 | CopyDataToITensorHandle(inputHandle.get(), input.data()); |
| 42 | |
| 43 | workload->Execute(); |
| 44 | |
| 45 | CopyDataFromITensorHandle(actualOutput.data(), outputHandle.get()); |
| 46 | |
| 47 | return LayerTestResult<int32_t, 1>(actualOutput, |
| 48 | expectedOutputData, |
| 49 | outputHandle->GetShape(), |
| 50 | outputTensorInfo.GetShape()); |
| 51 | } |
| 52 | |
| 53 | template<armnn::DataType ArmnnType, typename T> |
| 54 | LayerTestResult<int32_t, 1> ShapeDimSize1Test( |
| 55 | armnn::IWorkloadFactory& workloadFactory, |
| 56 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 57 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
| 58 | { |
| 59 | armnn::TensorInfo inputTensorInfo({ 1 }, ArmnnType, 1.0f, 0); |
| 60 | armnn::TensorInfo outputTensorInfo({ 1 }, armnn::DataType::Signed32); |
| 61 | |
| 62 | auto input = ConvertToDataType<ArmnnType>({ 1.0f }, inputTensorInfo); |
| 63 | |
| 64 | return ShapeTest<T, 1>(inputTensorInfo, input, outputTensorInfo, { 1 }, workloadFactory, memoryManager, |
| 65 | tensorHandleFactory); |
| 66 | } |
| 67 | |
| 68 | template<armnn::DataType ArmnnType, typename T> |
| 69 | LayerTestResult<int32_t, 1> ShapeDimSize2Test( |
| 70 | armnn::IWorkloadFactory& workloadFactory, |
| 71 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 72 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
| 73 | { |
| 74 | armnn::TensorInfo inputTensorInfo({ 1, 2 }, ArmnnType, 1.0f, 0); |
| 75 | armnn::TensorInfo outputTensorInfo({ 2 }, armnn::DataType::Signed32); |
| 76 | |
| 77 | auto input = ConvertToDataType<ArmnnType>({ 1.0f, 1.0f }, inputTensorInfo); |
| 78 | |
| 79 | return ShapeTest<T, 2>(inputTensorInfo, input, outputTensorInfo, { 1, 2 }, workloadFactory, memoryManager, |
| 80 | tensorHandleFactory); |
| 81 | } |
| 82 | |
| 83 | template<armnn::DataType ArmnnType, typename T> |
| 84 | LayerTestResult<int32_t, 1> ShapeDimSize3Test( |
| 85 | armnn::IWorkloadFactory& workloadFactory, |
| 86 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 87 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
| 88 | { |
| 89 | armnn::TensorInfo inputTensorInfo({ 1, 2, 3 }, ArmnnType, 1.0f, 0); |
| 90 | armnn::TensorInfo outputTensorInfo({ 3 }, armnn::DataType::Signed32); |
| 91 | |
| 92 | auto input = ConvertToDataType<ArmnnType>({ 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f }, inputTensorInfo); |
| 93 | |
| 94 | return ShapeTest<T, 3>(inputTensorInfo, input, outputTensorInfo, { 1, 2, 3 }, workloadFactory, memoryManager, |
| 95 | tensorHandleFactory); |
| 96 | } |
| 97 | |
| 98 | template<armnn::DataType ArmnnType, typename T> |
| 99 | LayerTestResult<int32_t, 1> ShapeDimSize4Test( |
| 100 | armnn::IWorkloadFactory& workloadFactory, |
| 101 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 102 | const armnn::ITensorHandleFactory& tensorHandleFactory) |
| 103 | { |
| 104 | armnn::TensorInfo inputTensorInfo({ 2, 2, 3, 2 }, ArmnnType, 1.0f, 0); |
| 105 | armnn::TensorInfo outputTensorInfo({ 4 }, armnn::DataType::Signed32); |
| 106 | |
| 107 | auto input = ConvertToDataType<ArmnnType>({ 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, |
| 108 | 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, |
| 109 | 1.0f }, |
| 110 | inputTensorInfo); |
| 111 | |
| 112 | return ShapeTest<T, 4>(inputTensorInfo, input, outputTensorInfo, { 2, 2, 3, 2 }, workloadFactory, memoryManager, |
| 113 | tensorHandleFactory); |
| 114 | } |
| 115 | |
| 116 | template LayerTestResult<int32_t, 1> |
| 117 | ShapeDimSize4Test<armnn::DataType::Float16>( |
| 118 | armnn::IWorkloadFactory& workloadFactory, |
| 119 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 120 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
| 121 | |
| 122 | template LayerTestResult<int32_t, 1> |
| 123 | ShapeDimSize4Test<armnn::DataType::Float32>( |
| 124 | armnn::IWorkloadFactory& workloadFactory, |
| 125 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 126 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
| 127 | |
| 128 | template LayerTestResult<int32_t, 1> |
| 129 | ShapeDimSize4Test<armnn::DataType::QAsymmU8>( |
| 130 | armnn::IWorkloadFactory& workloadFactory, |
| 131 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 132 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
| 133 | |
| 134 | template LayerTestResult<int32_t, 1> |
| 135 | ShapeDimSize4Test<armnn::DataType::Signed32>( |
| 136 | armnn::IWorkloadFactory& workloadFactory, |
| 137 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 138 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
| 139 | |
| 140 | template LayerTestResult<int32_t, 1> |
| 141 | ShapeDimSize4Test<armnn::DataType::QSymmS16>( |
| 142 | armnn::IWorkloadFactory& workloadFactory, |
| 143 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 144 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
| 145 | |
| 146 | template LayerTestResult<int32_t, 1> |
| 147 | ShapeDimSize4Test<armnn::DataType::QSymmS8>( |
| 148 | armnn::IWorkloadFactory& workloadFactory, |
| 149 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 150 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
| 151 | |
| 152 | template LayerTestResult<int32_t, 1> |
| 153 | ShapeDimSize4Test<armnn::DataType::QAsymmS8>( |
| 154 | armnn::IWorkloadFactory& workloadFactory, |
| 155 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 156 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
| 157 | |
| 158 | template LayerTestResult<int32_t, 1> |
| 159 | ShapeDimSize4Test<armnn::DataType::BFloat16>( |
| 160 | armnn::IWorkloadFactory& workloadFactory, |
| 161 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 162 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
| 163 | |
| 164 | template LayerTestResult<int32_t, 1> |
| 165 | ShapeDimSize3Test<armnn::DataType::Float16>( |
| 166 | armnn::IWorkloadFactory& workloadFactory, |
| 167 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 168 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
| 169 | |
| 170 | template LayerTestResult<int32_t, 1> |
| 171 | ShapeDimSize3Test<armnn::DataType::Float32>( |
| 172 | armnn::IWorkloadFactory& workloadFactory, |
| 173 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 174 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
| 175 | |
| 176 | template LayerTestResult<int32_t, 1> |
| 177 | ShapeDimSize3Test<armnn::DataType::QAsymmU8>( |
| 178 | armnn::IWorkloadFactory& workloadFactory, |
| 179 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 180 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
| 181 | |
| 182 | template LayerTestResult<int32_t, 1> |
| 183 | ShapeDimSize3Test<armnn::DataType::Signed32>( |
| 184 | armnn::IWorkloadFactory& workloadFactory, |
| 185 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 186 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
| 187 | |
| 188 | template LayerTestResult<int32_t, 1> |
| 189 | ShapeDimSize3Test<armnn::DataType::QSymmS16>( |
| 190 | armnn::IWorkloadFactory& workloadFactory, |
| 191 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 192 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
| 193 | |
| 194 | template LayerTestResult<int32_t, 1> |
| 195 | ShapeDimSize3Test<armnn::DataType::QSymmS8>( |
| 196 | armnn::IWorkloadFactory& workloadFactory, |
| 197 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 198 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
| 199 | |
| 200 | template LayerTestResult<int32_t, 1> |
| 201 | ShapeDimSize3Test<armnn::DataType::QAsymmS8>( |
| 202 | armnn::IWorkloadFactory& workloadFactory, |
| 203 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 204 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
| 205 | |
| 206 | template LayerTestResult<int32_t, 1> |
| 207 | ShapeDimSize3Test<armnn::DataType::BFloat16>( |
| 208 | armnn::IWorkloadFactory& workloadFactory, |
| 209 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 210 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
| 211 | |
| 212 | template LayerTestResult<int32_t, 1> |
| 213 | ShapeDimSize2Test<armnn::DataType::Float16>( |
| 214 | armnn::IWorkloadFactory& workloadFactory, |
| 215 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 216 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
| 217 | |
| 218 | template LayerTestResult<int32_t, 1> |
| 219 | ShapeDimSize2Test<armnn::DataType::Float32>( |
| 220 | armnn::IWorkloadFactory& workloadFactory, |
| 221 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 222 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
| 223 | |
| 224 | template LayerTestResult<int32_t, 1> |
| 225 | ShapeDimSize2Test<armnn::DataType::QAsymmU8>( |
| 226 | armnn::IWorkloadFactory& workloadFactory, |
| 227 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 228 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
| 229 | |
| 230 | template LayerTestResult<int32_t, 1> |
| 231 | ShapeDimSize2Test<armnn::DataType::Signed32>( |
| 232 | armnn::IWorkloadFactory& workloadFactory, |
| 233 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 234 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
| 235 | |
| 236 | template LayerTestResult<int32_t, 1> |
| 237 | ShapeDimSize2Test<armnn::DataType::QSymmS16>( |
| 238 | armnn::IWorkloadFactory& workloadFactory, |
| 239 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 240 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
| 241 | |
| 242 | template LayerTestResult<int32_t, 1> |
| 243 | ShapeDimSize2Test<armnn::DataType::QSymmS8>( |
| 244 | armnn::IWorkloadFactory& workloadFactory, |
| 245 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 246 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
| 247 | |
| 248 | template LayerTestResult<int32_t, 1> |
| 249 | ShapeDimSize2Test<armnn::DataType::QAsymmS8>( |
| 250 | armnn::IWorkloadFactory& workloadFactory, |
| 251 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 252 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
| 253 | |
| 254 | template LayerTestResult<int32_t, 1> |
| 255 | ShapeDimSize2Test<armnn::DataType::BFloat16>( |
| 256 | armnn::IWorkloadFactory& workloadFactory, |
| 257 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 258 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
| 259 | |
| 260 | template LayerTestResult<int32_t, 1> |
| 261 | ShapeDimSize1Test<armnn::DataType::Float16>( |
| 262 | armnn::IWorkloadFactory& workloadFactory, |
| 263 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 264 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
| 265 | |
| 266 | template LayerTestResult<int32_t, 1> |
| 267 | ShapeDimSize1Test<armnn::DataType::Float32>( |
| 268 | armnn::IWorkloadFactory& workloadFactory, |
| 269 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 270 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
| 271 | |
| 272 | template LayerTestResult<int32_t, 1> |
| 273 | ShapeDimSize1Test<armnn::DataType::QAsymmU8>( |
| 274 | armnn::IWorkloadFactory& workloadFactory, |
| 275 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 276 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
| 277 | |
| 278 | template LayerTestResult<int32_t, 1> |
| 279 | ShapeDimSize1Test<armnn::DataType::Signed32>( |
| 280 | armnn::IWorkloadFactory& workloadFactory, |
| 281 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 282 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
| 283 | |
| 284 | template LayerTestResult<int32_t, 1> |
| 285 | ShapeDimSize1Test<armnn::DataType::QSymmS16>( |
| 286 | armnn::IWorkloadFactory& workloadFactory, |
| 287 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 288 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
| 289 | |
| 290 | template LayerTestResult<int32_t, 1> |
| 291 | ShapeDimSize1Test<armnn::DataType::QSymmS8>( |
| 292 | armnn::IWorkloadFactory& workloadFactory, |
| 293 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 294 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
| 295 | |
| 296 | template LayerTestResult<int32_t, 1> |
| 297 | ShapeDimSize1Test<armnn::DataType::QAsymmS8>( |
| 298 | armnn::IWorkloadFactory& workloadFactory, |
| 299 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 300 | const armnn::ITensorHandleFactory& tensorHandleFactory); |
| 301 | |
| 302 | template LayerTestResult<int32_t, 1> |
| 303 | ShapeDimSize1Test<armnn::DataType::BFloat16>( |
| 304 | armnn::IWorkloadFactory& workloadFactory, |
| 305 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 306 | const armnn::ITensorHandleFactory& tensorHandleFactory); |