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