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