ArmNN
 21.02
UtilsTests.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #include <boost/test/unit_test.hpp>
6 
7 
9 #include <armnn/Utils.hpp>
10 #include <armnn/Types.hpp>
11 #include <armnn/TypesUtils.hpp>
12 #include <armnn/Descriptors.hpp>
13 #include <armnnUtils/Permute.hpp>
14 #include <GraphTopologicalSort.hpp>
15 #include <Graph.hpp>
16 #include <ResolveType.hpp>
17 
19 
20 BOOST_AUTO_TEST_CASE(DataTypeSize)
21 {
26 }
27 
28 BOOST_AUTO_TEST_CASE(PermuteDescriptorWithTooManyMappings)
29 {
30  BOOST_CHECK_THROW(armnn::PermuteDescriptor({ 0u, 1u, 2u, 3u, 4u, 5u }), armnn::InvalidArgumentException);
31 }
32 
33 BOOST_AUTO_TEST_CASE(PermuteDescriptorWithInvalidMappings1d)
34 {
36 }
37 
38 BOOST_AUTO_TEST_CASE(PermuteDescriptorWithInvalidMappings2d)
39 {
40  BOOST_CHECK_THROW(armnn::PermuteDescriptor({ 2u, 0u }), armnn::InvalidArgumentException);
41 }
42 
43 BOOST_AUTO_TEST_CASE(PermuteDescriptorWithInvalidMappings3d)
44 {
45  BOOST_CHECK_THROW(armnn::PermuteDescriptor({ 0u, 3u, 1u }), armnn::InvalidArgumentException);
46 }
47 
48 BOOST_AUTO_TEST_CASE(PermuteDescriptorWithInvalidMappings4d)
49 {
50  BOOST_CHECK_THROW(armnn::PermuteDescriptor({ 0u, 1u, 2u, 4u }), armnn::InvalidArgumentException);
51 }
52 
53 BOOST_AUTO_TEST_CASE(PermuteDescriptorWithInvalidMappings5d)
54 {
55  BOOST_CHECK_THROW(armnn::PermuteDescriptor({ 0u, 1u, 2u, 3u, 5u }), armnn::InvalidArgumentException);
56 }
57 
58 BOOST_AUTO_TEST_CASE(PermuteDescriptorWithDuplicatedMappings)
59 {
60  BOOST_CHECK_THROW(armnn::PermuteDescriptor({ 1u, 1u, 0u }), armnn::InvalidArgumentException);
61 }
62 
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  BOOST_CHECK_EQUAL(a, b);
73  BOOST_CHECK_EQUAL(sizeof(c), 2);
74 
75  // Test half type is floating point type
76  BOOST_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  BOOST_CHECK(isHalfType);
82 
83  //Test utility functions return correct size
84  BOOST_CHECK(GetDataTypeSize(armnn::DataType::Float16) == 2);
85 
86  //Test utility functions return correct name
87  BOOST_CHECK((GetDataTypeName(armnn::DataType::Float16) == std::string("Float16")));
88 }
89 
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  BOOST_CHECK_EQUAL(sizeof(a), 2);
99  BOOST_CHECK_EQUAL(a, b);
100  BOOST_CHECK_EQUAL(a.Val(), v);
101  BOOST_CHECK_EQUAL(a, 1.0f);
102  BOOST_CHECK_EQUAL(zero, 0.0f);
103 
104  // Infinity
105  float infFloat = std::numeric_limits<float>::infinity();
106  armnn::BFloat16 infBF(infFloat);
107  BOOST_CHECK_EQUAL(infBF, armnn::BFloat16::Inf());
108 
109  // NaN
110  float nan = std::numeric_limits<float>::quiet_NaN();
111  armnn::BFloat16 nanBF(nan);
112  BOOST_CHECK_EQUAL(nanBF, armnn::BFloat16::Nan());
113 
114  // Test utility function returns correct type.
116  constexpr bool isBFloat16Type = std::is_same<armnn::BFloat16, ResolvedType>::value;
117  BOOST_CHECK(isBFloat16Type);
118 
119  //Test utility functions return correct size
120  BOOST_CHECK(GetDataTypeSize(armnn::DataType::BFloat16) == 2);
121 
122  //Test utility functions return correct name
123  BOOST_CHECK((GetDataTypeName(armnn::DataType::BFloat16) == std::string("BFloat16")));
124 }
125 
126 BOOST_AUTO_TEST_CASE(Float32ToBFloat16Test)
127 {
128  // LSB = 0, R = 0 -> round down
129  armnn::BFloat16 roundDown0 = armnn::BFloat16::Float32ToBFloat16(1.704735E38f); // 0x7F004000
130  BOOST_CHECK_EQUAL(roundDown0.Val(), 0x7F00);
131  // LSB = 1, R = 0 -> round down
132  armnn::BFloat16 roundDown1 = armnn::BFloat16::Float32ToBFloat16(9.18355E-41f); // 0x00010000
133  BOOST_CHECK_EQUAL(roundDown1.Val(), 0x0001);
134  // LSB = 0, R = 1 all 0 -> round down
135  armnn::BFloat16 roundDown2 = armnn::BFloat16::Float32ToBFloat16(1.14794E-40f); // 0x00014000
136  BOOST_CHECK_EQUAL(roundDown2.Val(), 0x0001);
137  // LSB = 1, R = 1 -> round up
138  armnn::BFloat16 roundUp = armnn::BFloat16::Float32ToBFloat16(-2.0234377f); // 0xC0018001
139  BOOST_CHECK_EQUAL(roundUp.Val(), 0xC002);
140  // LSB = 0, R = 1 -> round up
141  armnn::BFloat16 roundUp1 = armnn::BFloat16::Float32ToBFloat16(4.843037E-35f); // 0x0680C000
142  BOOST_CHECK_EQUAL(roundUp1.Val(), 0x0681);
143  // Max positive value -> infinity
144  armnn::BFloat16 maxPositive = armnn::BFloat16::Float32ToBFloat16(std::numeric_limits<float>::max()); // 0x7F7FFFFF
145  BOOST_CHECK_EQUAL(maxPositive, armnn::BFloat16::Inf());
146  // Max negative value -> -infinity
147  armnn::BFloat16 maxNeg = armnn::BFloat16::Float32ToBFloat16(std::numeric_limits<float>::lowest()); // 0xFF7FFFFF
148  BOOST_CHECK_EQUAL(maxNeg.Val(), 0xFF80);
149  // Min positive value
150  armnn::BFloat16 minPositive = armnn::BFloat16::Float32ToBFloat16(1.1754942E-38f); // 0x007FFFFF
151  BOOST_CHECK_EQUAL(minPositive.Val(), 0x0080);
152  // Min negative value
153  armnn::BFloat16 minNeg = armnn::BFloat16::Float32ToBFloat16(-1.1754942E-38f); // 0x807FFFFF
154  BOOST_CHECK_EQUAL(minNeg.Val(), 0x8080);
155 }
156 
157 BOOST_AUTO_TEST_CASE(BFloat16ToFloat32Test)
158 {
159  armnn::BFloat16 bf0(1.5f);
160  BOOST_CHECK_EQUAL(bf0.ToFloat32(), 1.5f);
161  armnn::BFloat16 bf1(-5.525308E-25f);
162  BOOST_CHECK_EQUAL(bf1.ToFloat32(), -5.525308E-25f);
163  armnn::BFloat16 bf2(-2.0625f);
164  BOOST_CHECK_EQUAL(bf2.ToFloat32(), -2.0625f);
165  uint16_t v = 32639;
166  armnn::BFloat16 bf3(v);
167  BOOST_CHECK_EQUAL(bf3.ToFloat32(), 3.3895314E38f);
168  // Infinity
169  BOOST_CHECK_EQUAL(armnn::BFloat16::Inf().ToFloat32(), std::numeric_limits<float>::infinity());
170  // NaN
171  BOOST_CHECK(std::isnan(armnn::BFloat16::Nan().ToFloat32()));
172 }
173 
174 BOOST_AUTO_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  BOOST_TEST(sortCompleted);
196 
197  std::vector<int> correctResult = {5, 4, 2, 0, 3, 1};
198  BOOST_CHECK_EQUAL_COLLECTIONS(output.begin(), output.end(), correctResult.begin(), correctResult.end());
199 }
200 
201 BOOST_AUTO_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  BOOST_TEST(sortCompleted);
224 
225  std::vector<int> correctResult = {6, 5, 3, 4, 2, 0, 1};
226  BOOST_CHECK_EQUAL_COLLECTIONS(output.begin(), output.end(), correctResult.begin(), correctResult.end());
227 }
228 
229 BOOST_AUTO_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  BOOST_TEST(!sortCompleted);
248 }
249 
250 BOOST_AUTO_TEST_CASE(PermuteQuantizationDim)
251 {
252  std::vector<float> scales;
253 
254  // Set QuantizationDim to be index 1
255  const armnn::TensorInfo info({ 1, 2, 3, 4 }, armnn::DataType::Float32, scales, 1U);
256  BOOST_CHECK(info.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(info, mappings, true);
261  auto permuted = armnnUtils::Permuted(info, mappings);
262 
263  // Check that QuantizationDim is in index 3
264  BOOST_CHECK(permutedPerChannel.GetQuantizationDim().value() == 3U);
265 
266  // Check previous implementation unchanged
267  BOOST_CHECK(permuted.GetQuantizationDim().value() == 1U);
268 }
269 
270 #if defined(ARMNNREF_ENABLED)
271 BOOST_AUTO_TEST_CASE(LayerSupportHandle)
272 {
273  auto layerSupportObject = armnn::GetILayerSupportByBackendId("CpuRef");
274  armnn::TensorInfo input;
275  std::string reasonIfUnsupported;
276  // InputLayer always supported for CpuRef
277  BOOST_CHECK_EQUAL(layerSupportObject.IsInputSupported(input, reasonIfUnsupported), true);
278 
279  BOOST_CHECK(layerSupportObject.IsBackendRegistered());
280 }
281 #endif
282 
BOOST_AUTO_TEST_SUITE(TensorflowLiteParser)
float ToFloat32() const
Definition: BFloat16.hpp:85
typename ResolveTypeImpl< DT >::Type ResolveType
Definition: ResolveType.hpp:73
static BFloat16 Inf()
Definition: BFloat16.hpp:109
constexpr const char * GetDataTypeName(DataType dataType)
Definition: TypesUtils.hpp:180
BOOST_AUTO_TEST_CASE(DataTypeSize)
Definition: UtilsTests.cpp:20
uint16_t Val() const
Definition: BFloat16.hpp:92
static BFloat16 Float32ToBFloat16(const float v)
Definition: BFloat16.hpp:50
BOOST_AUTO_TEST_SUITE_END()
static BFloat16 Nan()
Definition: BFloat16.hpp:103
LayerSupportHandle GetILayerSupportByBackendId(const armnn::BackendId &backend)
Convenience function to retrieve the ILayerSupportHandle for a backend.
half_float::half Half
Definition: Half.hpp:16
armnn::TensorShape Permuted(const armnn::TensorShape &srcShape, const armnn::PermutationVector &mappings)
Definition: Permute.cpp:98
constexpr unsigned int GetDataTypeSize(DataType dataType)
Definition: TypesUtils.hpp:126
A PermuteDescriptor for the PermuteLayer.