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 | // |
Keith Davis | a57eccb | 2019-06-14 17:33:22 +0100 | [diff] [blame] | 5 | |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 6 | #include "SpaceToDepthTestImpl.hpp" |
| 7 | |
| 8 | #include <Permute.hpp> |
| 9 | #include <ResolveType.hpp> |
Keith Davis | a57eccb | 2019-06-14 17:33:22 +0100 | [diff] [blame] | 10 | |
| 11 | #include <armnn/ArmNN.hpp> |
Keith Davis | a57eccb | 2019-06-14 17:33:22 +0100 | [diff] [blame] | 12 | |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 13 | #include <backendsCommon/test/TensorCopyUtils.hpp> |
| 14 | #include <backendsCommon/test/WorkloadTestUtils.hpp> |
Keith Davis | a57eccb | 2019-06-14 17:33:22 +0100 | [diff] [blame] | 15 | |
| 16 | #include <test/TensorHelpers.hpp> |
| 17 | |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 18 | namespace |
| 19 | { |
| 20 | |
Keith Davis | a57eccb | 2019-06-14 17:33:22 +0100 | [diff] [blame] | 21 | template<typename T> |
| 22 | LayerTestResult<T, 4> SpaceToDepthTestImpl( |
| 23 | armnn::IWorkloadFactory& workloadFactory, |
| 24 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 25 | armnn::TensorInfo& inputTensorInfo, |
| 26 | armnn::TensorInfo& outputTensorInfo, |
| 27 | std::vector<float>& inputData, |
| 28 | std::vector<float>& outputExpectedData, |
| 29 | armnn::SpaceToDepthQueueDescriptor descriptor, |
| 30 | const float qScale = 1.0f, |
| 31 | const int32_t qOffset = 0) |
| 32 | { |
| 33 | const armnn::PermutationVector NHWCToNCHW = {0, 2, 3, 1}; |
| 34 | |
| 35 | if (descriptor.m_Parameters.m_DataLayout == armnn::DataLayout::NCHW) |
| 36 | { |
| 37 | inputTensorInfo = armnnUtils::Permuted(inputTensorInfo, NHWCToNCHW); |
| 38 | outputTensorInfo = armnnUtils::Permuted(outputTensorInfo, NHWCToNCHW); |
| 39 | |
| 40 | std::vector<float> inputTmp(inputData.size()); |
| 41 | armnnUtils::Permute(inputTensorInfo.GetShape(), NHWCToNCHW, |
| 42 | inputData.data(), inputTmp.data(), sizeof(float)); |
| 43 | inputData = inputTmp; |
| 44 | |
| 45 | std::vector<float> outputTmp(outputExpectedData.size()); |
| 46 | armnnUtils::Permute(outputTensorInfo.GetShape(), NHWCToNCHW, |
| 47 | outputExpectedData.data(), outputTmp.data(), sizeof(float)); |
| 48 | outputExpectedData = outputTmp; |
| 49 | } |
| 50 | |
| 51 | if(armnn::IsQuantizedType<T>()) |
| 52 | { |
| 53 | inputTensorInfo.SetQuantizationScale(qScale); |
| 54 | inputTensorInfo.SetQuantizationOffset(qOffset); |
| 55 | outputTensorInfo.SetQuantizationScale(qScale); |
| 56 | outputTensorInfo.SetQuantizationOffset(qOffset); |
| 57 | } |
| 58 | |
| 59 | boost::multi_array<T, 4> input = MakeTensor<T, 4>(inputTensorInfo, QuantizedVector<T>(qScale, qOffset, inputData)); |
| 60 | |
| 61 | LayerTestResult<T, 4> ret(outputTensorInfo); |
| 62 | ret.outputExpected = MakeTensor<T, 4>(outputTensorInfo, QuantizedVector<T>(qScale, qOffset, outputExpectedData)); |
| 63 | |
| 64 | std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo); |
| 65 | std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo); |
| 66 | |
| 67 | armnn::WorkloadInfo info; |
| 68 | AddInputToWorkload(descriptor, info, inputTensorInfo, inputHandle.get()); |
| 69 | AddOutputToWorkload(descriptor, info, outputTensorInfo, outputHandle.get()); |
| 70 | |
| 71 | std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateSpaceToDepth(descriptor, info); |
| 72 | |
| 73 | inputHandle->Allocate(); |
| 74 | outputHandle->Allocate(); |
| 75 | |
| 76 | CopyDataToITensorHandle(inputHandle.get(), &input[0][0][0][0]); |
| 77 | |
| 78 | workload->Execute(); |
| 79 | |
| 80 | CopyDataFromITensorHandle(&ret.output[0][0][0][0], outputHandle.get()); |
| 81 | |
| 82 | return ret; |
| 83 | } |
| 84 | |
| 85 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
James Conroy | d2aa85e | 2019-07-01 17:12:40 +0100 | [diff] [blame] | 86 | LayerTestResult<T, 4> SpaceToDepthSimpleTest1( |
Keith Davis | a57eccb | 2019-06-14 17:33:22 +0100 | [diff] [blame] | 87 | armnn::IWorkloadFactory& workloadFactory, |
| 88 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 89 | armnn::DataLayout dataLayout = armnn::DataLayout::NHWC) |
| 90 | { |
| 91 | unsigned int inputShape[] = {1, 2, 2, 1}; |
| 92 | unsigned int outputShape[] = {1, 1, 1, 4}; |
| 93 | |
| 94 | std::vector<float> input = std::vector<float>( |
| 95 | { |
| 96 | 1.0f, 2.0f, 3.0f, 4.0f |
| 97 | }); |
| 98 | |
| 99 | std::vector<float> outputExpected = std::vector<float>( |
| 100 | { |
| 101 | 1.0f, 2.0f, 3.0f, 4.0f |
| 102 | }); |
| 103 | |
| 104 | armnn::TensorInfo inputTensorInfo; |
| 105 | armnn::TensorInfo outputTensorInfo; |
| 106 | |
| 107 | armnn::SpaceToDepthQueueDescriptor desc; |
| 108 | desc.m_Parameters.m_DataLayout = dataLayout; |
| 109 | desc.m_Parameters.m_BlockSize = 2; |
| 110 | |
| 111 | inputTensorInfo = armnn::TensorInfo(4, inputShape, ArmnnType); |
| 112 | outputTensorInfo = armnn::TensorInfo(4, outputShape, ArmnnType); |
| 113 | |
| 114 | return SpaceToDepthTestImpl<T>( |
| 115 | workloadFactory, memoryManager, inputTensorInfo, outputTensorInfo, input, outputExpected, desc); |
| 116 | } |
| 117 | |
| 118 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
James Conroy | d2aa85e | 2019-07-01 17:12:40 +0100 | [diff] [blame] | 119 | LayerTestResult<T, 4> SpaceToDepthSimpleTest2( |
Keith Davis | a57eccb | 2019-06-14 17:33:22 +0100 | [diff] [blame] | 120 | armnn::IWorkloadFactory& workloadFactory, |
| 121 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, |
| 122 | armnn::DataLayout dataLayout = armnn::DataLayout::NHWC) |
| 123 | { |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 124 | unsigned int inputShape[] = {1, 2, 2, 2}; |
Keith Davis | a57eccb | 2019-06-14 17:33:22 +0100 | [diff] [blame] | 125 | unsigned int outputShape[] = {1, 1, 1, 8}; |
| 126 | |
| 127 | std::vector<float> input = std::vector<float>( |
| 128 | { |
| 129 | 1.4f, 2.3f, 3.2f, 4.1f, 5.4f, 6.3f, 7.2f, 8.1f |
| 130 | }); |
| 131 | |
| 132 | std::vector<float> outputExpected = std::vector<float>( |
| 133 | { |
| 134 | 1.4f, 2.3f, 3.2f, 4.1f, 5.4f, 6.3f, 7.2f, 8.1f |
| 135 | }); |
| 136 | |
| 137 | armnn::TensorInfo inputTensorInfo; |
| 138 | armnn::TensorInfo outputTensorInfo; |
| 139 | |
| 140 | armnn::SpaceToDepthQueueDescriptor desc; |
| 141 | desc.m_Parameters.m_DataLayout = dataLayout; |
| 142 | desc.m_Parameters.m_BlockSize = 2; |
| 143 | |
| 144 | inputTensorInfo = armnn::TensorInfo(4, inputShape, ArmnnType); |
| 145 | outputTensorInfo = armnn::TensorInfo(4, outputShape, ArmnnType); |
| 146 | |
| 147 | return SpaceToDepthTestImpl<T>( |
| 148 | workloadFactory, memoryManager, inputTensorInfo, outputTensorInfo, input, outputExpected, desc); |
| 149 | } |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 150 | |
| 151 | } // anonymous namespace |
| 152 | |
| 153 | LayerTestResult<uint8_t, 4> SpaceToDepthNhwcAsymmQ8Test( |
| 154 | armnn::IWorkloadFactory& workloadFactory, |
| 155 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 156 | { |
| 157 | return SpaceToDepthSimpleTest1<armnn::DataType::QuantisedAsymm8>( |
| 158 | workloadFactory, |
| 159 | memoryManager); |
| 160 | } |
| 161 | |
| 162 | LayerTestResult<uint8_t, 4> SpaceToDepthNchwAsymmQ8Test( |
| 163 | armnn::IWorkloadFactory& workloadFactory, |
| 164 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 165 | { |
| 166 | return SpaceToDepthSimpleTest1<armnn::DataType::QuantisedAsymm8>( |
| 167 | workloadFactory, |
| 168 | memoryManager, |
| 169 | armnn::DataLayout::NCHW); |
| 170 | } |
| 171 | |
Matthew Jackson | 9bff144 | 2019-09-12 09:08:23 +0100 | [diff] [blame] | 172 | LayerTestResult<armnn::Half, 4> SpaceToDepthNhwcFloat16Test( |
| 173 | armnn::IWorkloadFactory& workloadFactory, |
| 174 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 175 | { |
| 176 | return SpaceToDepthSimpleTest1<armnn::DataType::Float16>( |
| 177 | workloadFactory, |
| 178 | memoryManager); |
| 179 | } |
| 180 | |
| 181 | LayerTestResult<armnn::Half, 4> SpaceToDepthNchwFloat16Test( |
| 182 | armnn::IWorkloadFactory& workloadFactory, |
| 183 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 184 | { |
| 185 | return SpaceToDepthSimpleTest1<armnn::DataType::Float16>( |
| 186 | workloadFactory, |
| 187 | memoryManager, |
| 188 | armnn::DataLayout::NCHW); |
| 189 | } |
| 190 | |
Aron Virginas-Tar | 00d306e | 2019-08-28 18:08:46 +0100 | [diff] [blame] | 191 | LayerTestResult<float, 4> SpaceToDepthNhwcFloat32Test1( |
| 192 | armnn::IWorkloadFactory& workloadFactory, |
| 193 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 194 | { |
| 195 | return SpaceToDepthSimpleTest1<armnn::DataType::Float32>( |
| 196 | workloadFactory, |
| 197 | memoryManager); |
| 198 | } |
| 199 | |
| 200 | LayerTestResult<float, 4> SpaceToDepthNchwFloat32Test1( |
| 201 | armnn::IWorkloadFactory& workloadFactory, |
| 202 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 203 | { |
| 204 | return SpaceToDepthSimpleTest1<armnn::DataType::Float32>( |
| 205 | workloadFactory, |
| 206 | memoryManager, |
| 207 | armnn::DataLayout::NCHW); |
| 208 | } |
| 209 | |
| 210 | LayerTestResult<float, 4> SpaceToDepthNhwcFloat32Test2( |
| 211 | armnn::IWorkloadFactory& workloadFactory, |
| 212 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 213 | { |
| 214 | return SpaceToDepthSimpleTest2<armnn::DataType::Float32>( |
| 215 | workloadFactory, |
| 216 | memoryManager); |
| 217 | } |
| 218 | |
| 219 | LayerTestResult<float, 4> SpaceToDepthNchwFloat32Test2( |
| 220 | armnn::IWorkloadFactory& workloadFactory, |
| 221 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 222 | { |
| 223 | return SpaceToDepthSimpleTest2<armnn::DataType::Float32>( |
| 224 | workloadFactory, |
| 225 | memoryManager, |
| 226 | armnn::DataLayout::NCHW); |
| 227 | } |
| 228 | |
| 229 | LayerTestResult<int16_t, 4> SpaceToDepthNhwcQSymm16Test( |
| 230 | armnn::IWorkloadFactory& workloadFactory, |
| 231 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 232 | { |
| 233 | return SpaceToDepthSimpleTest2<armnn::DataType::QuantisedSymm16>( |
| 234 | workloadFactory, |
| 235 | memoryManager); |
| 236 | } |
| 237 | |
| 238 | LayerTestResult<int16_t, 4> SpaceToDepthNchwQSymm16Test( |
| 239 | armnn::IWorkloadFactory& workloadFactory, |
| 240 | const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) |
| 241 | { |
| 242 | return SpaceToDepthSimpleTest2<armnn::DataType::QuantisedSymm16>( |
| 243 | workloadFactory, |
| 244 | memoryManager, |
| 245 | armnn::DataLayout::NCHW); |
| 246 | } |