shape.cpp 20.3 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

Paul's avatar
Paul committed
25
26
#include <migraphx/shape.hpp>
#include <migraphx/stringutils.hpp>
27
#include <migraphx/serialize.hpp>
28
#include <migraphx/permutation.hpp>
Charlie Lin's avatar
Charlie Lin committed
29
#include <migraphx/ranges.hpp>
Paul's avatar
Paul committed
30
31
32
#include <numeric>
#include <algorithm>
#include <functional>
33
#include <unordered_map>
Paul's avatar
Paul committed
34
#include <iostream>
Paul's avatar
Paul committed
35

Paul's avatar
Paul committed
36
namespace migraphx {
Paul's avatar
Paul committed
37
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
38

Paul's avatar
Paul committed
39
40
struct shape_impl
{
Paul's avatar
Paul committed
41
42
    static std::shared_ptr<shape_impl> default_shape()
    {
43
        static const std::shared_ptr<shape_impl> result = std::make_shared<shape_impl>();
Paul's avatar
Paul committed
44
45
46
        return result;
    }

Paul Fultz II's avatar
Paul Fultz II committed
47
    shape_impl() : m_type(shape::float_type) {}
Paul's avatar
Paul committed
48

Paul Fultz II's avatar
Paul Fultz II committed
49
50
51
52
    shape_impl(shape::type_t t) : m_type(t), m_lens({1}), m_strides({0}), m_standard(true)
    {
        assert(t != shape::tuple_type);
    }
Paul's avatar
Paul committed
53
54
55
    shape_impl(shape::type_t t, std::vector<std::size_t> l)
        : m_type(t), m_lens(std::move(l)), m_standard(true)
    {
Paul Fultz II's avatar
Paul Fultz II committed
56
        assert(t != shape::tuple_type);
Paul's avatar
Paul committed
57
58
59
60
61
62
        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))
    {
Paul Fultz II's avatar
Paul Fultz II committed
63
        assert(t != shape::tuple_type);
Paul's avatar
Paul committed
64
        assert(m_lens.size() == m_strides.size());
Shiv's avatar
Shiv committed
65
66
67
68
69
        std::vector<std::size_t> std_strides = {};
        this->calculate_standard_strides(std_strides);
        bool is_scalar = std::accumulate(m_strides.begin(), m_strides.end(), std::size_t(0)) == 0;
        m_standard     = this->elements() == this->element_space() and not skips() and
                     (m_strides == std_strides or is_scalar);
Paul's avatar
Paul committed
70
    }
Paul Fultz II's avatar
Paul Fultz II committed
71

Charlie Lin's avatar
Charlie Lin committed
72
73
74
75
76
    shape_impl(shape::type_t t, std::vector<shape::dynamic_dimension> dims)
        : m_type(t), m_dyn_dims(std::move(dims))
    {
    }

77
78
79
80
81
82
83
84
85
86
87
88
89
    shape_impl(shape::type_t t,
               std::vector<std::size_t> mins,
               std::vector<std::size_t> maxes,
               std::vector<std::size_t> opts)
        : m_type(t)
    {
        assert(mins.size() == maxes.size() and maxes.size() == opts.size());
        for(size_t i = 0; i < mins.size(); ++i)
        {
            m_dyn_dims.push_back(shape::dynamic_dimension{mins[i], maxes[i], opts[i]});
        }
    }

Paul Fultz II's avatar
Paul Fultz II committed
90
    shape_impl(const std::vector<shape>& subs) : m_type(shape::tuple_type), m_shapes(subs) {}
Charlie Lin's avatar
Charlie Lin committed
91

Paul's avatar
Paul committed
92
    shape::type_t m_type;
Paul Fultz II's avatar
Paul Fultz II committed
93
94
95
96
    std::vector<std::size_t> m_lens    = {};
    std::vector<std::size_t> m_strides = {};
    std::vector<shape> m_shapes        = {};
    bool m_standard                    = false;
Paul's avatar
Paul committed
97

Charlie Lin's avatar
Charlie Lin committed
98
99
    std::vector<shape::dynamic_dimension> m_dyn_dims = {};

Shiv's avatar
Shiv committed
100
101
102
    void calculate_strides() { this->calculate_standard_strides(m_strides); }

    void calculate_standard_strides(std::vector<std::size_t>& strides)
Paul's avatar
Paul committed
103
    {
Shiv's avatar
Shiv committed
104
105
106
        strides.clear();
        strides.resize(m_lens.size(), 0);
        if(strides.empty())
Paul's avatar
Paul committed
107
            return;
Shiv's avatar
Shiv committed
108
        strides.back() = 1;
Paul's avatar
Paul committed
109
110
        std::partial_sum(m_lens.rbegin(),
                         m_lens.rend() - 1,
Shiv's avatar
Shiv committed
111
                         strides.rbegin() + 1,
Paul's avatar
Paul committed
112
113
114
115
116
                         std::multiplies<std::size_t>());
    }

    std::size_t element_space() const
    {
Charlie Lin's avatar
Charlie Lin committed
117
118
119
120
121
122
        if(not m_dyn_dims.empty())
        {
            auto maxes = max_lens();
            return std::accumulate(maxes.begin(), maxes.end(), std::size_t{1}, std::multiplies<>());
        }

Paul's avatar
Paul committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
        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
    {
Charlie Lin's avatar
Charlie Lin committed
137
138
139
140
141
        if(not m_dyn_dims.empty())
        {
            MIGRAPHX_THROW("SHAPE: elements() called on dynamic shape");
        }

Paul's avatar
Paul committed
142
143
144
145
146
147
        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>());
    }
148

Charlie Lin's avatar
Charlie Lin committed
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
    std::vector<std::size_t> min_lens() const
    {
        std::vector<std::size_t> ret(m_dyn_dims.size());
        std::transform(m_dyn_dims.cbegin(),
                       m_dyn_dims.cend(),
                       ret.begin(),
                       [](shape::dynamic_dimension x) { return x.min; });
        return ret;
    }

    std::vector<std::size_t> max_lens() const
    {
        std::vector<std::size_t> ret(m_dyn_dims.size());
        std::transform(m_dyn_dims.cbegin(),
                       m_dyn_dims.cend(),
                       ret.begin(),
                       [](shape::dynamic_dimension x) { return x.max; });
        return ret;
    }

    std::vector<std::size_t> opt_lens() const
    {
        std::vector<std::size_t> ret(m_dyn_dims.size());
        std::transform(m_dyn_dims.cbegin(),
                       m_dyn_dims.cend(),
                       ret.begin(),
                       [](shape::dynamic_dimension x) { return x.opt; });
        return ret;
    }
178
179
180
181
182
183
184
185
186
    // Does the shape skip over elements?
    bool skips() const
    {
        assert(m_lens.size() == m_strides.size());
        if(elements() == 1)
            return false;
        return std::none_of(m_strides.begin(), m_strides.end(), [](auto x) { return x == 1; });
    }

187
    std::shared_ptr<shape_impl> copy() const { return std::make_shared<shape_impl>(*this); }
Paul's avatar
Paul committed
188
};
Paul's avatar
Paul committed
189

190
191
192
193
const std::vector<shape::type_t>& shape::types()
{
    static const std::vector<shape::type_t> result = {
#define MIGRAPHX_GENERATE_TYPE_VECTOR(x, t) x,
Paul Fultz II's avatar
Paul Fultz II committed
194
        MIGRAPHX_SHAPE_VISIT_TYPES(MIGRAPHX_GENERATE_TYPE_VECTOR) tuple_type};
195
196
197
    return result;
}

198
199
200
201
std::string shape::name(shape::type_t t)
{
    switch(t)
    {
Paul Fultz II's avatar
Paul Fultz II committed
202
    case tuple_type: return "tuple_type";
203
204
205
206
207
208
209
210
211
212
213
#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)
    {
Paul Fultz II's avatar
Paul Fultz II committed
214
    case tuple_type: MIGRAPHX_THROW("No C++ type for tuple");
215
216
217
218
219
220
221
222
#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
223
224
225
shape::shape() : impl(shape_impl::default_shape()) {}

shape::shape(type_t t) : impl(std::make_shared<shape_impl>(t)) {}
Paul's avatar
Paul committed
226
shape::shape(type_t t, std::vector<std::size_t> l)
Paul's avatar
Paul committed
227
    : impl(std::make_shared<shape_impl>(t, std::move(l)))
Paul's avatar
Paul committed
228
229
230
{
}
shape::shape(type_t t, std::vector<std::size_t> l, std::vector<std::size_t> s)
Paul's avatar
Paul committed
231
    : impl(std::make_shared<shape_impl>(t, std::move(l), std::move(s)))
Paul's avatar
Paul committed
232
233
234
{
}

Charlie Lin's avatar
Charlie Lin committed
235
236
237
238
239
240
241
242
243
244
shape::shape(type_t t, std::initializer_list<std::size_t> d)
    : shape::shape(t, std::vector<std::size_t>{d.begin(), d.end()})
{
}

shape::shape(type_t t, std::vector<shape::dynamic_dimension> dims)
    : impl(std::make_shared<shape_impl>(t, std::move(dims)))
{
}

245
246
247
248
249
250
251
252
shape::shape(type_t t,
             std::vector<std::size_t> mins,
             std::vector<std::size_t> maxes,
             std::vector<std::size_t> opts)
    : impl(std::make_shared<shape_impl>(t, std::move(mins), std::move(maxes), std::move(opts)))
{
}

Paul Fultz II's avatar
Paul Fultz II committed
253
254
shape::shape(const std::vector<shape>& subs) : impl(std::make_shared<shape_impl>(subs)) {}

255
256
shape::shape(std::shared_ptr<shape_impl> pimpl) : impl(std::move(pimpl)) {}

257
258
259
260
261
262
263
264
265
266
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
267
shape::type_t shape::type() const { return impl->m_type; }
Charlie Lin's avatar
Charlie Lin committed
268

Paul's avatar
Paul committed
269
const std::vector<std::size_t>& shape::lens() const { return impl->m_lens; }
Charlie Lin's avatar
Charlie Lin committed
270

Paul's avatar
Paul committed
271
const std::vector<std::size_t>& shape::strides() const { return impl->m_strides; }
Charlie Lin's avatar
Charlie Lin committed
272

273
274
275
276
277
278
279
280
281
std::size_t shape::ndim() const
{
    if(this->dynamic())
    {
        return dyn_dims().size();
    }
    return lens().size();
}

Paul's avatar
Paul committed
282
std::size_t shape::elements() const { return impl->elements(); }
Charlie Lin's avatar
Charlie Lin committed
283

Paul's avatar
Paul committed
284
285
std::size_t shape::bytes() const
{
Paul Fultz II's avatar
Paul Fultz II committed
286
287
288
289
290
291
292
293
294
295
296
297
298
    if(this->sub_shapes().empty())
    {
        std::size_t n = 0;
        this->visit_type([&](auto as) { n = as.size(); });
        return n * this->element_space();
    }
    else
    {
        return std::accumulate(this->sub_shapes().begin(),
                               this->sub_shapes().end(),
                               std::size_t{0},
                               [&](auto x, auto y) { return x + y.bytes(); });
    }
Paul's avatar
Paul committed
299
}
Charlie Lin's avatar
Charlie Lin committed
300

Scott Thornton's avatar
Scott Thornton committed
301
302
303
std::size_t shape::type_size() const
{
    std::size_t n = 0;
Paul Fultz II's avatar
Paul Fultz II committed
304
305
    if(this->sub_shapes().empty())
        this->visit_type([&](auto as) { n = as.size(); });
Scott Thornton's avatar
Scott Thornton committed
306
307
    return n;
}
Charlie Lin's avatar
Charlie Lin committed
308

Paul's avatar
Paul committed
309
310
std::size_t shape::index(std::initializer_list<std::size_t> l) const
{
Charlie Lin's avatar
Charlie Lin committed
311
312
313
314
    if(this->dynamic())
    {
        MIGRAPHX_THROW("SHAPE: index() called on dynamic shape");
    }
Paul's avatar
Paul committed
315
316
317
318
    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});
}
Charlie Lin's avatar
Charlie Lin committed
319

Paul's avatar
Paul committed
320
321
std::size_t shape::index(const std::vector<std::size_t>& l) const
{
Charlie Lin's avatar
Charlie Lin committed
322
323
324
325
    if(this->dynamic())
    {
        MIGRAPHX_THROW("SHAPE: index() called on dynamic shape");
    }
Paul's avatar
Paul committed
326
327
328
329
    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});
}
Charlie Lin's avatar
Charlie Lin committed
330

Paul's avatar
Paul committed
331
332
std::size_t shape::index(std::size_t i) const
{
Charlie Lin's avatar
Charlie Lin committed
333
334
335
336
    if(this->dynamic())
    {
        MIGRAPHX_THROW("SHAPE: index() called on dynamic shape");
    }
Paul's avatar
Paul committed
337
    assert(this->lens().size() == this->strides().size());
Paul's avatar
Paul committed
338
    if(this->standard())
Paul's avatar
Paul committed
339
340
        return i;
    else
Paul's avatar
Paul committed
341
    {
Paul's avatar
Paul committed
342
        std::size_t s      = 1;
Paul's avatar
Paul committed
343
        std::size_t result = 0;
Paul's avatar
Paul committed
344
        for(std::size_t j = 0; j < this->lens().size(); j++)
Paul's avatar
Paul committed
345
        {
Paul's avatar
Paul committed
346
            const std::size_t k      = this->lens().size() - j - 1;
Paul's avatar
Paul committed
347
            const std::size_t stride = this->strides()[k];
Paul's avatar
Paul committed
348
349
            const std::size_t len    = this->lens()[k];
            const std::size_t idx    = (i % (s * len)) / s;
Paul's avatar
Paul committed
350
351
352
353
354
            result += stride * idx;
            s *= len;
        }
        return result;
    }
Paul's avatar
Paul committed
355
}
356
357
358
359
360
361

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

    std::vector<std::size_t> indices(lens().size());
362
363
364
365
366
367
368
369
370
371
    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));
372
    std::transform(strides().begin(),
Shucai Xiao's avatar
Shucai Xiao committed
373
374
                   strides().end(),
                   lens().begin(),
375
                   start,
Shucai Xiao's avatar
Shucai Xiao committed
376
377
378
379
                   [&](std::size_t stride, std::size_t len) {
                       assert(len > 0 and stride > 0);
                       return (i / stride) % len;
                   });
380
381
}

Paul Fultz II's avatar
Paul Fultz II committed
382
383
bool shape::packed() const
{
Charlie Lin's avatar
Charlie Lin committed
384
385
386
387
    if(this->dynamic())
    {
        return false;
    }
388
389
    return this->sub_shapes().empty() and not impl->skips() and
           this->elements() == this->element_space();
Paul Fultz II's avatar
Paul Fultz II committed
390
}
Paul's avatar
Paul committed
391

Paul's avatar
Paul committed
392
393
bool shape::transposed() const
{
Charlie Lin's avatar
Charlie Lin committed
394
395
396
397
    if(this->dynamic())
    {
        return false;
    }
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
    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
413
}
Paul's avatar
Paul committed
414
415
416

bool shape::broadcasted() const
{
Charlie Lin's avatar
Charlie Lin committed
417
418
419
420
    if(this->dynamic())
    {
        return false;
    }
Paul's avatar
Paul committed
421
    assert(this->lens().size() == this->strides().size());
422
423
    return std::any_of(
        this->strides().begin(), this->strides().end(), [](auto x) { return x == 0; });
Paul's avatar
Paul committed
424
425
}

Khalique's avatar
Khalique committed
426
427
bool shape::scalar() const
{
Charlie Lin's avatar
Charlie Lin committed
428
429
430
431
    if(this->dynamic())
    {
        return false;
    }
Khalique's avatar
Khalique committed
432
433
    assert(this->lens().size() == this->strides().size());
    // if any stride > 0, then accumulate will return false
Paul Fultz II's avatar
Paul Fultz II committed
434
435
    return this->sub_shapes().empty() and
           std::accumulate(this->strides().begin(), this->strides().end(), std::size_t(0)) == 0;
Khalique's avatar
Khalique committed
436
437
}

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

440
441
442
443
444
445
446
447
shape shape::normalize_standard() const
{
    if(this->standard())
        return {this->type(), this->lens()};
    else
        return *this;
}

448
449
shape shape::with_lens(type_t t, const std::vector<std::size_t>& l) const
{
Charlie Lin's avatar
Charlie Lin committed
450
451
452
453
    if(this->dynamic())
    {
        MIGRAPHX_THROW("SHAPE: with_lens() called on dynamic shape");
    }
454
455
456
457
458
459
460
    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
{
Charlie Lin's avatar
Charlie Lin committed
461
462
463
464
    if(this->dynamic())
    {
        MIGRAPHX_THROW("SHAPE: with_lens() called on dynamic shape");
    }
465
466
467
    return this->with_lens(this->type(), l);
}

468
469
470
471
472
473
474
shape shape::with_type(type_t t) const
{
    auto c    = impl->copy();
    c->m_type = t;
    return {c};
}

475
476
477
478
479
480
481
482
483
484
shape shape::to_dynamic() const
{
    if(this->dynamic())
    {
        return *this;
    }
    std::vector<std::size_t> zeroes(this->ndim(), 0);
    return {type(), lens(), lens(), zeroes};
}

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

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

Charlie Lin's avatar
Charlie Lin committed
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
bool shape::dynamic() const { return not impl->m_dyn_dims.empty(); }

const std::vector<shape::dynamic_dimension>& shape::dyn_dims() const { return impl->m_dyn_dims; }

std::vector<std::size_t> shape::min_lens() const
{
    return this->dynamic() ? impl->min_lens() : this->lens();
}

std::vector<std::size_t> shape::max_lens() const
{
    return this->dynamic() ? impl->max_lens() : this->lens();
}

std::vector<std::size_t> shape::opt_lens() const
{
    return this->dynamic() ? impl->opt_lens() : this->lens();
}

bool shape::dynamic_dimension::is_fixed() const { return this->min == this->max; }

bool shape::dynamic_dimension::has_optimal() const { return opt != 0; }

Charlie Lin's avatar
Charlie Lin committed
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
shape::dynamic_dimension& shape::dynamic_dimension::operator+=(const std::size_t& x)
{
    this->min += x;
    this->max += x;
    if(this->opt != 0)
    {
        this->opt += x;
    };
    return *this;
}

shape::dynamic_dimension& shape::dynamic_dimension::operator-=(const std::size_t& x)
{
    assert(this->min >= x);
    assert(this->max >= x);
    this->min -= x;
    this->max -= x;
    if(this->opt != 0)
    {
        assert(this->opt >= x);
        this->opt -= x;
    }
    return *this;
}

Charlie Lin's avatar
Charlie Lin committed
537
538
bool operator==(const shape::dynamic_dimension& x, const shape::dynamic_dimension& y)
{
539
540
541
    // don't check opt if both are fixed
    return (x.min == y.min and x.max == y.max and
            ((x.is_fixed() and y.is_fixed()) or (x.opt == y.opt)));
Charlie Lin's avatar
Charlie Lin committed
542
543
544
545
}

bool operator!=(const shape::dynamic_dimension& x, const shape::dynamic_dimension& y)
{
546
    return not(x == y);
Charlie Lin's avatar
Charlie Lin committed
547
548
549
550
551
552
553
}
std::ostream& operator<<(std::ostream& os, const shape::dynamic_dimension& x)
{
    os << "[" << x.min << ", " << x.max << ", " << x.opt << "]";
    return os;
}

554
555
556
557
558
559
560
561
bool operator==(const shape::dynamic_dimension& x, const std::size_t& y)
{
    return x.min == y and x.max == y;
}
bool operator==(const std::size_t& x, const shape::dynamic_dimension& y) { return y == x; }
bool operator!=(const shape::dynamic_dimension& x, const std::size_t& y) { return not(x == y); }
bool operator!=(const std::size_t& x, const shape::dynamic_dimension& y) { return not(x == y); }

Charlie Lin's avatar
Charlie Lin committed
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
shape::dynamic_dimension operator+(const shape::dynamic_dimension& x, const std::size_t& y)
{
    auto dd = x;
    return dd += y;
}

shape::dynamic_dimension operator+(const std::size_t& x, const shape::dynamic_dimension& y)
{
    return y + x;
}

shape::dynamic_dimension operator-(const shape::dynamic_dimension& x, const std::size_t& y)
{
    auto dd = x;
    return dd -= y;
}

Paul's avatar
Paul committed
579
580
bool operator==(const shape& x, const shape& y)
{
Charlie Lin's avatar
Charlie Lin committed
581
582
583
584
585
586
587
588
    if(x.dynamic() and y.dynamic())
    {
        return x.impl == y.impl or (x.type() == y.type() and x.dyn_dims() == y.dyn_dims() and
                                    x.sub_shapes() == y.sub_shapes());
    }
    return x.impl == y.impl or
           (x.dynamic() == y.dynamic() and x.type() == y.type() and x.lens() == y.lens() and
            x.strides() == y.strides() and x.sub_shapes() == y.sub_shapes());
Paul's avatar
Paul committed
589
}
Charlie Lin's avatar
Charlie Lin committed
590

591
bool operator!=(const shape& x, const shape& y) { return not(x == y); }
Paul's avatar
Paul committed
592

Paul's avatar
Paul committed
593
594
std::ostream& operator<<(std::ostream& os, const shape& x)
{
Paul Fultz II's avatar
Paul Fultz II committed
595
596
    if(x.sub_shapes().empty())
    {
Charlie Lin's avatar
Charlie Lin committed
597
598
599
600
601
602
603
604
605
606
607
608
        if(x.dynamic())
        {
            os << "dynamic, ";
            os << x.type_string() << ", ";
            os << "{" << to_string_range(x.dyn_dims()) << "}";
        }
        else
        {
            os << x.type_string() << ", ";
            os << "{" << to_string_range(x.lens()) << "}, ";
            os << "{" << to_string_range(x.strides()) << "}";
        }
Paul Fultz II's avatar
Paul Fultz II committed
609
610
611
612
613
    }
    else
    {
        os << "[" << to_string_range(x.sub_shapes()) << "]";
    }
Paul's avatar
Paul committed
614
615
616
    return os;
}

617
618
shape::type_t shape::parse_type(const std::string& s)
{
619
    static const std::unordered_map<std::string, shape::type_t> m = {
620
#define MIGRAPHX_SHAPE_GENERATE_TYPE_STRING_MAP(x, t) {#x, x}, {#t, x},
Paul Fultz II's avatar
Paul Fultz II committed
621
622
623
        MIGRAPHX_SHAPE_VISIT_TYPES(MIGRAPHX_SHAPE_GENERATE_TYPE_STRING_MAP){"tuple_type",
                                                                            tuple_type},
        {"tuple", tuple_type}};
624
625
626
    return m.at(s);
}

Paul Fultz II's avatar
Paul Fultz II committed
627
628
const std::vector<shape>& shape::sub_shapes() const { return impl->m_shapes; }

629
630
631
void migraphx_to_value(value& v, const shape& s)
{
    value result;
Charlie Lin's avatar
Charlie Lin committed
632
633
634
635
636
637
    result["type"]               = migraphx::to_value(s.type_string());
    result["lens"]               = migraphx::to_value(s.lens());
    result["strides"]            = migraphx::to_value(s.strides());
    result["sub_shapes"]         = migraphx::to_value(s.sub_shapes());
    result["dynamic_dimensions"] = migraphx::to_value(s.dyn_dims());
    v                            = result;
638
}
Charlie Lin's avatar
Charlie Lin committed
639

640
641
void migraphx_from_value(const value& v, shape& s)
{
Paul Fultz II's avatar
Paul Fultz II committed
642
643
644
645
646
647
648
    auto t = v.at("type").get_string();
    if(t == "tuple_type")
    {
        s = shape{migraphx::from_value<std::vector<migraphx::shape>>(v.at("sub_shapes"))};
    }
    else
    {
Charlie Lin's avatar
Charlie Lin committed
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
        if(v.at("dynamic_dimensions").empty())
        {
            s = shape{shape::parse_type(t),
                      v.at("lens").to_vector<std::size_t>(),
                      v.at("strides").to_vector<std::size_t>()};
        }
        else
        {
            auto v_dd = v.at("dynamic_dimensions");
            std::vector<shape::dynamic_dimension> dyn_dims(v.at("dynamic_dimensions").size());
            std::transform(v_dd.begin(), v_dd.end(), dyn_dims.begin(), [](migraphx::value x) {
                auto x_min = x.at("min").template to<size_t>();
                auto x_max = x.at("max").template to<size_t>();
                auto x_opt = x.at("opt").template to<size_t>();
                return shape::dynamic_dimension{x_min, x_max, x_opt};
            });

            s = shape{shape::parse_type(t), dyn_dims};
        }
Paul Fultz II's avatar
Paul Fultz II committed
668
    }
669
670
}

Paul's avatar
Paul committed
671
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
672
} // namespace migraphx