| 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 | |
| 558 | struct find_nop_reshapes |
nothing calls this directly
no test coverage detected