eliminate_concat.cpp 3.12 KB
Newer Older
1
#include <iterator>
Paul's avatar
Paul committed
2
3
4
#include <migraphx/eliminate_concat.hpp>
#include <migraphx/program.hpp>
#include <migraphx/instruction.hpp>
5
6
#include <migraphx/op/load.hpp>
#include <migraphx/op/identity.hpp>
Paul's avatar
Paul committed
7
8
#include <migraphx/iterator_for.hpp>
#include <migraphx/dfor.hpp>
9

Paul's avatar
Paul committed
10
namespace migraphx {
Paul's avatar
Paul committed
11
inline namespace MIGRAPHX_INLINE_NS {
12
13
14
15
16
17
18
19
20
21
22
23
void eliminate_concat::apply(program& p) const
{
    for(auto ins : iterator_for(p))
    {
        // Look for the concat operator
        if(ins->name() != concat_opt.name())
            continue;
        // If any inputs are literals then abort
        if(std::any_of(ins->inputs().begin() + 1, ins->inputs().end(), [](auto arg) {
               return arg->name() == "@literal";
           }))
            continue;
Scott Thornton's avatar
Scott Thornton committed
24
        // We can only do this optimization when concat axis is either the leftmost
25
26
27
        // axis OR the sizes to the left of this axis are all equal to 1
        // Since we've already checked that the non-axis dimensions are identical
        // we only need to check the first input
Scott Thornton's avatar
Scott Thornton committed
28
        auto lens      = ins->inputs().front()->get_shape().lens();
29
        auto concat_op = concat_opt.get_concat(ins->get_operator());
Scott Thornton's avatar
Scott Thornton committed
30
31
        if(concat_op.axis == 0 ||
           std::all_of(lens.begin(), lens.begin() + concat_op.axis, [](auto x) { return x == 1; }))
32
33
34
        {
            // Last input should be an allocation
            auto last = ins->inputs().back();
Scott Thornton's avatar
Scott Thornton committed
35
36
            if(last->name() != concat_opt.allocate())
                continue;
37
38
39
            // Where are the allocations for the tensors to be concatenated?
            std::vector<instruction_ref> allocations;

Paul's avatar
Paul committed
40
41
42
43
44
            std::transform(
                ins->inputs().begin(),
                std::prev(ins->inputs().end()),
                std::back_inserter(allocations),
                [&](instruction_ref x) { return instruction::get_output_alias(x, true); });
Paul's avatar
Paul committed
45

Paul's avatar
Paul committed
46
47
48
            if(std::any_of(allocations.begin(), allocations.end(), [&](auto x) {
                   return x->name() != concat_opt.allocate();
               }))
Paul's avatar
Paul committed
49
50
                continue;

Scott Thornton's avatar
Scott Thornton committed
51
            // Need to sort the allocations, so that we know where to
52
            // insert the "super"-allocation
Scott Thornton's avatar
Scott Thornton committed
53
54
55
56
            std::sort(
                allocations.begin(), allocations.end(), [&](instruction_ref x, instruction_ref y) {
                    return std::distance(p.begin(), x) < std::distance(p.begin(), y);
                });
57
            // Move "super" allocation to the front
Paul's avatar
Paul committed
58
59
            auto first = allocations.front();
            auto super = p.move_instruction(last, first);
Paul's avatar
Paul committed
60
            // Replace each allocation with a load
61
            std::size_t offset = 0;
Paul's avatar
Paul committed
62
            for(auto alloc : allocations)
63
            {
Paul's avatar
Paul committed
64
65
66
                op::load op{alloc->get_shape(), offset};
                p.replace_instruction(alloc, op, {super});
                offset += alloc->get_shape().bytes();
67
68
            }
            std::vector<instruction_ref> args = {super};
Scott Thornton's avatar
Scott Thornton committed
69
            std::copy(ins->inputs().begin(), ins->inputs().end() - 1, std::back_inserter(args));
Paul's avatar
Paul committed
70
            p.replace_instruction(ins, migraphx::op::identity{}, args);
71
72
73
        }
    }
}
Paul's avatar
Paul committed
74
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
75
} // namespace migraphx