Skip to content

Commit

Permalink
Apply comments
Browse files Browse the repository at this point in the history
  • Loading branch information
echuraev committed Feb 2, 2023
1 parent 499e358 commit e19925c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
10 changes: 9 additions & 1 deletion apps/cpp_rtvm/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ static const string kUsage =
"--input - Numpy file for the model input (optional and we use random of not given)\n"
"--output - Numpy file name to dump the model output as numpy\n"
"--dump-meta - Dump model meta information\n"
"--pre-compiled - The file name of a file where pre-compiled programs should be stored"
"\n"
" Example\n"
" ./rtvm --model=keras-resnet50 --device=\"opencl\" --dump-meta\n"
Expand All @@ -66,12 +67,14 @@ static const string kUsage =
* \arg device The target device to use {llvm, cl, ...etc.}
* \arg input Numpy file for the model input
* \arg output Numpy file name to dump the model output as numpy
* \arg pre_compiled File name where pre-compiled programs should be stored
*/
struct ToolArgs {
string model;
string device;
string input;
string output;
string pre_compiled;
bool dump_meta = false;
};

Expand All @@ -84,6 +87,7 @@ void PrintArgs(const ToolArgs& args) {
LOG(INFO) << "Device = " << args.device;
LOG(INFO) << "Input = " << args.input;
LOG(INFO) << "Output = " << args.output;
LOG(INFO) << "Pre-compiled = " << args.pre_compiled;
LOG(INFO) << "Dump Metadata = " << ((args.dump_meta) ? ("True") : ("False"));
}

Expand Down Expand Up @@ -172,6 +176,8 @@ void ParseCmdArgs(int argc, char* argv[], struct ToolArgs& args) {
if (!pmeta.empty()) {
args.dump_meta = true;
}

args.pre_compiled = GetCmdOption(argc, argv, "--pre-compiled=");
}

/*!
Expand All @@ -190,7 +196,9 @@ int ExecuteModel(ToolArgs& args) {

// Load the model
runner.Load();
runner.UsePreCompiledPrograms("pre_compiled");
if (!args.pre_compiled.empty()) {
runner.UsePreCompiledPrograms(args.pre_compiled);
}

// Query Model meta Information
TVMMetaInfo mInfo = runner.GetMetaInfo();
Expand Down
16 changes: 5 additions & 11 deletions apps/cpp_rtvm/tvm_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

#include <cnpy.h>

#include <filesystem>
#include <fstream>
#include <iterator>
#include <streambuf>
Expand Down Expand Up @@ -115,27 +114,22 @@ int TVMRunner::Load(void) {

/*!
* \brief Specify if the run programs should be dumped to binary and reused in the next runs.
* \param pathToDir Path to the existed directory where pre-compiled programs should be stored.
* \param file_name File name where pre-compiled programs should be stored.
*/
void TVMRunner::UsePreCompiledPrograms(std::string pathToDir) {
void TVMRunner::UsePreCompiledPrograms(std::string file_name) {
if (r_run_was_called) {
LOG(INFO) << "TVMRunner UsePreCompiledPrograms: should be called before first run";
return;
}
if (!std::filesystem::exists(pathToDir))
ICHECK(std::filesystem::create_directories(pathToDir) == true);
std::filesystem::path binary_path = pathToDir;
auto f_get = r_mod_handle->GetFunction("opencl.GetPreCompiledPrograms", true);
auto f_set = r_mod_handle->GetFunction("opencl.SetPreCompiledPrograms", true);
if (f_get != nullptr && f_set != nullptr) {
std::string file_name = "pre_compiled.bin";
auto file_path = binary_path / file_name;
if (!std::filesystem::exists(file_path)) {
std::ifstream ifs(file_name, std::ios::in | std::ios::binary);
if (ifs.fail()) {
auto bytes = String(f_get());
std::ofstream fs(file_path.string(), std::ofstream::binary);
std::ofstream fs(file_name, std::ofstream::binary);
fs.write(bytes.c_str(), bytes.size());
} else {
std::ifstream ifs(file_path.string(), std::ios::in | std::ios::binary);
std::string bytes((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
f_set(String(bytes));
}
Expand Down

0 comments on commit e19925c

Please sign in to comment.