simplify_reshapes.cpp 4.33 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
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());
Paul's avatar
Paul committed
55
    for(std::size_t i = 0; i < dims.size(); i++)
56
    {
Paul's avatar
Paul committed
57
        result[i] = dims[permutation[i]];
58
59
60
61
62
63
    }
    return result;
}

bool is_no_transpose(const std::vector<int64_t>& dims)
{
Paul's avatar
Paul committed
64
    if(dims.empty())
65
        return true;
Paul's avatar
Paul committed
66
    if(dims.front() != 0)
67
        return false;
Paul's avatar
Paul committed
68
69
    return std::adjacent_find(
               dims.begin(), dims.end(), [](auto x, auto y) { return (y - x) != 1; }) == dims.end();
70
71
}

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

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

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