--- /dev/null
+#ifndef __NNFW_UTIL_FP32_H__
+#define __NNFW_UTIL_FP32_H__
+
+#include <cmath>
+#include <cfloat>
+
+namespace nnfw
+{
+namespace util
+{
+namespace fp32
+{
+
+inline float relative_diff(float lhs, float rhs)
+{
+ const auto diff = std::fabs(lhs - rhs);
+ const auto base = std::max(std::fabs(lhs), std::fabs(rhs));
+
+ return diff / base;
+}
+
+bool epsilon_equal(float expected, float obtained)
+{
+ if (std::isnan(expected) && std::isnan(obtained))
+ {
+ return true;
+ }
+
+ // Let's use relative epsilon comparision
+ const auto diff = std::fabs(expected - obtained);
+ const auto max = std::max(std::fabs(expected), std::fabs(obtained));
+
+ return diff <= max * FLT_EPSILON;
+}
+
+} // namespace fp32
+} // namespace util
+} // namespace nnfw
+
+#endif // __NNFW_UTIL_FP32_H__
#include "tensorflow/contrib/lite/model.h"
#include "util/environment.h"
+#include "util/fp32.h"
#include <iostream>
#include <chrono>
using namespace tflite;
using namespace tflite::ops::builtin;
-inline float diff_ratio(float lhs, float rhs)
-{
- const auto diff = std::fabs(lhs - rhs);
- const auto max = std::max(std::fabs(lhs), std::fabs(rhs));
-
- return diff / max;
-}
-
inline void check(const TfLiteStatus &status) { assert(status != kTfLiteError); }
std::unique_ptr<Interpreter> build_interpreter(const FlatBufferModel &model, bool use_nnapi)
assert(pure->inputs() == delegated->inputs());
assert(pure->outputs() == delegated->outputs());
- auto compare_fn = [](float expected, float obtained)
- {
- if ( std::isnan(expected) && std::isnan(obtained) )
- {
- return true;
- }
-
- // Let's use relative epsilon comparision
- const auto diff = std::fabs(expected - obtained);
- const auto max = std::max(std::fabs(expected), std::fabs(obtained));
-
- return diff <= max * FLT_EPSILON;
- };
-
- TfLiteTensorComparator comparator(compare_fn);
+ TfLiteTensorComparator comparator(nnfw::util::fp32::epsilon_equal);
for (const auto &id : pure->outputs())
{
std::cout << " Diff at offset " << diff.offset << std::endl;
std::cout << " expected: " << diff.expected << std::endl;
std::cout << " obtained: " << diff.obtained << std::endl;
- std::cout << " relative diff: " << diff_ratio(diff.expected, diff.obtained) << std::endl;
+ std::cout << " relative diff: " << nnfw::util::fp32::relative_diff(diff.expected, diff.obtained) << std::endl;
}
}
#include "util/feature/RandomObject.h"
#include "util/feature/Reader.h"
#include "util/kernel/RandomObject.h"
+#include "util/fp32.h"
#include <iostream>
#include <cassert>
const auto value_from_interp = interp_output.at(ch, row, col);
const auto value_from_NNAPI = nnapi_output.at(ch, row, col);
- if (value_from_interp != value_from_NNAPI)
+ if (!nnfw::util::fp32::epsilon_equal(value_from_interp, value_from_NNAPI))
{
+ const auto rdiff = nnfw::util::fp32::relative_diff(value_from_interp, value_from_NNAPI);
+
std::cerr << "Diff at (ch: " << ch << ", row: " << row << ", col: " << col << ")" << std::endl;
std::cerr << " Value from interpreter: " << value_from_interp << std::endl;
std::cerr << " Value from NNAPI: " << value_from_NNAPI << std::endl;
+ std::cerr << " Relative difference: " << rdiff << std::endl;
}
}
}