"examples/vscode:/vscode.git/clone" did not exist on "97aa9b3284566a4d84c08f7c1fee3699bf694e3d"
eliminate_concat.cpp 2.98 KB
Newer Older
1
#include <iterator>
Paul's avatar
Paul committed
2
3
4
5
6
7
#include <migraphx/eliminate_concat.hpp>
#include <migraphx/program.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/operators.hpp>
#include <migraphx/iterator_for.hpp>
#include <migraphx/dfor.hpp>
8

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

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