ArmNN
 21.08
SerializerTestUtils.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 
7 #include "../Serializer.hpp"
8 
9 #include <doctest/doctest.h>
10 
12 
13 LayerVerifierBase::LayerVerifierBase(const std::string& layerName,
14  const std::vector<armnn::TensorInfo>& inputInfos,
15  const std::vector<armnn::TensorInfo>& outputInfos)
16  : m_LayerName(layerName)
17  , m_InputTensorInfos(inputInfos)
18  , m_OutputTensorInfos(outputInfos)
19 {}
20 
22  const armnn::BaseDescriptor& descriptor,
23  const std::vector<armnn::ConstTensor>& constants,
24  const char* name,
25  const armnn::LayerBindingId id)
26 {
27  armnn::IgnoreUnused(descriptor, constants, id);
28  switch (layer->GetType())
29  {
30  case armnn::LayerType::Input: break;
31  case armnn::LayerType::Output: break;
32  default:
33  {
34  VerifyNameAndConnections(layer, name);
35  }
36  }
37 }
38 
39 
41 {
42  CHECK(std::string(name) == m_LayerName.c_str());
43 
44  CHECK(layer->GetNumInputSlots() == m_InputTensorInfos.size());
45  CHECK(layer->GetNumOutputSlots() == m_OutputTensorInfos.size());
46 
47  for (unsigned int i = 0; i < m_InputTensorInfos.size(); i++)
48  {
49  const armnn::IOutputSlot* connectedOutput = layer->GetInputSlot(i).GetConnection();
50  CHECK(connectedOutput);
51 
52  const armnn::TensorInfo& connectedInfo = connectedOutput->GetTensorInfo();
53  CHECK(connectedInfo.GetShape() == m_InputTensorInfos[i].GetShape());
54  CHECK(
55  GetDataTypeName(connectedInfo.GetDataType()) == GetDataTypeName(m_InputTensorInfos[i].GetDataType()));
56 
57  CHECK(connectedInfo.GetQuantizationScale() == m_InputTensorInfos[i].GetQuantizationScale());
58  CHECK(connectedInfo.GetQuantizationOffset() == m_InputTensorInfos[i].GetQuantizationOffset());
59  }
60 
61  for (unsigned int i = 0; i < m_OutputTensorInfos.size(); i++)
62  {
63  const armnn::TensorInfo& outputInfo = layer->GetOutputSlot(i).GetTensorInfo();
64  CHECK(outputInfo.GetShape() == m_OutputTensorInfos[i].GetShape());
65  CHECK(GetDataTypeName(outputInfo.GetDataType()) == GetDataTypeName(m_OutputTensorInfos[i].GetDataType()));
66 
67  CHECK(outputInfo.GetQuantizationScale() == m_OutputTensorInfos[i].GetQuantizationScale());
68  CHECK(outputInfo.GetQuantizationOffset() == m_OutputTensorInfos[i].GetQuantizationOffset());
69  }
70 }
71 
72 void LayerVerifierBase::VerifyConstTensors(const std::string& tensorName,
73  const armnn::ConstTensor* expectedPtr,
74  const armnn::ConstTensor* actualPtr)
75 {
76  if (expectedPtr == nullptr)
77  {
78  CHECK_MESSAGE(actualPtr == nullptr, tensorName + " should not exist");
79  }
80  else
81  {
82  CHECK_MESSAGE(actualPtr != nullptr, tensorName + " should have been set");
83  if (actualPtr != nullptr)
84  {
85  const armnn::TensorInfo& expectedInfo = expectedPtr->GetInfo();
86  const armnn::TensorInfo& actualInfo = actualPtr->GetInfo();
87 
88  CHECK_MESSAGE(expectedInfo.GetShape() == actualInfo.GetShape(),
89  tensorName + " shapes don't match");
90  CHECK_MESSAGE(
91  GetDataTypeName(expectedInfo.GetDataType()) == GetDataTypeName(actualInfo.GetDataType()),
92  tensorName + " data types don't match");
93 
94  CHECK_MESSAGE(expectedPtr->GetNumBytes() == actualPtr->GetNumBytes(),
95  tensorName + " (GetNumBytes) data sizes do not match");
96  if (expectedPtr->GetNumBytes() == actualPtr->GetNumBytes())
97  {
98  //check the data is identical
99  const char* expectedData = static_cast<const char*>(expectedPtr->GetMemoryArea());
100  const char* actualData = static_cast<const char*>(actualPtr->GetMemoryArea());
101  bool same = true;
102  for (unsigned int i = 0; i < expectedPtr->GetNumBytes(); ++i)
103  {
104  same = expectedData[i] == actualData[i];
105  if (!same)
106  {
107  break;
108  }
109  }
110  CHECK_MESSAGE(same, tensorName + " data does not match");
111  }
112  }
113  }
114 }
115 
116 void CompareConstTensor(const armnn::ConstTensor& tensor1, const armnn::ConstTensor& tensor2)
117 {
118  CHECK(tensor1.GetShape() == tensor2.GetShape());
119  CHECK(GetDataTypeName(tensor1.GetDataType()) == GetDataTypeName(tensor2.GetDataType()));
120 
121  switch (tensor1.GetDataType())
122  {
124  CompareConstTensorData<const float*>(
125  tensor1.GetMemoryArea(), tensor2.GetMemoryArea(), tensor1.GetNumElements());
126  break;
129  CompareConstTensorData<const uint8_t*>(
130  tensor1.GetMemoryArea(), tensor2.GetMemoryArea(), tensor1.GetNumElements());
131  break;
133  CompareConstTensorData<const int8_t*>(
134  tensor1.GetMemoryArea(), tensor2.GetMemoryArea(), tensor1.GetNumElements());
135  break;
137  CompareConstTensorData<const int32_t*>(
138  tensor1.GetMemoryArea(), tensor2.GetMemoryArea(), tensor1.GetNumElements());
139  break;
140  default:
141  // Note that Float16 is not yet implemented
142  MESSAGE("Unexpected datatype");
143  CHECK(false);
144  }
145 }
146 
147 armnn::INetworkPtr DeserializeNetwork(const std::string& serializerString)
148 {
149  std::vector<std::uint8_t> const serializerVector{serializerString.begin(), serializerString.end()};
150  return IDeserializer::Create()->CreateNetworkFromBinary(serializerVector);
151 }
152 
153 std::string SerializeNetwork(const armnn::INetwork& network)
154 {
156 
157  serializer->Serialize(network);
158 
159  std::stringstream stream;
160  serializer->SaveSerializedToStream(stream);
161 
162  std::string serializerString{stream.str()};
163  return serializerString;
164 }
virtual unsigned int GetNumOutputSlots() const =0
Returns the number of connectable output slots.
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
Definition: INetwork.hpp:61
virtual unsigned int GetNumInputSlots() const =0
Returns the number of connectable input slots.
const TensorShape & GetShape() const
Definition: Tensor.hpp:191
armnn::INetworkPtr DeserializeNetwork(const std::string &serializerString)
const TensorShape & GetShape() const
Definition: Tensor.hpp:297
Main network class which provides the interface for building up a neural network. ...
Definition: INetwork.hpp:177
unsigned int GetNumElements() const
Definition: Tensor.hpp:303
MemoryType GetMemoryArea() const
Definition: Tensor.hpp:305
void IgnoreUnused(Ts &&...)
int LayerBindingId
Type of identifiers for bindable layers (inputs, outputs).
Definition: Types.hpp:244
constexpr const char * GetDataTypeName(DataType dataType)
Definition: TypesUtils.hpp:193
Base class for all descriptors.
Definition: Descriptors.hpp:22
An output connection slot for a layer.
Definition: INetwork.hpp:37
void VerifyNameAndConnections(const armnn::IConnectableLayer *layer, const char *name)
int32_t GetQuantizationOffset() const
Definition: Tensor.cpp:480
float GetQuantizationScale() const
Definition: Tensor.cpp:463
DataType GetDataType() const
Definition: Tensor.hpp:198
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:327
LayerVerifierBase(const std::string &layerName, const std::vector< armnn::TensorInfo > &inputInfos, const std::vector< armnn::TensorInfo > &outputInfos)
const TensorInfo & GetInfo() const
Definition: Tensor.hpp:295
void VerifyConstTensors(const std::string &tensorName, const armnn::ConstTensor *expectedPtr, const armnn::ConstTensor *actualPtr)
virtual LayerType GetType() const =0
Returns the armnn::LayerType of this layer.
std::unique_ptr< ISerializer, void(*)(ISerializer *serializer)> ISerializerPtr
Definition: ISerializer.hpp:15
void CompareConstTensor(const armnn::ConstTensor &tensor1, const armnn::ConstTensor &tensor2)
virtual const IInputSlot & GetInputSlot(unsigned int index) const =0
Get a const input slot handle by slot index.
virtual const IOutputSlot * GetConnection() const =0
static ISerializerPtr Create()
Definition: Serializer.cpp:35
virtual const TensorInfo & GetTensorInfo() const =0
virtual const IOutputSlot & GetOutputSlot(unsigned int index) const =0
Get the const output slot handle by slot index.
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
Definition: INetwork.hpp:172
DataType GetDataType() const
Definition: Tensor.hpp:300
std::string SerializeNetwork(const armnn::INetwork &network)
void ExecuteStrategy(const armnn::IConnectableLayer *layer, const armnn::BaseDescriptor &descriptor, const std::vector< armnn::ConstTensor > &constants, const char *name, const armnn::LayerBindingId id=0) override
unsigned int GetNumBytes() const
Definition: Tensor.hpp:302