From edb6f90591f68ef46c862c4f31bf5c2ee735445e Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Staff=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Tue, 3 Jul 2018 08:45:32 +0900 Subject: [PATCH] [nnkit] Introduce 'show' action (#437) This commit introduces 'show' action which dumps the content of tensors to standard output. Signed-off-by: Jonghyun Park --- contrib/nnkit/actions/builtin/CMakeLists.txt | 3 ++ contrib/nnkit/actions/builtin/Show.cpp | 56 ++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 contrib/nnkit/actions/builtin/Show.cpp diff --git a/contrib/nnkit/actions/builtin/CMakeLists.txt b/contrib/nnkit/actions/builtin/CMakeLists.txt index 51db367..4de70df 100644 --- a/contrib/nnkit/actions/builtin/CMakeLists.txt +++ b/contrib/nnkit/actions/builtin/CMakeLists.txt @@ -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 index 0000000..cf5c30b --- /dev/null +++ b/contrib/nnkit/actions/builtin/Show.cpp @@ -0,0 +1,56 @@ +#include + +#include + +#include + +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 &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 +#include + +extern "C" std::unique_ptr make_action(const nnkit::CmdlineArguments &args) +{ + return nncc::foundation::make_unique(); +} -- 2.7.4