[nnkit] Introduce 'show' action (#437)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Mon, 2 Jul 2018 23:45:32 +0000 (08:45 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Mon, 2 Jul 2018 23:45:32 +0000 (08:45 +0900)
This commit introduces 'show' action which dumps the content of tensors
to standard output.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/nnkit/actions/builtin/CMakeLists.txt
contrib/nnkit/actions/builtin/Show.cpp [new file with mode: 0644]

index 51db367..4de70df 100644 (file)
@@ -1,2 +1,5 @@
+add_library(nnkit_show_action SHARED Show.cpp)
+target_link_libraries(nnkit_show_action nnkit_intf_action)
+
 add_library(nnkit_randomize_action SHARED Randomize.cpp)
 target_link_libraries(nnkit_randomize_action nnkit_intf_action)
diff --git a/contrib/nnkit/actions/builtin/Show.cpp b/contrib/nnkit/actions/builtin/Show.cpp
new file mode 100644 (file)
index 0000000..cf5c30b
--- /dev/null
@@ -0,0 +1,56 @@
+#include <nnkit/Action.h>
+
+#include <nncc/core/ADT/tensor/IndexEnumerator.h>
+
+#include <iostream>
+
+using nncc::core::ADT::tensor::Index;
+using nncc::core::ADT::tensor::IndexEnumerator;
+
+std::ostream &operator<<(std::ostream &os, const Index &index)
+{
+  if (index.rank() > 0)
+  {
+    os << index.at(0);
+    for (uint32_t axis = 1; axis < index.rank(); ++axis)
+    {
+      os << "," << index.at(axis);
+    }
+  }
+  return os;
+}
+
+struct ShowAction final : public nnkit::Action
+{
+  void run(nnkit::TensorContext &ctx) override;
+};
+
+void ShowAction::run(nnkit::TensorContext &ctx)
+{
+  std::cout << "count: " << ctx.size() << std::endl;
+  for (uint32_t n = 0; n < ctx.size(); ++n)
+  {
+    std::cout << "  tensor(" << n << ") : " << ctx.name(n) << std::endl;
+
+    using nncc::core::ADT::tensor::Reader;
+    using nnkit::TensorContext;
+
+    ctx.getConstFloatTensor(n, [] (const TensorContext &ctx, uint32_t n, const Reader<float> &t)
+    {
+      for (IndexEnumerator e{ctx.shape(n)}; e.valid(); e.advance())
+      {
+        const auto &index = e.current();
+
+        std::cout << "    " << index << ": " << t.at(index) << std::endl;
+      }
+    });
+  }
+}
+
+#include <nnkit/CmdlineArguments.h>
+#include <nncc/foundation/Memory.h>
+
+extern "C" std::unique_ptr<nnkit::Action> make_action(const nnkit::CmdlineArguments &args)
+{
+  return nncc::foundation::make_unique<ShowAction>();
+}