Aron Virginas-Tar | 92b9f87 | 2019-09-17 17:27:04 +0100 | [diff] [blame] | 1 | // |
Teresa Charlin | fbf0e5b | 2020-08-17 01:01:06 +0100 | [diff] [blame] | 2 | // Copyright © 2019 Arm Ltd and Contributors. All rights reserved. |
Aron Virginas-Tar | 92b9f87 | 2019-09-17 17:27:04 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "SliceTestImpl.hpp" |
| 7 | |
Aron Virginas-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 8 | #include <QuantizeHelper.hpp> |
Aron Virginas-Tar | 92b9f87 | 2019-09-17 17:27:04 +0100 | [diff] [blame] | 9 | #include <ResolveType.hpp> |
| 10 | |
Aron Virginas-Tar | 92b9f87 | 2019-09-17 17:27:04 +0100 | [diff] [blame] | 11 | |
| 12 | #include <backendsCommon/test/TensorCopyUtils.hpp> |
| 13 | #include <backendsCommon/test/WorkloadTestUtils.hpp> |
| 14 | |
| 15 | #include <test/TensorHelpers.hpp> |
| 16 | |
| 17 | namespace |
| 18 | { |
| 19 | |
| 20 | template<typename T, std::size_t NumDims> |
| 21 | LayerTestResult<T, NumDims> SliceTestImpl( |
| 22 | armnn::IWorkloadFactory& workloadFactory, |
| 23 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 24 | armnn::TensorInfo& inputInfo, |
| 25 | armnn::TensorInfo& outputInfo, |
| 26 | std::vector<float>& inputData, |
| 27 | std::vector<float>& expectedOutputData, |
| 28 | armnn::SliceQueueDescriptor descriptor, |
| 29 | const float qScale = 1.0f, |
| 30 | const int qOffset = 0) |
| 31 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 32 | IgnoreUnused(memoryManager); |
Aron Virginas-Tar | 92b9f87 | 2019-09-17 17:27:04 +0100 | [diff] [blame] | 33 | if(armnn::IsQuantizedType<T>()) |
| 34 | { |
| 35 | inputInfo.SetQuantizationScale(qScale); |
| 36 | inputInfo.SetQuantizationOffset(qOffset); |
| 37 | |
| 38 | outputInfo.SetQuantizationScale(qScale); |
| 39 | outputInfo.SetQuantizationOffset(qOffset); |
| 40 | } |
| 41 | |
| 42 | boost::multi_array<T, NumDims> input = |
Aron Virginas-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 43 | MakeTensor<T, NumDims>(inputInfo, armnnUtils::QuantizedVector<T>(inputData, qScale, qOffset)); |
Aron Virginas-Tar | 92b9f87 | 2019-09-17 17:27:04 +0100 | [diff] [blame] | 44 | |
| 45 | LayerTestResult<T, NumDims> result(outputInfo); |
| 46 | result.outputExpected = |
Aron Virginas-Tar | 48623a0 | 2019-10-22 10:00:28 +0100 | [diff] [blame] | 47 | MakeTensor<T, NumDims>(outputInfo, armnnUtils::QuantizedVector<T>(expectedOutputData, qScale, qOffset)); |
Aron Virginas-Tar | 92b9f87 | 2019-09-17 17:27:04 +0100 | [diff] [blame] | 48 | |
Teresa Charlin | fbf0e5b | 2020-08-17 01:01:06 +0100 | [diff] [blame] | 49 | ARMNN_NO_DEPRECATE_WARN_BEGIN |
Aron Virginas-Tar | 92b9f87 | 2019-09-17 17:27:04 +0100 | [diff] [blame] | 50 | std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputInfo); |
| 51 | std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputInfo); |
Teresa Charlin | fbf0e5b | 2020-08-17 01:01:06 +0100 | [diff] [blame] | 52 | ARMNN_NO_DEPRECATE_WARN_END |
Aron Virginas-Tar | 92b9f87 | 2019-09-17 17:27:04 +0100 | [diff] [blame] | 53 | |
| 54 | armnn::WorkloadInfo info; |
| 55 | AddInputToWorkload(descriptor, info, inputInfo, inputHandle.get()); |
| 56 | AddOutputToWorkload(descriptor, info, outputInfo, outputHandle.get()); |
| 57 | |
| 58 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateSlice(descriptor, info); |
| 59 | |
| 60 | inputHandle->Allocate(); |
| 61 | outputHandle->Allocate(); |
| 62 | |
| 63 | CopyDataToITensorHandle(inputHandle.get(), input.data()); |
| 64 | |
| 65 | ExecuteWorkload(*workload, memoryManager); |
| 66 | |
| 67 | CopyDataFromITensorHandle(result.output.data(), outputHandle.get()); |
| 68 | |
| 69 | return result; |
| 70 | } |
| 71 | |
| 72 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 73 | LayerTestResult<T, 4> Slice4dTest(armnn::IWorkloadFactory& workloadFactory, |
| 74 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 75 | { |
| 76 | armnn::TensorShape inputShape = { 3, 2, 3, 5 }; |
| 77 | armnn::TensorShape outputShape = { 2, 1, 2, 3 }; |
| 78 | |
| 79 | armnn::SliceQueueDescriptor desc; |
| 80 | desc.m_Parameters.m_Begin = { 1, 0, 1, 2 }; |
| 81 | desc.m_Parameters.m_Size = { 2, 1, 2, 3 }; |
| 82 | |
| 83 | armnn::TensorInfo inputInfo(inputShape, ArmnnType); |
| 84 | armnn::TensorInfo outputInfo(outputShape, ArmnnType); |
| 85 | |
| 86 | std::vector<float> input = |
| 87 | { |
| 88 | 0.f, 1.f, 2.f, 3.f, 4.f, |
| 89 | 5.f, 6.f, 7.f, 8.f, 9.f, |
| 90 | 10.f, 11.f, 12.f, 13.f, 14.f, |
| 91 | |
| 92 | 15.f, 16.f, 17.f, 18.f, 19.f, |
| 93 | 20.f, 21.f, 22.f, 23.f, 24.f, |
| 94 | 25.f, 26.f, 27.f, 28.f, 29.f, |
| 95 | |
| 96 | |
| 97 | 30.f, 31.f, 32.f, 33.f, 34.f, |
| 98 | 35.f, 36.f, 37.f, 38.f, 39.f, |
| 99 | 40.f, 41.f, 42.f, 43.f, 44.f, |
| 100 | |
| 101 | 45.f, 46.f, 47.f, 48.f, 49.f, |
| 102 | 50.f, 51.f, 52.f, 53.f, 54.f, |
| 103 | 55.f, 56.f, 57.f, 58.f, 59.f, |
| 104 | |
| 105 | |
| 106 | 60.f, 61.f, 62.f, 63.f, 64.f, |
| 107 | 65.f, 66.f, 67.f, 68.f, 69.f, |
| 108 | 70.f, 71.f, 72.f, 73.f, 74.f, |
| 109 | |
| 110 | 75.f, 76.f, 77.f, 78.f, 79.f, |
| 111 | 80.f, 81.f, 82.f, 83.f, 84.f, |
| 112 | 85.f, 86.f, 87.f, 88.f, 89.f |
| 113 | }; |
| 114 | |
| 115 | std::vector<float> expectedOutput = |
| 116 | { |
| 117 | 37.f, 38.f, 39.f, |
| 118 | 42.f, 43.f, 44.f, |
| 119 | |
| 120 | |
| 121 | 67.f, 68.f, 69.f, |
| 122 | 72.f, 73.f, 74.f |
| 123 | }; |
| 124 | |
| 125 | return SliceTestImpl<T, 4>(workloadFactory, memoryManager, inputInfo, outputInfo, input, expectedOutput, desc); |
| 126 | } |
| 127 | |
| 128 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 129 | LayerTestResult<T, 3> Slice3dTest(armnn::IWorkloadFactory& workloadFactory, |
| 130 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 131 | { |
| 132 | armnn::TensorShape inputShape = { 2, 3, 5 }; |
| 133 | armnn::TensorShape outputShape = { 1, 2, 3 }; |
| 134 | |
| 135 | armnn::SliceQueueDescriptor desc; |
| 136 | desc.m_Parameters.m_Begin = { 0, 1, 2 }; |
| 137 | desc.m_Parameters.m_Size = { 1, 2, 3 }; |
| 138 | |
| 139 | armnn::TensorInfo inputInfo(inputShape, ArmnnType); |
| 140 | armnn::TensorInfo outputInfo(outputShape, ArmnnType); |
| 141 | |
| 142 | std::vector<float> input = |
| 143 | { |
| 144 | 0.f, 1.f, 2.f, 3.f, 4.f, |
| 145 | 5.f, 6.f, 7.f, 8.f, 9.f, |
| 146 | 10.f, 11.f, 12.f, 13.f, 14.f, |
| 147 | |
| 148 | 15.f, 16.f, 17.f, 18.f, 19.f, |
| 149 | 20.f, 21.f, 22.f, 23.f, 24.f, |
| 150 | 25.f, 26.f, 27.f, 28.f, 29.f, |
| 151 | }; |
| 152 | |
| 153 | std::vector<float> expectedOutput = |
| 154 | { |
| 155 | 7.f, 8.f, 9.f, |
| 156 | 12.f, 13.f, 14.f |
| 157 | }; |
| 158 | |
| 159 | return SliceTestImpl<T, 3>(workloadFactory, memoryManager, inputInfo, outputInfo, input, expectedOutput, desc); |
| 160 | } |
| 161 | |
| 162 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 163 | LayerTestResult<T, 2> Slice2dTest(armnn::IWorkloadFactory& workloadFactory, |
| 164 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 165 | { |
| 166 | armnn::TensorShape inputShape = { 3, 5 }; |
| 167 | armnn::TensorShape outputShape = { 2, 3 }; |
| 168 | |
| 169 | armnn::SliceQueueDescriptor desc; |
| 170 | desc.m_Parameters.m_Begin = { 1, 2 }; |
| 171 | desc.m_Parameters.m_Size = { 2, 3 }; |
| 172 | |
| 173 | armnn::TensorInfo inputInfo(inputShape, ArmnnType); |
| 174 | armnn::TensorInfo outputInfo(outputShape, ArmnnType); |
| 175 | |
| 176 | std::vector<float> input = |
| 177 | { |
| 178 | 0.f, 1.f, 2.f, 3.f, 4.f, |
| 179 | 5.f, 6.f, 7.f, 8.f, 9.f, |
| 180 | 10.f, 11.f, 12.f, 13.f, 14.f |
| 181 | }; |
| 182 | |
| 183 | std::vector<float> expectedOutput = |
| 184 | { |
| 185 | 7.f, 8.f, 9.f, |
| 186 | 12.f, 13.f, 14.f |
| 187 | }; |
| 188 | |
| 189 | return SliceTestImpl<T, 2>(workloadFactory, memoryManager, inputInfo, outputInfo, input, expectedOutput, desc); |
| 190 | } |
| 191 | |
| 192 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 193 | LayerTestResult<T, 1> Slice1dTest(armnn::IWorkloadFactory& workloadFactory, |
| 194 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 195 | { |
| 196 | armnn::TensorShape inputShape = { 5 }; |
| 197 | armnn::TensorShape outputShape = { 3 }; |
| 198 | |
| 199 | armnn::SliceQueueDescriptor desc; |
| 200 | desc.m_Parameters.m_Begin = { 2 }; |
| 201 | desc.m_Parameters.m_Size = { 3 }; |
| 202 | |
| 203 | armnn::TensorInfo inputInfo(inputShape, ArmnnType); |
| 204 | armnn::TensorInfo outputInfo(outputShape, ArmnnType); |
| 205 | |
| 206 | std::vector<float> input = |
| 207 | { |
| 208 | 0.f, 1.f, 2.f, 3.f, 4.f |
| 209 | }; |
| 210 | |
| 211 | std::vector<float> expectedOutput = |
| 212 | { |
| 213 | 2.f, 3.f, 4.f |
| 214 | }; |
| 215 | |
| 216 | return SliceTestImpl<T, 1>(workloadFactory, memoryManager, inputInfo, outputInfo, input, expectedOutput, desc); |
| 217 | } |
| 218 | |
| 219 | } // anonymous namespace |
| 220 | |
| 221 | // Float32 tests |
| 222 | LayerTestResult<float, 4> Slice4dFloat32Test(armnn::IWorkloadFactory& workloadFactory, |
| 223 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 224 | { |
| 225 | return Slice4dTest<armnn::DataType::Float32>(workloadFactory, memoryManager); |
| 226 | } |
| 227 | |
| 228 | LayerTestResult<float, 3> Slice3dFloat32Test(armnn::IWorkloadFactory& workloadFactory, |
| 229 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 230 | { |
| 231 | return Slice3dTest<armnn::DataType::Float32>(workloadFactory, memoryManager); |
| 232 | } |
| 233 | |
| 234 | LayerTestResult<float, 2> Slice2dFloat32Test(armnn::IWorkloadFactory& workloadFactory, |
| 235 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 236 | { |
| 237 | return Slice2dTest<armnn::DataType::Float32>(workloadFactory, memoryManager); |
| 238 | } |
| 239 | |
| 240 | LayerTestResult<float, 1> Slice1dFloat32Test(armnn::IWorkloadFactory& workloadFactory, |
| 241 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 242 | { |
| 243 | return Slice1dTest<armnn::DataType::Float32>(workloadFactory, memoryManager); |
| 244 | } |
| 245 | |
| 246 | // Uint8 tests |
| 247 | LayerTestResult<uint8_t, 4> Slice4dUint8Test(armnn::IWorkloadFactory& workloadFactory, |
| 248 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 249 | { |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 250 | return Slice4dTest<armnn::DataType::QAsymmU8>(workloadFactory, memoryManager); |
Aron Virginas-Tar | 92b9f87 | 2019-09-17 17:27:04 +0100 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | LayerTestResult<uint8_t, 3> Slice3dUint8Test(armnn::IWorkloadFactory& workloadFactory, |
| 254 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 255 | { |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 256 | return Slice3dTest<armnn::DataType::QAsymmU8>(workloadFactory, memoryManager); |
Aron Virginas-Tar | 92b9f87 | 2019-09-17 17:27:04 +0100 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | LayerTestResult<uint8_t, 2> Slice2dUint8Test(armnn::IWorkloadFactory& workloadFactory, |
| 260 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 261 | { |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 262 | return Slice2dTest<armnn::DataType::QAsymmU8>(workloadFactory, memoryManager); |
Aron Virginas-Tar | 92b9f87 | 2019-09-17 17:27:04 +0100 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | LayerTestResult<uint8_t, 1> Slice1dUint8Test(armnn::IWorkloadFactory& workloadFactory, |
| 266 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 267 | { |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 268 | return Slice1dTest<armnn::DataType::QAsymmU8>(workloadFactory, memoryManager); |
Aron Virginas-Tar | 92b9f87 | 2019-09-17 17:27:04 +0100 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | // Int16 tests |
| 272 | LayerTestResult<int16_t, 4> Slice4dInt16Test(armnn::IWorkloadFactory& workloadFactory, |
| 273 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 274 | { |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 275 | return Slice4dTest<armnn::DataType::QSymmS16>(workloadFactory, memoryManager); |
Aron Virginas-Tar | 92b9f87 | 2019-09-17 17:27:04 +0100 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | LayerTestResult<int16_t, 3> Slice3dInt16Test(armnn::IWorkloadFactory& workloadFactory, |
| 279 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 280 | { |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 281 | return Slice3dTest<armnn::DataType::QSymmS16>(workloadFactory, memoryManager); |
Aron Virginas-Tar | 92b9f87 | 2019-09-17 17:27:04 +0100 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | LayerTestResult<int16_t, 2> Slice2dInt16Test(armnn::IWorkloadFactory& workloadFactory, |
| 285 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 286 | { |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 287 | return Slice2dTest<armnn::DataType::QSymmS16>(workloadFactory, memoryManager); |
Aron Virginas-Tar | 92b9f87 | 2019-09-17 17:27:04 +0100 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | LayerTestResult<int16_t, 1> Slice1dInt16Test(armnn::IWorkloadFactory& workloadFactory, |
| 291 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 292 | { |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 293 | return Slice1dTest<armnn::DataType::QSymmS16>(workloadFactory, memoryManager); |
Aron Virginas-Tar | 92b9f87 | 2019-09-17 17:27:04 +0100 | [diff] [blame] | 294 | } |