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

Fixed instruction::replace() logic. #3553

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
26 changes: 26 additions & 0 deletions src/instruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,43 @@
{
result = r;
std::deque<instruction_ref> q(output.begin(), output.end());
std::unordered_map<instruction*, int> arg_count;
std::unordered_map<instruction*, instruction_ref> addr2ref;
while(not q.empty())
{
instruction_ref ins = q.front();
q.pop_front();
assert(ins->name() == "@return" or ins->name().front() != '@');
int n = ins->arguments.size();
if(n > 1)
{
instruction* addr = as_address(ins);
if(arg_count.find(addr) == arg_count.end())
{
arg_count[addr] = n;

Check warning on line 80 in src/instruction.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

performance: Searching before insertion is not necessary. Instead of 'arg_count[addr]=n' consider using 'arg_count.try_emplace(addr, n);'. [stlFindInsert]
addr2ref[addr] = ins;
}
arg_count[addr] -= 1;
if(arg_count[addr] > 0)
{
//If there is still arguments that hasn't been replaced, skip this instruction and wait for it to be called later.
continue;
}
arg_count.erase(addr);
}
shape new_r = compute_shape(ins->op, ins->arguments, ins->module_args);
if(new_r != ins->result)
{
ins->result = new_r;
std::copy(ins->output.begin(), ins->output.end(), std::back_inserter(q));
}
if(q.empty())
{
for(auto&& kv: arg_count){
instruction* addr = kv.first;
q.push_back(addr2ref[addr]);
}
}
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions test/instruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,24 @@ TEST_CASE(check_replace_shape)
EXPECT(add->get_shape() == r);
}

TEST_CASE(check_replace_dag)
{
migraphx::module m;
migraphx::shape s{migraphx::shape::float_type, {3, 2}};
auto input = m.add_parameter("x", s);
auto reduce = m.add_instruction(migraphx::make_op("reduce_sum", {{"axes", {0}}}), input);
auto abs = m.add_instruction(migraphx::make_op("abs"), reduce);
auto sin = m.add_instruction(migraphx::make_op("sin"), reduce);
auto add = m.add_instruction(migraphx::make_op("add"), abs, sin);
auto add2 = m.add_instruction(migraphx::make_op("add"), add, reduce);

reduce->replace(migraphx::make_op("reduce_sum", {{"axes", {1}}}));

migraphx::shape r{migraphx::shape::float_type, {3, 1}};
EXPECT(reduce->get_shape() == r);
EXPECT(abs->get_shape() == r);
EXPECT(sin->get_shape() == r);
EXPECT(add->get_shape() == r);
EXPECT(add2->get_shape() == r);
}
int main(int argc, const char* argv[]) { test::run(argc, argv); }
Loading