ArmNN
 22.02
TensorHelpers.hpp File Reference
#include <armnnTestUtils/PredicateResult.hpp>
#include <armnn/Tensor.hpp>
#include <armnn/utility/Assert.hpp>
#include <armnnUtils/FloatingPointComparison.hpp>
#include <armnnUtils/QuantizeHelper.hpp>
#include <doctest/doctest.h>
#include <array>
#include <cmath>
#include <random>
#include <vector>

Go to the source code of this file.

Classes

struct  SelectiveComparer< T, isQuantized >
 
struct  SelectiveComparer< T, false >
 

Functions

template<typename T >
bool SelectiveCompare (T a, T b)
 
template<typename T >
bool SelectiveCompareBoolean (T a, T b)
 
template<typename T >
armnn::PredicateResult CompareTensors (const std::vector< T > &actualData, const std::vector< T > &expectedData, const armnn::TensorShape &actualShape, const armnn::TensorShape &expectedShape, bool compareBoolean=false, bool isDynamic=false)
 
template<typename T >
std::vector< T > MakeRandomTensor (const armnn::TensorInfo &tensorInfo, unsigned int seed, float min=-10.0f, float max=10.0f)
 

Variables

constexpr float g_FloatCloseToZeroTolerance = 1.0e-6f
 

Function Documentation

◆ CompareTensors()

armnn::PredicateResult CompareTensors ( const std::vector< T > &  actualData,
const std::vector< T > &  expectedData,
const armnn::TensorShape actualShape,
const armnn::TensorShape expectedShape,
bool  compareBoolean = false,
bool  isDynamic = false 
)

Definition at line 73 of file TensorHelpers.hpp.

References TensorShape::GetNumDimensions(), TensorShape::GetNumElements(), PredicateResult::Message(), SelectiveCompare(), SelectiveCompareBoolean(), and PredicateResult::SetResult().

Referenced by CompareTestResultIfSupported(), DetectionPostProcessImpl(), ParserPrototxtFixture< TParser >::RunTest(), ParserFlatbuffersSerializeFixture::RunTest(), ParserFlatbuffersFixture::RunTest(), TEST_CASE_FIXTURE(), and TEST_SUITE().

79 {
80  if (actualData.size() != expectedData.size())
81  {
82  armnn::PredicateResult res(false);
83  res.Message() << "Different data size ["
84  << actualData.size()
85  << "!="
86  << expectedData.size()
87  << "]";
88  return res;
89  }
90 
91  if (actualShape.GetNumDimensions() != expectedShape.GetNumDimensions())
92  {
93  armnn::PredicateResult res(false);
94  res.Message() << "Different number of dimensions ["
95  << actualShape.GetNumDimensions()
96  << "!="
97  << expectedShape.GetNumDimensions()
98  << "]";
99  return res;
100  }
101 
102  if (actualShape.GetNumElements() != expectedShape.GetNumElements())
103  {
104  armnn::PredicateResult res(false);
105  res.Message() << "Different number of elements ["
106  << actualShape.GetNumElements()
107  << "!="
108  << expectedShape.GetNumElements()
109  << "]";
110  return res;
111  }
112 
113  unsigned int numberOfDimensions = actualShape.GetNumDimensions();
114 
115  if (!isDynamic)
116  {
117  // Checks they are same shape.
118  for (unsigned int i = 0; i < numberOfDimensions; ++i)
119  {
120  if (actualShape[i] != expectedShape[i])
121  {
122  armnn::PredicateResult res(false);
123  res.Message() << "Different shapes ["
124  << actualShape[i]
125  << "!="
126  << expectedShape[i]
127  << "]";
128  return res;
129  }
130  }
131  }
132 
133  // Fun iteration over n dimensions.
134  std::vector<unsigned int> indices;
135  for (unsigned int i = 0; i < numberOfDimensions; i++)
136  {
137  indices.emplace_back(0);
138  }
139 
140  std::stringstream errorString;
141  int numFailedElements = 0;
142  constexpr int maxReportedDifferences = 3;
143  unsigned int index = 0;
144 
145  // Compare data element by element.
146  while (true)
147  {
148  bool comparison;
149  // As true for uint8_t is non-zero (1-255) we must have a dedicated compare for Booleans.
150  if(compareBoolean)
151  {
152  comparison = SelectiveCompareBoolean(actualData[index], expectedData[index]);
153  }
154  else
155  {
156  comparison = SelectiveCompare(actualData[index], expectedData[index]);
157  }
158 
159  if (!comparison)
160  {
161  ++numFailedElements;
162 
163  if (numFailedElements <= maxReportedDifferences)
164  {
165  if (numFailedElements >= 2)
166  {
167  errorString << ", ";
168  }
169  errorString << "[";
170  for (unsigned int i = 0; i < numberOfDimensions; ++i)
171  {
172  errorString << indices[i];
173  if (i != numberOfDimensions - 1)
174  {
175  errorString << ",";
176  }
177  }
178  errorString << "]";
179 
180  errorString << " (" << +actualData[index] << " != " << +expectedData[index] << ")";
181  }
182  }
183 
184  ++indices[numberOfDimensions - 1];
185  for (unsigned int i=numberOfDimensions-1; i>0; i--)
186  {
187  if (indices[i] == actualShape[i])
188  {
189  indices[i] = 0;
190  ++indices[i - 1];
191  }
192  }
193  if (indices[0] == actualShape[0])
194  {
195  break;
196  }
197 
198  index++;
199  }
200 
201  armnn::PredicateResult comparisonResult(true);
202  if (numFailedElements > 0)
203  {
204  comparisonResult.SetResult(false);
205  comparisonResult.Message() << numFailedElements << " different values at: ";
206  if (numFailedElements > maxReportedDifferences)
207  {
208  errorString << ", ... (and " << (numFailedElements - maxReportedDifferences) << " other differences)";
209  }
210  comparisonResult.Message() << errorString.str();
211  }
212 
213  return comparisonResult;
214 }
unsigned int GetNumElements() const
Function that calculates the tensor elements by multiplying all dimension size which are Specified...
Definition: Tensor.cpp:181
bool SelectiveCompareBoolean(T a, T b)
unsigned int GetNumDimensions() const
Function that returns the tensor rank.
Definition: Tensor.cpp:174
bool SelectiveCompare(T a, T b)

◆ MakeRandomTensor()

std::vector<T> MakeRandomTensor ( const armnn::TensorInfo tensorInfo,
unsigned int  seed,
float  min = -10.0f,
float  max = 10.0f 
)

Definition at line 217 of file TensorHelpers.hpp.

References TensorInfo::GetNumElements(), TensorInfo::GetQuantizationOffset(), and TensorInfo::GetQuantizationScale().

221 {
222  std::mt19937 gen(seed);
223  std::uniform_real_distribution<float> dist(min, max);
224 
225  std::vector<float> init(tensorInfo.GetNumElements());
226  for (unsigned int i = 0; i < init.size(); i++)
227  {
228  init[i] = dist(gen);
229  }
230 
231  const float qScale = tensorInfo.GetQuantizationScale();
232  const int32_t qOffset = tensorInfo.GetQuantizationOffset();
233 
234  return armnnUtils::QuantizedVector<T>(init, qScale, qOffset);
235 }
int32_t GetQuantizationOffset() const
Definition: Tensor.cpp:480
float GetQuantizationScale() const
Definition: Tensor.cpp:463
unsigned int GetNumElements() const
Definition: Tensor.hpp:196

◆ SelectiveCompare()

bool SelectiveCompare ( a,
b 
)

Definition at line 61 of file TensorHelpers.hpp.

References SelectiveComparer< T, isQuantized >::Compare().

Referenced by CompareTensors().

62 {
64 };
bool Compare(T a, T b, float tolerance=0.000001f)

◆ SelectiveCompareBoolean()

bool SelectiveCompareBoolean ( a,
b 
)

Definition at line 67 of file TensorHelpers.hpp.

Referenced by CompareTensors().

68 {
69  return (((a == 0) && (b == 0)) || ((a != 0) && (b != 0)));
70 };

Variable Documentation

◆ g_FloatCloseToZeroTolerance

constexpr float g_FloatCloseToZeroTolerance = 1.0e-6f

Definition at line 22 of file TensorHelpers.hpp.

Referenced by SelectiveComparer< T, false >::Compare().