"vscode:/vscode.git/clone" did not exist on "e5f07268b9d59a141faf087fb79d7b4deb1b3ec4"
eliminate_concat.cpp 2.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iterator>
#include <migraph/eliminate_concat.hpp>
#include <migraph/program.hpp>
#include <migraph/instruction.hpp>
#include <migraph/operators.hpp>
#include <migraph/iterator_for.hpp>
#include <migraph/dfor.hpp>

namespace migraph {
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
22
        // We can only do this optimization when concat axis is either the leftmost
23
24
25
        // 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
26
        auto lens      = ins->inputs().front()->get_shape().lens();
27
        auto concat_op = concat_opt.get_concat(ins->get_operator());
Scott Thornton's avatar
Scott Thornton committed
28
29
        if(concat_op.axis == 0 ||
           std::all_of(lens.begin(), lens.begin() + concat_op.axis, [](auto x) { return x == 1; }))
30
31
32
        {
            // Last input should be an allocation
            auto last = ins->inputs().back();
Scott Thornton's avatar
Scott Thornton committed
33
34
            if(last->name() != concat_opt.allocate())
                continue;
35
36
37
            // Where are the allocations for the tensors to be concatenated?
            std::vector<instruction_ref> allocations;

Scott Thornton's avatar
Scott Thornton committed
38
            for(auto ins2 = ins->inputs().begin(); ins2 != ins->inputs().end() - 1; ins2++)
39
40
            {
                auto last2 = (*ins2)->inputs().back();
Scott Thornton's avatar
Scott Thornton committed
41
                if(last2->name() == concat_opt.allocate())
42
43
44
45
                {
                    allocations.push_back(last2);
                }
            }
Scott Thornton's avatar
Scott Thornton committed
46
            // Need to sort the allocations, so that we know where to
47
            // insert the "super"-allocation
Scott Thornton's avatar
Scott Thornton committed
48
49
50
51
            std::sort(
                allocations.begin(), allocations.end(), [&](instruction_ref x, instruction_ref y) {
                    return std::distance(p.begin(), x) < std::distance(p.begin(), y);
                });
52
            // Move "super" allocation to the front
Scott Thornton's avatar
Scott Thornton committed
53
54
            auto first         = allocations.front();
            auto super         = p.move_instruction(last, first);
55
            std::size_t offset = 0;
Scott Thornton's avatar
Scott Thornton committed
56
            for(auto x : allocations)
57
58
            {
                migraph::op::load op{x->get_shape(), offset};
wsttiger's avatar
wsttiger committed
59
                // migraph::op::load op{x->get_shape(), 0};
60
                p.replace_instruction(x, op, {super});
wsttiger's avatar
wsttiger committed
61
                offset += x->get_shape().bytes();
62
63
            }
            std::vector<instruction_ref> args = {super};
Scott Thornton's avatar
Scott Thornton committed
64
            std::copy(ins->inputs().begin(), ins->inputs().end() - 1, std::back_inserter(args));
65
66
67
68
69
            p.replace_instruction(ins, migraph::op::identity{}, args);
        }
    }
}
} // namespace migraph