diff --git a/apps/cpp_rtvm/main.cc b/apps/cpp_rtvm/main.cc index 332bfee7c5b67..c38a5f62bd9a1 100644 --- a/apps/cpp_rtvm/main.cc +++ b/apps/cpp_rtvm/main.cc @@ -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" @@ -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; }; @@ -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")); } @@ -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="); } /*! @@ -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(); diff --git a/apps/cpp_rtvm/tvm_runner.cc b/apps/cpp_rtvm/tvm_runner.cc index c61ed9ec7f2c5..2fd4f2281e010 100644 --- a/apps/cpp_rtvm/tvm_runner.cc +++ b/apps/cpp_rtvm/tvm_runner.cc @@ -26,7 +26,6 @@ #include -#include #include #include #include @@ -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(ifs)), std::istreambuf_iterator()); f_set(String(bytes)); }