shape.cpp 9.72 KB
Newer Older
Paul's avatar
Paul committed
1

Paul's avatar
Paul committed
2
3
#include <migraphx/shape.hpp>
#include <migraphx/stringutils.hpp>
4
#include <migraphx/serialize.hpp>
5
#include <migraphx/permutation.hpp>
Paul's avatar
Paul committed
6
7
8
#include <numeric>
#include <algorithm>
#include <functional>
9
#include <unordered_map>
Paul's avatar
Paul committed
10
#include <iostream>
Paul's avatar
Paul committed
11

Paul's avatar
Paul committed
12
namespace migraphx {
Paul's avatar
Paul committed
13
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
14

Paul's avatar
Paul committed
15
16
struct shape_impl
{
Paul's avatar
Paul committed
17
18
    static std::shared_ptr<shape_impl> default_shape()
    {
19
        static const std::shared_ptr<shape_impl> result = std::make_shared<shape_impl>();
Paul's avatar
Paul committed
20
21
22
23
24
        return result;
    }

    shape_impl() : m_type(shape::float_type), m_standard(false) {}

25
    shape_impl(shape::type_t t) : m_type(t), m_lens({1}), m_strides({0}), m_standard(true) {}
Paul's avatar
Paul committed
26
27
28
29
30
31
32
33
34
35
    shape_impl(shape::type_t t, std::vector<std::size_t> l)
        : m_type(t), m_lens(std::move(l)), m_standard(true)
    {
        this->calculate_strides();
        assert(m_lens.size() == m_strides.size());
    }
    shape_impl(shape::type_t t, std::vector<std::size_t> l, std::vector<std::size_t> s)
        : m_type(t), m_lens(std::move(l)), m_strides(std::move(s))
    {
        assert(m_lens.size() == m_strides.size());
Khalique's avatar
Khalique committed
36
37
        // assert(std::any_of(m_strides.begin(), m_strides.end(), [](auto x) { return x > 0; }) and
        //        "At least one stride must be non-zero");
38
39
        m_standard = this->elements() == this->element_space() and
                     std::is_sorted(m_strides.rbegin(), m_strides.rend());
Paul's avatar
Paul committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    }
    shape::type_t m_type;
    std::vector<std::size_t> m_lens;
    std::vector<std::size_t> m_strides;
    bool m_standard;

    void calculate_strides()
    {
        m_strides.clear();
        m_strides.resize(m_lens.size(), 0);
        if(m_strides.empty())
            return;
        m_strides.back() = 1;
        std::partial_sum(m_lens.rbegin(),
                         m_lens.rend() - 1,
                         m_strides.rbegin() + 1,
                         std::multiplies<std::size_t>());
    }

    std::size_t element_space() const
    {
        assert(m_lens.size() == m_strides.size());
        if(m_lens.empty())
            return 0;
        return std::inner_product(m_lens.begin(),
                                  m_lens.end(),
                                  m_strides.begin(),
                                  std::size_t{0},
                                  std::plus<std::size_t>{},
                                  [](std::size_t l, std::size_t s) { return (l - 1) * s; }) +
               1;
    }

    std::size_t elements() const
    {
        assert(m_lens.size() == m_strides.size());
        if(m_lens.empty())
            return 0;
        return std::accumulate(
            m_lens.begin(), m_lens.end(), std::size_t{1}, std::multiplies<std::size_t>());
    }
Paul's avatar
Paul committed
81
};
Paul's avatar
Paul committed
82

83
84
85
86
87
88
89
90
const std::vector<shape::type_t>& shape::types()
{
    static const std::vector<shape::type_t> result = {
#define MIGRAPHX_GENERATE_TYPE_VECTOR(x, t) x,
        MIGRAPHX_SHAPE_VISIT_TYPES(MIGRAPHX_GENERATE_TYPE_VECTOR)};
    return result;
}

91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
std::string shape::name(shape::type_t t)
{
    switch(t)
    {
#define MIGRAPHX_SHAPE_GENERATE_TYPE_NAME_CASE(x, t) \
    case x: return #x;
        MIGRAPHX_SHAPE_VISIT_TYPES(MIGRAPHX_SHAPE_GENERATE_TYPE_NAME_CASE)
#undef MIGRAPHX_SHAPE_GENERATE_TYPE_NAME_CASE
    }
    MIGRAPHX_THROW("Invalid type");
}
std::string shape::cpp_type(shape::type_t t)
{
    switch(t)
    {
#define MIGRAPHX_SHAPE_GENERATE_CPP_TYPE_CASE(x, t) \
    case x: return #t;
        MIGRAPHX_SHAPE_VISIT_TYPES(MIGRAPHX_SHAPE_GENERATE_CPP_TYPE_CASE)
#undef MIGRAPHX_SHAPE_GENERATE_CPP_TYPE_CASE
    }
    MIGRAPHX_THROW("Invalid type");
}

Paul's avatar
Paul committed
114
115
116
shape::shape() : impl(shape_impl::default_shape()) {}

shape::shape(type_t t) : impl(std::make_shared<shape_impl>(t)) {}
Paul's avatar
Paul committed
117
shape::shape(type_t t, std::vector<std::size_t> l)
Paul's avatar
Paul committed
118
    : impl(std::make_shared<shape_impl>(t, std::move(l)))
Paul's avatar
Paul committed
119
120
121
{
}
shape::shape(type_t t, std::vector<std::size_t> l, std::vector<std::size_t> s)
Paul's avatar
Paul committed
122
    : impl(std::make_shared<shape_impl>(t, std::move(l), std::move(s)))
Paul's avatar
Paul committed
123
124
125
{
}

126
127
128
129
130
131
132
133
134
135
shape shape::from_permutation(type_t t,
                              const std::vector<std::size_t>& l,
                              const std::vector<int64_t>& perm)
{
    auto new_lens = reorder_dims(l, perm);
    shape result  = reorder_shape({t, new_lens}, invert_permutation(perm));
    assert(result.lens() == l);
    return result;
}

Paul's avatar
Paul committed
136
137
138
shape::type_t shape::type() const { return impl->m_type; }
const std::vector<std::size_t>& shape::lens() const { return impl->m_lens; }
const std::vector<std::size_t>& shape::strides() const { return impl->m_strides; }
Paul's avatar
Paul committed
139
std::size_t shape::elements() const { return impl->elements(); }
Paul's avatar
Paul committed
140
141
142
143
144
145
std::size_t shape::bytes() const
{
    std::size_t n = 0;
    this->visit_type([&](auto as) { n = as.size(); });
    return n * this->element_space();
}
Scott Thornton's avatar
Scott Thornton committed
146
147
148
149
150
151
std::size_t shape::type_size() const
{
    std::size_t n = 0;
    this->visit_type([&](auto as) { n = as.size(); });
    return n;
}
Paul's avatar
Paul committed
152
153
154
155
156
157
158
159
160
161
162
163
std::size_t shape::index(std::initializer_list<std::size_t> l) const
{
    assert(l.size() <= this->lens().size());
    assert(this->lens().size() == this->strides().size());
    return std::inner_product(l.begin(), l.end(), this->strides().begin(), std::size_t{0});
}
std::size_t shape::index(const std::vector<std::size_t>& l) const
{
    assert(l.size() <= this->lens().size());
    assert(this->lens().size() == this->strides().size());
    return std::inner_product(l.begin(), l.end(), this->strides().begin(), std::size_t{0});
}
Paul's avatar
Paul committed
164
165
166
std::size_t shape::index(std::size_t i) const
{
    assert(this->lens().size() == this->strides().size());
Paul's avatar
Paul committed
167
    if(this->standard())
Paul's avatar
Paul committed
168
169
        return i;
    else
Paul's avatar
Paul committed
170
    {
Paul's avatar
Paul committed
171
        std::size_t s      = 1;
Paul's avatar
Paul committed
172
        std::size_t result = 0;
Paul's avatar
Paul committed
173
        for(std::size_t j = 0; j < this->lens().size(); j++)
Paul's avatar
Paul committed
174
        {
Paul's avatar
Paul committed
175
            const std::size_t k      = this->lens().size() - j - 1;
Paul's avatar
Paul committed
176
            const std::size_t stride = this->strides()[k];
Paul's avatar
Paul committed
177
178
            const std::size_t len    = this->lens()[k];
            const std::size_t idx    = (i % (s * len)) / s;
Paul's avatar
Paul committed
179
180
181
182
183
            result += stride * idx;
            s *= len;
        }
        return result;
    }
Paul's avatar
Paul committed
184
}
185
186
187
188
189
190

std::vector<std::size_t> shape::multi(std::size_t i) const
{
    assert(this->standard());

    std::vector<std::size_t> indices(lens().size());
191
192
193
194
195
196
197
198
199
200
    multi_copy(i, indices.data(), indices.data() + lens().size());

    return indices;
}

void shape::multi_copy(std::size_t i, std::size_t* start, const std::size_t* end) const
{
    assert(this->standard());
    (void)end;
    assert(lens().size() <= (end - start));
201
    std::transform(strides().begin(),
Shucai Xiao's avatar
Shucai Xiao committed
202
203
                   strides().end(),
                   lens().begin(),
204
                   start,
Shucai Xiao's avatar
Shucai Xiao committed
205
206
207
208
                   [&](std::size_t stride, std::size_t len) {
                       assert(len > 0 and stride > 0);
                       return (i / stride) % len;
                   });
209
210
}

Paul's avatar
Paul committed
211
212
bool shape::packed() const { return this->elements() == this->element_space(); }

Paul's avatar
Paul committed
213
214
bool shape::transposed() const
{
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
    if(this->broadcasted())
    {
        // TODO: Use a filter_iterator instead
        std::vector<std::size_t> s;
        s.reserve(this->strides().size());
        std::copy_if(this->strides().begin(),
                     this->strides().end(),
                     std::back_inserter(s),
                     [](std::size_t x) { return x != 0; });
        return not std::is_sorted(s.rbegin(), s.rend());
    }
    else
    {
        return not std::is_sorted(this->strides().rbegin(), this->strides().rend());
    }
Paul's avatar
Paul committed
230
}
Paul's avatar
Paul committed
231
232
233
234

bool shape::broadcasted() const
{
    assert(this->lens().size() == this->strides().size());
Paul's avatar
Paul committed
235
236
237
238
    return std::accumulate(this->strides().begin(),
                           this->strides().end(),
                           std::size_t{1},
                           std::multiplies<std::size_t>()) == 0;
Paul's avatar
Paul committed
239
240
}

Khalique's avatar
Khalique committed
241
242
243
244
bool shape::scalar() const
{
    assert(this->lens().size() == this->strides().size());
    // if any stride > 0, then accumulate will return false
Khalique's avatar
Khalique committed
245
    return std::accumulate(this->strides().begin(), this->strides().end(), std::size_t(0)) == 0;
Khalique's avatar
Khalique committed
246
247
}

Paul's avatar
Paul committed
248
bool shape::standard() const { return impl->m_standard; }
Paul's avatar
Paul committed
249

250
251
252
253
254
255
256
257
shape shape::normalize_standard() const
{
    if(this->standard())
        return {this->type(), this->lens()};
    else
        return *this;
}

258
259
260
261
262
263
264
265
266
267
268
269
shape shape::with_lens(type_t t, const std::vector<std::size_t>& l) const
{
    assert(l.size() == this->lens().size());
    auto perm = find_permutation(*this);
    return shape::from_permutation(t, l, perm);
}

shape shape::with_lens(const std::vector<std::size_t>& l) const
{
    return this->with_lens(this->type(), l);
}

Paul's avatar
Paul committed
270
std::size_t shape::element_space() const { return impl->element_space(); }
Paul's avatar
Paul committed
271

272
std::string shape::type_string() const { return name(this->type()); }
Paul's avatar
Paul committed
273

Paul's avatar
Paul committed
274
275
276
277
bool operator==(const shape& x, const shape& y)
{
    return x.type() == y.type() && x.lens() == y.lens() && x.strides() == y.strides();
}
Paul's avatar
Paul committed
278
bool operator!=(const shape& x, const shape& y) { return !(x == y); }
Paul's avatar
Paul committed
279

Paul's avatar
Paul committed
280
281
282
std::ostream& operator<<(std::ostream& os, const shape& x)
{
    os << x.type_string() << ", ";
Paul's avatar
Paul committed
283
284
    os << "{" << to_string_range(x.lens()) << "}, ";
    os << "{" << to_string_range(x.strides()) << "}";
Paul's avatar
Paul committed
285
286
287
    return os;
}

288
289
shape::type_t shape::parse_type(const std::string& s)
{
290
    static const std::unordered_map<std::string, shape::type_t> m = {
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#define MIGRAPHX_SHAPE_GENERATE_TYPE_STRING_MAP(x, t) {#x, x}, {#t, x},
        MIGRAPHX_SHAPE_VISIT_TYPES(MIGRAPHX_SHAPE_GENERATE_TYPE_STRING_MAP)};
    return m.at(s);
}

void migraphx_to_value(value& v, const shape& s)
{
    value result;
    result["type"]    = migraphx::to_value(s.type_string());
    result["lens"]    = migraphx::to_value(s.lens());
    result["strides"] = migraphx::to_value(s.strides());
    v                 = result;
}
void migraphx_from_value(const value& v, shape& s)
{
    s = shape{shape::parse_type(v.at("type").get_string()),
              v.at("lens").to_vector<std::size_t>(),
              v.at("strides").to_vector<std::size_t>()};
}

Paul's avatar
Paul committed
311
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
312
} // namespace migraphx