layout_nhwc.cpp 3.06 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
#include <migraphx/ranges.hpp>
#include <migraphx/make_op.hpp>
#include <migraphx/eliminate_contiguous.hpp>
#include <migraphx/dead_code_elimination.hpp>
Paul's avatar
Paul committed
11
#include <migraphx/pass_manager.hpp>
Paul's avatar
Paul committed
12
13
14
15

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

Paul's avatar
Paul committed
16
template <class Predicate>
Paul's avatar
Paul committed
17
18
19
20
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
21
        if(pred(ins))
Paul's avatar
Paul committed
22
23
24
25
        {
            result.push_back(ins);
            return;
        }
Paul's avatar
Paul committed
26
        for(auto input : ins->inputs())
Paul's avatar
Paul committed
27
28
29
30
31
            self(input);
    })(std::prev(m.end()));
    return result;
}

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

Paul's avatar
Paul committed
47
48
49
50
void transform_convolutions(module& m)
{
    for(auto ins : iterator_for(m))
    {
Paul's avatar
Paul committed
51
        if(ins->name() != "convolution")
Paul's avatar
Paul committed
52
            continue;
Paul's avatar
Paul committed
53
        if(ins->get_shape().lens().size() != 4)
Paul's avatar
Paul committed
54
            continue;
Paul's avatar
Paul committed
55
        auto v = ins->get_operator().to_value();
Paul's avatar
Format  
Paul committed
56
        if(v.at("group").to<int>() > 1)
Paul's avatar
Paul committed
57
            continue;
Paul's avatar
Paul committed
58
        auto args = ins->inputs();
Paul's avatar
Paul committed
59
        std::transform(args.begin(), args.end(), args.begin(), [&](const auto& i) {
Paul's avatar
Paul committed
60
61
62
            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
63
        auto c    = m.insert_instruction(ins, make_op("contiguous"), conv);
Paul's avatar
Paul committed
64
65
66
67
        m.replace_instruction(ins, c);
    }
}

68
69
70
71
72
73
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
74
        if(ins->get_shape() != ins->inputs().front()->get_shape())
75
            continue;
Paul's avatar
Paul committed
76
        if(contains(output_layouts, ins))
77
78
79
80
81
            continue;
        m.replace_instruction(ins, ins->inputs().front());
    }
}

Paul's avatar
Paul committed
82
void layout_nhwc::apply(module_pass_manager& mpm) const
Paul's avatar
Paul committed
83
{
Paul's avatar
Paul committed
84
85
86
87
88
89
90
    std::unordered_set<instruction_ref> output_layouts = preserve_output_layout(mpm.get_module());
    transform_convolutions(mpm.get_module());
    mpm.run_pass(dead_code_elimination{});
    mpm.run_pass(eliminate_contiguous{"contiguous"});
    mpm.run_pass(dead_code_elimination{});
    remove_layout(mpm.get_module(), output_layouts);
    mpm.run_pass(dead_code_elimination{});
Paul's avatar
Paul committed
91
92
93
94
}

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx