ArmNN
 22.08
UtilsTests.cpp File Reference
#include <doctest/doctest.h>
#include <armnn/BackendHelper.hpp>
#include <armnn/Utils.hpp>
#include <armnn/Types.hpp>
#include <armnn/TypesUtils.hpp>
#include <armnn/Descriptors.hpp>
#include <armnnUtils/Permute.hpp>
#include <GraphTopologicalSort.hpp>
#include <Graph.hpp>
#include <ResolveType.hpp>

Go to the source code of this file.

Functions

 TEST_SUITE ("Utils")
 

Function Documentation

◆ TEST_SUITE()

TEST_SUITE ( "Utils"  )

Definition at line 18 of file UtilsTests.cpp.

References PermutationVector::begin(), armnn::BFloat16, armnn::Boolean, armnn::Float16, armnn::Float32, BFloat16::Float32ToBFloat16(), armnn::GetDataTypeName(), armnn::GetDataTypeSize(), armnn::GetILayerSupportByBackendId(), BFloat16::Inf(), BFloat16::Nan(), armnnUtils::Permuted(), armnn::QAsymmU8, armnn::Signed32, BFloat16::ToFloat32(), and BFloat16::Val().

19 {
20 TEST_CASE("DataTypeSize")
21 {
26 }
27 
28 TEST_CASE("PermuteDescriptorWithTooManyMappings")
29 {
30  CHECK_THROWS_AS(armnn::PermuteDescriptor({ 0u, 1u, 2u, 3u, 4u, 5u }), armnn::InvalidArgumentException);
31 }
32 
33 TEST_CASE("PermuteDescriptorWithInvalidMappings1d")
34 {
36 }
37 
38 TEST_CASE("PermuteDescriptorWithInvalidMappings2d")
39 {
40  CHECK_THROWS_AS(armnn::PermuteDescriptor({ 2u, 0u }), armnn::InvalidArgumentException);
41 }
42 
43 TEST_CASE("PermuteDescriptorWithInvalidMappings3d")
44 {
45  CHECK_THROWS_AS(armnn::PermuteDescriptor({ 0u, 3u, 1u }), armnn::InvalidArgumentException);
46 }
47 
48 TEST_CASE("PermuteDescriptorWithInvalidMappings4d")
49 {
50  CHECK_THROWS_AS(armnn::PermuteDescriptor({ 0u, 1u, 2u, 4u }), armnn::InvalidArgumentException);
51 }
52 
53 TEST_CASE("PermuteDescriptorWithInvalidMappings5d")
54 {
55  CHECK_THROWS_AS(armnn::PermuteDescriptor({ 0u, 1u, 2u, 3u, 5u }), armnn::InvalidArgumentException);
56 }
57 
58 TEST_CASE("PermuteDescriptorWithDuplicatedMappings")
59 {
60  CHECK_THROWS_AS(armnn::PermuteDescriptor({ 1u, 1u, 0u }), armnn::InvalidArgumentException);
61 }
62 
63 TEST_CASE("HalfType")
64 {
65  using namespace half_float::literal;
66  armnn::Half a = 1.0_h;
67 
68  float b = 1.0f;
69  armnn::Half c(b);
70 
71  // Test half type
72  CHECK_EQ(a, b);
73  CHECK_EQ(sizeof(c), 2);
74 
75  // Test half type is floating point type
76  CHECK(std::is_floating_point<armnn::Half>::value);
77 
78  // Test utility function returns correct type.
80  constexpr bool isHalfType = std::is_same<armnn::Half, ResolvedType>::value;
81  CHECK(isHalfType);
82 
83  //Test utility functions return correct size
85 
86  //Test utility functions return correct name
87  CHECK((GetDataTypeName(armnn::DataType::Float16) == std::string("Float16")));
88 }
89 
90 TEST_CASE("BFloatType")
91 {
92  uint16_t v = 16256;
93  armnn::BFloat16 a(v);
94  armnn::BFloat16 b(1.0f);
95  armnn::BFloat16 zero;
96 
97  // Test BFloat16 type
98  CHECK_EQ(sizeof(a), 2);
99  CHECK_EQ(a, b);
100  CHECK_EQ(a.Val(), v);
101  CHECK_EQ(a, 1.0f);
102  CHECK_EQ(zero, 0.0f);
103 
104  // Infinity
105  float infFloat = std::numeric_limits<float>::infinity();
106  armnn::BFloat16 infBF(infFloat);
107  CHECK_EQ(infBF, armnn::BFloat16::Inf());
108 
109  // NaN
110  float nan = std::numeric_limits<float>::quiet_NaN();
111  armnn::BFloat16 nanBF(nan);
112  CHECK_EQ(nanBF, armnn::BFloat16::Nan());
113 
114  // Test utility function returns correct type.
116  constexpr bool isBFloat16Type = std::is_same<armnn::BFloat16, ResolvedType>::value;
117  CHECK(isBFloat16Type);
118 
119  //Test utility functions return correct size
121 
122  //Test utility functions return correct name
123  CHECK((GetDataTypeName(armnn::DataType::BFloat16) == std::string("BFloat16")));
124 }
125 
126 TEST_CASE("Float32ToBFloat16Test")
127 {
128  // LSB = 0, R = 0 -> round down
129  armnn::BFloat16 roundDown0 = armnn::BFloat16::Float32ToBFloat16(1.704735E38f); // 0x7F004000
130  CHECK_EQ(roundDown0.Val(), 0x7F00);
131  // LSB = 1, R = 0 -> round down
132  armnn::BFloat16 roundDown1 = armnn::BFloat16::Float32ToBFloat16(9.18355E-41f); // 0x00010000
133  CHECK_EQ(roundDown1.Val(), 0x0001);
134  // LSB = 0, R = 1 all 0 -> round down
135  armnn::BFloat16 roundDown2 = armnn::BFloat16::Float32ToBFloat16(1.14794E-40f); // 0x00014000
136  CHECK_EQ(roundDown2.Val(), 0x0001);
137  // LSB = 1, R = 1 -> round up
138  armnn::BFloat16 roundUp = armnn::BFloat16::Float32ToBFloat16(-2.0234377f); // 0xC0018001
139  CHECK_EQ(roundUp.Val(), 0xC002);
140  // LSB = 0, R = 1 -> round up
141  armnn::BFloat16 roundUp1 = armnn::BFloat16::Float32ToBFloat16(4.843037E-35f); // 0x0680C000
142  CHECK_EQ(roundUp1.Val(), 0x0681);
143  // Max positive value -> infinity
144  armnn::BFloat16 maxPositive = armnn::BFloat16::Float32ToBFloat16(std::numeric_limits<float>::max()); // 0x7F7FFFFF
145  CHECK_EQ(maxPositive, armnn::BFloat16::Inf());
146  // Max negative value -> -infinity
147  armnn::BFloat16 maxNeg = armnn::BFloat16::Float32ToBFloat16(std::numeric_limits<float>::lowest()); // 0xFF7FFFFF
148  CHECK_EQ(maxNeg.Val(), 0xFF80);
149  // Min positive value
150  armnn::BFloat16 minPositive = armnn::BFloat16::Float32ToBFloat16(1.1754942E-38f); // 0x007FFFFF
151  CHECK_EQ(minPositive.Val(), 0x0080);
152  // Min negative value
153  armnn::BFloat16 minNeg = armnn::BFloat16::Float32ToBFloat16(-1.1754942E-38f); // 0x807FFFFF
154  CHECK_EQ(minNeg.Val(), 0x8080);
155 }
156 
157 TEST_CASE("BFloat16ToFloat32Test")
158 {
159  armnn::BFloat16 bf0(1.5f);
160  CHECK_EQ(bf0.ToFloat32(), 1.5f);
161  armnn::BFloat16 bf1(-5.525308E-25f);
162  CHECK_EQ(bf1.ToFloat32(), -5.525308E-25f);
163  armnn::BFloat16 bf2(-2.0625f);
164  CHECK_EQ(bf2.ToFloat32(), -2.0625f);
165  uint16_t v = 32639;
166  armnn::BFloat16 bf3(v);
167  CHECK_EQ(bf3.ToFloat32(), 3.3895314E38f);
168  // Infinity
169  CHECK_EQ(armnn::BFloat16::Inf().ToFloat32(), std::numeric_limits<float>::infinity());
170  // NaN
171  CHECK(std::isnan(armnn::BFloat16::Nan().ToFloat32()));
172 }
173 
174 TEST_CASE("GraphTopologicalSortSimpleTest")
175 {
176  std::map<int, std::vector<int>> graph;
177 
178  graph[0] = {2};
179  graph[1] = {3};
180  graph[2] = {4};
181  graph[3] = {4};
182  graph[4] = {5};
183  graph[5] = {};
184 
185  auto getNodeInputs = [graph](int node) -> std::vector<int>
186  {
187  return graph.find(node)->second;
188  };
189 
190  std::vector<int> targetNodes = {0, 1};
191 
192  std::vector<int> output;
193  bool sortCompleted = armnnUtils::GraphTopologicalSort<int>(targetNodes, getNodeInputs, output);
194 
195  CHECK(sortCompleted);
196 
197  std::vector<int> correctResult = {5, 4, 2, 0, 3, 1};
198  CHECK(std::equal(output.begin(), output.end(), correctResult.begin(), correctResult.end()));
199 }
200 
201 TEST_CASE("GraphTopologicalSortVariantTest")
202 {
203  std::map<int, std::vector<int>> graph;
204 
205  graph[0] = {2};
206  graph[1] = {2};
207  graph[2] = {3, 4};
208  graph[3] = {5};
209  graph[4] = {5};
210  graph[5] = {6};
211  graph[6] = {};
212 
213  auto getNodeInputs = [graph](int node) -> std::vector<int>
214  {
215  return graph.find(node)->second;
216  };
217 
218  std::vector<int> targetNodes = {0, 1};
219 
220  std::vector<int> output;
221  bool sortCompleted = armnnUtils::GraphTopologicalSort<int>(targetNodes, getNodeInputs, output);
222 
223  CHECK(sortCompleted);
224 
225  std::vector<int> correctResult = {6, 5, 3, 4, 2, 0, 1};
226  CHECK(std::equal(output.begin(), output.end(), correctResult.begin(), correctResult.end()));
227 }
228 
229 TEST_CASE("CyclicalGraphTopologicalSortTest")
230 {
231  std::map<int, std::vector<int>> graph;
232 
233  graph[0] = {1};
234  graph[1] = {2};
235  graph[2] = {0};
236 
237  auto getNodeInputs = [graph](int node) -> std::vector<int>
238  {
239  return graph.find(node)->second;
240  };
241 
242  std::vector<int> targetNodes = {0};
243 
244  std::vector<int> output;
245  bool sortCompleted = armnnUtils::GraphTopologicalSort<int>(targetNodes, getNodeInputs, output);
246 
247  CHECK(!sortCompleted);
248 }
249 
250 TEST_CASE("PermuteQuantizationDim")
251 {
252  std::vector<float> scales {1.0f, 1.0f};
253 
254  // Set QuantizationDim to be index 1
255  const armnn::TensorInfo perChannelInfo({ 1, 2, 3, 4 }, armnn::DataType::Float32, scales, 1U);
256  CHECK(perChannelInfo.GetQuantizationDim().value() == 1U);
257 
258  // Permute so that index 1 moves to final index i.e. index 3
259  armnn::PermutationVector mappings({ 0, 3, 2, 1 });
260  auto permutedPerChannel = armnnUtils::Permuted(perChannelInfo, mappings);
261 
262  // Check that QuantizationDim is in index 3
263  CHECK(permutedPerChannel.GetQuantizationDim().value() == 3U);
264 
265  // Even if there is only a single scale the quantization dim still exists and needs to be permuted
266  std::vector<float> scale {1.0f};
267  const armnn::TensorInfo perChannelInfo1({ 1, 2, 3, 4 }, armnn::DataType::Float32, scale, 1U);
268  auto permuted = armnnUtils::Permuted(perChannelInfo1, mappings);
269  CHECK(permuted.GetQuantizationDim().value() == 3U);
270 }
271 
272 TEST_CASE("EmptyPermuteVectorIndexOutOfBounds")
273 {
275  CHECK_THROWS_AS(pv[0], armnn::InvalidArgumentException);
276 }
277 
278 TEST_CASE("PermuteDescriptorIndexOutOfBounds")
279 {
282  CHECK_THROWS_AS(desc.m_DimMappings[3], armnn::InvalidArgumentException);
283  CHECK(desc.m_DimMappings[0] == 1u);
284 }
285 
286 TEST_CASE("TransposeDescriptorIndexOutOfBounds")
287 {
290  CHECK_THROWS_AS(desc.m_DimMappings[3], armnn::InvalidArgumentException);
291  CHECK(desc.m_DimMappings[2] == 0u);
292 }
293 
294 TEST_CASE("PermuteVectorIterator")
295 {
296  // We're slightly breaking the spirit of std::array.end() because we're using it as a
297  // variable length rather than fixed length. This test is to use a couple of iterators and
298  // make sure it still mostly makes sense.
299 
300  // Create zero length.
301  armnn::PermutationVector zeroPVector({});
302  // Begin should be equal to end.
303  CHECK(zeroPVector.begin() == zeroPVector.end());
304 
305  // Create length 4. Summing the 4 values should be 6.
306  armnn::PermutationVector fourPVector({ 0, 3, 2, 1 });
307  unsigned int sum = 0;
308  for (unsigned int it : fourPVector)
309  {
310  sum += it;
311  }
312  CHECK(sum == 6);
313  // Directly use begin and end, make sure there are 4 iterations.
314  unsigned int iterations = 0;
315  auto itr = fourPVector.begin();
316  while(itr != fourPVector.end())
317  {
318  ++iterations;
319  itr++;
320  }
321  CHECK(iterations == 4);
322 
323  // Do the same with 2 elements.
324  armnn::PermutationVector twoPVector({ 0, 1 });
325  iterations = 0;
326  itr = twoPVector.begin();
327  while(itr != twoPVector.end())
328  {
329  ++iterations;
330  itr++;
331  }
332  CHECK(iterations == 2);
333 }
334 
335 #if defined(ARMNNREF_ENABLED)
336 TEST_CASE("LayerSupportHandle")
337 {
338  auto layerSupportObject = armnn::GetILayerSupportByBackendId("CpuRef");
339  armnn::TensorInfo input;
340  std::string reasonIfUnsupported;
341  // InputLayer always supported for CpuRef
342  CHECK_EQ(layerSupportObject.IsInputSupported(input, reasonIfUnsupported), true);
343 
344  CHECK(layerSupportObject.IsBackendRegistered());
345 }
346 #endif
347 
348 }
typename ResolveTypeImpl< DT >::Type ResolveType
Definition: ResolveType.hpp:79
static BFloat16 Inf()
Definition: BFloat16.hpp:112
constexpr const char * GetDataTypeName(DataType dataType)
Definition: TypesUtils.hpp:202
uint16_t Val() const
Definition: BFloat16.hpp:95
static BFloat16 Float32ToBFloat16(const float v)
Definition: BFloat16.hpp:51
ConstIterator begin() const
Definition: Types.hpp:340
A TransposeDescriptor for the TransposeLayer.
static BFloat16 Nan()
Definition: BFloat16.hpp:106
LayerSupportHandle GetILayerSupportByBackendId(const armnn::BackendId &backend)
Convenience function to retrieve the ILayerSupportHandle for a backend.
half_float::half Half
Definition: Half.hpp:18
armnn::TensorShape Permuted(const armnn::TensorShape &srcShape, const armnn::PermutationVector &mappings)
Definition: Permute.cpp:98
constexpr unsigned int GetDataTypeSize(DataType dataType)
Definition: TypesUtils.hpp:151
A PermuteDescriptor for the PermuteLayer.