Extract Tensor IndexFormatter as utility (#489)
author박종현/동작제어Lab(SR)/Senior Engineer/삼성전자 <jh1302.park@samsung.com>
Mon, 9 Apr 2018 06:52:04 +0000 (15:52 +0900)
committer김정현/동작제어Lab(SR)/Senior Engineer/삼성전자 <jh0822.kim@samsung.com>
Mon, 9 Apr 2018 06:52:04 +0000 (15:52 +0900)
This commit extracts TensorIndexFormatter (implemented in NNAPI test) as
utility.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
include/util/tensor/IndexFormatter.h [new file with mode: 0644]
src/util/CMakeLists.txt
src/util/src/tensor/IndexFormatter.cpp [new file with mode: 0644]
tools/nnapi_test/src/nnapi_test.cc

diff --git a/include/util/tensor/IndexFormatter.h b/include/util/tensor/IndexFormatter.h
new file mode 100644 (file)
index 0000000..2210d51
--- /dev/null
@@ -0,0 +1,36 @@
+#ifndef __NNFW_UTIL_TENSOR_INDEX_FORMATTER_H__
+#define __NNFW_UTIL_TENSOR_INDEX_FORMATTER_H__
+
+#include "util/tensor/Index.h"
+
+#include <ostream>
+
+namespace nnfw
+{
+namespace util
+{
+namespace tensor
+{
+
+class IndexFormatter
+{
+public:
+  IndexFormatter(const nnfw::util::tensor::Index &index) : _index(index)
+  {
+    // DO NOTHING
+  }
+
+public:
+  const nnfw::util::tensor::Index &index(void) const { return _index; }
+
+private:
+  const nnfw::util::tensor::Index &_index;
+};
+
+std::ostream &operator<<(std::ostream &os, const IndexFormatter &fmt);
+
+} // namespace tensor
+} // namespace util
+} // namespace nnfw
+
+#endif // __NNFW_UTIL_TENSOR_INDEX_FORMATTER_H__
index 046f01d..d3ff7cd 100644 (file)
@@ -2,6 +2,7 @@
 set(NNFW_UTILITY_SRCS src/environment.cpp)
 list(APPEND NNFW_UTILITY_SRCS src/tensor/Shape.cpp)
 list(APPEND NNFW_UTILITY_SRCS src/tensor/NonIncreasingStride.cpp)
+list(APPEND NNFW_UTILITY_SRCS src/tensor/IndexFormatter.cpp)
 
 set(NNFW_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/include)
 
diff --git a/src/util/src/tensor/IndexFormatter.cpp b/src/util/src/tensor/IndexFormatter.cpp
new file mode 100644 (file)
index 0000000..f5667b9
--- /dev/null
@@ -0,0 +1,33 @@
+#include "util/tensor/IndexFormatter.h"
+
+#include <cassert>
+
+namespace nnfw
+{
+namespace util
+{
+namespace tensor
+{
+
+std::ostream &operator<<(std::ostream &os, const IndexFormatter &fmt)
+{
+  const auto rank = fmt.index().rank();
+
+  assert(rank > 0);
+
+  os << fmt.index().at(0);
+
+  if (rank > 1)
+  {
+    for (uint32_t axis = 1; axis < rank; ++axis)
+    {
+      os << ", " << fmt.index().at(axis);
+    }
+  }
+
+  return os;
+}
+
+} // namespace tensor
+} // namespace util
+} // namespace nnfw
index c5c7d9d..7ba2cc9 100644 (file)
@@ -4,7 +4,9 @@
 #include "util/environment.h"
 #include "util/fp32.h"
 #include "util/tensor/IndexIterator.h"
+#include "util/tensor/IndexFormatter.h"
 #include "util/tensor/Zipper.h"
+
 #include "support/tflite/TensorView.h"
 #include "support/tflite/interp/FlatBufferBuilder.h"
 
@@ -45,40 +47,6 @@ void invoke_interpreter(Interpreter &interpreter)
   check(interpreter.Invoke());
 }
 
-class TensorIndexFormatter
-{
-public:
-  TensorIndexFormatter(const nnfw::util::tensor::Index &index) : _index(index)
-  {
-    // DO NOTHING
-  }
-
-public:
-  const nnfw::util::tensor::Index &index(void) const { return _index; }
-
-private:
-  const nnfw::util::tensor::Index &_index;
-};
-
-std::ostream &operator<<(std::ostream &os, const TensorIndexFormatter &fmt)
-{
-  const auto rank = fmt.index().rank();
-
-  assert(rank > 0);
-
-  os << fmt.index().at(0);
-
-  if (rank > 1)
-  {
-    for (uint32_t axis = 1; axis < rank; ++axis)
-    {
-      os << ", " << fmt.index().at(axis);
-    }
-  }
-
-  return os;
-}
-
 struct TfLiteTensorDiff
 {
   nnfw::util::tensor::Index index;
@@ -206,7 +174,7 @@ int main(const int argc, char **argv)
     // Print out max_diff
     if (max_diff.relative_diff > 0)
     {
-      std::cout << "  Max Diff at [" << TensorIndexFormatter(max_diff.index) << "]" << std::endl;
+      std::cout << "  Max Diff at [" << nnfw::util::tensor::IndexFormatter(max_diff.index) << "]" << std::endl;
       std::cout << "       expected: " << max_diff.expected << std::endl;
       std::cout << "       obtained: " << max_diff.obtained << std::endl;
       std::cout << "  relative diff: " << max_diff.relative_diff << std::endl;
@@ -218,7 +186,7 @@ int main(const int argc, char **argv)
       {
         for (const auto &diff : diffs)
         {
-          std::cout << "    Diff at [" << TensorIndexFormatter(diff.index) << "]" << std::endl;
+          std::cout << "    Diff at [" << nnfw::util::tensor::IndexFormatter(diff.index) << "]" << std::endl;
           std::cout << "      expected: " << diff.expected << std::endl;
           std::cout << "      obtained: " << diff.obtained << std::endl;
           std::cout << "      relative diff: " << diff.relative_diff << std::endl;