simplify_reshapes.cpp 5.86 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
#include <migraphx/simplify_reshapes.hpp>
#include <migraphx/program.hpp>
#include <migraphx/instruction.hpp>
4
#include <migraphx/op/as_shape.hpp>
5
#include <migraphx/op/transpose.hpp>
Paul's avatar
Paul committed
6
#include <migraphx/op/concat.hpp>
Paul's avatar
Paul committed
7
8
#include <migraphx/iterator_for.hpp>
#include <migraphx/ranges.hpp>
Paul's avatar
Paul committed
9
#include <migraphx/matcher.hpp>
10
#include <migraphx/permutation.hpp>
Paul's avatar
Paul committed
11
12
#include <unordered_set>

Paul's avatar
Paul committed
13
namespace migraphx {
Paul's avatar
Paul committed
14
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
15

Paul's avatar
Paul committed
16
const auto& reshaper_names()
Paul's avatar
Paul committed
17
{
18
19
20
    // clang-format off
    static const std::unordered_set<std::string> names = {
        "reshape",
21
22
23
        "contiguous",
        "squeeze",
        "unsqueeze"
24
25
    };
    // clang-format on
Paul's avatar
Paul committed
26
    return names;
Paul's avatar
Paul committed
27
28
}

Paul's avatar
Paul committed
29
bool is_reshaper(instruction_ref ins) { return contains(reshaper_names(), ins->name()); }
Paul's avatar
Paul committed
30
31
32

instruction_ref find_transpose_input(instruction_ref ins)
{
Paul's avatar
Paul committed
33
    if(ins->inputs().size() != 1)
Paul's avatar
Paul committed
34
        return ins;
Paul's avatar
Paul committed
35
    if(ins->inputs().front()->name() == "contiguous")
Paul's avatar
Paul committed
36
37
38
39
        return find_transpose_input(ins->inputs().front());
    if(ins->inputs().front()->name() == "transpose")
        return ins->inputs().front();
    return ins;
Paul's avatar
Paul committed
40
41
}

42
43
44
45
46
47
48
auto get_transpose_dims(instruction_ref ins)
{
    return any_cast<const op::transpose&>(ins->get_operator()).dims;
}

bool is_no_transpose(const std::vector<int64_t>& dims)
{
Paul's avatar
Paul committed
49
    if(dims.empty())
50
        return true;
Paul's avatar
Paul committed
51
    if(dims.front() != 0)
52
        return false;
Paul's avatar
Paul committed
53
54
    return std::adjacent_find(
               dims.begin(), dims.end(), [](auto x, auto y) { return (y - x) != 1; }) == dims.end();
55
56
}

Paul's avatar
Paul committed
57
struct find_reshaper
Paul's avatar
Paul committed
58
{
Paul's avatar
Paul committed
59
    auto matcher() const
Paul's avatar
Paul committed
60
    {
Paul's avatar
Paul committed
61
62
        return match::name(reshaper_names())(
            match::any_of[match::outputs()](match::name(reshaper_names())));
Paul's avatar
Paul committed
63
64
    }

Paul's avatar
Paul committed
65
    void apply(program& p, const match::matcher_result& mr) const
Paul's avatar
Paul committed
66
67
68
69
    {
        auto ins = mr.result;
        std::vector<instruction_ref> reshapes{ins};
        while(is_reshaper(reshapes.back()))
Paul's avatar
Paul committed
70
        {
Paul's avatar
Paul committed
71
72
73
74
75
            assert(!reshapes.back()->inputs().empty());
            assert(p.has_instruction(reshapes.back()->inputs().front()));
            auto input = reshapes.back()->inputs().front();
            reshapes.push_back(input);
        }
Paul's avatar
Paul committed
76

Paul's avatar
Paul committed
77
78
79
80
81
82
83
        std::pair<instruction_ref, instruction_ref> r{p.end(), p.end()};
        for(auto start : iterator_for(reshapes))
        {
            auto last = std::find_if(reshapes.rbegin(), reshapes.rend(), [&](auto&& i) {
                return i->get_shape() == (*start)->get_shape() and i != (*start);
            });
            if(last != reshapes.rend())
Paul's avatar
Paul committed
84
            {
Paul's avatar
Paul committed
85
86
                r = std::make_pair(*start, *last);
                break;
Paul's avatar
Paul committed
87
88
            }
        }
Paul's avatar
Paul committed
89
        if(r.first != r.second)
Paul's avatar
Paul committed
90
        {
Paul's avatar
Paul committed
91
            p.replace_instruction(r.first, r.second);
Paul's avatar
Paul committed
92
        }
Paul's avatar
Paul committed
93
94
95
    }
};

Paul's avatar
Paul committed
96
97
98
99
100
101
102
struct find_nop_reshapes
{
    auto matcher() const
    {
        auto reshapes = reshaper_names();
        reshapes.insert("transpose");
        reshapes.insert("slice");
Paul's avatar
Paul committed
103
        return match::name(reshapes)(match::same_shape(match::arg(0)));
Paul's avatar
Paul committed
104
105
    }

Paul's avatar
Paul committed
106
    void apply(program& p, const match::matcher_result& mr) const
Paul's avatar
Paul committed
107
108
109
110
111
112
    {
        auto ins = mr.result;
        p.replace_instruction(ins, ins->inputs().front());
    }
};

Paul's avatar
Paul committed
113
114
115
116
struct find_transpose
{
    auto matcher() const
    {
Paul's avatar
Paul committed
117
118
        return match::name("transpose")(match::none_of(
            match::skip_output(match::name("contiguous"))(match::name("transpose"))));
Paul's avatar
Paul committed
119
120
    }

Paul's avatar
Paul committed
121
    void apply(program& p, const match::matcher_result& mr) const
Paul's avatar
Paul committed
122
123
    {
        auto ins = mr.result;
Paul's avatar
Paul committed
124
125
        auto x   = ins;
        auto t   = ins;
Paul's avatar
Paul committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
        std::vector<std::int64_t> dims(ins->get_shape().lens().size());
        std::iota(dims.begin(), dims.end(), 0);
        do
        {
            dims = reorder_dims(get_transpose_dims(t), dims);
            x    = t;
            t    = find_transpose_input(x);
        } while(x != t and t->name() == "transpose");
        if(t == ins or t->name() != "transpose")
            return;
        if(is_no_transpose(dims))
        {
            p.replace_instruction(ins, t->inputs().front());
        }
        else
Paul's avatar
Paul committed
141
        {
Paul's avatar
Paul committed
142
            p.replace_instruction(ins, op::transpose{{dims}}, t->inputs().front());
Paul's avatar
Paul committed
143
        }
Paul's avatar
Paul committed
144
    }
Paul's avatar
Paul committed
145
146
147
148
149
150
};

struct find_concat_transpose
{
    auto matcher() const
    {
151
        return match::name("concat")(match::all_of[match::inputs()](match::transpose_shape()));
Paul's avatar
Paul committed
152
153
    }

Paul's avatar
Paul committed
154
    void apply(program& p, const match::matcher_result& mr) const
Paul's avatar
Paul committed
155
156
    {
        auto ins = mr.result;
Paul's avatar
Paul committed
157
        auto s   = ins->inputs().front()->get_shape();
Paul's avatar
Paul committed
158
        assert(s.transposed());
Paul's avatar
Paul committed
159
160
        auto op           = any_cast<op::concat>(ins->get_operator());
        auto permutation  = find_permutation(s);
Paul's avatar
Paul committed
161
        auto ipermutation = invert_permutation(permutation);
Paul's avatar
Paul committed
162
        op.axis           = ipermutation[op.axis];
Paul's avatar
Paul committed
163
164
165

        std::vector<instruction_ref> inputs;
        std::transform(
Paul's avatar
Paul committed
166
167
168
            ins->inputs().begin(), ins->inputs().end(), std::back_inserter(inputs), [&](auto i) {
                return p.insert_instruction(ins, op::transpose{permutation}, i);
            });
Paul's avatar
Paul committed
169
        auto concat = p.insert_instruction(ins, op, inputs);
Paul's avatar
Paul committed
170
171
        auto t      = p.insert_instruction(ins, op::transpose{ipermutation}, concat);
        assert(ins->get_shape().lens() == t->get_shape().lens());
Paul's avatar
Paul committed
172
173
174
175
176
177
        p.replace_instruction(ins, t);
    }
};

void simplify_reshapes::apply(program& p) const
{
178
    for(int i = 0; i < 2; i++)
Paul's avatar
Paul committed
179
    {
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
        auto end = std::prev(p.end());
        for(auto ins : iterator_for(p))
        {
            if(ins == end and ins->name() == "contiguous")
                continue;
            // Skip possible dead instructions
            if(ins->outputs().empty() and ins != end)
                continue;
            match::find_matches(p,
                                ins,
                                find_nop_reshapes{},
                                find_reshaper{},
                                find_transpose{},
                                find_concat_transpose{});
        }
Paul's avatar
Paul committed
195
    }
Paul's avatar
Paul committed
196
197
}

Paul's avatar
Paul committed
198
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
199
} // namespace migraphx