Keith Davis | a57eccb | 2019-06-14 17:33:22 +0100 | [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 | template<typename T> |
| 20 | LayerTestResult<T, 4> SpaceToDepthTestImpl( |
| 21 | armnn::IWorkloadFactory& workloadFactory, |
| 22 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 23 | armnn::TensorInfo& inputTensorInfo, |
| 24 | armnn::TensorInfo& outputTensorInfo, |
| 25 | std::vector<float>& inputData, |
| 26 | std::vector<float>& outputExpectedData, |
| 27 | armnn::SpaceToDepthQueueDescriptor descriptor, |
| 28 | const float qScale = 1.0f, |
| 29 | const int32_t qOffset = 0) |
| 30 | { |
| 31 | const armnn::PermutationVector NHWCToNCHW = {0, 2, 3, 1}; |
| 32 | |
| 33 | if (descriptor.m_Parameters.m_DataLayout == armnn::DataLayout::NCHW) |
| 34 | { |
| 35 | inputTensorInfo = armnnUtils::Permuted(inputTensorInfo, NHWCToNCHW); |
| 36 | outputTensorInfo = armnnUtils::Permuted(outputTensorInfo, NHWCToNCHW); |
| 37 | |
| 38 | std::vector<float> inputTmp(inputData.size()); |
| 39 | armnnUtils::Permute(inputTensorInfo.GetShape(), NHWCToNCHW, |
| 40 | inputData.data(), inputTmp.data(), sizeof(float)); |
| 41 | inputData = inputTmp; |
| 42 | |
| 43 | std::vector<float> outputTmp(outputExpectedData.size()); |
| 44 | armnnUtils::Permute(outputTensorInfo.GetShape(), NHWCToNCHW, |
| 45 | outputExpectedData.data(), outputTmp.data(), sizeof(float)); |
| 46 | outputExpectedData = outputTmp; |
| 47 | } |
| 48 | |
| 49 | if(armnn::IsQuantizedType<T>()) |
| 50 | { |
| 51 | inputTensorInfo.SetQuantizationScale(qScale); |
| 52 | inputTensorInfo.SetQuantizationOffset(qOffset); |
| 53 | outputTensorInfo.SetQuantizationScale(qScale); |
| 54 | outputTensorInfo.SetQuantizationOffset(qOffset); |
| 55 | } |
| 56 | |
| 57 | boost::multi_array<T, 4> input = MakeTensor<T, 4>(inputTensorInfo, QuantizedVector<T>(qScale, qOffset, inputData)); |
| 58 | |
| 59 | LayerTestResult<T, 4> ret(outputTensorInfo); |
| 60 | ret.outputExpected = MakeTensor<T, 4>(outputTensorInfo, QuantizedVector<T>(qScale, qOffset, outputExpectedData)); |
| 61 | |
| 62 | std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo); |
| 63 | std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo); |
| 64 | |
| 65 | armnn::WorkloadInfo info; |
| 66 | AddInputToWorkload(descriptor, info, inputTensorInfo, inputHandle.get()); |
| 67 | AddOutputToWorkload(descriptor, info, outputTensorInfo, outputHandle.get()); |
| 68 | |
| 69 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateSpaceToDepth(descriptor, info); |
| 70 | |
| 71 | inputHandle->Allocate(); |
| 72 | outputHandle->Allocate(); |
| 73 | |
| 74 | CopyDataToITensorHandle(inputHandle.get(), &input[0][0][0][0]); |
| 75 | |
| 76 | workload->Execute(); |
| 77 | |
| 78 | CopyDataFromITensorHandle(&ret.output[0][0][0][0], outputHandle.get()); |
| 79 | |
| 80 | return ret; |
| 81 | } |
| 82 | |
| 83 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
James Conroy | d2aa85e | 2019-07-01 17:12:40 +0100 | [diff] [blame^] | 84 | LayerTestResult<T, 4> SpaceToDepthSimpleTest1( |
Keith Davis | a57eccb | 2019-06-14 17:33:22 +0100 | [diff] [blame] | 85 | armnn::IWorkloadFactory& workloadFactory, |
| 86 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 87 | armnn::DataLayout dataLayout = armnn::DataLayout::NHWC) |
| 88 | { |
| 89 | unsigned int inputShape[] = {1, 2, 2, 1}; |
| 90 | unsigned int outputShape[] = {1, 1, 1, 4}; |
| 91 | |
| 92 | std::vector<float> input = std::vector<float>( |
| 93 | { |
| 94 | 1.0f, 2.0f, 3.0f, 4.0f |
| 95 | }); |
| 96 | |
| 97 | std::vector<float> outputExpected = std::vector<float>( |
| 98 | { |
| 99 | 1.0f, 2.0f, 3.0f, 4.0f |
| 100 | }); |
| 101 | |
| 102 | armnn::TensorInfo inputTensorInfo; |
| 103 | armnn::TensorInfo outputTensorInfo; |
| 104 | |
| 105 | armnn::SpaceToDepthQueueDescriptor desc; |
| 106 | desc.m_Parameters.m_DataLayout = dataLayout; |
| 107 | desc.m_Parameters.m_BlockSize = 2; |
| 108 | |
| 109 | inputTensorInfo = armnn::TensorInfo(4, inputShape, ArmnnType); |
| 110 | outputTensorInfo = armnn::TensorInfo(4, outputShape, ArmnnType); |
| 111 | |
| 112 | return SpaceToDepthTestImpl<T>( |
| 113 | workloadFactory, memoryManager, inputTensorInfo, outputTensorInfo, input, outputExpected, desc); |
| 114 | } |
| 115 | |
| 116 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
James Conroy | d2aa85e | 2019-07-01 17:12:40 +0100 | [diff] [blame^] | 117 | LayerTestResult<T, 4> SpaceToDepthSimpleTest2( |
Keith Davis | a57eccb | 2019-06-14 17:33:22 +0100 | [diff] [blame] | 118 | armnn::IWorkloadFactory& workloadFactory, |
| 119 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 120 | armnn::DataLayout dataLayout = armnn::DataLayout::NHWC) |
| 121 | { |
| 122 | unsigned int inputShape[] = {1, 2, 2, 2}; |
| 123 | unsigned int outputShape[] = {1, 1, 1, 8}; |
| 124 | |
| 125 | std::vector<float> input = std::vector<float>( |
| 126 | { |
| 127 | 1.4f, 2.3f, 3.2f, 4.1f, 5.4f, 6.3f, 7.2f, 8.1f |
| 128 | }); |
| 129 | |
| 130 | std::vector<float> outputExpected = std::vector<float>( |
| 131 | { |
| 132 | 1.4f, 2.3f, 3.2f, 4.1f, 5.4f, 6.3f, 7.2f, 8.1f |
| 133 | }); |
| 134 | |
| 135 | armnn::TensorInfo inputTensorInfo; |
| 136 | armnn::TensorInfo outputTensorInfo; |
| 137 | |
| 138 | armnn::SpaceToDepthQueueDescriptor desc; |
| 139 | desc.m_Parameters.m_DataLayout = dataLayout; |
| 140 | desc.m_Parameters.m_BlockSize = 2; |
| 141 | |
| 142 | inputTensorInfo = armnn::TensorInfo(4, inputShape, ArmnnType); |
| 143 | outputTensorInfo = armnn::TensorInfo(4, outputShape, ArmnnType); |
| 144 | |
| 145 | return SpaceToDepthTestImpl<T>( |
| 146 | workloadFactory, memoryManager, inputTensorInfo, outputTensorInfo, input, outputExpected, desc); |
| 147 | } |