eliminate_concat.cpp 4.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
22
23
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
24
#include <iterator>
Paul's avatar
Paul committed
25
26
27
#include <migraphx/eliminate_concat.hpp>
#include <migraphx/program.hpp>
#include <migraphx/instruction.hpp>
28
29
#include <migraphx/op/load.hpp>
#include <migraphx/op/identity.hpp>
Paul's avatar
Paul committed
30
#include <migraphx/iterator_for.hpp>
31
#include <migraphx/ranges.hpp>
32
33
#include <migraphx/make_op.hpp>

Paul's avatar
Paul committed
34
#include <migraphx/dfor.hpp>
35
#include <migraphx/tune_axis.hpp>
36

Paul's avatar
Paul committed
37
namespace migraphx {
Paul's avatar
Paul committed
38
inline namespace MIGRAPHX_INLINE_NS {
39
void eliminate_concat::apply(module& m) const
40
{
41
    for(auto ins : iterator_for(m))
42
43
44
45
    {
        // Look for the concat operator
        if(ins->name() != concat_opt.name())
            continue;
46
47
48
49
50
51
52
53
        // If any inputs are builtin or context free then abort
        // If any inputs are used more than once, then abort since there could
        // be errors due to aliasing
        if(std::any_of(ins->inputs().begin(), ins->inputs().end(), [](auto arg) {
               return arg->name().front() == '@' or
                      (arg->get_operator().is_context_free() and
                       not contains({"concat", "identity"}, arg->name())) or
                      arg->outputs().size() > 1;
54
55
           }))
            continue;
Scott Thornton's avatar
Scott Thornton committed
56
        // We can only do this optimization when concat axis is either the leftmost
57
58
59
        // 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
60
61
62
        auto lens              = ins->inputs().front()->get_shape().lens();
        auto concat_op         = concat_opt.get_concat(ins->get_operator());
        std::size_t axis_index = tune_axis(lens.size(), concat_op.axis, concat_op.name());
Shucai Xiao's avatar
Shucai Xiao committed
63
        if(axis_index == 0 ||
64
           std::all_of(lens.begin(), lens.begin() + axis_index, [](auto x) { return x == 1; }))
65
66
67
        {
            // Last input should be an allocation
            auto last = ins->inputs().back();
Scott Thornton's avatar
Scott Thornton committed
68
69
            if(last->name() != concat_opt.allocate())
                continue;
70
71
72
            // Where are the allocations for the tensors to be concatenated?
            std::vector<instruction_ref> allocations;

Paul's avatar
Paul committed
73
74
75
76
77
            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
78

Paul's avatar
Paul committed
79
80
81
            if(std::any_of(allocations.begin(), allocations.end(), [&](auto x) {
                   return x->name() != concat_opt.allocate();
               }))
Paul's avatar
Paul committed
82
83
                continue;

Scott Thornton's avatar
Scott Thornton committed
84
            // Need to sort the allocations, so that we know where to
85
            // insert the "super"-allocation
86
87
88
89
            auto sorted_allocations = allocations;
            std::sort(sorted_allocations.begin(),
                      sorted_allocations.end(),
                      [&](instruction_ref x, instruction_ref y) {
90
                          return std::distance(m.begin(), x) < std::distance(m.begin(), y);
91
                      });
92
            // Move "super" allocation to the front
93
            auto first = sorted_allocations.front();
94
            auto super = m.move_instruction(last, first);
Paul's avatar
Paul committed
95
            // Replace each allocation with a load
96
            std::size_t offset = 0;
Paul's avatar
Paul committed
97
            for(auto alloc : allocations)
98
            {
Paul's avatar
Paul committed
99
                op::load op{alloc->get_shape(), offset};
100
                m.replace_instruction(alloc, op, {super});
Paul's avatar
Paul committed
101
                offset += alloc->get_shape().bytes();
102
103
            }
            std::vector<instruction_ref> args = {super};
Scott Thornton's avatar
Scott Thornton committed
104
            std::copy(ins->inputs().begin(), ins->inputs().end() - 1, std::back_inserter(args));
105
            m.replace_instruction(ins, migraphx::make_op("identity"), args);
106
107
108
        }
    }
}
Paul's avatar
Paul committed
109
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
110
} // namespace migraphx