"tools/nni_trial_tool/trial_keeper.py" did not exist on "b29b7e557be954a0a5909a3fd25027652fd67e93"
shape.hpp 11.2 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
#ifndef MIGRAPHX_GUARD_MIGRAPHLIB_SHAPE_HPP
#define MIGRAPHX_GUARD_MIGRAPHLIB_SHAPE_HPP
Paul's avatar
Paul committed
26
27
28

#include <vector>
#include <cassert>
Paul's avatar
Paul committed
29
#include <ostream>
Paul's avatar
Paul committed
30
#include <numeric>
Paul's avatar
Paul committed
31
#include <memory>
Paul's avatar
Paul committed
32

33
#include <migraphx/functional.hpp>
Paul's avatar
Paul committed
34
35
36
#include <migraphx/errors.hpp>
#include <migraphx/half.hpp>
#include <migraphx/config.hpp>
Paul's avatar
Paul committed
37

Paul's avatar
Paul committed
38
namespace migraphx {
Paul's avatar
Paul committed
39
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
40

41
struct value;
Paul's avatar
Paul committed
42
43
struct shape_impl;

Paul's avatar
Paul committed
44
45
struct shape
{
Paul's avatar
Paul committed
46
47

// Add new types here
Paul's avatar
Paul committed
48
// clang-format off
Paul's avatar
Paul committed
49
#define MIGRAPHX_SHAPE_VISIT_TYPES(m) \
50
    m(bool_type, bool) \
Paul's avatar
Paul committed
51
    m(half_type, half) \
Paul's avatar
Paul committed
52
    m(float_type, float) \
Paul's avatar
Paul committed
53
54
55
56
57
58
59
60
    m(double_type, double) \
    m(uint8_type, uint8_t) \
    m(int8_type, int8_t) \
    m(uint16_type, uint16_t) \
    m(int16_type, int16_t) \
    m(int32_type, int32_t) \
    m(int64_type, int64_t) \
    m(uint32_type, uint32_t) \
Paul's avatar
Paul committed
61
    m(uint64_type, uint64_t)
bpickrel's avatar
bpickrel committed
62
    // clang-format on
Paul's avatar
Paul committed
63

Paul's avatar
Paul committed
64
#define MIGRAPHX_SHAPE_GENERATE_ENUM_TYPES(x, t) x,
Paul's avatar
Paul committed
65
66
    enum type_t
    {
Paul Fultz II's avatar
Paul Fultz II committed
67
        MIGRAPHX_SHAPE_VISIT_TYPES(MIGRAPHX_SHAPE_GENERATE_ENUM_TYPES) tuple_type
Paul's avatar
Paul committed
68
    };
Paul's avatar
Paul committed
69
#undef MIGRAPHX_SHAPE_GENERATE_ENUM_TYPES
Paul's avatar
Paul committed
70

Paul's avatar
Paul committed
71
    template <class T, class = void>
Paul's avatar
Paul committed
72
    struct get_type;
Paul's avatar
Paul committed
73
#define MIGRAPHX_SHAPE_GENERATE_GET_TYPE(x, t)                \
Paul's avatar
Paul committed
74
    template <class T>                                        \
Paul's avatar
Paul committed
75
    struct get_type<t, T> : std::integral_constant<type_t, x> \
Paul's avatar
Paul committed
76
77
    {                                                         \
    };
Paul's avatar
Paul committed
78
79
    MIGRAPHX_SHAPE_VISIT_TYPES(MIGRAPHX_SHAPE_GENERATE_GET_TYPE)
#undef MIGRAPHX_SHAPE_GENERATE_GET_TYPE
Paul's avatar
Paul committed
80

wsttiger's avatar
wsttiger committed
81
82
83
84
85
    template <class T>
    struct get_type<const T> : get_type<T>
    {
    };

Charlie Lin's avatar
Charlie Lin committed
86
87
88
89
90
91
92
    struct dynamic_dimension
    {
        std::size_t min = 0;
        std::size_t max = 0;
        std::size_t opt = 0;

        template <class Self, class F>
93
94
95
96
        static auto reflect(Self& self, F f)
        {
            return pack(f(self.min, "min"), f(self.max, "max"), f(self.opt, "opt"));
        }
Charlie Lin's avatar
Charlie Lin committed
97
98
99
100
101
102
103

        bool is_fixed() const;
        bool has_optimal() const;

        friend bool operator==(const dynamic_dimension& x, const dynamic_dimension& y);
        friend bool operator!=(const dynamic_dimension& x, const dynamic_dimension& y);
        friend std::ostream& operator<<(std::ostream& os, const dynamic_dimension& x);
104
105
106
107
108
109

        // compare to fixed std::size_t dimension
        friend bool operator==(const dynamic_dimension& x, const std::size_t& y);
        friend bool operator==(const std::size_t& x, const dynamic_dimension& y);
        friend bool operator!=(const dynamic_dimension& x, const std::size_t& y);
        friend bool operator!=(const std::size_t& x, const dynamic_dimension& y);
Charlie Lin's avatar
Charlie Lin committed
110
111
    };

112
113
    static const std::vector<type_t>& types();

114
115
116
    static std::string name(type_t t);
    static std::string cpp_type(type_t t);

Paul's avatar
Paul committed
117
118
119
120
    shape();
    shape(type_t t);
    shape(type_t t, std::vector<std::size_t> l);
    shape(type_t t, std::vector<std::size_t> l, std::vector<std::size_t> s);
Paul's avatar
Paul committed
121

Charlie Lin's avatar
Charlie Lin committed
122
123
124
125
126
127
    // Force all calls of the format `shape( type_t, { size_t compatibles } )` to map to
    // shape(type_t, std::vector<std::size_t> l)
    shape(type_t t, std::initializer_list<std::size_t> d);

    shape(type_t t, std::vector<dynamic_dimension> dims);

128
129
130
131
132
133
    // Construct a dynamic shape from three sets of lengths (of the same rank)
    shape(type_t t,
          std::vector<std::size_t> mins,
          std::vector<std::size_t> maxes,
          std::vector<std::size_t> opts);

Paul's avatar
Paul committed
134
135
136
137
138
139
    template <class Range>
    shape(type_t t, const Range& l) : shape(t, std::vector<std::size_t>(l.begin(), l.end()))
    {
    }

    template <class Range1, class Range2>
Paul's avatar
Paul committed
140
    shape(type_t t, const Range1& l, const Range2& s)
Paul's avatar
Paul committed
141
142
143
144
145
        : shape(t,
                std::vector<std::size_t>(l.begin(), l.end()),
                std::vector<std::size_t>(s.begin(), s.end()))
    {
    }
Paul's avatar
Paul committed
146

Paul Fultz II's avatar
Paul Fultz II committed
147
148
    shape(const std::vector<shape>& subs);

149
150
    static shape
    from_permutation(type_t t, const std::vector<std::size_t>& l, const std::vector<int64_t>& perm);
Paul's avatar
Paul committed
151
    type_t type() const;
Paul's avatar
Paul committed
152
153
    const std::vector<std::size_t>& lens() const;
    const std::vector<std::size_t>& strides() const;
Charlie Lin's avatar
Charlie Lin committed
154

155
156
157
158
159
160
    /*!
     * The number of dimensions in the shape.
     * Same as the number of indices required to get a data value.
     */
    std::size_t ndim() const;

Charlie Lin's avatar
Charlie Lin committed
161
162
163
    /*!
     * Return the number of elements in the tensor.
     */
Paul's avatar
Paul committed
164
    std::size_t elements() const;
Charlie Lin's avatar
Charlie Lin committed
165
166
167
168
169

    /*!
     * Return the number of total bytes used for storage of the tensor data; includes subshapes.
     * For dynamic shape, returns the maximum number of bytes presuming a packed shape.
     */
Paul's avatar
Paul committed
170
    std::size_t bytes() const;
Charlie Lin's avatar
Charlie Lin committed
171
172
173
174
175

    /*!
     * Return the size of the type of the main shape.
     * Returns 0 if there are subshapes.
     */
Scott Thornton's avatar
Scott Thornton committed
176
    std::size_t type_size() const;
Paul's avatar
Paul committed
177

Charlie Lin's avatar
Charlie Lin committed
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
    const std::vector<dynamic_dimension>& dyn_dims() const;

    /*!
     * Minimum lengths for dynamic shape.
     * lens() for fixed shape.
     */
    std::vector<std::size_t> min_lens() const;

    /*!
     * Maximum lengths for dynamic shape.
     * lens() for fixed shape.
     */
    std::vector<std::size_t> max_lens() const;

    /*!
     * Optimum lengths for dynamic shape.
     * lens() for fixed shape.
     */
    std::vector<std::size_t> opt_lens() const;

Paul's avatar
Paul committed
198
    /// Map multiple indices to space index
Paul's avatar
Paul committed
199
    std::size_t index(std::initializer_list<std::size_t> l) const;
Paul's avatar
Paul committed
200
    /// Map multiple indices to space index
Paul's avatar
Paul committed
201
    std::size_t index(const std::vector<std::size_t>& l) const;
Paul's avatar
Paul committed
202

Paul's avatar
Paul committed
203
    /// Map multiple indices from a range of iterator to a space index
Paul's avatar
Paul committed
204
    template <class Iterator>
Paul's avatar
Paul committed
205
206
207
208
    std::size_t index(Iterator start, Iterator last) const
    {
        assert(std::distance(start, last) <= this->lens().size());
        assert(this->lens().size() == this->strides().size());
Paul Fultz II's avatar
Paul Fultz II committed
209
        return std::inner_product(start, last, this->strides().begin(), std::size_t{0}); // NOLINT
Paul's avatar
Paul committed
210
    }
Paul's avatar
Paul committed
211

Paul's avatar
Paul committed
212
    /// Map element index to space index
Paul's avatar
Paul committed
213
214
    std::size_t index(std::size_t i) const;

215
    std::vector<std::size_t> multi(std::size_t i) const;
216
    void multi_copy(std::size_t i, std::size_t* start, const std::size_t* end) const;
217

Charlie Lin's avatar
Charlie Lin committed
218
219
    /// Returns true if the shape is packed (number of elements and buffer size the same) with no
    /// padding
Paul's avatar
Paul committed
220
    bool packed() const;
Charlie Lin's avatar
Charlie Lin committed
221

Paul's avatar
Paul committed
222
223
    /// Returns true is the shape has been transposed. That is the strides are not in descending
    /// order
Paul's avatar
Paul committed
224
    bool transposed() const;
Charlie Lin's avatar
Charlie Lin committed
225

Paul's avatar
Paul committed
226
    /// Returns true if the shape is broadcasting a dimension. That is, one of the strides are zero
Paul's avatar
Paul committed
227
    bool broadcasted() const;
Charlie Lin's avatar
Charlie Lin committed
228

Paul's avatar
Paul committed
229
230
    /// Returns true if the shape is in its standard format. That is, the shape is both packed and
    /// not transposed.
Paul's avatar
Paul committed
231
    bool standard() const;
Charlie Lin's avatar
Charlie Lin committed
232

Khalique's avatar
Khalique committed
233
234
    /// Returns true if all strides are equal to 0 (scalar tensor)
    bool scalar() const;
Paul's avatar
Paul committed
235

Charlie Lin's avatar
Charlie Lin committed
236
237
238
    /// Return true if the shape is dynamic
    bool dynamic() const;

239
240
    shape normalize_standard() const;

241
242
243
    shape with_lens(type_t t, const std::vector<std::size_t>& l) const;
    shape with_lens(const std::vector<std::size_t>& l) const;

244
245
    shape with_type(type_t t) const;

246
247
248
    // convert the shape to an equivalent dynamic shape
    shape to_dynamic() const;

Paul's avatar
Paul committed
249
250
    friend bool operator==(const shape& x, const shape& y);
    friend bool operator!=(const shape& x, const shape& y);
Paul's avatar
Paul committed
251
    friend std::ostream& operator<<(std::ostream& os, const shape& x);
Paul's avatar
Paul committed
252

Paul's avatar
Paul committed
253
    template <class T>
Paul's avatar
Paul committed
254
255
    struct as
    {
256
        using type = std::conditional_t<std::is_same<T, bool>{}, int8_t, T>;
Paul's avatar
Paul committed
257

258
259
260
261
        type max() const { return std::numeric_limits<type>::max(); }

        type min() const { return std::numeric_limits<type>::lowest(); }

Paul's avatar
Paul committed
262
        template <class U>
263
        type operator()(U u) const
Paul's avatar
Paul committed
264
        {
265
            return type(u);
Paul's avatar
Paul committed
266
267
        }

Paul's avatar
Paul committed
268
        template <class U>
269
        type* operator()(U* u) const
Paul's avatar
Paul committed
270
        {
271
            return static_cast<type*>(u);
Paul's avatar
Paul committed
272
273
        }

Paul's avatar
Paul committed
274
        template <class U>
275
        const type* operator()(const U* u) const
Paul's avatar
Paul committed
276
        {
277
            return static_cast<type*>(u);
Paul's avatar
Paul committed
278
279
        }

280
        type operator()() const { return {}; }
Paul's avatar
Paul committed
281

282
        std::size_t size(std::size_t n = 1) const { return sizeof(type) * n; }
Paul's avatar
Paul committed
283

Paul Fultz II's avatar
Paul Fultz II committed
284
285
286
287
        auto is_integral() const { return std::is_integral<type>{}; }
        auto is_signed() const { return std::is_signed<type>{}; }
        auto is_unsigned() const { return std::is_unsigned<type>{}; }

Paul's avatar
Paul committed
288
        template <class U>
289
        type* from(U* buffer, std::size_t n = 0) const
Paul's avatar
Paul committed
290
        {
291
            return reinterpret_cast<type*>(buffer) + n;
Paul's avatar
Paul committed
292
        }
Paul's avatar
Paul committed
293

Paul's avatar
Paul committed
294
        template <class U>
295
        const type* from(const U* buffer, std::size_t n = 0) const
Paul's avatar
Paul committed
296
        {
297
            return reinterpret_cast<const type*>(buffer) + n;
Paul's avatar
Paul committed
298
        }
Paul's avatar
Paul committed
299

300
        type_t type_enum() const { return get_type<type>{}; }
Paul's avatar
Paul committed
301
302
    };

Paul Fultz II's avatar
Paul Fultz II committed
303
304
    template <class Visitor, class TupleVisitor>
    static void visit(type_t t, Visitor v, TupleVisitor tv)
Paul's avatar
Paul committed
305
    {
306
        switch(t)
Paul's avatar
Paul committed
307
        {
bpickrel's avatar
bpickrel committed
308
        case tuple_type: {
Paul Fultz II's avatar
Paul Fultz II committed
309
310
311
            tv();
            return;
        }
Paul's avatar
Paul committed
312
#define MIGRAPHX_SHAPE_GENERATE_VISITOR_CASE(x, t) \
Paul's avatar
Paul committed
313
    case x: v(as<t>()); return;
Paul's avatar
Paul committed
314
315
            MIGRAPHX_SHAPE_VISIT_TYPES(MIGRAPHX_SHAPE_GENERATE_VISITOR_CASE)
#undef MIGRAPHX_SHAPE_GENERATE_VISITOR_CASE
Paul's avatar
Paul committed
316
        }
Paul's avatar
Paul committed
317
        MIGRAPHX_THROW("Unknown type");
Paul's avatar
Paul committed
318
    }
Paul's avatar
Paul committed
319

320
    template <class Visitor>
Paul Fultz II's avatar
Paul Fultz II committed
321
    static void visit(type_t t, Visitor v)
322
    {
Paul Fultz II's avatar
Paul Fultz II committed
323
324
325
326
327
328
329
        return visit(t, v, [] { MIGRAPHX_THROW("Tuple cannot be visited."); });
    }

    template <class... Visitors>
    void visit_type(Visitors... vs) const
    {
        visit(this->type(), vs...);
330
331
    }

Paul's avatar
Paul committed
332
333
334
    template <class Visitor>
    static void visit_types(Visitor v)
    {
Paul's avatar
Paul committed
335
#define MIGRAPHX_SHAPE_GENERATE_VISITOR_ALL(x, t) v(as<t>());
Paul's avatar
Paul committed
336
337
338
339
        MIGRAPHX_SHAPE_VISIT_TYPES(MIGRAPHX_SHAPE_GENERATE_VISITOR_ALL)
#undef MIGRAPHX_SHAPE_GENERATE_VISITOR_ALL
    }

340
    std::string type_string() const;
341
    static type_t parse_type(const std::string& s);
342

Paul Fultz II's avatar
Paul Fultz II committed
343
344
    const std::vector<shape>& sub_shapes() const;

Charlie Lin's avatar
Charlie Lin committed
345
346
347
348
349
    /*!
     * Returns the number of elements in the data buffer.
     * For a dynamic shape, returns the maximum number of elements of the data buffer and assumes it
     * is packed.
     */
350
351
    std::size_t element_space() const;

Paul's avatar
Paul committed
352
    private:
353
    shape(std::shared_ptr<shape_impl> pimpl);
Paul's avatar
Paul committed
354
    std::shared_ptr<const shape_impl> impl;
Paul's avatar
Paul committed
355
356
};

357
358
359
void migraphx_to_value(value& v, const shape& s);
void migraphx_from_value(const value& v, shape& s);

Paul's avatar
Paul committed
360
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
361
} // namespace migraphx
Paul's avatar
Paul committed
362
363

#endif