ArmNN
 20.11
FullyConnectedTestImpl.hpp File Reference

Go to the source code of this file.

Functions

template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>>
LayerTestResult< T, 2 > FullyConnectedTest (armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager, const armnn::ITensorHandleFactory &tensorHandleFactory, bool biasEnabled)
 
LayerTestResult< float, 2 > FullyConnectedFloat32Test (armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager, const armnn::ITensorHandleFactory &tensorHandleFactory, bool biasEnabled, bool transposeWeights)
 
LayerTestResult< float, 2 > FullyConnectedLargeTest (armnn::IWorkloadFactory &workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr &memoryManager, const armnn::ITensorHandleFactory &tensorHandleFactory, bool transposeWeights)
 

Function Documentation

◆ FullyConnectedFloat32Test()

LayerTestResult<float, 2> FullyConnectedFloat32Test ( armnn::IWorkloadFactory workloadFactory,
const armnn::IBackendInternal::IMemoryManagerSharedPtr memoryManager,
const armnn::ITensorHandleFactory tensorHandleFactory,
bool  biasEnabled,
bool  transposeWeights 
)

Definition at line 253 of file FullyConnectedTestImpl.cpp.

References armnn::Float32, LayerTestResult< T, n >::outputExpected, and armnn::swap().

259 {
260  unsigned int inputWidth = 1;
261  unsigned int inputHeight = 1;
262  unsigned int inputChannels = 5;
263  unsigned int inputNum = 2;
264 
265  unsigned int outputChannels = 3;
266  unsigned int outputNum = 2;
267 
268  // Define the tensor descriptors.
269  armnn::TensorInfo inputTensorInfo;
270  armnn::TensorInfo outputTensorInfo;
271  armnn::TensorInfo weightsDesc;
272  armnn::TensorInfo biasesDesc;
273 
274  unsigned int inputShape[] = { inputNum, inputChannels, inputHeight, inputWidth };
275  unsigned int outputShape[] = { outputNum, outputChannels };
276  unsigned int weightsShape[] = { inputChannels, outputChannels };
277 
278  if (transposeWeights)
279  {
280  std::swap(weightsShape[0], weightsShape[1]);
281  }
282 
283  unsigned int biasShape[] = { outputChannels };
284 
285  inputTensorInfo = armnn::TensorInfo(4, inputShape, armnn::DataType::Float32);
286  outputTensorInfo = armnn::TensorInfo(2, outputShape, armnn::DataType::Float32);
287  weightsDesc = armnn::TensorInfo(2, weightsShape, armnn::DataType::Float32);
288  biasesDesc = armnn::TensorInfo(1, biasShape, armnn::DataType::Float32);
289 
290  LayerTestResult<float, 2> result(outputTensorInfo);
291 
292  boost::multi_array<float, 4> input = MakeTensor<float, 4>(inputTensorInfo, std::vector<float>(
293  {
294  1.0f, 2.0f, 3.0f, 4.0f, 5.0f,
295 
296  5.0f, 4.0f, 3.0f, 2.0f, 1.0f
297  })
298  );
299 
300  boost::multi_array<float, 2> weights = MakeTensor<float, 2>(weightsDesc, std::vector<float>(
301  {
302  .5f, 2.f, .5f,
303  .5f, 2.f, 1.f,
304  .5f, 2.f, 2.f,
305  .5f, 2.f, 3.f,
306  .5f, 2.f, 4.f
307  }));
308 
309  if (transposeWeights)
310  {
311  weights = MakeTensor<float, 2>(weightsDesc, std::vector<float>(
312  {
313  .5f, .5f, .5f, .5f, .5f,
314  2.f, 2.f, 2.f, 2.f, 2.f,
315  .5f, 1.f, 2.f, 3.f, 4.f
316  }));
317  }
318 
319 
320  std::vector<float> biasValues({0.f, 0.f, 0.f});
321  if (biasEnabled)
322  {
323  biasValues = std::vector<float>({10.f, 20.f, 30.f});
324  }
325  boost::multi_array<float, 1> bias = MakeTensor<float, 1>(biasesDesc, biasValues);
326 
327  result = SimpleFullyConnectedTestImpl<float>(
328  workloadFactory,
329  memoryManager,
330  tensorHandleFactory,
331  inputTensorInfo, outputTensorInfo,
332  weightsDesc, biasesDesc,
333  weights, bias, input,
334  biasEnabled, transposeWeights
335  );
336 
337  result.outputExpected = MakeTensor<float, 2>(outputTensorInfo, std::vector<float>(
338  {
339  0.5f + 1.0f + 1.5f + 2.0f + 2.5f + biasValues[0],
340  2.0f + 4.0f + 6.0f + 8.0f + 10.f + biasValues[1],
341  0.5f + 2.0f + 6.0f + 12.f + 20.f + biasValues[2],
342 
343  2.5f + 2.0f + 1.5f + 1.0f + 0.5f + biasValues[0],
344  10.0f + 8.0f + 6.0f + 4.0f + 2.f + biasValues[1],
345  2.5f + 4.0f + 6.0f + 6.f + 4.f + biasValues[2]
346  })
347  );
348 
349  return result;
350 }
void swap(OriginsDescriptor &first, OriginsDescriptor &second)

◆ FullyConnectedLargeTest()

LayerTestResult<float, 2> FullyConnectedLargeTest ( armnn::IWorkloadFactory workloadFactory,
const armnn::IBackendInternal::IMemoryManagerSharedPtr memoryManager,
const armnn::ITensorHandleFactory tensorHandleFactory,
bool  transposeWeights 
)

Definition at line 352 of file FullyConnectedTestImpl.cpp.

357 {
358  return FullyConnectedLargeTestCommon<armnn::DataType::Float32>(workloadFactory,
359  memoryManager,
360  tensorHandleFactory,
361  transposeWeights);
362 }

◆ FullyConnectedTest()

LayerTestResult<T, 2> FullyConnectedTest ( armnn::IWorkloadFactory workloadFactory,
const armnn::IBackendInternal::IMemoryManagerSharedPtr memoryManager,
const armnn::ITensorHandleFactory tensorHandleFactory,
bool  biasEnabled 
)

Definition at line 71 of file FullyConnectedTestImpl.cpp.

References armnn::GetBiasTypeFromWeightsType(), LayerTestResult< T, n >::outputExpected, TensorInfo::SetQuantizationScale(), and true.

76 {
77  constexpr static unsigned int inputWidth = 3u;
78  constexpr static unsigned int inputHeight = 2u;
79  constexpr static unsigned int inputChannels = 1u;
80 
81  constexpr static unsigned int inputSize = inputWidth * inputHeight * inputChannels;
82 
83  constexpr static unsigned int outputChannels = 2u;
84 
85  armnn::TensorInfo inputTensorInfo({ 1, inputChannels, inputHeight, inputWidth }, ArmnnType);
86  inputTensorInfo.SetQuantizationScale(0.1f);
87  inputTensorInfo.SetQuantizationOffset(63);
88 
89  armnn::TensorInfo outputTensorInfo({ 1, outputChannels }, ArmnnType);
90  outputTensorInfo.SetQuantizationScale(5.f);
91  outputTensorInfo.SetQuantizationOffset(biasEnabled ? -50 : 10);
92 
93  armnn::TensorInfo weightsDesc({ outputChannels, inputSize }, ArmnnType);
94  weightsDesc.SetQuantizationScale(0.2f);
95  weightsDesc.SetQuantizationOffset(93);
96 
97  armnn::TensorInfo biasesDesc({ outputChannels }, GetBiasTypeFromWeightsType(weightsDesc.GetDataType()).value());
98  biasesDesc.SetQuantizationScale(inputTensorInfo.GetQuantizationScale() * weightsDesc.GetQuantizationScale());
99  biasesDesc.SetQuantizationOffset(0);
100 
101  LayerTestResult<T, 2> result(outputTensorInfo);
102 
103  auto input = MakeTensor<T, 4>(inputTensorInfo, ConvertToDataType<ArmnnType>(
104  {
105  -1.2f, 6.1f, -3.5f,
106  18.8f, -5.5f, 2.9f
107  },
108  inputTensorInfo));
109 
110  auto weights = MakeTensor<T, 2>(weightsDesc, ConvertToDataType<ArmnnType>(
111  {
112  -8.4f, 20.0f, -10.4f, -8, 16.4f, -11.8f,
113  23.4f, 10.4f, -14.0f, -3.8f, -11.8f, 11.4f
114  },
115  weightsDesc));
116 
117  auto bias = MakeTensor<int32_t, 1>(biasesDesc, std::vector<int32_t>{9250, 67500});
118 
119  result = SimpleFullyConnectedTestImpl<T>(
120  workloadFactory,
121  memoryManager,
122  tensorHandleFactory,
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 }
armnn::Optional< armnn::DataType > GetBiasTypeFromWeightsType(armnn::Optional< armnn::DataType > weightsType)
void SetQuantizationScale(float scale)
Definition: Tensor.cpp:464
DataLayout::NCHW DataLayout::NCHW DataLayout::NHWC DataLayout::NHWC true