Commit aff10174 authored by umangyadav's avatar umangyadav
Browse files

dump IR passes into seperate files, dump modules passes into seperate directories

parent 671f24be
...@@ -201,10 +201,7 @@ rocm_install_targets( ...@@ -201,10 +201,7 @@ rocm_install_targets(
) )
check_cxx_linker_flag(-lstdc++fs HAS_LIB_STD_FILESYSTEM)
if(HAS_LIB_STD_FILESYSTEM)
target_link_libraries(migraphx PRIVATE -lstdc++fs) target_link_libraries(migraphx PRIVATE -lstdc++fs)
endif()
target_link_libraries(migraphx PRIVATE -ldl) target_link_libraries(migraphx PRIVATE -ldl)
......
...@@ -61,7 +61,7 @@ struct program ...@@ -61,7 +61,7 @@ struct program
instruction_ref validate() const; instruction_ref validate() const;
void compile(const target& t, compile_options options = compile_options{}); void compile(const target& t, compile_options options = compile_options{}, std::string ir_dump_path="passes");
bool is_compiled() const; bool is_compiled() const;
......
#ifndef MIGRAPHX_GUARD_RTGLIB_TRACER_HPP #ifndef MIGRAPHX_GUARD_RTGLIB_TRACER_HPP
#define MIGRAPHX_GUARD_RTGLIB_TRACER_HPP #define MIGRAPHX_GUARD_RTGLIB_TRACER_HPP
#include <ostream> #include <fstream>
#include <migraphx/functional.hpp> #include <migraphx/functional.hpp>
#include <migraphx/config.hpp> #include <migraphx/config.hpp>
#include <migraphx/filesystem.hpp>
namespace migraphx { namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS { inline namespace MIGRAPHX_INLINE_NS {
...@@ -12,24 +12,31 @@ struct tracer ...@@ -12,24 +12,31 @@ struct tracer
{ {
tracer() {} tracer() {}
tracer(std::ostream& s) : os(&s) {} tracer(std::string dump_directory) : dump_dir(dump_directory), counter(0) {}
bool enabled() const { return os != nullptr; } bool enabled() const { return !dump_dir.empty(); }
template <class... Ts> template <class... Ts>
void operator()(const Ts&... xs) const void operator()(const std::string& program_name, const Ts&... xs)
{ {
if(os != nullptr) if(this->enabled()) {
{ fs::path dir_path = fs::current_path() / this->dump_dir;
swallow{*os << xs...}; if(not fs::exists(dir_path)) {
*os << std::endl; fs::create_directories(dir_path);
}
fs::path ir_file_path = dir_path / (std::to_string(this->counter++)+"_"+program_name+".mxr");
std::ofstream ofs(ir_file_path);
swallow{ofs<<xs...};
ofs<<std::endl;
ofs.close();
} }
} }
std::string dump_dir;
private: private:
std::ostream* os = nullptr; uint counter;
}; };
} // namespace MIGRAPHX_INLINE_NS } // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx } // namespace migraphx
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <algorithm> #include <algorithm>
#include <unordered_map>
#include <utility> #include <utility>
namespace migraphx { namespace migraphx {
...@@ -17,13 +18,12 @@ inline namespace MIGRAPHX_INLINE_NS { ...@@ -17,13 +18,12 @@ inline namespace MIGRAPHX_INLINE_NS {
MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_TRACE_PASSES); MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_TRACE_PASSES);
void validate_pass(module& mod, const pass& p, tracer trace) void validate_pass(module& mod, const pass& p)
{ {
(void)mod; (void)mod;
(void)p; (void)p;
(void)trace;
#ifndef NDEBUG #ifndef NDEBUG
trace("Validate ..."); std::cout << "Validate..." << std::endl;
auto invalid = mod.validate(); auto invalid = mod.validate();
if(invalid != mod.end()) if(invalid != mod.end())
{ {
...@@ -31,14 +31,13 @@ void validate_pass(module& mod, const pass& p, tracer trace) ...@@ -31,14 +31,13 @@ void validate_pass(module& mod, const pass& p, tracer trace)
MIGRAPHX_THROW(p.name() + " pass produces invalid program at instruction " + MIGRAPHX_THROW(p.name() + " pass produces invalid program at instruction " +
std::to_string(index) + ": " + invalid->name()); std::to_string(index) + ": " + invalid->name());
} }
trace();
#endif #endif
} }
void run_pass(program& prog, const pass& p, tracer trace)
void run_pass(program& prog, const pass& p, tracer& trace)
{ {
trace("Pass: ", p.name());
p.apply(prog); p.apply(prog);
trace(prog); trace(p.name(), prog);
} }
struct module_pm : module_pass_manager struct module_pm : module_pass_manager
...@@ -72,11 +71,10 @@ struct module_pm : module_pass_manager ...@@ -72,11 +71,10 @@ struct module_pm : module_pass_manager
virtual void run_pass(const pass& p) override virtual void run_pass(const pass& p) override
{ {
assert(mod); assert(mod);
trace("Module: ", mod->name(), ", Pass: ", p.name());
assert(mod->validate() == mod->end()); assert(mod->validate() == mod->end());
p.apply(*this); p.apply(*this);
trace(*mod); trace(p.name(), *mod);
validate_pass(*mod, p, *t); validate_pass(*mod, p);
} }
}; };
...@@ -85,7 +83,7 @@ module& get_module(module_pass_manager& mpm) { return mpm.get_module(); } ...@@ -85,7 +83,7 @@ module& get_module(module_pass_manager& mpm) { return mpm.get_module(); }
void run_passes(module& mod, const std::vector<pass>& passes, tracer trace) void run_passes(module& mod, const std::vector<pass>& passes, tracer trace)
{ {
if(enabled(MIGRAPHX_TRACE_PASSES{})) if(enabled(MIGRAPHX_TRACE_PASSES{}))
trace = tracer{std::cout}; trace = tracer{mod.name()+"_passes"};
for(const auto& p : passes) for(const auto& p : passes)
{ {
module_pm{&mod, nullptr, &trace}.run_pass(p); module_pm{&mod, nullptr, &trace}.run_pass(p);
...@@ -94,16 +92,22 @@ void run_passes(module& mod, const std::vector<pass>& passes, tracer trace) ...@@ -94,16 +92,22 @@ void run_passes(module& mod, const std::vector<pass>& passes, tracer trace)
void run_passes(program& prog, const std::vector<pass>& passes, tracer trace) void run_passes(program& prog, const std::vector<pass>& passes, tracer trace)
{ {
if(enabled(MIGRAPHX_TRACE_PASSES{})) if(enabled(MIGRAPHX_TRACE_PASSES{}) and not trace.enabled())
trace = tracer{std::cout}; trace = tracer{"passes"};
auto module_trace = trace;
std::unordered_map<std::string, tracer> module_tracer_map;
for(const auto& p : passes) for(const auto& p : passes)
{ {
auto mods = prog.get_modules(); auto mods = prog.get_modules();
for(const auto& mod : reverse(mods)) for(const auto& mod : reverse(mods))
{ {
if(!module_tracer_map.count(mod->name())) {
module_tracer_map[mod->name()] = module_trace;
module_tracer_map[mod->name()].dump_dir += "/"+mod->name();
}
if(mod->bypass()) if(mod->bypass())
continue; continue;
module_pm{mod, &prog, &trace}.run_pass(p); module_pm{mod, &prog, &module_tracer_map[mod->name()]}.run_pass(p);
} }
run_pass(prog, p, trace); run_pass(prog, p, trace);
} }
......
...@@ -138,16 +138,15 @@ instruction_ref program::validate() const ...@@ -138,16 +138,15 @@ instruction_ref program::validate() const
bool program::is_compiled() const { return not this->impl->target_name.empty(); } bool program::is_compiled() const { return not this->impl->target_name.empty(); }
void program::compile(const target& t, compile_options options) void program::compile(const target& t, compile_options options, std::string ir_dump_path)
{ {
assert(not this->is_compiled()); assert(not this->is_compiled());
this->impl->target_name = t.name(); this->impl->target_name = t.name();
this->impl->ctx = t.get_context(); this->impl->ctx = t.get_context();
if(enabled(MIGRAPHX_TRACE_COMPILE{})) if(enabled(MIGRAPHX_TRACE_COMPILE{}))
options.trace = tracer{std::cout}; options.trace = tracer{t.name()+"_"+ir_dump_path};
options.trace(*this); options.trace("input_program", *this);
options.trace();
auto&& passes = t.get_passes(this->impl->ctx, options); auto&& passes = t.get_passes(this->impl->ctx, options);
run_passes(*this, passes, options.trace); run_passes(*this, passes, options.trace);
......
...@@ -90,7 +90,7 @@ void quantize_int8(program& prog, ...@@ -90,7 +90,7 @@ void quantize_int8(program& prog,
// use the calibration data to compute the quantization scale // use the calibration data to compute the quantization scale
auto capture_prog = prog; auto capture_prog = prog;
capture_prog.compile(t); capture_prog.compile(t, compile_options{}, "quantization");
// use all calibration data to run the program to calculate the // use all calibration data to run the program to calculate the
// quantization scale and shift // quantization scale and shift
......
...@@ -13,7 +13,7 @@ namespace cpu { ...@@ -13,7 +13,7 @@ namespace cpu {
struct lowering struct lowering
{ {
std::string name() const { return "cpu::lowering"; } std::string name() const { return "lowering"; }
void apply(module& m) const; void apply(module& m) const;
}; };
......
...@@ -14,7 +14,7 @@ struct lowering ...@@ -14,7 +14,7 @@ struct lowering
{ {
context* ctx; context* ctx;
bool offload_copy; bool offload_copy;
std::string name() const { return "gpu::lowering"; } std::string name() const { return "lowering"; }
void apply(module& m) const; void apply(module& m) const;
}; };
......
...@@ -10,7 +10,7 @@ namespace ref { ...@@ -10,7 +10,7 @@ namespace ref {
struct lowering struct lowering
{ {
std::string name() const { return "ref::lowering"; } std::string name() const { return "lowering"; }
void apply(module& m) const; void apply(module& m) const;
}; };
......
...@@ -41,7 +41,7 @@ inline void compile_check(migraphx::program& p, const migraphx::target& t, bool ...@@ -41,7 +41,7 @@ inline void compile_check(migraphx::program& p, const migraphx::target& t, bool
std::stringstream ss; std::stringstream ss;
migraphx::compile_options options; migraphx::compile_options options;
if(show_trace) if(show_trace)
options.trace = migraphx::tracer{std::cout}; options.trace = migraphx::tracer{t.name()+"_passes"};
p.compile(t, options); p.compile(t, options);
if(shapes.size() != p.get_output_shapes().size()) if(shapes.size() != p.get_output_shapes().size())
{ {
......
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