MCPcopy Create free account
hub / github.com/ROCm/AMDMIGraphX / apply

Method apply

src/simplify_reshapes.cpp:502–555  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

500 }
501
502 void apply(module& m, const match::matcher_result& mr) const
503 {
504 auto ins = mr.result;
505 auto slice = mr.instructions["slice"];
506 auto slice_op = slice->get_operator().to_value();
507 auto axes = slice_op.at("axes").to_vector<std::size_t>();
508
509 if(ins->get_shape().scalar())
510 return;
511
512 std::vector<operation> ops;
513 auto x = ins;
514 while(contains(shape_transform_ops(), x->get_operator().name()))
515 {
516 ops.push_back(x->get_operator());
517 x = x->inputs().front();
518 }
519 if(x != slice)
520 return;
521 x = x->inputs().front();
522 std::reverse(ops.begin(), ops.end());
523 auto desc = shape_transform_descriptor::create(slice->get_shape().lens(), ops);
524
525 std::vector<std::size_t> new_axes;
526 std::transform(axes.begin(),
527 axes.end(),
528 join_back_inserter(new_axes),
529 [&](auto axis) -> std::vector<std::size_t> {
530 auto result = desc.get_dst_axes_from_src(axis);
531 if(result.size() != 1)
532 return {};
533 return result;
534 });
535
536 // Optimizes shape transforms if the slice cant be optimized
537 if(axes.size() != new_axes.size())
538 {
539 auto opt_ops = desc.generate();
540 auto y = insert_ops(m, ins, opt_ops, slice);
541 m.replace_instruction(ins, y);
542 return;
543 }
544 slice_op["axes"] = new_axes;
545
546 auto new_desc = desc.rebase(slice->inputs().front()->get_shape().lens());
547 if(new_desc.empty())
548 return;
549 new_desc.simplify();
550
551 auto opt_ops = new_desc.generate();
552 auto y = insert_ops(m, ins, opt_ops, x);
553 y = m.insert_instruction(ins, make_op("slice", slice_op), y);
554 m.replace_instruction(ins, y);
555 }
556};
557
558struct find_nop_reshapes

Callers

nothing calls this directly

Calls 15

containsFunction · 0.85
join_back_inserterFunction · 0.85
insert_opsFunction · 0.85
atMethod · 0.80
scalarMethod · 0.80
frontMethod · 0.80
lensMethod · 0.80
get_dst_axes_from_srcMethod · 0.80
replace_instructionMethod · 0.80
rebaseMethod · 0.80
simplifyMethod · 0.80
insert_instructionMethod · 0.80

Tested by

no test coverage detected