From eb38655fc8a0f67778940ba82eba8e84193d2b70 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EC=9D=B4=EC=83=81=EA=B7=9C/On-Device=20Lab=28SR=29/Princip?= =?utf8?q?al=20Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Fri, 16 Aug 2019 13:39:51 +0900 Subject: [PATCH] [nnpackage_run] Introduce tensor_dumper (#6602) - Introduce `dump` option in args.* - Introduce tensor_dumper - Use tensor_dumper in nnpackage_run Signed-off-by: Sanggyu Lee --- tests/tools/nnpackage_run/CMakeLists.txt | 1 + tests/tools/nnpackage_run/src/args.cc | 9 ++++- tests/tools/nnpackage_run/src/args.h | 2 + tests/tools/nnpackage_run/src/nnpackage_run.cc | 14 +++++++ tests/tools/nnpackage_run/src/tensor_dumper.cc | 54 ++++++++++++++++++++++++++ tests/tools/nnpackage_run/src/tensor_dumper.h | 31 +++++++++++++++ 6 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 tests/tools/nnpackage_run/src/tensor_dumper.cc create mode 100644 tests/tools/nnpackage_run/src/tensor_dumper.h diff --git a/tests/tools/nnpackage_run/CMakeLists.txt b/tests/tools/nnpackage_run/CMakeLists.txt index e1e50f7..6900b76 100644 --- a/tests/tools/nnpackage_run/CMakeLists.txt +++ b/tests/tools/nnpackage_run/CMakeLists.txt @@ -8,6 +8,7 @@ endif(NOT BUILD_NEURUN) list(APPEND NNPACKAGE_RUN_SRCS "src/nnpackage_run.cc") list(APPEND NNPACKAGE_RUN_SRCS "src/args.cc") +list(APPEND NNPACKAGE_RUN_SRCS "src/tensor_dumper.cc") nnfw_find_package(Boost REQUIRED) diff --git a/tests/tools/nnpackage_run/src/args.cc b/tests/tools/nnpackage_run/src/args.cc index 30e276b..bcdbb91 100644 --- a/tests/tools/nnpackage_run/src/args.cc +++ b/tests/tools/nnpackage_run/src/args.cc @@ -30,14 +30,14 @@ Args::Args(const int argc, char **argv) noexcept void Args::Initialize(void) { - // General options po::options_description general("General options"); // clang-format off general.add_options() ("help,h", "Display available options") - ("nnpackage", po::value()->required()); + ("nnpackage", po::value()->required()) + ("dump,d", po::value()->default_value(""), "Output filename"); // clang-format on _options.add(general); @@ -71,6 +71,11 @@ void Args::Parse(const int argc, char **argv) exit(0); } + if (vm.count("dump")) + { + _dump_filename = vm["dump"].as(); + } + if (vm.count("nnpackage")) { _package_filename = vm["nnpackage"].as(); diff --git a/tests/tools/nnpackage_run/src/args.h b/tests/tools/nnpackage_run/src/args.h index e25d418..4268700 100644 --- a/tests/tools/nnpackage_run/src/args.h +++ b/tests/tools/nnpackage_run/src/args.h @@ -32,6 +32,7 @@ public: void print(void); const std::string &getPackageFilename(void) const { return _package_filename; } + const std::string &getDumpFilename(void) const { return _dump_filename; } private: void Initialize(); @@ -42,6 +43,7 @@ private: po::options_description _options; std::string _package_filename; + std::string _dump_filename; }; } // end of namespace NNPackageRun diff --git a/tests/tools/nnpackage_run/src/nnpackage_run.cc b/tests/tools/nnpackage_run/src/nnpackage_run.cc index 9a6383a..aeb03b6 100644 --- a/tests/tools/nnpackage_run/src/nnpackage_run.cc +++ b/tests/tools/nnpackage_run/src/nnpackage_run.cc @@ -16,6 +16,7 @@ #include "args.h" #include "tflite/Diff.h" +#include "tensor_dumper.h" #include "nnfw.h" #include @@ -120,6 +121,19 @@ int main(const int argc, char **argv) NNPR_ENSURE_STATUS(nnfw_run(session)); run_ms = NowMicros() - run_ms; + // Dump output tensors + if (!args.getDumpFilename().empty()) + { + NNPackageRun::TensorDumper dumper(args.getDumpFilename()); + dumper.dumpInt32(num_outputs); + for (uint32_t i = 0; i < num_outputs; i++) + { + nnfw_tensorinfo ti; + NNPR_ENSURE_STATUS(nnfw_output_tensorinfo(session, i, &ti)); + dumper.dumpTensor(ti, (void *)(outputs[i].data()), outputs[i].size() * sizeof(float)); + } + } + std::cout << "nnfw_prepare takes " << prepare_ms / 1e3 << " sec" << std::endl; std::cout << "nnfw_run takes " << run_ms / 1e3 << " sec" << std::endl; diff --git a/tests/tools/nnpackage_run/src/tensor_dumper.cc b/tests/tools/nnpackage_run/src/tensor_dumper.cc new file mode 100644 index 0000000..ae4ed95 --- /dev/null +++ b/tests/tools/nnpackage_run/src/tensor_dumper.cc @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "tensor_dumper.h" + +#include +#include +#include + +namespace NNPackageRun +{ +TensorDumper::TensorDumper(const std::string &filename) +{ + // TODO Handle file open/write error + file_.open(filename, std::ios::out | std::ios::binary); + dumpInt32(version); +} + +TensorDumper::~TensorDumper() { file_.close(); } + +void TensorDumper::dumpInt32(int32_t i) +{ + file_.write(reinterpret_cast(&i), sizeof(i)); +} + +void TensorDumper::dumpSizeT(size_t i) +{ + file_.write(reinterpret_cast(&i), sizeof(i)); +} + +void TensorDumper::dumpTensor(const nnfw_tensorinfo ti, void *buffer, size_t bytes) +{ + dumpInt32(ti.dtype); + dumpInt32(ti.rank); + for (uint i = 0; i < ti.rank; ++i) + dumpInt32(ti.dims[i]); + dumpSizeT(bytes); + file_.write(static_cast(buffer), bytes); +} + +} // end of namespace NNPackageRun \ No newline at end of file diff --git a/tests/tools/nnpackage_run/src/tensor_dumper.h b/tests/tools/nnpackage_run/src/tensor_dumper.h new file mode 100644 index 0000000..f274ad6 --- /dev/null +++ b/tests/tools/nnpackage_run/src/tensor_dumper.h @@ -0,0 +1,31 @@ +#ifndef __NNPACKAGE_RUN_TENSOR_DUMPER_H__ +#define __NNPACKAGE_RUN_TENSOR_DUMPER_H__ + +#include +#include +#include +#include +#include + +#include "nnfw.h" + +namespace NNPackageRun +{ + +class TensorDumper +{ +public: + TensorDumper(const std::string &filename); + void dumpTensor(const nnfw_tensorinfo ti, void *buffer, size_t bytes); + void dumpInt32(int32_t i); + void dumpSizeT(size_t i); + ~TensorDumper(); + +private: + static constexpr int version = 1; + std::ofstream file_; +}; + +} // end of namespace NNPackageRun + +#endif // __NNPACKAGE_RUN_TENSOR_DUMPER_H__ -- 2.7.4