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

Show mlir program when tracing benchmarking #2741

Merged
merged 18 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
8 changes: 8 additions & 0 deletions src/targets/gpu/compile_ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ struct compiled_result
{
compiler_replace replace;
instruction_ref ins;

friend std::ostream& operator<<(std::ostream& os, const compiled_result& cr)
{
cr.replace.trace(os, cr.ins);
return os;
}
};

struct compile_plan
Expand Down Expand Up @@ -179,6 +185,8 @@ struct compile_plan
std::cout << "No binary" << std::endl;
return std::numeric_limits<double>::max();
}
if(trace_level > 2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add this option to documents

std::cout << *cr << std::endl;
auto t = time_op(
*ctx, cr->replace.code_object, to_shapes(cr->ins->inputs()), 20);
if(trace_level > 1)
Expand Down
27 changes: 22 additions & 5 deletions src/targets/gpu/include/migraphx/gpu/compiler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,28 @@ struct compiler_replace
compiler_replace(const operation& op) : code_object{op} {}

template <class F>
compiler_replace(const operation& op, F f)
: code_object{op},
replace_fn([=](const compiler_replace& cr, module& m, instruction_ref ins) {
f(m, ins, cr.code_object);
})
compiler_replace(const operation& op, F f) : code_object{op}, replace_fn(make_replace(f))
{
}

template <class F, class Trace>
compiler_replace(const operation& op, F f, Trace t)
: code_object{op}, replace_fn(make_replace(f)), trace_fn(t)
{
}

operation code_object = {};
std::function<void(const compiler_replace& cr, module& m, instruction_ref ins)> replace_fn =
nullptr;
std::function<void(std::ostream& os, instruction_ref ins)> trace_fn = nullptr;

template <class F>
static auto make_replace(F f)
{
return [=](const compiler_replace& cr, module& m, instruction_ref ins) {
f(m, ins, cr.code_object);
};
}

void replace(module& m, instruction_ref ins) const
{
Expand All @@ -67,6 +78,12 @@ struct compiler_replace
else
m.replace_instruction(ins, code_object, ins->inputs());
}

void trace(std::ostream& os, instruction_ref ins) const
{
if(trace_fn)
trace_fn(os, ins);
}
};

using compiler_compile =
Expand Down
1 change: 1 addition & 0 deletions src/targets/gpu/include/migraphx/gpu/mlir.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ struct module;
namespace gpu {

MIGRAPHX_GPU_EXPORT std::string dump_mlir(const module& m);
MIGRAPHX_GPU_EXPORT std::string dump_mlir(const module& m, const std::vector<shape>& inputs);
MIGRAPHX_GPU_EXPORT code_object_op compile_mlir(const context& migraphx_ctx,
module m,
const std::vector<instruction_ref>& inputs,
Expand Down
13 changes: 11 additions & 2 deletions src/targets/gpu/jit/mlir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ struct mlir_compiler : compiler<mlir_compiler>

compiler_replace insert(code_object_op co) const
{
return {std::move(co), [](module& m, instruction_ref ins, const operation& op) {
return {std::move(co),
[](module& m, instruction_ref ins, const operation& op) {
auto mlir = insert_mlir(m, ins, any_cast<code_object_op>(op), ins->inputs());
m.replace_instruction(ins, mlir);
}};
},
&trace};
}

optional<tuning_config> get_tuning_config(const context& ctx,
Expand All @@ -61,6 +63,13 @@ struct mlir_compiler : compiler<mlir_compiler>
auto* smod = ins->module_inputs().front();
return get_tuning_config_mlir(ctx, *smod, shapes, exhaustive);
}

static void trace(std::ostream& os, instruction_ref ins)
{
auto shapes = to_shapes(ins->inputs());
auto* smod = ins->module_inputs().front();
os << dump_mlir(*smod, shapes);
}
};

} // namespace gpu
Expand Down
37 changes: 27 additions & 10 deletions src/targets/gpu/mlir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -934,14 +934,6 @@ struct mlir_program
std::string sym_name;
};

std::string dump_mlir(const module& m)
{
mlir_program mp;
mp.parse(m);
auto mod_op = mlirModuleGetOperation(mp.mmodule.get());
return mlir_print(&mlirOperationPrint, mod_op);
}

void adjust_param_shapes(module& m, const std::vector<shape>& inputs)
{
auto names = m.get_parameter_names();
Expand All @@ -960,6 +952,24 @@ void adjust_param_shapes(module& m, const std::vector<shape>& inputs)
}
}

std::string dump_mlir(const module& m, const std::vector<shape>& inputs)
{
module mm;
const_module_ref mr = &m;
if(not inputs.empty())
{
mm = m;
mr = &mm;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does it require const_ref ? Shouldn't just copied module mm work ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only want to copy the module if there is no input shapes because we wont be adjusting the parameters.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Module with empty input shape is unlikely case it would have been const-folded.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is better to assume inputs are not empty. it would be simpler

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is two overloads to dump_mlir. One just takes the module(which we dont want to copy) and the other overload takes the input shapes, which might be different than the shapes in the module so we need to modify the module which we will use a copy for this case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and the other overload takes the input shapes, which might be different than the shapes in the module so we need to modify the module which we will use a copy for this case.

Yes but those input shapes parameter would be input arguments to the precompile_op instruction. If they are empty that means MLIR module also doesn't take any inputs.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the inputs are empty it means skip doing param adjustments.

adjust_param_shapes(mm, inputs);
}
mlir_program mp;
mp.parse(*mr);
auto mod_op = mlirModuleGetOperation(mp.mmodule.get());
return mlir_print(&mlirOperationPrint, mod_op);
}

std::string dump_mlir(const module& m) { return dump_mlir(m, {}); }

code_object_op compile_mlir(const context& migraphx_ctx,
module m,
const std::vector<instruction_ref>& inputs,
Expand Down Expand Up @@ -1030,13 +1040,20 @@ tuning_config get_tuning_config_mlir(const context& migraphx_ctx,

#else

std::string dump_mlir(const module&) { return {}; }

template <class T>
void use(T&)
{
}

std::string dump_mlir(const module&) { return {}; }

std::string dump_mlir(const module& m, const std::vector<shape>& inputs)
{
use(m);
use(inputs);
return {};
}

// Disabling clang-tidy warning on non-real useage.
// NOLINTBEGIN(performance-unnecessary-value-param)
code_object_op
Expand Down
Loading