shape.hpp 5.5 KB
Newer Older
Paul's avatar
Paul committed
1
2
#ifndef MIGRAPHX_GUARD_MIGRAPHLIB_SHAPE_HPP
#define MIGRAPHX_GUARD_MIGRAPHLIB_SHAPE_HPP
Paul's avatar
Paul committed
3
4
5

#include <vector>
#include <cassert>
Paul's avatar
Paul committed
6
#include <ostream>
Paul's avatar
Paul committed
7
#include <numeric>
Paul's avatar
Paul committed
8
#include <memory>
Paul's avatar
Paul committed
9

Paul's avatar
Paul committed
10
11
12
#include <migraphx/errors.hpp>
#include <migraphx/half.hpp>
#include <migraphx/config.hpp>
Paul's avatar
Paul committed
13

Paul's avatar
Paul committed
14
namespace migraphx {
Paul's avatar
Paul committed
15
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
16

Paul's avatar
Paul committed
17
18
struct shape_impl;

Paul's avatar
Paul committed
19
20
struct shape
{
Paul's avatar
Paul committed
21
22

// Add new types here
Paul's avatar
Paul committed
23
// clang-format off
Paul's avatar
Paul committed
24
#define MIGRAPHX_SHAPE_VISIT_TYPES(m) \
Paul's avatar
Paul committed
25
    m(half_type, half) \
Paul's avatar
Paul committed
26
    m(float_type, float) \
Paul's avatar
Paul committed
27
28
29
30
31
32
33
34
    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
35
    m(uint64_type, uint64_t)
Paul's avatar
Paul committed
36
// clang-format on
Paul's avatar
Paul committed
37

Paul's avatar
Paul committed
38
#define MIGRAPHX_SHAPE_GENERATE_ENUM_TYPES(x, t) x,
Paul's avatar
Paul committed
39
40
    enum type_t
    {
Paul's avatar
Paul committed
41
        MIGRAPHX_SHAPE_VISIT_TYPES(MIGRAPHX_SHAPE_GENERATE_ENUM_TYPES)
Paul's avatar
Paul committed
42
    };
Paul's avatar
Paul committed
43
#undef MIGRAPHX_SHAPE_GENERATE_ENUM_TYPES
Paul's avatar
Paul committed
44

Paul's avatar
Paul committed
45
    template <class T, class = void>
Paul's avatar
Paul committed
46
    struct get_type;
Paul's avatar
Paul committed
47
#define MIGRAPHX_SHAPE_GENERATE_GET_TYPE(x, t)                \
Paul's avatar
Paul committed
48
    template <class T>                                        \
Paul's avatar
Paul committed
49
    struct get_type<t, T> : std::integral_constant<type_t, x> \
Paul's avatar
Paul committed
50
51
    {                                                         \
    };
Paul's avatar
Paul committed
52
53
    MIGRAPHX_SHAPE_VISIT_TYPES(MIGRAPHX_SHAPE_GENERATE_GET_TYPE)
#undef MIGRAPHX_SHAPE_GENERATE_GET_TYPE
Paul's avatar
Paul committed
54

wsttiger's avatar
wsttiger committed
55
56
57
58
59
    template <class T>
    struct get_type<const T> : get_type<T>
    {
    };

Paul's avatar
Paul committed
60
61
62
63
    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
64

Paul's avatar
Paul committed
65
66
67
68
69
70
    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
71
    shape(type_t t, const Range1& l, const Range2& s)
Paul's avatar
Paul committed
72
73
74
75
76
        : 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
77
78

    type_t type() const;
Paul's avatar
Paul committed
79
80
    const std::vector<std::size_t>& lens() const;
    const std::vector<std::size_t>& strides() const;
Paul's avatar
Paul committed
81
82
    std::size_t elements() const;
    std::size_t bytes() const;
Scott Thornton's avatar
Scott Thornton committed
83
    std::size_t type_size() const;
Paul's avatar
Paul committed
84

Paul's avatar
Paul committed
85
    /// Map multiple indices to space index
Paul's avatar
Paul committed
86
    std::size_t index(std::initializer_list<std::size_t> l) const;
Paul's avatar
Paul committed
87
    /// Map multiple indices to space index
Paul's avatar
Paul committed
88
    std::size_t index(const std::vector<std::size_t>& l) const;
Paul's avatar
Paul committed
89

Paul's avatar
Paul committed
90
    /// Map multiple indices from a range of iterator to a space index
Paul's avatar
Paul committed
91
    template <class Iterator>
Paul's avatar
Paul committed
92
93
94
95
    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
96
        return std::inner_product(start, last, this->strides().begin(), std::size_t{0}); // NOLINT
Paul's avatar
Paul committed
97
    }
Paul's avatar
Paul committed
98

Paul's avatar
Paul committed
99
    /// Map element index to space index
Paul's avatar
Paul committed
100
101
    std::size_t index(std::size_t i) const;

102
103
    std::vector<std::size_t> multi(std::size_t i) const;

Paul's avatar
Paul committed
104
    /// Returns true if the shape is packed with no padding
Paul's avatar
Paul committed
105
    bool packed() const;
Paul's avatar
Paul committed
106
107
    /// Returns true is the shape has been transposed. That is the strides are not in descending
    /// order
Paul's avatar
Paul committed
108
    bool transposed() const;
Paul's avatar
Paul committed
109
    /// Returns true if the shape is broadcasting a dimension. That is, one of the strides are zero
Paul's avatar
Paul committed
110
    bool broadcasted() const;
Paul's avatar
Paul committed
111
112
    /// 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
113
    bool standard() const;
Khalique's avatar
Khalique committed
114
115
    /// Returns true if all strides are equal to 0 (scalar tensor)
    bool scalar() const;
Paul's avatar
Paul committed
116

117
118
    shape normalize_standard() const;

Paul's avatar
Paul committed
119
120
    friend bool operator==(const shape& x, const shape& y);
    friend bool operator!=(const shape& x, const shape& y);
Paul's avatar
Paul committed
121
    friend std::ostream& operator<<(std::ostream& os, const shape& x);
Paul's avatar
Paul committed
122

Paul's avatar
Paul committed
123
    template <class T>
Paul's avatar
Paul committed
124
125
126
127
    struct as
    {
        using type = T;

Paul's avatar
Paul committed
128
        template <class U>
Paul's avatar
Paul committed
129
130
131
132
133
        T operator()(U u) const
        {
            return T(u);
        }

Paul's avatar
Paul committed
134
        template <class U>
Paul's avatar
Paul committed
135
136
137
138
139
        T* operator()(U* u) const
        {
            return static_cast<T*>(u);
        }

Paul's avatar
Paul committed
140
        template <class U>
Paul's avatar
Paul committed
141
142
143
144
145
        const T* operator()(const U* u) const
        {
            return static_cast<T*>(u);
        }

Paul's avatar
Paul committed
146
        T operator()() const { return {}; }
Paul's avatar
Paul committed
147

Paul's avatar
Paul committed
148
        std::size_t size(std::size_t n = 1) const { return sizeof(T) * n; }
Paul's avatar
Paul committed
149

Paul's avatar
Paul committed
150
151
        template <class U>
        T* from(U* buffer, std::size_t n = 0) const
Paul's avatar
Paul committed
152
        {
Paul's avatar
Paul committed
153
            return reinterpret_cast<T*>(buffer) + n;
Paul's avatar
Paul committed
154
        }
Paul's avatar
Paul committed
155

Paul's avatar
Paul committed
156
157
        template <class U>
        const T* from(const U* buffer, std::size_t n = 0) const
Paul's avatar
Paul committed
158
        {
Paul's avatar
Paul committed
159
            return reinterpret_cast<const T*>(buffer) + n;
Paul's avatar
Paul committed
160
        }
Paul's avatar
Paul committed
161

Paul's avatar
Paul committed
162
        type_t type_enum() const { return get_type<T>{}; }
Paul's avatar
Paul committed
163
164
    };

Paul's avatar
Paul committed
165
    template <class Visitor>
Paul's avatar
Paul committed
166
167
    void visit_type(Visitor v) const
    {
Paul's avatar
Paul committed
168
        switch(this->type())
Paul's avatar
Paul committed
169
        {
Paul's avatar
Paul committed
170
#define MIGRAPHX_SHAPE_GENERATE_VISITOR_CASE(x, t) \
Paul's avatar
Paul committed
171
    case x: v(as<t>()); return;
Paul's avatar
Paul committed
172
173
            MIGRAPHX_SHAPE_VISIT_TYPES(MIGRAPHX_SHAPE_GENERATE_VISITOR_CASE)
#undef MIGRAPHX_SHAPE_GENERATE_VISITOR_CASE
Paul's avatar
Paul committed
174
        }
Paul's avatar
Paul committed
175
        MIGRAPHX_THROW("Unknown type");
Paul's avatar
Paul committed
176
    }
Paul's avatar
Paul committed
177

Paul's avatar
Paul committed
178
179
180
    template <class Visitor>
    static void visit_types(Visitor v)
    {
Paul's avatar
Paul committed
181
#define MIGRAPHX_SHAPE_GENERATE_VISITOR_ALL(x, t) v(as<t>());
Paul's avatar
Paul committed
182
183
184
185
        MIGRAPHX_SHAPE_VISIT_TYPES(MIGRAPHX_SHAPE_GENERATE_VISITOR_ALL)
#undef MIGRAPHX_SHAPE_GENERATE_VISITOR_ALL
    }

186
187
    std::string type_string() const;

Paul's avatar
Paul committed
188
    private:
Paul's avatar
Paul committed
189
    std::shared_ptr<const shape_impl> impl;
Paul's avatar
Paul committed
190

Paul's avatar
Paul committed
191
192
193
    std::size_t element_space() const;
};

Paul's avatar
Paul committed
194
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
195
} // namespace migraphx
Paul's avatar
Paul committed
196
197

#endif