Skip to content

Commit

Permalink
Improve eliminate contiguous pass (ROCm#1223)
Browse files Browse the repository at this point in the history
Following up on issue ROCm#1166 and PR ROCm#1220. Using the same approach as in ROCm#1220 for parallelizing the eval calls, we can significantly reduce the time spent on eliminate_contiguous pass.
  • Loading branch information
shivadbhavsar authored May 30, 2022
1 parent d436a72 commit 86061b4
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/eliminate_contiguous.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <migraphx/stringutils.hpp>
#include <migraphx/op/contiguous.hpp>
#include <migraphx/op/identity.hpp>
#include <migraphx/par_for.hpp>
#include <utility>

namespace migraphx {
Expand Down Expand Up @@ -71,6 +72,8 @@ static bool try_compute_shape(instruction_ref ins,

void eliminate_contiguous::apply(module& m) const
{
std::vector<instruction_ref> const_instruction;

for(auto ins : iterator_for(m))
{
// return instruction should have inputs with standard shape
Expand All @@ -81,6 +84,7 @@ void eliminate_contiguous::apply(module& m) const
auto args = ins->inputs();
auto new_args = args;
auto mod_args = ins->module_inputs();

for(auto arg : ins->inputs())
{
if(arg->name() == op_name)
Expand All @@ -93,15 +97,25 @@ void eliminate_contiguous::apply(module& m) const
}
else if(prev->can_eval())
{
auto c = op::contiguous{};
auto r = c.compute(c.compute_shape({prev->get_shape()}), {prev->eval()});

auto l = m.add_literal(r.get_shape(), r.data());
m.replace_instruction(arg, l);
const_instruction.push_back(arg);
}
}
}
}

// Perform evaluations in parallel
std::vector<argument> literals(const_instruction.size());
par_for(const_instruction.size(), 1, [&](const auto i) {
auto c = op::contiguous{};
auto prev = const_instruction[i]->inputs().front();
literals[i] = c.compute(c.compute_shape({prev->get_shape()}), {prev->eval()});
});

for(size_t i = 0; i < const_instruction.size(); i++)
{
auto l = m.add_literal(literals[i].get_shape(), literals[i].data());
m.replace_instruction(const_instruction[i], l);
}
}

} // namespace MIGRAPHX_INLINE_NS
Expand Down

0 comments on commit 86061b4

Please sign in to comment.