layout_nhwc.cpp 3.48 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
Paul's avatar
Paul committed
24
25
26
27
#include <migraphx/layout_nhwc.hpp>
#include <migraphx/module.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/iterator_for.hpp>
Paul's avatar
Paul committed
28
29
#include <migraphx/permutation.hpp>
#include <migraphx/functional.hpp>
Paul's avatar
Paul committed
30
31
32
33
#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
34
#include <migraphx/pass_manager.hpp>
Paul's avatar
Paul committed
35
36
37
38

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

Khalique Ahmed's avatar
Khalique Ahmed committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
template <class Predicate>
std::vector<instruction_ref> find_lasts(const module& m, Predicate pred)
{
    std::vector<instruction_ref> result;
    fix([&](auto self, auto ins) {
        if(pred(ins))
        {
            result.push_back(ins);
            return;
        }
        for(auto input : ins->inputs())
            self(input);
    })(std::prev(m.end()));
    return result;
}
Paul's avatar
Paul committed
54

Khalique Ahmed's avatar
Khalique Ahmed committed
55
56
57
58
59
60
61
62
63
64
65
66
67
void preserve_output_layout(module& m)
{
    std::vector<instruction_ref> outputs = find_lasts(m, [](auto ins) {
        return ins->name() == "convolution" and ins->get_shape().lens().size() == 4;
    });
    for(auto output : outputs)
    {
        auto permutation = find_permutation(output->get_shape());
        auto layout      = m.insert_instruction(
            std::next(output), make_op("layout", {{"permutation", permutation}}), output);
        m.replace_instruction(output, layout);
    }
}
Paul's avatar
Paul committed
68

Khalique Ahmed's avatar
Khalique Ahmed committed
69
void transform_convolutions(module& m)
Paul's avatar
Paul committed
70
71
72
{
    for(auto ins : iterator_for(m))
    {
Paul's avatar
Paul committed
73
        if(ins->name() != "convolution")
Paul's avatar
Paul committed
74
            continue;
Paul's avatar
Paul committed
75
        if(ins->get_shape().lens().size() != 4)
Paul's avatar
Paul committed
76
            continue;
Paul's avatar
Paul committed
77
        auto v = ins->get_operator().to_value();
Paul's avatar
Format  
Paul committed
78
        if(v.at("group").to<int>() > 1)
Paul's avatar
Paul committed
79
            continue;
Paul's avatar
Paul committed
80
        auto args = ins->inputs();
Khalique Ahmed's avatar
Khalique Ahmed committed
81
82
83
        std::transform(args.begin(), args.end(), args.begin(), [&](const auto& i) {
            return m.insert_instruction(ins, make_op("layout", {{"permutation", {0, 2, 3, 1}}}), i);
        });
Paul's avatar
Paul committed
84
        auto conv = m.insert_instruction(ins, ins->get_operator(), args);
Khalique Ahmed's avatar
Khalique Ahmed committed
85
        auto c    = m.insert_instruction(ins, make_op("contiguous"), conv);
Khalique Ahmed's avatar
Khalique Ahmed committed
86
        m.replace_instruction(ins, c);
Paul's avatar
Paul committed
87
88
89
    }
}

Paul's avatar
Paul committed
90
void layout_nhwc::apply(module_pass_manager& mpm) const
Paul's avatar
Paul committed
91
{
Khalique Ahmed's avatar
Khalique Ahmed committed
92
93
94
    module& m = mpm.get_module();
    // preserve_output_layout(m);
    transform_convolutions(m);
Paul's avatar
Paul committed
95
    mpm.run_pass(dead_code_elimination{});
Paul's avatar
Paul committed
96
97
98
99
}

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx