Nattapat Chaimanowong | 1216b58 | 2018-11-23 15:33:41 +0000 | [diff] [blame^] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | #pragma once |
| 6 | |
| 7 | #include "WorkloadTestUtils.hpp" |
| 8 | |
| 9 | #include <armnn/ArmNN.hpp> |
| 10 | #include <armnn/Tensor.hpp> |
| 11 | #include <armnn/TypesUtils.hpp> |
| 12 | |
| 13 | #include <backendsCommon/CpuTensorHandle.hpp> |
| 14 | #include <backendsCommon/IBackendInternal.hpp> |
| 15 | #include <backendsCommon/WorkloadFactory.hpp> |
| 16 | |
| 17 | #include <test/TensorHelpers.hpp> |
| 18 | |
| 19 | namespace |
| 20 | { |
| 21 | |
| 22 | template<typename T, std::size_t InDim, std::size_t OutDim> |
| 23 | LayerTestResult<T, OutDim> StridedSliceTestImpl( |
| 24 | armnn::IWorkloadFactory& workloadFactory, |
| 25 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 26 | armnn::TensorInfo& inputTensorInfo, |
| 27 | armnn::TensorInfo& outputTensorInfo, |
| 28 | std::vector<float>& inputData, |
| 29 | std::vector<float>& outputExpectedData, |
| 30 | armnn::StridedSliceQueueDescriptor descriptor, |
| 31 | const float qScale = 1.0f, |
| 32 | const int32_t qOffset = 0) |
| 33 | { |
| 34 | if(armnn::IsQuantizedType<T>()) |
| 35 | { |
| 36 | inputTensorInfo.SetQuantizationScale(qScale); |
| 37 | inputTensorInfo.SetQuantizationOffset(qOffset); |
| 38 | |
| 39 | outputTensorInfo.SetQuantizationScale(qScale); |
| 40 | outputTensorInfo.SetQuantizationOffset(qOffset); |
| 41 | } |
| 42 | |
| 43 | boost::multi_array<T, InDim> input = |
| 44 | MakeTensor<T, InDim>(inputTensorInfo, QuantizedVector<T>(qScale, qOffset, inputData)); |
| 45 | |
| 46 | LayerTestResult<T, OutDim> ret(outputTensorInfo); |
| 47 | ret.outputExpected = |
| 48 | MakeTensor<T, OutDim>(outputTensorInfo, QuantizedVector<T>(qScale, qOffset, outputExpectedData)); |
| 49 | |
| 50 | std::unique_ptr<armnn::ITensorHandle> inputHandle = |
| 51 | workloadFactory.CreateTensorHandle(inputTensorInfo); |
| 52 | |
| 53 | std::unique_ptr<armnn::ITensorHandle> outputHandle = |
| 54 | workloadFactory.CreateTensorHandle(outputTensorInfo); |
| 55 | |
| 56 | armnn::WorkloadInfo info; |
| 57 | AddInputToWorkload(descriptor, info, inputTensorInfo, inputHandle.get()); |
| 58 | AddOutputToWorkload(descriptor, info, outputTensorInfo, outputHandle.get()); |
| 59 | |
| 60 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateStridedSlice(descriptor, info); |
| 61 | |
| 62 | inputHandle->Allocate(); |
| 63 | outputHandle->Allocate(); |
| 64 | |
| 65 | CopyDataToITensorHandle(inputHandle.get(), input.data()); |
| 66 | |
| 67 | ExecuteWorkload(*workload, memoryManager); |
| 68 | |
| 69 | CopyDataFromITensorHandle(ret.output.data(), outputHandle.get()); |
| 70 | |
| 71 | return ret; |
| 72 | } |
| 73 | |
| 74 | template <typename T> |
| 75 | LayerTestResult<T, 4> StridedSlice4DTest( |
| 76 | armnn::IWorkloadFactory& workloadFactory, |
| 77 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 78 | { |
| 79 | armnn::TensorInfo inputTensorInfo; |
| 80 | armnn::TensorInfo outputTensorInfo; |
| 81 | |
| 82 | unsigned int inputShape[] = {3, 2, 3, 1}; |
| 83 | unsigned int outputShape[] = {1, 2, 3, 1}; |
| 84 | |
| 85 | armnn::StridedSliceQueueDescriptor desc; |
| 86 | desc.m_Parameters.m_Begin = {1, 0, 0, 0}; |
| 87 | desc.m_Parameters.m_End = {2, 2, 3, 1}; |
| 88 | desc.m_Parameters.m_Stride = {1, 1, 1, 1}; |
| 89 | |
| 90 | inputTensorInfo = armnn::TensorInfo(4, inputShape, armnn::GetDataType<T>()); |
| 91 | outputTensorInfo = armnn::TensorInfo(4, outputShape, armnn::GetDataType<T>()); |
| 92 | |
| 93 | std::vector<float> input = std::vector<float>( |
| 94 | { |
| 95 | 1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f, |
| 96 | |
| 97 | 3.0f, 3.0f, 3.0f, 4.0f, 4.0f, 4.0f, |
| 98 | |
| 99 | 5.0f, 5.0f, 5.0f, 6.0f, 6.0f, 6.0f |
| 100 | }); |
| 101 | |
| 102 | std::vector<float> outputExpected = std::vector<float>( |
| 103 | { |
| 104 | 3.0f, 3.0f, 3.0f, 4.0f, 4.0f, 4.0f |
| 105 | }); |
| 106 | |
| 107 | return StridedSliceTestImpl<T, 4, 4>( |
| 108 | workloadFactory, memoryManager, inputTensorInfo, outputTensorInfo, input, outputExpected, desc); |
| 109 | } |
| 110 | |
| 111 | template <typename T> |
| 112 | LayerTestResult<T, 4> StridedSlice4DReverseTest( |
| 113 | armnn::IWorkloadFactory& workloadFactory, |
| 114 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 115 | { |
| 116 | armnn::TensorInfo inputTensorInfo; |
| 117 | armnn::TensorInfo outputTensorInfo; |
| 118 | |
| 119 | unsigned int inputShape[] = {3, 2, 3, 1}; |
| 120 | unsigned int outputShape[] = {1, 2, 3, 1}; |
| 121 | |
| 122 | armnn::StridedSliceQueueDescriptor desc; |
| 123 | desc.m_Parameters.m_Begin = {1, -1, 0, 0}; |
| 124 | desc.m_Parameters.m_End = {2, -3, 3, 1}; |
| 125 | desc.m_Parameters.m_Stride = {1, -1, 1, 1}; |
| 126 | |
| 127 | inputTensorInfo = armnn::TensorInfo(4, inputShape, armnn::GetDataType<T>()); |
| 128 | outputTensorInfo = armnn::TensorInfo(4, outputShape, armnn::GetDataType<T>()); |
| 129 | |
| 130 | std::vector<float> input = std::vector<float>( |
| 131 | { |
| 132 | 1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f, |
| 133 | |
| 134 | 3.0f, 3.0f, 3.0f, 4.0f, 4.0f, 4.0f, |
| 135 | |
| 136 | 5.0f, 5.0f, 5.0f, 6.0f, 6.0f, 6.0f |
| 137 | }); |
| 138 | |
| 139 | std::vector<float> outputExpected = std::vector<float>( |
| 140 | { |
| 141 | 4.0f, 4.0f, 4.0f, 3.0f, 3.0f, 3.0f |
| 142 | }); |
| 143 | |
| 144 | return StridedSliceTestImpl<T, 4, 4>( |
| 145 | workloadFactory, memoryManager, inputTensorInfo, outputTensorInfo, input, outputExpected, desc); |
| 146 | } |
| 147 | |
| 148 | template <typename T> |
| 149 | LayerTestResult<T, 4> StridedSliceSimpleStrideTest( |
| 150 | armnn::IWorkloadFactory& workloadFactory, |
| 151 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 152 | { |
| 153 | armnn::TensorInfo inputTensorInfo; |
| 154 | armnn::TensorInfo outputTensorInfo; |
| 155 | |
| 156 | unsigned int inputShape[] = {3, 2, 3, 1}; |
| 157 | unsigned int outputShape[] = {2, 1, 2, 1}; |
| 158 | |
| 159 | armnn::StridedSliceQueueDescriptor desc; |
| 160 | desc.m_Parameters.m_Begin = {0, 0, 0, 0}; |
| 161 | desc.m_Parameters.m_End = {3, 2, 3, 1}; |
| 162 | desc.m_Parameters.m_Stride = {2, 2, 2, 1}; |
| 163 | |
| 164 | inputTensorInfo = armnn::TensorInfo(4, inputShape, armnn::GetDataType<T>()); |
| 165 | outputTensorInfo = armnn::TensorInfo(4, outputShape, armnn::GetDataType<T>()); |
| 166 | |
| 167 | std::vector<float> input = std::vector<float>( |
| 168 | { |
| 169 | 1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f, |
| 170 | |
| 171 | 3.0f, 3.0f, 3.0f, 4.0f, 4.0f, 4.0f, |
| 172 | |
| 173 | 5.0f, 5.0f, 5.0f, 6.0f, 6.0f, 6.0f |
| 174 | }); |
| 175 | |
| 176 | std::vector<float> outputExpected = std::vector<float>( |
| 177 | { |
| 178 | 1.0f, 1.0f, |
| 179 | |
| 180 | 5.0f, 5.0f |
| 181 | }); |
| 182 | |
| 183 | return StridedSliceTestImpl<T, 4, 4>( |
| 184 | workloadFactory, memoryManager, inputTensorInfo, outputTensorInfo, input, outputExpected, desc); |
| 185 | } |
| 186 | |
| 187 | template <typename T> |
| 188 | LayerTestResult<T, 4> StridedSliceSimpleRangeMaskTest( |
| 189 | armnn::IWorkloadFactory& workloadFactory, |
| 190 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 191 | { |
| 192 | armnn::TensorInfo inputTensorInfo; |
| 193 | armnn::TensorInfo outputTensorInfo; |
| 194 | |
| 195 | unsigned int inputShape[] = {3, 2, 3, 1}; |
| 196 | unsigned int outputShape[] = {3, 2, 3, 1}; |
| 197 | |
| 198 | armnn::StridedSliceQueueDescriptor desc; |
| 199 | desc.m_Parameters.m_Begin = {1, 1, 1, 1}; |
| 200 | desc.m_Parameters.m_End = {1, 1, 1, 1}; |
| 201 | desc.m_Parameters.m_Stride = {1, 1, 1, 1}; |
| 202 | desc.m_Parameters.m_BeginMask = (1 << 4) - 1; |
| 203 | desc.m_Parameters.m_EndMask = (1 << 4) - 1; |
| 204 | |
| 205 | inputTensorInfo = armnn::TensorInfo(4, inputShape, armnn::GetDataType<T>()); |
| 206 | outputTensorInfo = armnn::TensorInfo(4, outputShape, armnn::GetDataType<T>()); |
| 207 | |
| 208 | std::vector<float> input = std::vector<float>( |
| 209 | { |
| 210 | 1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f, |
| 211 | |
| 212 | 3.0f, 3.0f, 3.0f, 4.0f, 4.0f, 4.0f, |
| 213 | |
| 214 | 5.0f, 5.0f, 5.0f, 6.0f, 6.0f, 6.0f |
| 215 | }); |
| 216 | |
| 217 | std::vector<float> outputExpected = std::vector<float>( |
| 218 | { |
| 219 | 1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f, |
| 220 | |
| 221 | 3.0f, 3.0f, 3.0f, 4.0f, 4.0f, 4.0f, |
| 222 | |
| 223 | 5.0f, 5.0f, 5.0f, 6.0f, 6.0f, 6.0f |
| 224 | }); |
| 225 | |
| 226 | return StridedSliceTestImpl<T, 4, 4>( |
| 227 | workloadFactory, memoryManager, inputTensorInfo, outputTensorInfo, input, outputExpected, desc); |
| 228 | } |
| 229 | |
| 230 | template <typename T> |
| 231 | LayerTestResult<T, 2> StridedSliceShrinkAxisMaskTest( |
| 232 | armnn::IWorkloadFactory& workloadFactory, |
| 233 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 234 | { |
| 235 | armnn::TensorInfo inputTensorInfo; |
| 236 | armnn::TensorInfo outputTensorInfo; |
| 237 | |
| 238 | unsigned int inputShape[] = {3, 2, 3, 1}; |
| 239 | unsigned int outputShape[] = {3, 1}; |
| 240 | |
| 241 | armnn::StridedSliceQueueDescriptor desc; |
| 242 | desc.m_Parameters.m_Begin = {0, 0, 1, 0}; |
| 243 | desc.m_Parameters.m_End = {1, 1, 1, 1}; |
| 244 | desc.m_Parameters.m_Stride = {1, 1, 1, 1}; |
| 245 | desc.m_Parameters.m_EndMask = (1 << 4) - 1; |
| 246 | desc.m_Parameters.m_ShrinkAxisMask = (1 << 1) | (1 << 2); |
| 247 | |
| 248 | inputTensorInfo = armnn::TensorInfo(4, inputShape, armnn::GetDataType<T>()); |
| 249 | outputTensorInfo = armnn::TensorInfo(2, outputShape, armnn::GetDataType<T>()); |
| 250 | |
| 251 | std::vector<float> input = std::vector<float>( |
| 252 | { |
| 253 | 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, |
| 254 | |
| 255 | 7.0f, 8.0f, 9.0f, 10.0f, 11.0f, 12.0f, |
| 256 | |
| 257 | 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f |
| 258 | }); |
| 259 | |
| 260 | std::vector<float> outputExpected = std::vector<float>( |
| 261 | { |
| 262 | 2.0f, 8.0f, 14.0f |
| 263 | }); |
| 264 | |
| 265 | return StridedSliceTestImpl<T, 4, 2>( |
| 266 | workloadFactory, memoryManager, inputTensorInfo, outputTensorInfo, input, outputExpected, desc); |
| 267 | } |
| 268 | |
| 269 | template <typename T> |
| 270 | LayerTestResult<T, 3> StridedSlice3DTest( |
| 271 | armnn::IWorkloadFactory& workloadFactory, |
| 272 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 273 | { |
| 274 | armnn::TensorInfo inputTensorInfo; |
| 275 | armnn::TensorInfo outputTensorInfo; |
| 276 | |
| 277 | unsigned int inputShape[] = {3, 3, 3}; |
| 278 | unsigned int outputShape[] = {2, 2, 2}; |
| 279 | |
| 280 | armnn::StridedSliceQueueDescriptor desc; |
| 281 | desc.m_Parameters.m_Begin = {0, 0, 0}; |
| 282 | desc.m_Parameters.m_End = {1, 1, 1}; |
| 283 | desc.m_Parameters.m_Stride = {2, 2, 2}; |
| 284 | desc.m_Parameters.m_EndMask = (1 << 3) - 1; |
| 285 | |
| 286 | inputTensorInfo = armnn::TensorInfo(3, inputShape, armnn::GetDataType<T>()); |
| 287 | outputTensorInfo = armnn::TensorInfo(3, outputShape, armnn::GetDataType<T>()); |
| 288 | |
| 289 | std::vector<float> input = std::vector<float>( |
| 290 | { |
| 291 | 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, |
| 292 | |
| 293 | 10.0f, 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f, |
| 294 | |
| 295 | 19.0f, 20.0f, 21.0f, 22.0f, 23.0f, 24.0f, 25.0f, 26.0f, 27.0f |
| 296 | }); |
| 297 | |
| 298 | std::vector<float> outputExpected = std::vector<float>( |
| 299 | { |
| 300 | 1.0f, 3.0f, 7.0f, 9.0f, |
| 301 | |
| 302 | 19.0f, 21.0f, 25.0f, 27.0f |
| 303 | }); |
| 304 | |
| 305 | return StridedSliceTestImpl<T, 3, 3>( |
| 306 | workloadFactory, memoryManager, inputTensorInfo, outputTensorInfo, input, outputExpected, desc); |
| 307 | } |
| 308 | |
| 309 | template <typename T> |
| 310 | LayerTestResult<T, 3> StridedSlice3DReverseTest( |
| 311 | armnn::IWorkloadFactory& workloadFactory, |
| 312 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 313 | { |
| 314 | armnn::TensorInfo inputTensorInfo; |
| 315 | armnn::TensorInfo outputTensorInfo; |
| 316 | |
| 317 | unsigned int inputShape[] = {3, 3, 3}; |
| 318 | unsigned int outputShape[] = {2, 2, 2}; |
| 319 | |
| 320 | armnn::StridedSliceQueueDescriptor desc; |
| 321 | desc.m_Parameters.m_Begin = {-1, -1, -1}; |
| 322 | desc.m_Parameters.m_End = {-4, -4, -4}; |
| 323 | desc.m_Parameters.m_Stride = {-2, -2, -2}; |
| 324 | |
| 325 | inputTensorInfo = armnn::TensorInfo(3, inputShape, armnn::GetDataType<T>()); |
| 326 | outputTensorInfo = armnn::TensorInfo(3, outputShape, armnn::GetDataType<T>()); |
| 327 | |
| 328 | std::vector<float> input = std::vector<float>( |
| 329 | { |
| 330 | 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, |
| 331 | |
| 332 | 10.0f, 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f, |
| 333 | |
| 334 | 19.0f, 20.0f, 21.0f, 22.0f, 23.0f, 24.0f, 25.0f, 26.0f, 27.0f |
| 335 | }); |
| 336 | |
| 337 | std::vector<float> outputExpected = std::vector<float>( |
| 338 | { |
| 339 | 27.0f, 25.0f, 21.0f, 19.0f, |
| 340 | |
| 341 | 9.0f, 7.0f, 3.0f, 1.0f |
| 342 | }); |
| 343 | |
| 344 | return StridedSliceTestImpl<T, 3, 3>( |
| 345 | workloadFactory, memoryManager, inputTensorInfo, outputTensorInfo, input, outputExpected, desc); |
| 346 | } |
| 347 | |
| 348 | template <typename T> |
| 349 | LayerTestResult<T, 2> StridedSlice2DTest( |
| 350 | armnn::IWorkloadFactory& workloadFactory, |
| 351 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 352 | { |
| 353 | armnn::TensorInfo inputTensorInfo; |
| 354 | armnn::TensorInfo outputTensorInfo; |
| 355 | |
| 356 | unsigned int inputShape[] = {3, 3}; |
| 357 | unsigned int outputShape[] = {2, 2}; |
| 358 | |
| 359 | armnn::StridedSliceQueueDescriptor desc; |
| 360 | desc.m_Parameters.m_Begin = {0, 0}; |
| 361 | desc.m_Parameters.m_End = {1, 1}; |
| 362 | desc.m_Parameters.m_Stride = {2, 2}; |
| 363 | desc.m_Parameters.m_EndMask = (1 << 2) - 1; |
| 364 | |
| 365 | inputTensorInfo = armnn::TensorInfo(2, inputShape, armnn::GetDataType<T>()); |
| 366 | outputTensorInfo = armnn::TensorInfo(2, outputShape, armnn::GetDataType<T>()); |
| 367 | |
| 368 | std::vector<float> input = std::vector<float>( |
| 369 | { |
| 370 | 1.0f, 2.0f, 3.0f, |
| 371 | |
| 372 | 4.0f, 5.0f, 6.0f, |
| 373 | |
| 374 | 7.0f, 8.0f, 9.0f |
| 375 | }); |
| 376 | |
| 377 | std::vector<float> outputExpected = std::vector<float>( |
| 378 | { |
| 379 | 1.0f, 3.0f, |
| 380 | |
| 381 | 7.0f, 9.0f |
| 382 | }); |
| 383 | |
| 384 | return StridedSliceTestImpl<T, 2, 2>( |
| 385 | workloadFactory, memoryManager, inputTensorInfo, outputTensorInfo, input, outputExpected, desc); |
| 386 | } |
| 387 | |
| 388 | template <typename T> |
| 389 | LayerTestResult<T, 2> StridedSlice2DReverseTest( |
| 390 | armnn::IWorkloadFactory& workloadFactory, |
| 391 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 392 | { |
| 393 | armnn::TensorInfo inputTensorInfo; |
| 394 | armnn::TensorInfo outputTensorInfo; |
| 395 | |
| 396 | unsigned int inputShape[] = {3, 3}; |
| 397 | unsigned int outputShape[] = {2, 2}; |
| 398 | |
| 399 | armnn::StridedSliceQueueDescriptor desc; |
| 400 | desc.m_Parameters.m_Begin = {0, 0}; |
| 401 | desc.m_Parameters.m_End = {1, 1}; |
| 402 | desc.m_Parameters.m_Stride = {-2, -2}; |
| 403 | desc.m_Parameters.m_BeginMask = (1 << 2) - 1; |
| 404 | desc.m_Parameters.m_EndMask = (1 << 2) - 1; |
| 405 | |
| 406 | inputTensorInfo = armnn::TensorInfo(2, inputShape, armnn::GetDataType<T>()); |
| 407 | outputTensorInfo = armnn::TensorInfo(2, outputShape, armnn::GetDataType<T>()); |
| 408 | |
| 409 | std::vector<float> input = std::vector<float>( |
| 410 | { |
| 411 | 1.0f, 2.0f, 3.0f, |
| 412 | |
| 413 | 4.0f, 5.0f, 6.0f, |
| 414 | |
| 415 | 7.0f, 8.0f, 9.0f |
| 416 | }); |
| 417 | |
| 418 | std::vector<float> outputExpected = std::vector<float>( |
| 419 | { |
| 420 | 9.0f, 7.0f, |
| 421 | |
| 422 | 3.0f, 1.0f |
| 423 | }); |
| 424 | |
| 425 | return StridedSliceTestImpl<T, 2, 2>( |
| 426 | workloadFactory, memoryManager, inputTensorInfo, outputTensorInfo, input, outputExpected, desc); |
| 427 | } |
| 428 | |
| 429 | } // anonymous namespace |