ArmNN
 20.05
Assert.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
8 #include "test/GraphUtils.hpp"
9 
11 
12 #include <boost/test/unit_test.hpp>
13 
14 BOOST_AUTO_TEST_SUITE(TensorflowParser)
15 
16 struct AssertSimpleFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser>
17 {
18  AssertSimpleFixture()
19  {
20  // Placeholder AssertInput
21  // | \ /
22  // Add ------ Assert
23 
24  m_Prototext = R"(
25  node {
26  name: "Placeholder"
27  op: "Placeholder"
28  attr {
29  key: "dtype"
30  value {
31  type: DT_FLOAT
32  }
33  }
34  attr {
35  key: "shape"
36  value {
37  shape {
38  unknown_rank: true
39  }
40  }
41  }
42  }
43  node {
44  name: "AssertInput"
45  op: "Const"
46  attr {
47  key: "dtype"
48  value {
49  type: DT_FLOAT
50  }
51  }
52  attr {
53  key: "value"
54  value {
55  tensor {
56  dtype: DT_FLOAT
57  tensor_shape {
58  dim {
59  size: 1
60  }
61  }
62  float_val: 17.0
63  }
64  }
65  }
66  }
67  node {
68  name: "Assert"
69  op: "Assert"
70  input: "Placeholder"
71  input: "AssertInput"
72  attr {
73  key: "T"
74  value {
75  type: DT_FLOAT
76  }
77  }
78  }
79  node {
80  name: "Add"
81  op: "Add"
82  input: "Placeholder"
83  input: "Placeholder"
84  input: "^Assert"
85  attr {
86  key: "T"
87  value {
88  type: DT_FLOAT
89  }
90  }
91  })";
92  }
93 };
94 
95 BOOST_FIXTURE_TEST_CASE(AssertSimpleTest, AssertSimpleFixture)
96 {
97  SetupSingleInputSingleOutput({ 1, 1, 1, 4 }, "Placeholder", "Add");
98  RunTest<4>({ 1.0f, 2.0f, 3.0f, 4.0f }, { 2.0f, 4.0f, 6.0f, 8.0f });
99 }
100 
101 BOOST_FIXTURE_TEST_CASE(AssertSimpleGraphStructureTest, AssertSimpleFixture)
102 {
103  auto optimized = SetupOptimizedNetwork({ { "Placeholder", { 1, 1, 1, 4 } } }, { "Add" });
104 
105  auto optimizedNetwork = armnn::PolymorphicDowncast<armnn::OptimizedNetwork*>(optimized.get());
106  auto graph = optimizedNetwork->GetGraph();
107 
108  BOOST_TEST((graph.GetNumInputs() == 1));
109  BOOST_TEST((graph.GetNumOutputs() == 1));
110  BOOST_TEST((graph.GetNumLayers() == 3));
111 
112  armnn::Layer* inputLayer = GetFirstLayerWithName(graph, "Placeholder");
113  BOOST_TEST((inputLayer->GetType() == armnn::LayerType::Input));
114  BOOST_TEST(CheckNumberOfInputSlot(inputLayer, 0));
115  BOOST_TEST(CheckNumberOfOutputSlot(inputLayer, 1));
116 
117  armnn::Layer* addLayer = GetFirstLayerWithName(graph, "Add");
118  BOOST_TEST((addLayer->GetType() == armnn::LayerType::Addition));
119  BOOST_TEST(CheckNumberOfInputSlot(addLayer, 2));
120  BOOST_TEST(CheckNumberOfOutputSlot(addLayer, 1));
121 
123  BOOST_TEST(IsConnected(inputLayer, addLayer, 0, 0, tensorInfo));
124  BOOST_TEST(IsConnected(inputLayer, addLayer, 0, 1, tensorInfo));
125 
126  for (auto&& outputLayer : graph.GetOutputLayers())
127  {
128  BOOST_TEST(IsConnected(addLayer, const_cast<armnn::OutputLayer*>(outputLayer), 0, 0, tensorInfo));
129  }
130 }
131 
132 struct AssertFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser>
133 {
134  AssertFixture()
135  {
136  // Input0 Input1 Input2
137  // | \ / |
138  // | Sub ------ Assert
139  // \ / /
140  // Output -------
141 
142  m_Prototext = R"(
143  node {
144  name: "Input0"
145  op: "Placeholder"
146  attr {
147  key: "dtype"
148  value {
149  type: DT_FLOAT
150  }
151  }
152  attr {
153  key: "shape"
154  value {
155  shape {
156  unknown_rank: true
157  }
158  }
159  }
160  }
161  node {
162  name: "Input1"
163  op: "Placeholder"
164  attr {
165  key: "dtype"
166  value {
167  type: DT_FLOAT
168  }
169  }
170  attr {
171  key: "shape"
172  value {
173  shape {
174  unknown_rank: true
175  }
176  }
177  }
178  }
179  node {
180  name: "Sub"
181  op: "Sub"
182  input: "Input0"
183  input: "Input1"
184  attr {
185  key: "T"
186  value {
187  type: DT_FLOAT
188  }
189  }
190  }
191  node {
192  name: "Input2"
193  op: "Placeholder"
194  attr {
195  key: "dtype"
196  value {
197  type: DT_FLOAT
198  }
199  }
200  attr {
201  key: "shape"
202  value {
203  shape {
204  unknown_rank: true
205  }
206  }
207  }
208  }
209  node {
210  name: "Assert"
211  op: "Assert"
212  input: "Input2"
213  input: "Sub"
214  attr {
215  key: "T"
216  value {
217  type: DT_FLOAT
218  }
219  }
220  }
221  node {
222  name: "Output"
223  op: "Add"
224  input: "Input0"
225  input: "Sub"
226  input: "^Assert"
227  attr {
228  key: "T"
229  value {
230  type: DT_FLOAT
231  }
232  }
233  })";
234 
235 
236  }
237 };
238 
239 BOOST_FIXTURE_TEST_CASE(AssertTest, AssertFixture)
240 {
241  Setup({ { "Input0", { 1, 1, 2, 2 } },
242  { "Input1", { 1, 1, 2, 2 } } },
243  { "Output" });
244 
245  RunTest<4>({ { "Input0", { 4.0f, 3.0f,
246  2.0f, 1.0f } },
247 
248  { "Input1", { 1.0f, 2.0f,
249  3.0f, 4.0f } } },
250 
251  { { "Output", { 7.0f, 4.0f,
252  1.0f, -2.0f } } });
253 }
254 
255 BOOST_FIXTURE_TEST_CASE(AssertGraphStructureTest, AssertFixture)
256 {
257  auto optimized = SetupOptimizedNetwork({ { "Input0", { 1, 1, 2, 2 } },
258  { "Input1", { 1, 1, 2, 2 } } },
259  { "Output" });
260 
261  auto optimizedNetwork = armnn::PolymorphicDowncast<armnn::OptimizedNetwork*>(optimized.get());
262  auto graph = optimizedNetwork->GetGraph();
263 
264  BOOST_TEST((graph.GetNumInputs() == 2));
265  BOOST_TEST((graph.GetNumOutputs() == 1));
266  BOOST_TEST((graph.GetNumLayers() == 5));
267 
268  armnn::Layer* inputLayer0 = GetFirstLayerWithName(graph, "Input0");
269  BOOST_TEST((inputLayer0->GetType() == armnn::LayerType::Input));
270  BOOST_TEST(CheckNumberOfInputSlot(inputLayer0, 0));
271  BOOST_TEST(CheckNumberOfOutputSlot(inputLayer0, 1));
272 
273  armnn::Layer* inputLayer1 = GetFirstLayerWithName(graph, "Input1");
274  BOOST_TEST((inputLayer1->GetType() == armnn::LayerType::Input));
275  BOOST_TEST(CheckNumberOfInputSlot(inputLayer1, 0));
276  BOOST_TEST(CheckNumberOfOutputSlot(inputLayer1, 1));
277 
278  armnn::Layer* subLayer = GetFirstLayerWithName(graph, "Sub");
279  BOOST_TEST((subLayer->GetType() == armnn::LayerType::Subtraction));
280  BOOST_TEST(CheckNumberOfInputSlot(subLayer, 2));
281  BOOST_TEST(CheckNumberOfOutputSlot(subLayer, 1));
282 
283  armnn::Layer* addLayer = GetFirstLayerWithName(graph, "Output");
284  BOOST_TEST((addLayer->GetType() == armnn::LayerType::Addition));
285  BOOST_TEST(CheckNumberOfInputSlot(addLayer, 2));
286  BOOST_TEST(CheckNumberOfOutputSlot(addLayer, 1));
287 
289  BOOST_TEST(IsConnected(inputLayer0, subLayer, 0, 0, tensorInfo));
290  BOOST_TEST(IsConnected(inputLayer1, subLayer, 0, 1, tensorInfo));
291  BOOST_TEST(IsConnected(inputLayer0, addLayer, 0, 0, tensorInfo));
292  BOOST_TEST(IsConnected(subLayer, addLayer, 0, 1, tensorInfo));
293 
294  for (auto&& outputLayer : graph.GetOutputLayers())
295  {
296  BOOST_TEST(IsConnected(addLayer, const_cast<armnn::OutputLayer*>(outputLayer), 0, 0, tensorInfo));
297  }
298 }
299 
300 
BOOST_AUTO_TEST_SUITE(TensorflowLiteParser)
armnn::Layer * GetFirstLayerWithName(armnn::Graph &graph, const std::string &name)
Definition: GraphUtils.cpp:22
BOOST_FIXTURE_TEST_CASE(AssertSimpleTest, AssertSimpleFixture)
Definition: Assert.cpp:95
armnn::IOptimizedNetworkPtr SetupOptimizedNetwork(const std::map< std::string, armnn::TensorShape > &inputShapes, const std::vector< std::string > &requestedOutputs)
bool IsConnected(armnn::Layer *srcLayer, armnn::Layer *destLayer, unsigned int srcSlot, unsigned int destSlot, const armnn::TensorInfo &expectedTensorInfo)
Definition: GraphUtils.cpp:44
BOOST_AUTO_TEST_SUITE_END()
void SetupSingleInputSingleOutput(const std::string &inputName, const std::string &outputName)
Parses and loads the network defined by the m_Prototext string.
LayerType GetType() const
Definition: Layer.hpp:259
bool CheckNumberOfInputSlot(armnn::Layer *layer, unsigned int num)
Definition: GraphUtils.cpp:34
bool CheckNumberOfOutputSlot(armnn::Layer *layer, unsigned int num)
Definition: GraphUtils.cpp:39