Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CLI TOOLS][RTVM] Improve rtvm tool with new options to measure native performance #15818

Merged
merged 3 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 29 additions & 26 deletions apps/cpp_rtvm/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -226,26 +226,26 @@ int ExecuteModel(ToolArgs& args) {
#endif

// Initialize TVM Runner
auto runner = new TVMRunner(args.model, args.device);
TVMRunner runner = TVMRunner(args.model, args.device);

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

// Query Model meta Information
TVMMetaInfo mInfo = runner->GetMetaInfo();
TVMMetaInfo mInfo = runner.GetMetaInfo();

// Print Meta Information
if (args.dump_meta) runner->PrintMetaInfo();
if (args.dump_meta) runner.PrintMetaInfo();

int total_exec_time = 0;

if (args.profile) {
if (args.dry_run) {
for (int ii = 0; ii < args.dry_run; ++ii) {
runner->Run();
runner.Run();
}
TVMSynchronize(GetTVMDevice(args.device), 0, nullptr);
}
Expand All @@ -269,7 +269,7 @@ int ExecuteModel(ToolArgs& args) {
DLDevice{GetTVMDevice(args.device), 0});
input_data_odd.insert({elem.first, ndarr});
} else {
char* data = (char*)malloc(runner->GetInputMemSize(elem.first));
char* data = (char*)malloc(runner.GetInputMemSize(elem.first));
input_data.insert({elem.first, data});
}
}
Expand All @@ -287,7 +287,7 @@ int ExecuteModel(ToolArgs& args) {
DLDevice{GetTVMDevice(args.device), 0});
output_data_odd.insert({elem.first, ndarr});
} else {
char* data = (char*)malloc(runner->GetOutputMemSize(elem.first));
char* data = (char*)malloc(runner.GetOutputMemSize(elem.first));
output_data.insert({elem.first, data});
}
}
Expand All @@ -299,33 +299,33 @@ int ExecuteModel(ToolArgs& args) {
for (auto& elem : mInfo.input_info) {
if (args.zero_copy) {
if (ii % 2) {
runner->SetInput(elem.first, input_data_even[elem.first]);
runner.SetInput(elem.first, input_data_even[elem.first]);
} else {
runner->SetInput(elem.first, input_data_odd[elem.first]);
runner.SetInput(elem.first, input_data_odd[elem.first]);
}
} else {
runner->SetInput(elem.first, input_data[elem.first]);
runner.SetInput(elem.first, input_data[elem.first]);
}
}

if (args.zero_copy) {
// With zero copy set the result NDArray up front
for (auto& elem : mInfo.output_info) {
if (ii % 2) {
runner->SetOutput(elem.first, output_data_even[elem.first]);
runner.SetOutput(elem.first, output_data_even[elem.first]);
} else {
runner->SetOutput(elem.first, output_data_odd[elem.first]);
runner.SetOutput(elem.first, output_data_odd[elem.first]);
}
}
}

// Run the model
runner->Run();
runner.Run();

if (!args.zero_copy) {
// W/o zero copy we need to invoke explicite data copy
for (auto& elem : mInfo.output_info) {
runner->GetOutput(elem.first, output_data[elem.first]);
runner.GetOutput(elem.first, output_data[elem.first]);
}
} else {
// Just wait for the run to complete.
Expand All @@ -350,43 +350,46 @@ int ExecuteModel(ToolArgs& args) {
} else if (!args.input.empty() && !args.output.empty()) {
LOG(INFO) << "Executing with Input:" << args.input << " Output:" << args.output;
// Set Input from Numpy Input
runner->SetInput(args.input);
runner.SetInput(args.input);
// Run the model
runner->Run();
runner.Run();
// Get Output as Numpy dump
runner->GetOutput(args.output);
runner.GetOutput(args.output);
} else {
LOG(INFO) << "Executing dry run ... ";
// Set random input for all inputs
for (auto& elem : mInfo.input_info) {
LOG(INFO) << "Set Random Input for :" << elem.first;
auto shape = elem.second.first;
size_t ssize = runner->GetInputMemSize(elem.first);
size_t ssize = runner.GetInputMemSize(elem.first);
char* data = (char*)malloc(ssize);
LOG(INFO) << "Random Input Size:" << ssize << " bytes";
runner->SetInput(elem.first, data);
runner.SetInput(elem.first, data);
free(data);
}
// Run the model
runner->Run();
runner.Run();
// Get Output and dump few values
for (auto& elem : mInfo.output_info) {
LOG(INFO) << "Get Output for :" << elem.first;
auto shape = elem.second.first;
size_t ssize = runner->GetOutputMemSize(elem.first);
size_t ssize = runner.GetOutputMemSize(elem.first);
char* data = (char*)malloc(ssize);
runner->GetOutput(elem.first, data);
runner.GetOutput(elem.first, data);
LOG(INFO) << "Output Size:" << ssize << " bytes";
free(data);
}
}

if (args.profile) {
// Print Stats
runner.PrintStats();
}
auto tstart = std::chrono::high_resolution_clock::now();
delete runner;
runner.~TVMRunner();
srkreddy1238 marked this conversation as resolved.
Show resolved Hide resolved
auto tend = std::chrono::high_resolution_clock::now();

if (args.profile) {
// Print Stats
runner->PrintStats();
LOG(INFO) << "Average ExecTime :" << total_exec_time / args.run_count << " ms";
LOG(INFO) << "Unload Time :" << static_cast<double>((tend - tstart).count()) / 1e6
<< " ms";
Expand Down
2 changes: 0 additions & 2 deletions apps/cpp_rtvm/tvm_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ inline size_t GetMemSize(NDArray& narr) {
* \return The memory size.
*/
size_t TVMRunner::GetInputMemSize(std::string input_id) {
// LOG(INFO) << "TVMRunner::GetInputMemSize:" << input_id;
NDArray in_arr = r_graph_handle.GetFunction("get_input")(input_id);
auto ssize = GetMemSize(in_arr);

Expand All @@ -206,7 +205,6 @@ size_t TVMRunner::GetInputMemSize(std::string input_id) {
* \return The memory size.
*/
size_t TVMRunner::GetOutputMemSize(std::string output_id) {
// LOG(INFO) << "TVMRunner::GetOutputMemSize:" << output_id;
NDArray out_arr = r_graph_handle.GetFunction("get_output")(output_id);
auto ssize = GetMemSize(out_arr);

Expand Down