Introduce 'nnfw::util::fp32' namespace (#445)
author박종현/동작제어Lab(SR)/Senior Engineer/삼성전자 <jh1302.park@samsung.com>
Thu, 5 Apr 2018 02:01:17 +0000 (11:01 +0900)
committer서상민/동작제어Lab(SR)/Senior Engineer/삼성전자 <sangmin7.seo@samsung.com>
Thu, 5 Apr 2018 02:01:17 +0000 (11:01 +0900)
This commit introduces 'nnfw::util::fp32' namespace which includes
various helps for handling floating-pointer numbers.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
include/util/fp32.h [new file with mode: 0644]
tools/nnapi_test/src/nnapi_test.cc
tools/nnapi_unittests/tests/conv_1.cpp

diff --git a/include/util/fp32.h b/include/util/fp32.h
new file mode 100644 (file)
index 0000000..c916865
--- /dev/null
@@ -0,0 +1,40 @@
+#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__
index b5c71d0..c7d0009 100644 (file)
@@ -2,6 +2,7 @@
 #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)
@@ -177,21 +170,7 @@ int main(const int argc, char **argv)
   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())
   {
@@ -212,7 +191,7 @@ int main(const int argc, char **argv)
           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;
         }
       }
 
index 458ac21..d82f727 100644 (file)
@@ -8,6 +8,7 @@
 #include "util/feature/RandomObject.h"
 #include "util/feature/Reader.h"
 #include "util/kernel/RandomObject.h"
+#include "util/fp32.h"
 
 #include <iostream>
 #include <cassert>
@@ -289,11 +290,14 @@ int main(int argc, char **argv)
           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;
           }
         }
       }