2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
6 #include "TypeUtils.hpp"
7 #include "WorkloadTestUtils.hpp"
9 #include <backendsCommon/IBackendInternal.hpp>
11 template<typename T, typename B>
12 LayerTestResult<T, 2> SimpleFullyConnectedTestImpl(
13 armnn::IWorkloadFactory& workloadFactory,
14 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
15 armnn::TensorInfo inputTensorInfo,
16 armnn::TensorInfo outputTensorInfo,
17 armnn::TensorInfo weightsDesc,
18 armnn::TensorInfo biasesDesc,
19 boost::multi_array<T, 2>& weights,
20 boost::multi_array<B, 1>& bias,
21 boost::multi_array<T, 4>& input,
23 bool transposeWeights)
25 std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputTensorInfo);
26 std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputTensorInfo);
28 armnn::FullyConnectedQueueDescriptor data;
29 armnn::WorkloadInfo info;
30 armnn::ScopedCpuTensorHandle weightsTensor(weightsDesc);
31 armnn::ScopedCpuTensorHandle biasTensor(biasesDesc);
33 AllocateAndCopyDataToITensorHandle(&weightsTensor, &weights[0][0]);
34 AllocateAndCopyDataToITensorHandle(&biasTensor, &bias[0]);
36 AddInputToWorkload(data, info, inputTensorInfo, inputHandle.get());
37 AddOutputToWorkload(data, info, outputTensorInfo, outputHandle.get());
38 data.m_Weight = &weightsTensor;
39 data.m_Bias = &biasTensor;
40 data.m_Parameters.m_BiasEnabled = biasEnabled;
41 data.m_Parameters.m_TransposeWeightMatrix = transposeWeights;
43 std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateFullyConnected(data, info);
44 LayerTestResult<T, 2> result(outputTensorInfo);
46 inputHandle->Allocate();
47 outputHandle->Allocate();
48 CopyDataToITensorHandle(inputHandle.get(), &input[0][0][0][0]);
50 ExecuteWorkload(*workload, memoryManager);
52 CopyDataFromITensorHandle(&result.output[0][0], outputHandle.get());
57 LayerTestResult<float, 2> FullyConnectedFloat32Test(
58 armnn::IWorkloadFactory& workloadFactory,
59 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
61 bool transposeWeights)
63 unsigned int inputWidth = 1;
64 unsigned int inputHeight = 1;
65 unsigned int inputChannels = 5;
66 unsigned int inputNum = 2;
68 unsigned int outputChannels = 3;
69 unsigned int outputNum = 2;
71 // Define the tensor descriptors.
72 armnn::TensorInfo inputTensorInfo;
73 armnn::TensorInfo outputTensorInfo;
74 armnn::TensorInfo weightsDesc;
75 armnn::TensorInfo biasesDesc;
77 unsigned int inputShape[] = { inputNum, inputChannels, inputHeight, inputWidth };
78 unsigned int outputShape[] = { outputNum, outputChannels };
79 unsigned int weightsShape[] = { inputChannels, outputChannels };
82 std::swap(weightsShape[0], weightsShape[1]);
84 unsigned int biasShape[] = { outputChannels };
86 inputTensorInfo = armnn::TensorInfo(4, inputShape, armnn::DataType::Float32);
87 outputTensorInfo = armnn::TensorInfo(2, outputShape, armnn::DataType::Float32);
88 weightsDesc = armnn::TensorInfo(2, weightsShape, armnn::DataType::Float32);
89 biasesDesc = armnn::TensorInfo(1, biasShape, armnn::DataType::Float32);
91 LayerTestResult<float, 2> result(outputTensorInfo);
93 boost::multi_array<float, 4> input = MakeTensor<float, 4>(inputTensorInfo, std::vector<float>(
95 1.0f, 2.0f, 3.0f, 4.0f, 5.0f,
97 5.0f, 4.0f, 3.0f, 2.0f, 1.0f
101 boost::multi_array<float, 2> weights = MakeTensor<float, 2>(weightsDesc, std::vector<float>(
110 if (transposeWeights)
112 weights = MakeTensor<float, 2>(weightsDesc, std::vector<float>(
114 .5f, .5f, .5f, .5f, .5f,
115 2.f, 2.f, 2.f, 2.f, 2.f,
116 .5f, 1.f, 2.f, 3.f, 4.f
121 std::vector<float> biasValues({0.f, 0.f, 0.f});
124 biasValues = std::vector<float>({10.f, 20.f, 30.f});
126 boost::multi_array<float, 1> bias = MakeTensor<float, 1>(biasesDesc, biasValues);
128 result = SimpleFullyConnectedTestImpl<float>(
131 inputTensorInfo, outputTensorInfo,
132 weightsDesc, biasesDesc,
133 weights, bias, input,
134 biasEnabled, transposeWeights
137 result.outputExpected = MakeTensor<float, 2>(outputTensorInfo, std::vector<float>(
139 0.5f + 1.0f + 1.5f + 2.0f + 2.5f + biasValues[0],
140 2.0f + 4.0f + 6.0f + 8.0f + 10.f + biasValues[1],
141 0.5f + 2.0f + 6.0f + 12.f + 20.f + biasValues[2],
143 2.5f + 2.0f + 1.5f + 1.0f + 0.5f + biasValues[0],
144 10.0f + 8.0f + 6.0f + 4.0f + 2.f + biasValues[1],
145 2.5f + 4.0f + 6.0f + 6.f + 4.f + biasValues[2]
152 LayerTestResult<uint8_t, 2> FullyConnectedUint8Test(
153 armnn::IWorkloadFactory& workloadFactory,
154 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
157 constexpr static unsigned int inputWidth = 3u;
158 constexpr static unsigned int inputHeight = 2u;
159 constexpr static unsigned int inputChannels = 1u;
161 constexpr static unsigned int inputSize = inputWidth * inputHeight * inputChannels;
163 constexpr static unsigned int outputChannels = 2u;
165 armnn::TensorInfo inputTensorInfo({ 1, inputChannels, inputHeight, inputWidth }, armnn::DataType::QuantisedAsymm8);
166 inputTensorInfo.SetQuantizationScale(0.1f);
167 inputTensorInfo.SetQuantizationOffset(63);
169 armnn::TensorInfo outputTensorInfo({ 1, outputChannels }, armnn::DataType::QuantisedAsymm8);
170 outputTensorInfo.SetQuantizationScale(5.f);
171 outputTensorInfo.SetQuantizationOffset(biasEnabled ? -50 : 10);
173 armnn::TensorInfo weightsDesc({ outputChannels, inputSize }, armnn::DataType::QuantisedAsymm8);
174 weightsDesc.SetQuantizationScale(0.2f);
175 weightsDesc.SetQuantizationOffset(93);
177 armnn::TensorInfo biasesDesc({ outputChannels }, armnn::DataType::Signed32);
178 biasesDesc.SetQuantizationScale(inputTensorInfo.GetQuantizationScale() * weightsDesc.GetQuantizationScale());
179 biasesDesc.SetQuantizationOffset(0);
181 LayerTestResult<uint8_t, 2> result(outputTensorInfo);
183 auto input = MakeTensor<uint8_t, 4>(inputTensorInfo, std::vector<uint8_t>{51, 124, 28,
186 auto weights = MakeTensor<uint8_t, 2>(weightsDesc, std::vector<uint8_t>{51, 193, 42, 53, 175, 34,
187 210, 145, 23, 74, 34, 150});
191 auto bias = MakeTensor<int32_t, 1>(biasesDesc, std::vector<int32_t>{9250, 67500});
193 result = SimpleFullyConnectedTestImpl<uint8_t>(
196 inputTensorInfo, outputTensorInfo,
197 weightsDesc, biasesDesc,
198 weights, bias, input,
202 // Manually calculated.
203 // Note one of these values has been clamped to 0.
206 result.outputExpected = MakeTensor<uint8_t, 2>(outputTensorInfo, std::vector<uint8_t>{0, 242});
210 result.outputExpected = MakeTensor<uint8_t, 2>(outputTensorInfo, std::vector<uint8_t>{0, 32});
219 // ArmNN variant of the AndroidNN fully_connected_float_large test.
221 // Tests the fully connected layer with large values, optionally transposing weights.
222 // Note this is templated for consistency, but the nature of this tests makes it unlikely to be useful in Uint8 mode.
224 template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
225 LayerTestResult<T, 2> FullyConnectedLargeTestCommon(
226 armnn::IWorkloadFactory& workloadFactory,
227 const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
228 bool transposeWeights,
232 unsigned int inputWidth = 1;
233 unsigned int inputHeight = 1;
234 unsigned int inputChannels = 5;
235 unsigned int inputNum = 1;
237 unsigned int outputChannels = 1;
238 unsigned int outputNum = 1;
240 // Define the tensor descriptors.
241 armnn::TensorInfo inputTensorInfo;
242 armnn::TensorInfo outputTensorInfo;
243 armnn::TensorInfo weightsDesc;
244 armnn::TensorInfo biasesDesc;
246 unsigned int inputShape[] = { inputNum, inputChannels, inputHeight, inputWidth };
247 unsigned int outputShape[] = { outputNum, outputChannels };
248 unsigned int weightsShape[] = { inputChannels, outputChannels };
249 if (transposeWeights)
251 std::swap(weightsShape[0], weightsShape[1]);
254 unsigned int biasShape[] = { outputChannels };
256 inputTensorInfo = armnn::TensorInfo(4, inputShape, ArmnnType);
257 outputTensorInfo = armnn::TensorInfo(2, outputShape, ArmnnType);
258 weightsDesc = armnn::TensorInfo(2, weightsShape, ArmnnType);
259 biasesDesc = armnn::TensorInfo(1, biasShape, ArmnnType);
261 // Set quantization parameters if the requested type is a quantized type.
262 if(armnn::IsQuantizedType<T>())
264 inputTensorInfo.SetQuantizationScale(qScale);
265 inputTensorInfo.SetQuantizationOffset(qOffset);
266 outputTensorInfo.SetQuantizationScale(qScale);
267 outputTensorInfo.SetQuantizationOffset(qOffset);
270 LayerTestResult<T, 2> result(outputTensorInfo);
272 boost::multi_array<T, 4> input = MakeTensor<T, 4>(inputTensorInfo,
273 QuantizedVector<T>(qScale, qOffset, {
274 1.0f, 10.0f, 100.0f, 1000.0f, 10000.0f,
278 boost::multi_array<T, 2> weights = MakeTensor<T, 2>(weightsDesc,
279 QuantizedVector<T>(qScale, qOffset, {
280 2.0f, 3.0f, 4.0f, 5.0f, 6.0f
284 std::vector<T> biasValues({900000.f});
285 boost::multi_array<T, 1> bias = MakeTensor<T, 1>(biasesDesc, biasValues);
287 result = SimpleFullyConnectedTestImpl<T>(
290 inputTensorInfo, outputTensorInfo,
291 weightsDesc, biasesDesc,
292 weights, bias, input,
293 true, transposeWeights
296 result.outputExpected = MakeTensor<T, 2>(outputTensorInfo,
297 QuantizedVector<T>(qScale, qOffset, {