Commit f4059729 authored by Paul's avatar Paul
Browse files

Add onnx perf driver

parent 12beb622
......@@ -88,6 +88,8 @@ struct program
void compile(const target& t);
void perf_report(std::ostream& os, std::size_t n, parameter_map params) const;
friend std::ostream& operator<<(std::ostream& os, const program& p);
friend bool operator==(const program& x, const program& y);
friend bool operator!=(const program& x, const program& y) { return !(x == y); }
......
......@@ -16,6 +16,7 @@ add_executable(read_onnx read_onnx.cpp)
rocm_clang_tidy_check(read_onnx)
target_link_libraries(read_onnx migraph_onnx)
add_executable(mnist mnist.cpp)
rocm_clang_tidy_check(mnist)
target_link_libraries(mnist migraph_cpu migraph_onnx)
......@@ -24,4 +25,8 @@ if(MIGRAPH_ENABLE_GPU)
add_executable(verify_onnx verify_onnx.cpp)
rocm_clang_tidy_check(verify_onnx)
target_link_libraries(verify_onnx migraph_onnx migraph_cpu migraph_gpu)
add_executable(perf_onnx perf_onnx.cpp)
rocm_clang_tidy_check(perf_onnx)
target_link_libraries(perf_onnx migraph_onnx migraph_cpu migraph_gpu)
endif()
#include <migraph/onnx.hpp>
#include <migraph/gpu/target.hpp>
#include <migraph/gpu/hip.hpp>
#include <migraph/generate.hpp>
#include <migraph/verify.hpp>
migraph::program::parameter_map create_param_map(const migraph::program& p, bool gpu=true)
{
migraph::program::parameter_map m;
for(auto&& x : p.get_parameter_shapes())
{
if(gpu)
m[x.first] = migraph::gpu::to_gpu(migraph::generate_argument(x.second));
else
m[x.first] = migraph::generate_argument(x.second);
}
return m;
}
int main(int argc, char const* argv[])
{
if(argc > 1)
{
std::string file = argv[1];
auto p = migraph::parse_onnx(file);
p.compile(migraph::gpu::target{});
auto m = create_param_map(p);
p.perf_report(std::cout, 10, m);
}
}
......@@ -2,6 +2,7 @@
#include <migraph/stringutils.hpp>
#include <migraph/instruction.hpp>
#include <migraph/env.hpp>
#include <migraph/time.hpp>
#include <iostream>
#include <sstream>
#include <algorithm>
......@@ -59,7 +60,7 @@ static void print_program(std::ostream& os, const program& p, F annonate)
os << " -> " << ins.result;
annonate(os, ins, names);
annonate(ins, names);
os << std::endl;
......@@ -305,6 +306,61 @@ argument program::eval(std::unordered_map<std::string, argument> params) const
return generic_eval(*this, this->impl->ctx, params, [](auto&, auto f) { f(); });
}
void program::perf_report(std::ostream& os, std::size_t n, parameter_map params) const
{
using milliseconds = std::chrono::duration<double, std::milli>;
// Run once by itself
eval(params);
// Run and time entire program
double total_acc = 0;
for(std::size_t i = 0; i< n;i++)
{
total_acc += time<milliseconds>([&] {
eval(params);
});
}
std::unordered_map<const instruction*, double> ins_acc;
// Fill the map
generic_eval(*this, this->impl->ctx, params, [&](auto& ins, auto) {
ins_acc[std::addressof(ins)] = 0;
});
// Run and time each instruction
for(std::size_t i = 0; i< n;i++)
{
generic_eval(*this, this->impl->ctx, params, [&](auto& ins, auto f) {
ins_acc[std::addressof(ins)] += time<milliseconds>(f);
});
}
// Run and time implicit overhead
double overhead_acc = 0;
for(std::size_t i = 0; i< n;i++)
{
overhead_acc += time<milliseconds>([&] {
generic_eval(*this, this->impl->ctx, params, [](auto&&...) {});
});
}
double total_time = total_acc / n;
double overhead_time = overhead_acc / n;
double overhead_percent = overhead_time*100.0 / total_time;
double total_instruction_time = 0.0;
for(auto&& p: ins_acc)
total_instruction_time += p.second / n;
double calculate_overhead_time = total_time - total_instruction_time;
double calculate_overhead_percent = calculate_overhead_time*100.0 / total_time;
print_program(os, *this, [&](auto& ins, auto&&) {
os << ": " << ins_acc[std::addressof(ins)] / n << "ms";
});
os << "Total time: " << total_time << "ms" << std::endl;
os << "Total instructions time: " << total_instruction_time << "ms" << std::endl;
os << "Overhead time: " << overhead_time << "ms" << ", " << calculate_overhead_time << "ms" << std::endl;
os << "Overhead: " << std::round(overhead_percent) << "%" << ", " << std::round(calculate_overhead_percent) << "%" << std::endl;
}
bool operator==(const program& x, const program& y) { return to_string(x) == to_string(y); }
std::ostream& operator<<(std::ostream& os, const program& p)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment