layout_nhwc.cpp 2.8 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
#include <migraphx/layout_nhwc.hpp>
#include <migraphx/module.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/iterator_for.hpp>
Paul's avatar
Paul committed
5
6
#include <migraphx/permutation.hpp>
#include <migraphx/functional.hpp>
Paul's avatar
Paul committed
7
8
9
10
11
12
13
14
#include <migraphx/ranges.hpp>
#include <migraphx/make_op.hpp>
#include <migraphx/eliminate_contiguous.hpp>
#include <migraphx/dead_code_elimination.hpp>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

Paul's avatar
Paul committed
15
template <class Predicate>
Paul's avatar
Paul committed
16
17
18
19
std::vector<instruction_ref> find_lasts(const module& m, Predicate pred)
{
    std::vector<instruction_ref> result;
    fix([&](auto self, auto ins) {
Paul's avatar
Paul committed
20
        if(pred(ins))
Paul's avatar
Paul committed
21
22
23
24
        {
            result.push_back(ins);
            return;
        }
Paul's avatar
Paul committed
25
        for(auto input : ins->inputs())
Paul's avatar
Paul committed
26
27
28
29
30
            self(input);
    })(std::prev(m.end()));
    return result;
}

31
std::unordered_set<instruction_ref> preserve_output_layout(module& m)
Paul's avatar
Paul committed
32
{
33
    std::unordered_set<instruction_ref> result;
Paul's avatar
Paul committed
34
35
36
    std::vector<instruction_ref> outputs =
        find_lasts(m, [](auto ins) { return ins->get_shape().lens().size() == 4; });
    for(auto output : outputs)
Paul's avatar
Paul committed
37
38
    {
        auto permutation = find_permutation(output->get_shape());
Paul's avatar
Paul committed
39
40
        auto layout      = m.insert_instruction(
            std::next(output), make_op("layout", {{"permutation", permutation}}), output);
41
        result.insert(m.replace_instruction(output, layout));
Paul's avatar
Paul committed
42
    }
43
    return result;
Paul's avatar
Paul committed
44
45
}

Paul's avatar
Paul committed
46
47
48
49
void transform_convolutions(module& m)
{
    for(auto ins : iterator_for(m))
    {
Paul's avatar
Paul committed
50
        if(ins->name() != "convolution")
Paul's avatar
Paul committed
51
            continue;
Paul's avatar
Paul committed
52
        if(ins->get_shape().lens().size() != 4)
Paul's avatar
Paul committed
53
54
55
56
57
58
            continue;
        auto args = ins->inputs();
        std::transform(args.begin(), args.end(), args.begin(), [&](auto& i) {
            return m.insert_instruction(ins, make_op("layout", {{"permutation", {0, 2, 3, 1}}}), i);
        });
        auto conv = m.insert_instruction(ins, ins->get_operator(), args);
Paul's avatar
Paul committed
59
        auto c    = m.insert_instruction(ins, make_op("contiguous"), conv);
Paul's avatar
Paul committed
60
61
62
63
        m.replace_instruction(ins, c);
    }
}

64
65
66
67
68
69
void remove_layout(module& m, const std::unordered_set<instruction_ref>& output_layouts)
{
    for(auto ins : iterator_for(m))
    {
        if(ins->name() != "layout")
            continue;
Paul's avatar
Paul committed
70
        if(ins->get_shape() != ins->inputs().front()->get_shape())
71
            continue;
Paul's avatar
Paul committed
72
        if(contains(output_layouts, ins))
73
74
75
76
77
            continue;
        m.replace_instruction(ins, ins->inputs().front());
    }
}

Paul's avatar
Paul committed
78
79
void layout_nhwc::apply(module& m) const
{
80
    std::unordered_set<instruction_ref> output_layouts = preserve_output_layout(m);
Paul's avatar
Paul committed
81
82
83
    transform_convolutions(m);
    dead_code_elimination{}.apply(m);
    eliminate_contiguous{"contiguous"}.apply(m);
84
85
86
    dead_code_elimination{}.apply(m);
    remove_layout(m, output_layouts);
    dead_code_elimination{}.apply(m);
Paul's avatar
Paul committed
87
88
89
90
}

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx