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

Paul's avatar
Paul committed
2
3
#include <migraph/shape.hpp>
#include <migraph/stringutils.hpp>
Paul's avatar
Paul committed
4
5
6
#include <numeric>
#include <algorithm>
#include <functional>
Paul's avatar
Paul committed
7
#include <iostream>
Paul's avatar
Paul committed
8

Paul's avatar
Paul committed
9
namespace migraph {
Paul's avatar
Paul committed
10

Paul's avatar
Paul committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
struct shape_impl
{

  static std::shared_ptr<shape_impl> default_shape()
  {
    static std::shared_ptr<shape_impl> result = std::make_shared<shape_impl>();
    return result;
  }

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

  shape_impl(shape::type_t t) : m_type(t), m_lens({1}), m_strides({1}), m_standard(true) {}
  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());
      assert(std::any_of(m_strides.begin(), m_strides.end(), [](auto x) { return x > 0; }) and
             "At least one stride must be non-zero");
      m_standard = this->elements() == this->element_space() and std::is_sorted(m_strides.rbegin(), m_strides.rend());
  }
  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
76

Paul's avatar
Paul committed
77
78
79
shape::shape() : impl(shape_impl::default_shape()) {}

shape::shape(type_t t) : impl(std::make_shared<shape_impl>(t)) {}
Paul's avatar
Paul committed
80
shape::shape(type_t t, std::vector<std::size_t> l)
Paul's avatar
Paul committed
81
    : impl(std::make_shared<shape_impl>(t, std::move(l)))
Paul's avatar
Paul committed
82
83
84
{
}
shape::shape(type_t t, std::vector<std::size_t> l, std::vector<std::size_t> s)
Paul's avatar
Paul committed
85
    : impl(std::make_shared<shape_impl>(t, std::move(l), std::move(s)))
Paul's avatar
Paul committed
86
87
88
{
}

Paul's avatar
Paul committed
89
90
91
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
92
93
std::size_t shape::elements() const
{
Paul's avatar
Paul committed
94
    return impl->elements();
Paul's avatar
Paul committed
95
96
97
98
99
100
101
}
std::size_t shape::bytes() const
{
    std::size_t n = 0;
    this->visit_type([&](auto as) { n = as.size(); });
    return n * this->element_space();
}
Paul's avatar
Paul committed
102
103
104
105
106
107
108
109
110
111
112
113
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
114
115
116
std::size_t shape::index(std::size_t i) const
{
    assert(this->lens().size() == this->strides().size());
Paul's avatar
Paul committed
117
    if(this->standard())
Paul's avatar
Paul committed
118
119
        return i;
    else
Paul's avatar
Paul committed
120
121
122
123
124
125
126
127
128
        return std::inner_product(this->lens().begin(),
                                  this->lens().end(),
                                  this->strides().begin(),
                                  std::size_t{0},
                                  std::plus<std::size_t>{},
                                  [&](std::size_t len, std::size_t stride) {
                                      assert(stride > 0 and len > 0);
                                      return ((i / stride) % len) * stride;
                                  });
Paul's avatar
Paul committed
129
}
Paul's avatar
Paul committed
130
131
bool shape::packed() const { return this->elements() == this->element_space(); }

Paul's avatar
Paul committed
132
133
134
135
bool shape::transposed() const
{
    return not std::is_sorted(this->strides().rbegin(), this->strides().rend());
}
Paul's avatar
Paul committed
136
137
138
139

bool shape::broadcasted() const
{
    assert(this->lens().size() == this->strides().size());
Paul's avatar
Paul committed
140
141
142
143
    return std::accumulate(this->strides().begin(),
                           this->strides().end(),
                           std::size_t{1},
                           std::multiplies<std::size_t>()) == 0;
Paul's avatar
Paul committed
144
145
}

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

Paul's avatar
Paul committed
148
149
std::size_t shape::element_space() const
{
Paul's avatar
Paul committed
150
    return impl->element_space();
Paul's avatar
Paul committed
151
152
}

Paul's avatar
Paul committed
153
154
std::string shape::type_string() const
{
Paul's avatar
Paul committed
155
    switch(this->type())
Paul's avatar
Paul committed
156
    {
Paul's avatar
Paul committed
157
#define MIGRAPH_SHAPE_TYPE_STRING_CASE(x, t) \
Paul's avatar
Paul committed
158
    case x: return #x;
Paul's avatar
Paul committed
159
160
        MIGRAPH_SHAPE_VISIT_TYPES(MIGRAPH_SHAPE_TYPE_STRING_CASE)
#undef MIGRAPH_SHAPE_TYPE_STRING_CASE
Paul's avatar
Paul committed
161
    }
Paul's avatar
Paul committed
162
    MIGRAPH_THROW("Invalid type");
Paul's avatar
Paul committed
163
164
}

Paul's avatar
Paul committed
165
166
167
168
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
169
bool operator!=(const shape& x, const shape& y) { return !(x == y); }
Paul's avatar
Paul committed
170

Paul's avatar
Paul committed
171
172
173
std::ostream& operator<<(std::ostream& os, const shape& x)
{
    os << x.type_string() << ", ";
Paul's avatar
Paul committed
174
175
    os << "{" << to_string_range(x.lens()) << "}, ";
    os << "{" << to_string_range(x.strides()) << "}";
Paul's avatar
Paul committed
176
177
178
    return os;
}

Paul's avatar
Paul committed
179
} // namespace migraph