11 #include <boost/assert.hpp> 12 #include <boost/multi_array.hpp> 13 #include <boost/numeric/conversion/cast.hpp> 14 #include <boost/random/uniform_real_distribution.hpp> 15 #include <boost/random/mersenne_twister.hpp> 16 #include <boost/test/tools/floating_point_comparison.hpp> 17 #include <boost/test/unit_test.hpp> 25 template<
typename T,
bool isQuantized = true>
30 return (std::max(a, b) - std::min(a, b)) <= 1;
41 if (a == 0.0f || b == 0.0f)
46 if (std::isinf(a) && a == b)
51 if (std::isnan(a) && std::isnan(b))
57 boost::math::fpc::close_at_tolerance<float> comparer(boost::math::fpc::percent_tolerance(1.0f));
58 return comparer(a, b);
71 return (((a == 0) && (b == 0)) || ((a != 0) && (b != 0)));
74 template <
typename T, std::
size_t n>
75 boost::test_tools::predicate_result
CompareTensors(
const boost::multi_array<T, n>& a,
76 const boost::multi_array<T, n>& b,
77 bool compareBoolean =
false)
80 for (
unsigned int i=0; i<n; i++)
82 if (a.shape()[i] != b.shape()[i])
84 boost::test_tools::predicate_result res(
false);
85 res.message() <<
"Different shapes [" 97 std::array<unsigned int, n> indices;
98 for (
unsigned int i = 0; i < n; i++)
103 std::stringstream errorString;
104 int numFailedElements = 0;
105 constexpr
int maxReportedDifferences = 3;
124 if (numFailedElements <= maxReportedDifferences)
126 if (numFailedElements >= 2)
131 for (
unsigned int i = 0; i < n; ++i)
133 errorString << indices[i];
141 errorString <<
" (" << +a(indices) <<
" != " << +b(indices) <<
")";
146 for (
unsigned int i=n-1; i>0; i--)
148 if (indices[i] == a.shape()[i])
155 if (indices[0] == a.shape()[0])
161 boost::test_tools::predicate_result comparisonResult(
true);
162 if (numFailedElements > 0)
164 comparisonResult =
false;
165 comparisonResult.message() << numFailedElements <<
" different values at: ";
166 if (numFailedElements > maxReportedDifferences)
168 errorString <<
", ... (and " << (numFailedElements - maxReportedDifferences) <<
" other differences)";
170 comparisonResult.message() << errorString.str();
173 return comparisonResult;
178 template <
typename T, std::
size_t n>
181 std::array<unsigned int, n> shape;
183 for (
unsigned int i = 0; i < n; i++)
185 shape[i] = tensorInfo.
GetShape()[i];
188 return boost::multi_array<T, n>(shape);
192 template <
typename T, std::
size_t n>
195 BOOST_ASSERT_MSG(flat.size() == tensorInfo.
GetNumElements(),
"Wrong number of components supplied to tensor");
197 std::array<unsigned int, n> shape;
200 const unsigned int returnDimensions =
static_cast<unsigned int>(n);
203 const unsigned int paddedDimensions =
204 returnDimensions > actualDimensions ? returnDimensions - actualDimensions : 0u;
206 for (
unsigned int i = 0u; i < returnDimensions; i++)
208 if (i < paddedDimensions)
214 shape[i] = tensorInfo.
GetShape()[i - paddedDimensions];
218 boost::const_multi_array_ref<T, n> arrayRef(&flat[0], shape);
219 return boost::multi_array<T, n>(arrayRef);
222 template <
typename T, std::
size_t n>
228 boost::random::mt19937 gen(seed);
229 boost::random::uniform_real_distribution<float> dist(min, max);
232 for (
unsigned int i = 0; i < init.size(); i++)
240 return MakeTensor<T, n>(tensorInfo, armnnUtils::QuantizedVector<T>(init, qScale, qOffset));
int32_t GetQuantizationOffset() const
static bool Compare(T a, T b)
unsigned int GetNumDimensions() const
static bool Compare(T a, T b)
boost::multi_array< T, n > MakeRandomTensor(const armnn::TensorInfo &tensorInfo, unsigned int seed, float min=-10.0f, float max=10.0f)
constexpr float g_FloatCloseToZeroTolerance
unsigned int GetNumElements() const
bool SelectiveCompareBoolean(T a, T b)
bool SelectiveCompare(T a, T b)
boost::multi_array< T, n > MakeTensor(const armnn::TensorInfo &tensorInfo)
const TensorShape & GetShape() const
boost::test_tools::predicate_result CompareTensors(const boost::multi_array< T, n > &a, const boost::multi_array< T, n > &b, bool compareBoolean=false)
float GetQuantizationScale() const