blob: ede35f5a3ddb5fb7983bb9c02eb8911264d9fcb8 [file] [log] [blame]
Aron Virginas-Tar00d306e2019-08-28 18:08:46 +01001//
Teresa Charlinfbf0e5b2020-08-17 01:01:06 +01002// Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
Aron Virginas-Tar00d306e2019-08-28 18:08:46 +01003// SPDX-License-Identifier: MIT
4//
5
6#include "FullyConnectedTestImpl.hpp"
7
Aron Virginas-Tar00d306e2019-08-28 18:08:46 +01008
Aron Virginas-Tar48623a02019-10-22 10:00:28 +01009#include <QuantizeHelper.hpp>
10
Aron Virginas-Tar00d306e2019-08-28 18:08:46 +010011#include <backendsCommon/CpuTensorHandle.hpp>
12
13#include <backendsCommon/test/DataTypeUtils.hpp>
14#include <backendsCommon/test/TensorCopyUtils.hpp>
15#include <backendsCommon/test/WorkloadTestUtils.hpp>
16
17#include <test/TensorHelpers.hpp>
18
19//
20// Implementation templates
21//
22
23template<typename T, typename B>
24LayerTestResult<T, 2> SimpleFullyConnectedTestImpl(
25 armnn::IWorkloadFactory& workloadFactory,
26 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
27 armnn::TensorInfo inputTensorInfo,
28 armnn::TensorInfo outputTensorInfo,
29 armnn::TensorInfo weightsDesc,
30 armnn::TensorInfo biasesDesc,
31 boost::multi_array<T, 2>& weights,
32 boost::multi_array<B, 1>& bias,
33 boost::multi_array<T, 4>& input,
34 bool biasEnabled,
35 bool transposeWeights)
36{
Jan Eilers8eb25602020-03-09 12:13:48 +000037 IgnoreUnused(memoryManager);
Teresa Charlinfbf0e5b2020-08-17 01:01:06 +010038 ARMNN_NO_DEPRECATE_WARN_BEGIN
Aron Virginas-Tar00d306e2019-08-28 18:08:46 +010039 std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo);
40 std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo);
Teresa Charlinfbf0e5b2020-08-17 01:01:06 +010041 ARMNN_NO_DEPRECATE_WARN_END
Aron Virginas-Tar00d306e2019-08-28 18:08:46 +010042
43 armnn::FullyConnectedQueueDescriptor data;
44 armnn::WorkloadInfo info;
45 armnn::ScopedCpuTensorHandle weightsTensor(weightsDesc);
46 armnn::ScopedCpuTensorHandle biasTensor(biasesDesc);
47
48 AllocateAndCopyDataToITensorHandle(&weightsTensor, &weights[0][0]);
49 AllocateAndCopyDataToITensorHandle(&biasTensor, &bias[0]);
50
51 AddInputToWorkload(data, info, inputTensorInfo, inputHandle.get());
52 AddOutputToWorkload(data, info, outputTensorInfo, outputHandle.get());
53 data.m_Weight = &weightsTensor;
54 data.m_Bias = &biasTensor;
55 data.m_Parameters.m_BiasEnabled = biasEnabled;
56 data.m_Parameters.m_TransposeWeightMatrix = transposeWeights;
57
58 std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateFullyConnected(data, info);
59 LayerTestResult<T, 2> result(outputTensorInfo);
60
61 inputHandle->Allocate();
62 outputHandle->Allocate();
63 CopyDataToITensorHandle(inputHandle.get(), &input[0][0][0][0]);
64
65 ExecuteWorkload(*workload, memoryManager);
66
67 CopyDataFromITensorHandle(&result.output[0][0], outputHandle.get());
68
69 return result;
70}
71
72template<armnn::DataType ArmnnType, typename T>
73LayerTestResult<T, 2> FullyConnectedTest(
74 armnn::IWorkloadFactory& workloadFactory,
75 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
76 bool biasEnabled)
77{
78 constexpr static unsigned int inputWidth = 3u;
79 constexpr static unsigned int inputHeight = 2u;
80 constexpr static unsigned int inputChannels = 1u;
81
82 constexpr static unsigned int inputSize = inputWidth * inputHeight * inputChannels;
83
84 constexpr static unsigned int outputChannels = 2u;
85
86 armnn::TensorInfo inputTensorInfo({ 1, inputChannels, inputHeight, inputWidth }, ArmnnType);
87 inputTensorInfo.SetQuantizationScale(0.1f);
88 inputTensorInfo.SetQuantizationOffset(63);
89
90 armnn::TensorInfo outputTensorInfo({ 1, outputChannels }, ArmnnType);
91 outputTensorInfo.SetQuantizationScale(5.f);
92 outputTensorInfo.SetQuantizationOffset(biasEnabled ? -50 : 10);
93
94 armnn::TensorInfo weightsDesc({ outputChannels, inputSize }, ArmnnType);
95 weightsDesc.SetQuantizationScale(0.2f);
96 weightsDesc.SetQuantizationOffset(93);
97
98 armnn::TensorInfo biasesDesc({ outputChannels }, GetBiasTypeFromWeightsType(weightsDesc.GetDataType()).value());
99 biasesDesc.SetQuantizationScale(inputTensorInfo.GetQuantizationScale() * weightsDesc.GetQuantizationScale());
100 biasesDesc.SetQuantizationOffset(0);
101
102 LayerTestResult<T, 2> result(outputTensorInfo);
103
104 auto input = MakeTensor<T, 4>(inputTensorInfo, ConvertToDataType<ArmnnType>(
105 {
106 -1.2f, 6.1f, -3.5f,
107 18.8f, -5.5f, 2.9f
108 },
109 inputTensorInfo));
110
111 auto weights = MakeTensor<T, 2>(weightsDesc, ConvertToDataType<ArmnnType>(
112 {
113 -8.4f, 20.0f, -10.4f, -8, 16.4f, -11.8f,
114 23.4f, 10.4f, -14.0f, -3.8f, -11.8f, 11.4f
115 },
116 weightsDesc));
117
118 auto bias = MakeTensor<int32_t, 1>(biasesDesc, std::vector<int32_t>{9250, 67500});
119
120 result = SimpleFullyConnectedTestImpl<T>(
121 workloadFactory,
122 memoryManager,
123 inputTensorInfo, outputTensorInfo,
124 weightsDesc, biasesDesc,
125 weights, bias, input,
126 biasEnabled, true
127 );
128
129 if (biasEnabled)
130 {
131 result.outputExpected = MakeTensor<T, 2>(outputTensorInfo,
132 ConvertToDataType<ArmnnType>({80.f, 1460.f}, outputTensorInfo));
133 }
134 else
135 {
136 result.outputExpected = MakeTensor<T, 2>(outputTensorInfo,
137 ConvertToDataType<ArmnnType>({-107.04f, 110.f}, outputTensorInfo));
138 }
139
140 return result;
141}
142
143//
144// ArmNN variant of the AndroidNN fully_connected_float_large test.
145//
146// Tests the fully connected layer with large values, optionally transposing weights.
147// Note this is templated for consistency, but the nature of this tests makes it unlikely to be useful in Uint8 mode.
148//
149template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
150LayerTestResult<T, 2> FullyConnectedLargeTestCommon(
151 armnn::IWorkloadFactory& workloadFactory,
152 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
153 bool transposeWeights,
154 float qScale = 0.0f,
155 int32_t qOffset = 0)
156{
157 unsigned int inputWidth = 1;
158 unsigned int inputHeight = 1;
159 unsigned int inputChannels = 5;
160 unsigned int inputNum = 1;
161
162 unsigned int outputChannels = 1;
163 unsigned int outputNum = 1;
164
165 // Define the tensor descriptors.
166 armnn::TensorInfo inputTensorInfo;
167 armnn::TensorInfo outputTensorInfo;
168 armnn::TensorInfo weightsDesc;
169 armnn::TensorInfo biasesDesc;
170
171 unsigned int inputShape[] = { inputNum, inputChannels, inputHeight, inputWidth };
172 unsigned int outputShape[] = { outputNum, outputChannels };
173 unsigned int weightsShape[] = { inputChannels, outputChannels };
174 if (transposeWeights)
175 {
176 std::swap(weightsShape[0], weightsShape[1]);
177 }
178
179 unsigned int biasShape[] = { outputChannels };
180
181 inputTensorInfo = armnn::TensorInfo(4, inputShape, ArmnnType);
182 outputTensorInfo = armnn::TensorInfo(2, outputShape, ArmnnType);
183 weightsDesc = armnn::TensorInfo(2, weightsShape, ArmnnType);
184 biasesDesc = armnn::TensorInfo(1, biasShape, ArmnnType);
185
186 // Set quantization parameters if the requested type is a quantized type.
187 if(armnn::IsQuantizedType<T>())
188 {
189 inputTensorInfo.SetQuantizationScale(qScale);
190 inputTensorInfo.SetQuantizationOffset(qOffset);
191 outputTensorInfo.SetQuantizationScale(qScale);
192 outputTensorInfo.SetQuantizationOffset(qOffset);
193 }
194
195 LayerTestResult<T, 2> result(outputTensorInfo);
196
197 boost::multi_array<T, 4> input = MakeTensor<T, 4>(inputTensorInfo,
Aron Virginas-Tar48623a02019-10-22 10:00:28 +0100198 armnnUtils::QuantizedVector<T>({
Aron Virginas-Tar00d306e2019-08-28 18:08:46 +0100199 1.0f, 10.0f, 100.0f, 1000.0f, 10000.0f,
Aron Virginas-Tar48623a02019-10-22 10:00:28 +0100200 },
201 qScale, qOffset)
Aron Virginas-Tar00d306e2019-08-28 18:08:46 +0100202 );
203
204 boost::multi_array<T, 2> weights = MakeTensor<T, 2>(weightsDesc,
Aron Virginas-Tar48623a02019-10-22 10:00:28 +0100205 armnnUtils::QuantizedVector<T>({
Aron Virginas-Tar00d306e2019-08-28 18:08:46 +0100206 2.0f, 3.0f, 4.0f, 5.0f, 6.0f
Aron Virginas-Tar48623a02019-10-22 10:00:28 +0100207 },
208 qScale, qOffset)
Aron Virginas-Tar00d306e2019-08-28 18:08:46 +0100209 );
210
211 std::vector<T> biasValues({900000.f});
212 boost::multi_array<T, 1> bias = MakeTensor<T, 1>(biasesDesc, biasValues);
213
214 result = SimpleFullyConnectedTestImpl<T>(
215 workloadFactory,
216 memoryManager,
217 inputTensorInfo, outputTensorInfo,
218 weightsDesc, biasesDesc,
219 weights, bias, input,
220 true, transposeWeights
221 );
222
223 result.outputExpected = MakeTensor<T, 2>(outputTensorInfo,
Aron Virginas-Tar48623a02019-10-22 10:00:28 +0100224 armnnUtils::QuantizedVector<T>({ 965432.0f }, qScale, qOffset));
Aron Virginas-Tar00d306e2019-08-28 18:08:46 +0100225
226 return result;
227}
228
229//
230// Explicit template specializations
231//
232
Derek Lambertif90c56d2020-01-10 17:14:08 +0000233template LayerTestResult<armnn::ResolveType<armnn::DataType::QAsymmU8>, 2>
234FullyConnectedTest<armnn::DataType::QAsymmU8>(
Aron Virginas-Tar00d306e2019-08-28 18:08:46 +0100235 armnn::IWorkloadFactory& workloadFactory,
236 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
237 bool biasEnabled);
238
Derek Lambertif90c56d2020-01-10 17:14:08 +0000239template LayerTestResult<armnn::ResolveType<armnn::DataType::QSymmS16>, 2>
240FullyConnectedTest<armnn::DataType::QSymmS16>(
Aron Virginas-Tar00d306e2019-08-28 18:08:46 +0100241 armnn::IWorkloadFactory& workloadFactory,
242 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
243 bool biasEnabled);
244
245//
246// Implementation functions
247//
248
249LayerTestResult<float, 2> FullyConnectedFloat32Test(
250 armnn::IWorkloadFactory& workloadFactory,
251 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
252 bool biasEnabled,
253 bool transposeWeights)
254{
255 unsigned int inputWidth = 1;
256 unsigned int inputHeight = 1;
257 unsigned int inputChannels = 5;
258 unsigned int inputNum = 2;
259
260 unsigned int outputChannels = 3;
261 unsigned int outputNum = 2;
262
263 // Define the tensor descriptors.
264 armnn::TensorInfo inputTensorInfo;
265 armnn::TensorInfo outputTensorInfo;
266 armnn::TensorInfo weightsDesc;
267 armnn::TensorInfo biasesDesc;
268
269 unsigned int inputShape[] = { inputNum, inputChannels, inputHeight, inputWidth };
270 unsigned int outputShape[] = { outputNum, outputChannels };
271 unsigned int weightsShape[] = { inputChannels, outputChannels };
272
273 if (transposeWeights)
274 {
275 std::swap(weightsShape[0], weightsShape[1]);
276 }
277
278 unsigned int biasShape[] = { outputChannels };
279
280 inputTensorInfo = armnn::TensorInfo(4, inputShape, armnn::DataType::Float32);
281 outputTensorInfo = armnn::TensorInfo(2, outputShape, armnn::DataType::Float32);
282 weightsDesc = armnn::TensorInfo(2, weightsShape, armnn::DataType::Float32);
283 biasesDesc = armnn::TensorInfo(1, biasShape, armnn::DataType::Float32);
284
285 LayerTestResult<float, 2> result(outputTensorInfo);
286
287 boost::multi_array<float, 4> input = MakeTensor<float, 4>(inputTensorInfo, std::vector<float>(
288 {
289 1.0f, 2.0f, 3.0f, 4.0f, 5.0f,
290
291 5.0f, 4.0f, 3.0f, 2.0f, 1.0f
292 })
293 );
294
295 boost::multi_array<float, 2> weights = MakeTensor<float, 2>(weightsDesc, std::vector<float>(
296 {
297 .5f, 2.f, .5f,
298 .5f, 2.f, 1.f,
299 .5f, 2.f, 2.f,
300 .5f, 2.f, 3.f,
301 .5f, 2.f, 4.f
302 }));
303
304 if (transposeWeights)
305 {
306 weights = MakeTensor<float, 2>(weightsDesc, std::vector<float>(
307 {
308 .5f, .5f, .5f, .5f, .5f,
309 2.f, 2.f, 2.f, 2.f, 2.f,
310 .5f, 1.f, 2.f, 3.f, 4.f
311 }));
312 }
313
314
315 std::vector<float> biasValues({0.f, 0.f, 0.f});
316 if (biasEnabled)
317 {
318 biasValues = std::vector<float>({10.f, 20.f, 30.f});
319 }
320 boost::multi_array<float, 1> bias = MakeTensor<float, 1>(biasesDesc, biasValues);
321
322 result = SimpleFullyConnectedTestImpl<float>(
323 workloadFactory,
324 memoryManager,
325 inputTensorInfo, outputTensorInfo,
326 weightsDesc, biasesDesc,
327 weights, bias, input,
328 biasEnabled, transposeWeights
329 );
330
331 result.outputExpected = MakeTensor<float, 2>(outputTensorInfo, std::vector<float>(
332 {
333 0.5f + 1.0f + 1.5f + 2.0f + 2.5f + biasValues[0],
334 2.0f + 4.0f + 6.0f + 8.0f + 10.f + biasValues[1],
335 0.5f + 2.0f + 6.0f + 12.f + 20.f + biasValues[2],
336
337 2.5f + 2.0f + 1.5f + 1.0f + 0.5f + biasValues[0],
338 10.0f + 8.0f + 6.0f + 4.0f + 2.f + biasValues[1],
339 2.5f + 4.0f + 6.0f + 6.f + 4.f + biasValues[2]
340 })
341 );
342
343 return result;
344}
345
346LayerTestResult<float, 2> FullyConnectedLargeTest(
347 armnn::IWorkloadFactory& workloadFactory,
348 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
349 bool transposeWeights)
350{
351 return FullyConnectedLargeTestCommon<armnn::DataType::Float32>(workloadFactory, memoryManager, transposeWeights);
352}