2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
6 #include "DebugTestImpl.hpp"
8 #include <QuantizeHelper.hpp>
9 #include <ResolveType.hpp>
11 #include <armnn/ArmNN.hpp>
13 #include <backendsCommon/test/TensorCopyUtils.hpp>
14 #include <backendsCommon/test/WorkloadTestUtils.hpp>
16 #include <test/TensorHelpers.hpp>
21 template<typename T, std::size_t Dim>
22 LayerTestResult<T, Dim> DebugTestImpl(
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::DebugQueueDescriptor descriptor,
30 const std::string expectedStringOutput,
31 const float qScale = 1.0f,
32 const int32_t qOffset = 0)
34 boost::ignore_unused(memoryManager);
35 if(armnn::IsQuantizedType<T>())
37 inputTensorInfo.SetQuantizationScale(qScale);
38 inputTensorInfo.SetQuantizationOffset(qOffset);
40 outputTensorInfo.SetQuantizationScale(qScale);
41 outputTensorInfo.SetQuantizationOffset(qOffset);
44 boost::multi_array<T, Dim> input =
45 MakeTensor<T, Dim>(inputTensorInfo, armnnUtils::QuantizedVector<T>(inputData, qScale, qOffset));
47 LayerTestResult<T, Dim> ret(outputTensorInfo);
49 MakeTensor<T, Dim>(outputTensorInfo, armnnUtils::QuantizedVector<T>(outputExpectedData, qScale, qOffset));
51 std::unique_ptr<armnn::ITensorHandle> inputHandle =
52 workloadFactory.CreateTensorHandle(inputTensorInfo);
54 std::unique_ptr<armnn::ITensorHandle> outputHandle =
55 workloadFactory.CreateTensorHandle(outputTensorInfo);
57 armnn::WorkloadInfo info;
58 AddInputToWorkload(descriptor, info, inputTensorInfo, inputHandle.get());
59 AddOutputToWorkload(descriptor, info, outputTensorInfo, outputHandle.get());
61 std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateDebug(descriptor, info);
63 inputHandle->Allocate();
64 outputHandle->Allocate();
66 CopyDataToITensorHandle(inputHandle.get(), input.data());
68 std::ostringstream oss;
69 std::streambuf* coutStreambuf = std::cout.rdbuf();
70 std::cout.rdbuf(oss.rdbuf());
72 ExecuteWorkload(*workload, memoryManager);
74 std::cout.rdbuf(coutStreambuf);
76 BOOST_TEST(oss.str() == expectedStringOutput);
78 CopyDataFromITensorHandle(ret.output.data(), outputHandle.get());
83 template <armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
84 LayerTestResult<T, 4> Debug4dTest(
85 armnn::IWorkloadFactory& workloadFactory,
86 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
88 armnn::TensorInfo inputTensorInfo;
89 armnn::TensorInfo outputTensorInfo;
91 unsigned int inputShape[] = {1, 2, 2, 3};
92 unsigned int outputShape[] = {1, 2, 2, 3};
94 armnn::DebugQueueDescriptor desc;
96 desc.m_LayerName = "TestOutput";
99 inputTensorInfo = armnn::TensorInfo(4, inputShape, ArmnnType);
100 outputTensorInfo = armnn::TensorInfo(4, outputShape, ArmnnType);
102 std::vector<float> input = std::vector<float>(
110 std::vector<float> outputExpected = std::vector<float>(
118 const std::string expectedStringOutput =
119 "{ \"layerGuid\": 1,"
120 " \"layerName\": \"TestOutput\","
121 " \"outputSlot\": 0,"
122 " \"shape\": [1, 2, 2, 3],"
123 " \"min\": 1, \"max\": 12,"
124 " \"data\": [[[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]] }\n";
126 return DebugTestImpl<T, 4>(workloadFactory,
133 expectedStringOutput);
136 template <armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
137 LayerTestResult<T, 3> Debug3dTest(
138 armnn::IWorkloadFactory& workloadFactory,
139 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
141 armnn::TensorInfo inputTensorInfo;
142 armnn::TensorInfo outputTensorInfo;
144 unsigned int inputShape[] = {3, 3, 1};
145 unsigned int outputShape[] = {3, 3, 1};
147 armnn::DebugQueueDescriptor desc;
149 desc.m_LayerName = "TestOutput";
150 desc.m_SlotIndex = 0;
152 inputTensorInfo = armnn::TensorInfo(3, inputShape, ArmnnType);
153 outputTensorInfo = armnn::TensorInfo(3, outputShape, ArmnnType);
155 std::vector<float> input = std::vector<float>(
162 std::vector<float> outputExpected = std::vector<float>(
169 const std::string expectedStringOutput =
170 "{ \"layerGuid\": 1,"
171 " \"layerName\": \"TestOutput\","
172 " \"outputSlot\": 0,"
173 " \"shape\": [3, 3, 1],"
174 " \"min\": 1, \"max\": 9,"
175 " \"data\": [[[1], [2], [3]], [[4], [5], [6]], [[7], [8], [9]]] }\n";
177 return DebugTestImpl<T, 3>(workloadFactory,
184 expectedStringOutput);
187 template <armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
188 LayerTestResult<T, 2> Debug2dTest(
189 armnn::IWorkloadFactory& workloadFactory,
190 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
192 armnn::TensorInfo inputTensorInfo;
193 armnn::TensorInfo outputTensorInfo;
195 unsigned int inputShape[] = {2, 2};
196 unsigned int outputShape[] = {2, 2};
198 armnn::DebugQueueDescriptor desc;
200 desc.m_LayerName = "TestOutput";
201 desc.m_SlotIndex = 0;
203 inputTensorInfo = armnn::TensorInfo(2, inputShape, ArmnnType);
204 outputTensorInfo = armnn::TensorInfo(2, outputShape, ArmnnType);
206 std::vector<float> input = std::vector<float>(
212 std::vector<float> outputExpected = std::vector<float>(
218 const std::string expectedStringOutput =
219 "{ \"layerGuid\": 1,"
220 " \"layerName\": \"TestOutput\","
221 " \"outputSlot\": 0,"
222 " \"shape\": [2, 2],"
223 " \"min\": 1, \"max\": 4,"
224 " \"data\": [[1, 2], [3, 4]] }\n";
226 return DebugTestImpl<T, 2>(workloadFactory,
233 expectedStringOutput);
236 template <armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
237 LayerTestResult<T, 1> Debug1dTest(
238 armnn::IWorkloadFactory& workloadFactory,
239 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
241 armnn::TensorInfo inputTensorInfo;
242 armnn::TensorInfo outputTensorInfo;
244 unsigned int inputShape[] = {4};
245 unsigned int outputShape[] = {4};
247 armnn::DebugQueueDescriptor desc;
249 desc.m_LayerName = "TestOutput";
250 desc.m_SlotIndex = 0;
252 inputTensorInfo = armnn::TensorInfo(1, inputShape, ArmnnType);
253 outputTensorInfo = armnn::TensorInfo(1, outputShape, ArmnnType);
255 std::vector<float> input = std::vector<float>(
257 1.0f, 2.0f, 3.0f, 4.0f,
260 std::vector<float> outputExpected = std::vector<float>(
262 1.0f, 2.0f, 3.0f, 4.0f,
265 const std::string expectedStringOutput =
266 "{ \"layerGuid\": 1,"
267 " \"layerName\": \"TestOutput\","
268 " \"outputSlot\": 0,"
270 " \"min\": 1, \"max\": 4,"
271 " \"data\": [1, 2, 3, 4] }\n";
273 return DebugTestImpl<T, 1>(workloadFactory,
280 expectedStringOutput);
283 } // anonymous namespace
285 LayerTestResult<float, 4> Debug4dFloat32Test(
286 armnn::IWorkloadFactory& workloadFactory,
287 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
289 return Debug4dTest<armnn::DataType::Float32>(workloadFactory, memoryManager);
292 LayerTestResult<float, 3> Debug3dFloat32Test(
293 armnn::IWorkloadFactory& workloadFactory,
294 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
296 return Debug3dTest<armnn::DataType::Float32>(workloadFactory, memoryManager);
299 LayerTestResult<float, 2> Debug2dFloat32Test(
300 armnn::IWorkloadFactory& workloadFactory,
301 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
303 return Debug2dTest<armnn::DataType::Float32>(workloadFactory, memoryManager);
306 LayerTestResult<float, 1> Debug1dFloat32Test(
307 armnn::IWorkloadFactory& workloadFactory,
308 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
310 return Debug1dTest<armnn::DataType::Float32>(workloadFactory, memoryManager);
313 LayerTestResult<uint8_t, 4> Debug4dUint8Test(
314 armnn::IWorkloadFactory& workloadFactory,
315 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
317 return Debug4dTest<armnn::DataType::QAsymmU8>(workloadFactory, memoryManager);
320 LayerTestResult<uint8_t, 3> Debug3dUint8Test(
321 armnn::IWorkloadFactory& workloadFactory,
322 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
324 return Debug3dTest<armnn::DataType::QAsymmU8>(workloadFactory, memoryManager);
327 LayerTestResult<uint8_t, 2> Debug2dUint8Test(
328 armnn::IWorkloadFactory& workloadFactory,
329 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
331 return Debug2dTest<armnn::DataType::QAsymmU8>(workloadFactory, memoryManager);
334 LayerTestResult<uint8_t, 1> Debug1dUint8Test(
335 armnn::IWorkloadFactory& workloadFactory,
336 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
338 return Debug1dTest<armnn::DataType::QAsymmU8>(workloadFactory, memoryManager);
341 LayerTestResult<int16_t, 4> Debug4dInt16Test(
342 armnn::IWorkloadFactory& workloadFactory,
343 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
345 return Debug4dTest<armnn::DataType::QSymmS16>(workloadFactory, memoryManager);
348 LayerTestResult<int16_t, 3> Debug3dInt16Test(
349 armnn::IWorkloadFactory& workloadFactory,
350 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
352 return Debug3dTest<armnn::DataType::QSymmS16>(workloadFactory, memoryManager);
355 LayerTestResult<int16_t, 2> Debug2dInt16Test(
356 armnn::IWorkloadFactory& workloadFactory,
357 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
359 return Debug2dTest<armnn::DataType::QSymmS16>(workloadFactory, memoryManager);
362 LayerTestResult<int16_t, 1> Debug1dInt16Test(
363 armnn::IWorkloadFactory& workloadFactory,
364 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager)
366 return Debug1dTest<armnn::DataType::QSymmS16>(workloadFactory, memoryManager);