layout_nhwc.cpp 5.85 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
#include <migraphx/ranges.hpp>
#include <migraphx/make_op.hpp>
#include <migraphx/eliminate_contiguous.hpp>
Khalique Ahmed's avatar
Khalique Ahmed committed
33
#include <migraphx/auto_contiguous.hpp>
Paul's avatar
Paul committed
34
#include <migraphx/dead_code_elimination.hpp>
Paul's avatar
Paul committed
35
#include <migraphx/pass_manager.hpp>
Khalique Ahmed's avatar
Khalique Ahmed committed
36
37
#include <stdexcept>
#include <system_error>
Paul's avatar
Paul committed
38
39
40
41

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

Khalique Ahmed's avatar
testing  
Khalique Ahmed committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// 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
57

Khalique Ahmed's avatar
testing  
Khalique Ahmed committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// std::unordered_set<instruction_ref> preserve_output_layout(module& m)
// {
//     std::unordered_set<instruction_ref> result;
//     std::vector<instruction_ref> outputs =
//         find_lasts(m, [](auto ins) { return 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);
//         result.insert(m.replace_instruction(output, layout));
//     }
//     return result;
// }
Paul's avatar
Paul committed
72

73
void transform_convolutions(module& m, bool skip_elim_contiguous)
Paul's avatar
Paul committed
74
75
76
{
    for(auto ins : iterator_for(m))
    {
Paul's avatar
Paul committed
77
        if(ins->name() != "convolution")
Paul's avatar
Paul committed
78
            continue;
Paul's avatar
Paul committed
79
        if(ins->get_shape().lens().size() != 4)
Paul's avatar
Paul committed
80
            continue;
Paul's avatar
Paul committed
81
        auto v = ins->get_operator().to_value();
Paul's avatar
Format  
Paul committed
82
        if(v.at("group").to<int>() > 1)
Paul's avatar
Paul committed
83
            continue;
Paul's avatar
Paul committed
84
        auto args = ins->inputs();
85
86
87
88
        if(skip_elim_contiguous)
        {
            for(auto i = 0; i < args.size(); i++)
            {
Khalique Ahmed's avatar
Khalique Ahmed committed
89

90
91
                if(args[i]->name() != "layout" and args[i]->get_shape().standard())
                {
Khalique Ahmed's avatar
Khalique Ahmed committed
92
93
                    args[i] = m.insert_instruction(
                        ins, make_op("layout", {{"permutation", {0, 2, 3, 1}}}), args[i]);
94
95
96
97
98
                }
            }
        }
        else
            std::transform(args.begin(), args.end(), args.begin(), [&](auto& i) {
Khalique Ahmed's avatar
Khalique Ahmed committed
99
100
                return m.insert_instruction(
                    ins, make_op("layout", {{"permutation", {0, 2, 3, 1}}}), i);
101
            });
Paul's avatar
Paul committed
102
        auto conv = m.insert_instruction(ins, ins->get_operator(), args);
103
104
        // m.debug_print(conv);
        // auto c    = conv;
Khalique Ahmed's avatar
Khalique Ahmed committed
105
106
        // auto nchw = m.insert_instruction(ins, make_op("layout", {{"permutation", {0, 1, 2, 3}}}),
        // conv); m.debug_print(); if(not skip_elim_contiguous)
Khalique Ahmed's avatar
Khalique Ahmed committed
107
        auto c = m.insert_instruction(ins, make_op("contiguous"), conv);
Khalique Ahmed's avatar
Khalique Ahmed committed
108
        m.replace_instruction(ins, c);
Paul's avatar
Paul committed
109
110
111
    }
}

112
113
114
115
116
117
void insert_contiguous(module& m)
{
    for(auto ins : iterator_for(m))
    {
        if(ins->name() != "reshape" and ins->name() != "pooling")
            continue;
Khalique Ahmed's avatar
Khalique Ahmed committed
118
        auto c       = m.insert_instruction(ins, make_op("contiguous"), ins->inputs().front());
119
120
121
122
123
124
        auto reshape = m.insert_instruction(ins, ins->get_operator(), c);
        m.replace_instruction(ins, reshape);
    }
    // m.debug_print();
}

Khalique Ahmed's avatar
testing  
Khalique Ahmed committed
125
126
127
128
129
130
131
132
133
134
135
136
137
// void remove_layout(module& m, const std::unordered_set<instruction_ref>& output_layouts)
// {
//     for(auto ins : iterator_for(m))
//     {
//         if(ins->name() != "layout")
//             continue;
//         if(ins->get_shape() != ins->inputs().front()->get_shape())
//             continue;
//         if(contains(output_layouts, ins))
//             continue;
//         m.replace_instruction(ins, ins->inputs().front());
//     }
// }
138

Paul's avatar
Paul committed
139
void layout_nhwc::apply(module_pass_manager& mpm) const
Paul's avatar
Paul committed
140
{
Khalique Ahmed's avatar
Khalique Ahmed committed
141
142
    // std::unordered_set<instruction_ref> output_layouts =
    // preserve_output_layout(mpm.get_module());
Khalique Ahmed's avatar
Khalique Ahmed committed
143
    // insert_contiguous(mpm.get_module());
144
    mpm.run_pass(dead_code_elimination{});
Khalique Ahmed's avatar
Khalique Ahmed committed
145
    // mpm.get_module().debug_print();
146
    transform_convolutions(mpm.get_module(), this->skip_elim_contiguous);
Khalique Ahmed's avatar
Khalique Ahmed committed
147

Paul's avatar
Paul committed
148
    mpm.run_pass(dead_code_elimination{});
Khalique Ahmed's avatar
Khalique Ahmed committed
149
150
    // std::cout << "after layout" << std::endl;
    // mpm.get_module().debug_print();
Khalique Ahmed's avatar
Khalique Ahmed committed
151
    // if(not this->skip_elim_contiguous)
Khalique Ahmed's avatar
Khalique Ahmed committed
152
153
154
155
    // mpm.run_pass(eliminate_contiguous{"contiguous"});
    // mpm.run_pass(dead_code_elimination{});
    // mpm.run_pass(auto_contiguous{});
    // mpm.run_pass(dead_code_elimination{});
Khalique Ahmed's avatar
testing  
Khalique Ahmed committed
156
157
    // remove_layout(mpm.get_module(), output_layouts);
    // mpm.run_pass(dead_code_elimination{});
Paul's avatar
Paul committed
158
159
160
161
}

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx