simplify_reshapes.cpp 4.32 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
7
#include <migraphx/iterator_for.hpp>
#include <migraphx/ranges.hpp>
Paul's avatar
Paul committed
8
9
#include <unordered_set>

Paul's avatar
Paul committed
10
namespace migraphx {
Paul's avatar
Paul committed
11
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
12

Paul's avatar
Paul committed
13
bool is_reshaper(instruction_ref ins)
Paul's avatar
Paul committed
14
{
15
16
17
    // clang-format off
    static const std::unordered_set<std::string> names = {
        "reshape",
18
19
20
        "contiguous",
        "squeeze",
        "unsqueeze"
21
22
    };
    // clang-format on
Paul's avatar
Paul committed
23
24
25
26
27
    return contains(names, ins->name());
}

bool is_transpose_output(instruction_ref ins)
{
Paul's avatar
Paul committed
28
    if(ins->outputs().size() != 1)
Paul's avatar
Paul committed
29
        return false;
Paul's avatar
Paul committed
30
    if(ins->outputs().front()->name() == "contiguous")
Paul's avatar
Paul committed
31
32
33
34
35
36
        return is_transpose_output(ins->outputs().front());
    return ins->outputs().front()->name() == "transpose";
}

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

46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
auto get_transpose_dims(instruction_ref ins)
{
    return any_cast<const op::transpose&>(ins->get_operator()).dims;
}

std::vector<int64_t> reorder_dims(std::vector<int64_t> dims, std::vector<int64_t> permutation)
{
    std::vector<int64_t> result(dims.size());
    assert(dims.size() == permutation.size());
    for(std::size_t i = 0;i <dims.size();i++)
    {
        result[i]    = dims[permutation[i]];
    }
    return result;
}

bool is_no_transpose(const std::vector<int64_t>& dims)
{
    if (dims.empty())
        return true;
    if (dims.front() != 0)
        return false;
    return std::adjacent_find(dims.begin(), dims.end(), [](auto x, auto y) {
        return (y - x) != 1;
    }) == dims.end();
}

Paul's avatar
Paul committed
73
74
void simplify_reshapes::apply(program& p) const
{
Paul's avatar
Paul committed
75
    auto end = std::prev(p.end());
Paul's avatar
Paul committed
76
77
    for(auto ins : iterator_for(p))
    {
Paul's avatar
Paul committed
78
        if(ins == end and ins->name() == "contiguous")
Paul's avatar
Paul committed
79
80
            continue;
        // Skip possible dead instructions
Paul's avatar
Paul committed
81
        if(ins->outputs().empty() and ins != end)
Paul's avatar
Paul committed
82
            continue;
Paul's avatar
Paul committed
83
        if(is_reshaper(ins))
Paul's avatar
Paul committed
84
        {
Paul's avatar
Paul committed
85
86
87
88
89
90
91
92
93
94
95
            if(std::any_of(ins->outputs().begin(), ins->outputs().end(), &is_reshaper))
                continue;
            // Gather reshapes
            std::vector<instruction_ref> reshapes{ins};
            while(is_reshaper(reshapes.back()))
            {
                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
96

Paul's avatar
Paul committed
97
98
            std::pair<instruction_ref, instruction_ref> r{p.end(), p.end()};
            for(auto start : iterator_for(reshapes))
Paul's avatar
Paul committed
99
            {
Paul's avatar
Paul committed
100
101
102
103
104
105
106
107
108
109
110
111
                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())
                {
                    r = std::make_pair(*start, *last);
                    break;
                }
            }
            if(r.first != r.second)
            {
                p.replace_instruction(r.first, r.second);
Paul's avatar
Paul committed
112
113
            }
        }
Paul's avatar
Paul committed
114
        else if(ins->name() == "transpose")
Paul's avatar
Paul committed
115
        {
Paul's avatar
Paul committed
116
            if(is_transpose_output(ins))
Paul's avatar
Paul committed
117
118
119
                continue;
            auto x = ins;
            auto t = ins;
120
121
            std::vector<std::int64_t> dims(ins->get_shape().lens().size());
            std::iota(dims.begin(), dims.end(), 0);
Paul's avatar
Paul committed
122
123
            do
            {
124
                dims = reorder_dims(get_transpose_dims(t), dims);
Paul's avatar
Paul committed
125
126
127
                x = t;
                t = find_transpose_input(x);
            } while(x != t and t->name() == "transpose");
Paul's avatar
Paul committed
128
            if(t == ins or t->name() != "transpose")
Paul's avatar
Paul committed
129
                continue;
130
131
132
133
134
135
136
137
            if (is_no_transpose(dims))
            {
                p.replace_instruction(ins, t->inputs().front());
            }
            else
            {
                p.replace_instruction(ins, op::transpose{{dims}}, t->inputs().front());
            }
Paul's avatar
Paul committed
138
139
140
141
        }
    }
}

Paul's avatar
Paul committed
142
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
143
} // namespace migraphx