normalize_attributes.cpp 9.41 KB
Newer Older
1
2
3
/*
 * The MIT License (MIT)
 *
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.
 */
Shucai Xiao's avatar
Shucai Xiao committed
24
25
26
27
28
29
30
31
32
#include <migraphx/operation.hpp>
#include <migraphx/ranges.hpp>
#include <migraphx/normalize_attributes.hpp>
#include <migraphx/stringutils.hpp>
#include <migraphx/op/normalize_attribute.hpp>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

33
34
35
36
37
/**
 * Parameters:
 * vec: the vector attribute to normalize
 * axes: the operator's axes attribute if it exists, empty otherwise
 * val: the normalize_axes key and options. Ex: normalize["axes"] =
38
39
40
 * value::array{normalize_attribute::include_min};
 * input_shape: input shape passed when calling
 * normalize_attributes(op&, input_shape)
41
42
43
 *
 * See normalize_attribute.hpp for explaining the options.
 */
44
template <class Message>
Shucai Xiao's avatar
Shucai Xiao committed
45
46
47
auto tune_attribute(const std::vector<int64_t>& vec,
                    const std::vector<int64_t>& axes,
                    const value& val,
48
                    const shape& input_shape,
49
                    Message m)
Shucai Xiao's avatar
Shucai Xiao committed
50
51
{
    std::vector<int64_t> result(vec);
Charlie Lin's avatar
Charlie Lin committed
52
53
54
55
    if(result.empty())
    {
        return result;
    };
56
    int64_t n_rank                                 = input_shape.ndim();
Shucai Xiao's avatar
Shucai Xiao committed
57
58
59
60
61
62
63
    std::vector<op::normalize_attribute> vec_attrs = val.to_vector<op::normalize_attribute>();
    if(contains(vec_attrs, op::normalize_attribute::use_output))
    {
        n_rank = n_rank + vec.size();
    }

    std::vector<int64_t> max_vals(vec.size(), n_rank);
64

Shucai Xiao's avatar
Shucai Xiao committed
65
66
    if(contains(vec_attrs, op::normalize_attribute::use_len))
    {
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
        if(input_shape.dynamic())
        {
            std::transform(axes.begin(), axes.end(), max_vals.begin(), [&](auto i) {
                const auto& dd = input_shape.dyn_dims().at(i);
                if(not dd.is_fixed())
                {
                    MIGRAPHX_THROW(
                        "NORMALIZE_ATTR: 'use_lens' on a non-fixed dynamic dimension, axis=" +
                        std::to_string(i));
                }
                return dd.max;
            });
        }
        else
        {
            std::transform(axes.begin(), axes.end(), max_vals.begin(), [&](auto i) {
                return input_shape.lens().at(i);
            });
        }
Shucai Xiao's avatar
Shucai Xiao committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
    }

    if(contains(vec_attrs, op::normalize_attribute::clip_max))
    {
        if(contains(vec_attrs, op::normalize_attribute::include_max))
        {
            std::transform(result.begin(),
                           result.end(),
                           max_vals.begin(),
                           result.begin(),
                           [](auto v, auto mv) { return v > mv ? mv : v; });
        }
        else
        {
            std::transform(result.begin(),
                           result.end(),
                           max_vals.begin(),
                           result.begin(),
                           [](auto v, auto mv) { return v >= mv ? mv - 1 : v; });
        }
    }
    else
    {
        if(contains(vec_attrs, op::normalize_attribute::include_max))
        {
111
            if(not std::equal(result.begin(), result.end(), max_vals.begin(), std::less_equal<>{}))
Shucai Xiao's avatar
Shucai Xiao committed
112
            {
113
                MIGRAPHX_THROW(m() + "value out of range!");
Shucai Xiao's avatar
Shucai Xiao committed
114
115
116
117
            }
        }
        else
        {
118
            if(not std::equal(result.begin(), result.end(), max_vals.begin(), std::less<>{}))
Shucai Xiao's avatar
Shucai Xiao committed
119
            {
120
                MIGRAPHX_THROW(m() + "value out of range!");
Shucai Xiao's avatar
Shucai Xiao committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
            }
        }
    }

    std::vector<int64_t> min_vals = max_vals;
    std::transform(min_vals.begin(), min_vals.end(), min_vals.begin(), [](auto v) { return -v; });
    if(contains(vec_attrs, op::normalize_attribute::clip_min))
    {
        if(contains(vec_attrs, op::normalize_attribute::include_min))
        {
            std::transform(result.begin(),
                           result.end(),
                           min_vals.begin(),
                           result.begin(),
                           [](auto v, auto mv) { return v < mv ? mv : v; });
        }
        else
        {
            std::transform(result.begin(),
                           result.end(),
                           min_vals.begin(),
                           result.begin(),
                           [](auto v, auto mv) { return v < mv + 1 ? mv + 1 : v; });
        }
    }
    else
    {
        if(contains(vec_attrs, op::normalize_attribute::include_min))
        {
150
151
            if(not std::equal(
                   min_vals.begin(), min_vals.end(), result.begin(), std::less_equal<>{}))
Shucai Xiao's avatar
Shucai Xiao committed
152
            {
153
                MIGRAPHX_THROW(m() + "attribute out of range!");
Shucai Xiao's avatar
Shucai Xiao committed
154
155
156
157
            }
        }
        else
        {
158
            if(not std::equal(result.begin(), result.end(), min_vals.begin(), std::less<>{}))
Shucai Xiao's avatar
Shucai Xiao committed
159
            {
160
                MIGRAPHX_THROW(m() + "attribute out of range!");
Shucai Xiao's avatar
Shucai Xiao committed
161
162
163
164
165
166
167
168
169
170
171
172
            }
        }
    }

    std::transform(
        result.begin(), result.end(), max_vals.begin(), result.begin(), [](auto v, auto mv) {
            return v < 0 ? v + mv : v;
        });

    return result;
}

kahmed10's avatar
kahmed10 committed
173
174
175
176
177
178
179
180
181
182
auto tune_pad_attribute(const value& val)
{

    std::vector<size_t> vec_attrs = val.to_vector<size_t>();
    std::vector<size_t> result(vec_attrs.begin(), vec_attrs.end());
    std::copy(vec_attrs.begin(), vec_attrs.end(), std::back_inserter(result));

    return result;
}

183
184
185
/**
 * Assumptions:
 *  Dimensions to pad start from the third dimension (index 2).
186
 *  Called by compute_shape_op() with the shape of the first input.
187
 */
188
bool normalize_attributes(operation& op, const shape& input_shape)
Shucai Xiao's avatar
Shucai Xiao committed
189
190
191
192
{
    bool tuned = false;
    auto attrs = op.attributes();
    auto val   = op.to_value();
kahmed10's avatar
kahmed10 committed
193
194
    if(attrs.contains("normalize_padding"))
    {
195
196
        auto padding       = val.at(attrs.at("normalize_padding").to<std::string>());
        auto padding_size  = padding.size();
kahmed10's avatar
kahmed10 committed
197
198
        auto padding_start = 2;

199
        if(padding_size == 2 * (input_shape.ndim() - padding_start))
kahmed10's avatar
kahmed10 committed
200
            tuned = true;
201
        else if(padding_size != (input_shape.ndim() - padding_start))
kahmed10's avatar
kahmed10 committed
202
203
204
205
206
207
208
209
210
            MIGRAPHX_THROW("inconsistent padding size");
        else
        {
            auto result    = tune_pad_attribute(padding);
            val["padding"] = result;
            op.from_value(val);
            tuned = true;
        }
    }
211
    if(not attrs.contains("normalize_axes"))
Shucai Xiao's avatar
Shucai Xiao committed
212
    {
kahmed10's avatar
kahmed10 committed
213
        return tuned;
Shucai Xiao's avatar
Shucai Xiao committed
214
215
216
217
218
219
220
221
    }

    auto attr_v = attrs.at("normalize_axes").without_key();
    for(const auto& rv : attr_v)
    {
        const auto& key = rv.get_key();
        if(val.contains(key))
        {
222
223
            auto message = [&] { return op.name() + ": " + key + ": "; };
            auto vv      = val.at(key).without_key();
Shucai Xiao's avatar
Shucai Xiao committed
224
225
226
227
228
229
230
231
            if(vv.is_array())
            {
                std::vector<int64_t> axes;
                if(val.contains("axes"))
                {
                    axes = val.at("axes").without_key().to_vector<int64_t>();
                }
                auto vec    = vv.to_vector<int64_t>();
232
                auto result = tune_attribute(vec, axes, rv.without_key(), input_shape, message);
Shucai Xiao's avatar
Shucai Xiao committed
233
234
235
236
237
238
239
240
                val[key]    = result;
                op.from_value(val);
                val   = op.to_value();
                tuned = true;
            }
            else
            {
                auto num    = vv.to<int64_t>();
241
                auto result = tune_attribute({num}, {num}, rv.without_key(), input_shape, message);
Shucai Xiao's avatar
Shucai Xiao committed
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
                val[key]    = result.front();
                op.from_value(val);
                val   = op.to_value();
                tuned = true;
            }
        }
        else
        {
            MIGRAPHX_THROW("NORMALIZE_ATTR : op " + op.name() + " attribute \"" + key +
                           "\" not exist!");
        }
    }

    return tuned;
}

Charlie Lin's avatar
Charlie Lin committed
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
std::vector<int64_t> normalize_axes(const std::vector<int64_t>& axes,
                                    const shape& input_shape,
                                    const value& attr_val,
                                    const std::string& prefix)
{
    return tune_attribute(axes, {}, attr_val, input_shape, [&] { return prefix; });
}

std::vector<int64_t> normalize_indices(const std::vector<int64_t>& indices,
                                       const std::vector<int64_t>& axes,
                                       const shape& input_shape,
                                       const value& attr_val,
                                       const std::string& prefix)
{
    return tune_attribute(indices, axes, attr_val, input_shape, [&] { return prefix; });
}

Shucai Xiao's avatar
Shucai Xiao committed
275
276
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx