ArmNN
 21.02
ComparisonSerializationTests.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "../Serializer.hpp"
8 
9 #include <armnn/Descriptors.hpp>
10 #include <armnn/INetwork.hpp>
11 #include <armnn/IRuntime.hpp>
14 
15 #include <boost/test/unit_test.hpp>
16 
17 
18 BOOST_AUTO_TEST_SUITE(SerializerTests)
19 
20 struct ComparisonModel
21 {
22  ComparisonModel(const std::string& layerName,
23  const armnn::TensorInfo& inputInfo,
24  const armnn::TensorInfo& outputInfo,
25  armnn::ComparisonDescriptor& descriptor)
26  : m_network(armnn::INetwork::Create())
27  {
28  armnn::IConnectableLayer* const inputLayer0 = m_network->AddInputLayer(0);
29  armnn::IConnectableLayer* const inputLayer1 = m_network->AddInputLayer(1);
30  armnn::IConnectableLayer* const equalLayer = m_network->AddComparisonLayer(descriptor, layerName.c_str());
31  armnn::IConnectableLayer* const outputLayer = m_network->AddOutputLayer(0);
32 
33  inputLayer0->GetOutputSlot(0).Connect(equalLayer->GetInputSlot(0));
34  inputLayer1->GetOutputSlot(0).Connect(equalLayer->GetInputSlot(1));
35  equalLayer->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0));
36 
37  inputLayer0->GetOutputSlot(0).SetTensorInfo(inputInfo);
38  inputLayer1->GetOutputSlot(0).SetTensorInfo(inputInfo);
39  equalLayer->GetOutputSlot(0).SetTensorInfo(outputInfo);
40  }
41 
42  armnn::INetworkPtr m_network;
43 };
44 
45 class ComparisonLayerVerifier : public LayerVerifierBase
46 {
47 public:
48  ComparisonLayerVerifier(const std::string& layerName,
49  const std::vector<armnn::TensorInfo>& inputInfos,
50  const std::vector<armnn::TensorInfo>& outputInfos,
51  const armnn::ComparisonDescriptor& descriptor)
52  : LayerVerifierBase(layerName, inputInfos, outputInfos)
53  , m_Descriptor (descriptor) {}
54 
55  void ExecuteStrategy(const armnn::IConnectableLayer* layer,
56  const armnn::BaseDescriptor& descriptor,
57  const std::vector<armnn::ConstTensor>& constants,
58  const char* name,
59  const armnn::LayerBindingId id = 0) override
60  {
61  armnn::IgnoreUnused(descriptor, constants, id);
62  switch (layer->GetType())
63  {
64  case armnn::LayerType::Input: break;
65  case armnn::LayerType::Output: break;
67  {
68  VerifyNameAndConnections(layer, name);
69  const armnn::ComparisonDescriptor& layerDescriptor =
70  static_cast<const armnn::ComparisonDescriptor&>(descriptor);
71  BOOST_CHECK(layerDescriptor.m_Operation == m_Descriptor.m_Operation);
72  break;
73  }
74  default:
75  {
76  throw armnn::Exception("Unexpected layer type in Comparison test model");
77  }
78  }
79  }
80 
81 private:
82  armnn::ComparisonDescriptor m_Descriptor;
83 };
84 
85 BOOST_AUTO_TEST_CASE(SerializeEqual)
86 {
87  const std::string layerName("equal");
88 
89  const armnn::TensorShape shape{2, 1, 2, 4};
92 
94 
95  ComparisonModel model(layerName, inputInfo, outputInfo, descriptor);
96 
97  armnn::INetworkPtr deserializedNetwork = DeserializeNetwork(SerializeNetwork(*model.m_network));
98  BOOST_CHECK(deserializedNetwork);
99 
100  ComparisonLayerVerifier verifier(layerName, { inputInfo, inputInfo }, { outputInfo }, descriptor);
101  deserializedNetwork->ExecuteStrategy(verifier);
102 }
103 
104 BOOST_AUTO_TEST_CASE(SerializeGreater)
105 {
106  const std::string layerName("greater");
107 
108  const armnn::TensorShape shape{2, 1, 2, 4};
111 
113 
114  ComparisonModel model(layerName, inputInfo, outputInfo, descriptor);
115 
116  armnn::INetworkPtr deserializedNetwork = DeserializeNetwork(SerializeNetwork(*model.m_network));
117  BOOST_CHECK(deserializedNetwork);
118 
119  ComparisonLayerVerifier verifier(layerName, { inputInfo, inputInfo }, { outputInfo }, descriptor);
120  deserializedNetwork->ExecuteStrategy(verifier);
121 }
122 
BOOST_AUTO_TEST_SUITE(TensorflowLiteParser)
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
Definition: INetwork.hpp:62
armnn::INetworkPtr DeserializeNetwork(const std::string &serializerString)
A ComparisonDescriptor for the ComparisonLayer.
Definition: Descriptors.hpp:78
Copyright (c) 2021 ARM Limited and Contributors.
void IgnoreUnused(Ts &&...)
int LayerBindingId
Type of identifiers for bindable layers (inputs, outputs).
Definition: Types.hpp:210
virtual void SetTensorInfo(const TensorInfo &tensorInfo)=0
Base class for all descriptors.
Definition: Descriptors.hpp:22
virtual LayerType GetType() const =0
Returns the armnn::LayerType of this layer.
ComparisonOperation m_Operation
Specifies the comparison operation to execute.
Definition: Descriptors.hpp:94
BOOST_AUTO_TEST_SUITE_END()
Base class for all ArmNN exceptions so that users can filter to just those.
Definition: Exceptions.hpp:46
virtual const IInputSlot & GetInputSlot(unsigned int index) const =0
Get a const input slot handle by slot index.
virtual const IOutputSlot & GetOutputSlot(unsigned int index) const =0
Get the const output slot handle by slot index.
BOOST_AUTO_TEST_CASE(SerializeEqual)
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
Definition: INetwork.hpp:173
virtual int Connect(IInputSlot &destination)=0
std::string SerializeNetwork(const armnn::INetwork &network)