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)
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<std::string>()->required());
+ ("nnpackage", po::value<std::string>()->required())
+ ("dump,d", po::value<std::string>()->default_value(""), "Output filename");
// clang-format on
_options.add(general);
exit(0);
}
+ if (vm.count("dump"))
+ {
+ _dump_filename = vm["dump"].as<std::string>();
+ }
+
if (vm.count("nnpackage"))
{
_package_filename = vm["nnpackage"].as<std::string>();
void print(void);
const std::string &getPackageFilename(void) const { return _package_filename; }
+ const std::string &getDumpFilename(void) const { return _dump_filename; }
private:
void Initialize();
po::options_description _options;
std::string _package_filename;
+ std::string _dump_filename;
};
} // end of namespace NNPackageRun
#include "args.h"
#include "tflite/Diff.h"
+#include "tensor_dumper.h"
#include "nnfw.h"
#include <iostream>
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;
--- /dev/null
+/*
+ * 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 <fstream>
+#include <iostream>
+#include <cstring>
+
+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<const char *>(&i), sizeof(i));
+}
+
+void TensorDumper::dumpSizeT(size_t i)
+{
+ file_.write(reinterpret_cast<const char *>(&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<char *>(buffer), bytes);
+}
+
+} // end of namespace NNPackageRun
\ No newline at end of file
--- /dev/null
+#ifndef __NNPACKAGE_RUN_TENSOR_DUMPER_H__
+#define __NNPACKAGE_RUN_TENSOR_DUMPER_H__
+
+#include <memory>
+#include <string>
+#include <vector>
+#include <stddef.h>
+#include <fstream>
+
+#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__