"test/wmma_op/wmma_op.cpp" did not exist on "8784a72e23538d594ea6b1bd527478fba2962d30"
parse_pooling.cpp 9.82 KB
Newer Older
1
2
3
/*
 * The MIT License (MIT)
 *
Brian Pickrell's avatar
Brian Pickrell committed
4
 * Copyright (c) 2015-2023 Advanced Micro Devices, Inc. All rights reserved.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 * 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 Fultz II's avatar
Paul Fultz II committed
24
25
26
27
#include <migraphx/onnx/op_parser.hpp>
#include <migraphx/onnx/checks.hpp>
#include <migraphx/onnx/padding.hpp>
#include <migraphx/op/pad.hpp>
28
#include <migraphx/op/pooling.hpp>
Paul Fultz II's avatar
Paul Fultz II committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <migraphx/instruction.hpp>
#include <migraphx/ranges.hpp>
#include <migraphx/stringutils.hpp>
#include <migraphx/make_op.hpp>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace onnx {

struct parse_pooling : op_parser<parse_pooling>
{
    std::vector<op_desc> operators() const
    {
        return {{"AveragePool", "average"},
                {"GlobalAveragePool", "average"},
                {"GlobalMaxPool", "max"},
45
46
47
                {"MaxPool", "max"},
                {"LpPool", "lpnorm"},
                {"GlobalLpPool", "lpnorm"}};
Paul Fultz II's avatar
Paul Fultz II committed
48
49
    }

Charlie Lin's avatar
Charlie Lin committed
50
51
52
53
    value handle_values(const op_desc& opd,
                        onnx_parser::node_info info,
                        const shape& in_shape,
                        value values) const
Paul Fultz II's avatar
Paul Fultz II committed
54
    {
Charlie Lin's avatar
Charlie Lin committed
55
        auto kdims = in_shape.ndim() - 2;
Paul Fultz II's avatar
Paul Fultz II committed
56
57
        if(starts_with(opd.onnx_name, "Global"))
        {
Charlie Lin's avatar
Charlie Lin committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
            // if spatial dimensions are dynamic use dyn_global flag
            if(in_shape.dynamic() and std::any_of(in_shape.dyn_dims().cbegin() + 2,
                                                  in_shape.dyn_dims().cend(),
                                                  [](auto dd) { return not dd.is_fixed(); }))
            {
                values["dyn_global"] = true;
                values["lengths"]    = std::vector<size_t>();
            }
            else
            {
                // works with static and fixed dynamic shape
                auto m_lens       = in_shape.max_lens();
                values["lengths"] = std::vector<size_t>(m_lens.begin() + 2, m_lens.end());
            }
Paul Fultz II's avatar
Paul Fultz II committed
72
73
74
75
76
77
78
79
80
81
82
83
84
        }

        if(contains(info.attributes, "ceil_mode"))
        {
            values["ceil_mode"] = static_cast<bool>(info.attributes.at("ceil_mode").i());
        }

        if(contains(info.attributes, "strides"))
        {
            values["stride"].clear();
            copy(info.attributes["strides"].ints(), std::back_inserter(values["stride"]));
            check_attr_sizes(kdims, values["stride"].size(), "PARSE_POOLING: inconsistent strides");
        }
Charlie Lin's avatar
Charlie Lin committed
85

Paul Fultz II's avatar
Paul Fultz II committed
86
87
88
89
90
91
92
93
        if(contains(info.attributes, "kernel_shape"))
        {
            values["lengths"].clear();
            copy(info.attributes["kernel_shape"].ints(), std::back_inserter(values["lengths"]));
            check_attr_sizes(
                kdims, values["lengths"].size(), "PARSE_POOLING: inconsistent lengths");
        }

94
95
96
97
98
99
        // lp_order attribute
        if(contains(info.attributes, "p"))
        {
            values["lp_order"] = info.attributes.at("p").i();
        }

100
        // ensure pads available only when auto_pad is "NOT_SET"
Paul Fultz II's avatar
Paul Fultz II committed
101
102
        check_padding_mode(info, "POOLING");

Charlie Lin's avatar
Charlie Lin committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
        return values;
    }

    instruction_ref parse(const op_desc& opd,
                          const onnx_parser& /*parser*/,
                          onnx_parser::node_info info,
                          std::vector<instruction_ref> args) const
    {
        std::string mode                                                 = opd.op_name;
        const std::unordered_map<std::string, op::pooling_mode> mode_map = {
            {"max", op::pooling_mode::max},
            {"average", op::pooling_mode::average},
            {"lpnorm", op::pooling_mode::lpnorm}};
        if(not contains(mode_map, mode))
        {
            MIGRAPHX_THROW(
                "PARSE_POOLING: onnx pooling mode must be [\"max\", \"average\", \"lpnorm\"]");
        }
        operation op  = make_op("pooling", {{"mode", mode_map.at(mode)}});
        value values  = op.to_value();
        auto l0       = args[0];
        auto in_shape = l0->get_shape();
        assert(in_shape.ndim() > 2);
        auto kdims = in_shape.ndim() - 2;

        values = handle_values(opd, info, in_shape, values);

        // count include padding, if count include pad is 1, we always use
        // explicit pad
        int count_include_pad = 0;
        if(contains(info.attributes, "count_include_pad"))
        {
            if(in_shape.dynamic())
            {
                MIGRAPHX_THROW("PARSE_POOLING: count_include_pad attribute is not supported for "
                               "dynamic input shape");
            }
            count_include_pad = info.attributes.at("count_include_pad").i();
        }

Paul Fultz II's avatar
Paul Fultz II committed
143
144
        std::vector<int64_t> paddings;
        float pad_val = ((mode == "max") ? std::numeric_limits<float>::lowest() : 0.0f);
145

Paul Fultz II's avatar
Paul Fultz II committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
        if(contains(info.attributes, "pads"))
        {
            values["padding"].clear();
            copy(info.attributes["pads"].ints(), std::back_inserter(paddings));
            check_attr_sizes(
                kdims, paddings.size() / 2, "PARSE_POOLING: inconsistent explicit paddings");
        }

        if(paddings.size() != 2 * kdims)
        {
            paddings.resize(kdims * 2);
            std::fill_n(paddings.begin(), 2 * kdims, 0);
        }

        if(values["padding"].size() != kdims)
        {
            values["padding"].resize(kdims);
            std::fill_n(values["padding"].begin(), kdims, 0);
        }

        if(values["stride"].size() != kdims)
        {
            values["stride"].resize(kdims);
            std::fill_n(values["stride"].begin(), kdims, 1);
        }
Charlie Lin's avatar
Charlie Lin committed
171

Paul Fultz II's avatar
Paul Fultz II committed
172
        // used to calculate the supposed output shape
173
        std::vector<int64_t> orig_padding = paddings;
Paul Fultz II's avatar
Paul Fultz II committed
174

Brian Pickrell's avatar
Brian Pickrell committed
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
        // TODO:  add parsing for dilations
        if(contains(info.attributes, "auto_pad") and
           to_upper(info.attributes["auto_pad"].s()) != "NOTSET")
        {
            auto auto_pad = to_upper(info.attributes["auto_pad"].s());
            // don't use the given padding sizes, if any
            // values["padding"].clear();
            if(in_shape.dynamic())
            {
                // set padding_mode to trigger auto padding at runtime
                bool is_same_upper     = (auto_pad.find("SAME_UPPER") != std::string::npos);
                values["padding_mode"] = is_same_upper ? to_value(op::padding_mode_t::same_upper)
                                                       : to_value(op::padding_mode_t::same_lower);
            }
            else
            {
                // Calculate auto padding
                // dilations (argument 4) not supported; default to all 1's
                cal_auto_padding_size(info,
                                      values,
                                      values["lengths"].to_vector<std::size_t>(),
                                      std::vector<size_t>(in_shape.ndim() - 2, 1),
                                      in_shape.lens(),
                                      paddings);
                values["padding"] = paddings;
                // default padding_mode indicates that padding sizes are not calculated dynamically
                values["padding_mode"] = migraphx::op::padding_mode_t::default_;
            }
        }

Paul Fultz II's avatar
Paul Fultz II committed
205
206
207
208
        std::vector<int64_t> slice_start;
        std::vector<int64_t> slice_end;
        tune_padding_size(values, paddings, count_include_pad, slice_start);

209
        if(not slice_start.empty())
Paul Fultz II's avatar
Paul Fultz II committed
210
        {
Charlie Lin's avatar
Charlie Lin committed
211
212
213
214
215
            if(in_shape.dynamic())
            {
                MIGRAPHX_THROW(
                    "PARSE_POOLING: asymmetric padding not supported for dynamic input shape");
            }
Paul Fultz II's avatar
Paul Fultz II committed
216
217
218
219
220
221
            // calculate expected output shape
            orig_padding.insert(orig_padding.begin() + kdims, 2, 0);
            orig_padding.insert(orig_padding.begin(), 2, 0);
            op::pad pad{orig_padding, 0.0f};
            shape padded_shape = pad.compute_shape({l0->get_shape()});

Brian Pickrell's avatar
Brian Pickrell committed
222
223
            // make an op just to get its output shape
            auto out_lens = make_op("pooling", values).compute_shape({padded_shape}).lens();
Paul Fultz II's avatar
Paul Fultz II committed
224
225
226
227
228
229
230
231
            // compute slice_end information
            slice_end.resize(slice_start.size());
            std::transform(out_lens.begin() + 2,
                           out_lens.end(),
                           slice_start.begin(),
                           slice_end.begin(),
                           [](auto i, auto j) { return i + j; });
        }
kahmed10's avatar
kahmed10 committed
232
        values["padding"] = std::vector<size_t>(paddings.begin(), paddings.end());
Paul Fultz II's avatar
Paul Fultz II committed
233
234
235

        check_asym_padding(info, l0, paddings, values, count_include_pad, pad_val);
        op.from_value(values);
kahmed10's avatar
kahmed10 committed
236

Paul Fultz II's avatar
Paul Fultz II committed
237
        auto l1 = info.add_instruction(op, l0);
238
        if(not slice_start.empty())
Paul Fultz II's avatar
Paul Fultz II committed
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
        {
            std::vector<int64_t> axes(kdims);
            std::iota(axes.begin(), axes.end(), 2);
            l1 = info.add_instruction(
                make_op("slice", {{"axes", axes}, {"starts", slice_start}, {"ends", slice_end}}),
                l1);
        }

        return l1;
    }
};

} // namespace onnx
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx