[nnapi_test] Show details on test failure (#407)
author박종현/동작제어Lab(SR)/Senior Engineer/삼성전자 <jh1302.park@samsung.com>
Wed, 4 Apr 2018 06:52:51 +0000 (15:52 +0900)
committer서상민/동작제어Lab(SR)/Senior Engineer/삼성전자 <sangmin7.seo@samsung.com>
Wed, 4 Apr 2018 06:52:51 +0000 (15:52 +0900)
This commit allows users to check details on test failure via
environment variable (VERBOSE).

Note that this feature is turned off by default.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
tools/nnapi_test/CMakeLists.txt
tools/nnapi_test/src/nnapi_test.cc

index 8382404..525a6b2 100644 (file)
@@ -2,3 +2,4 @@ list(APPEND SOURCES "src/nnapi_test.cc")
 
 add_executable(nnapi_test ${SOURCES})
 target_link_libraries(nnapi_test tensorflow_lite)
+target_link_libraries(nnapi_test nnfw_util)
index 8e3ca26..b5c71d0 100644 (file)
@@ -1,6 +1,8 @@
 #include "tensorflow/contrib/lite/kernels/register.h"
 #include "tensorflow/contrib/lite/model.h"
 
+#include "util/environment.h"
+
 #include <iostream>
 #include <chrono>
 #include <algorithm>
 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)
@@ -140,6 +150,10 @@ std::vector<TfLiteTensorDiff> TfLiteTensorComparator::compare(const TfLiteTensor
 
 int main(const int argc, char **argv)
 {
+  int verbose = 0;
+
+  nnfw::util::env::IntAccessor("VERBOSE").access(verbose);
+
   const auto filename = argv[1];
 
   StderrReporter error_reporter;
@@ -182,8 +196,28 @@ int main(const int argc, char **argv)
   for (const auto &id : pure->outputs())
   {
     auto diffs = comparator.compare(*(pure->tensor(id)), *(delegated->tensor(id)));
-    assert(diffs.size() == 0);
-    std::cout << "  Tensor #" << id << ": MATCHED" << std::endl;
+
+    if (diffs.size() == 0)
+    {
+      std::cout << "  Tensor #" << id << ": MATCHED" << std::endl;
+    }
+    else
+    {
+      std::cout << "  Tensor #" << id << ": UMMATCHED" << std::endl;
+
+      if (verbose != 0)
+      {
+        for (const auto &diff : diffs)
+        {
+          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;
+        }
+      }
+
+      return 255;
+    }
   }
 
   std::cout << "[NNAPI TEST] PASSED" << std::endl;