tensor_view.hpp 3.87 KB
Newer Older
Paul's avatar
Paul committed
1
2
#ifndef MIGRAPH_GUARD_TENSOR_VIEW_HPP
#define MIGRAPH_GUARD_TENSOR_VIEW_HPP
Paul's avatar
Paul committed
3

Paul's avatar
Paul committed
4
5
6
#include <migraph/shape.hpp>
#include <migraph/float_equal.hpp>
#include <migraph/requires.hpp>
Paul's avatar
Paul committed
7
8
9

#include <iostream>

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

Paul's avatar
Paul committed
12
template <class T>
Paul's avatar
Paul committed
13
14
struct tensor_view
{
15
    using value_type = T;
Paul's avatar
Paul committed
16
17
    tensor_view() : m_data(nullptr) {}
    tensor_view(shape s, T* d) : m_data(d), m_shape(s) {}
Paul's avatar
Paul committed
18

Paul's avatar
Paul committed
19
    const shape& get_shape() const { return this->m_shape; }
Paul's avatar
Paul committed
20

Paul's avatar
Paul committed
21
    bool empty() const { return m_data == nullptr || m_shape.lens().empty(); }
Paul's avatar
Paul committed
22

Paul's avatar
Paul committed
23
    std::size_t size() const { return m_shape.elements(); }
Paul's avatar
Paul committed
24

Paul's avatar
Paul committed
25
    T* data() { return this->m_data; }
Paul's avatar
Paul committed
26

Paul's avatar
Paul committed
27
    const T* data() const { return this->m_data; }
Paul's avatar
Paul committed
28

Paul's avatar
Paul committed
29
    template <class... Ts, MIGRAPH_REQUIRES(std::is_integral<Ts>{}...)>
Paul's avatar
Paul committed
30
31
    const T& operator()(Ts... xs) const
    {
Scott Thornton's avatar
Scott Thornton committed
32
        assert(m_shape.index({static_cast<std::size_t>(xs)...}) < m_shape.bytes() / sizeof(T));
Paul's avatar
Paul committed
33
        return m_data[m_shape.index({static_cast<std::size_t>(xs)...})];
Paul's avatar
Paul committed
34
35
    }

Paul's avatar
Paul committed
36
    template <class... Ts, MIGRAPH_REQUIRES(std::is_integral<Ts>{}...)>
Paul's avatar
Paul committed
37
38
    T& operator()(Ts... xs)
    {
Scott Thornton's avatar
Scott Thornton committed
39
        assert(m_shape.index({static_cast<std::size_t>(xs)...}) < m_shape.bytes() / sizeof(T));
Paul's avatar
Paul committed
40
        return m_data[m_shape.index({static_cast<std::size_t>(xs)...})];
Paul's avatar
Paul committed
41
42
    }

Paul's avatar
Paul committed
43
    template <class Iterator, MIGRAPH_REQUIRES(not std::is_integral<Iterator>{})>
Paul's avatar
Paul committed
44
45
46
47
48
    const T& operator()(Iterator start, Iterator last) const
    {
        return m_data[m_shape.index(start, last)];
    }

Paul's avatar
Paul committed
49
    template <class Iterator, MIGRAPH_REQUIRES(not std::is_integral<Iterator>{})>
Paul's avatar
Paul committed
50
51
52
53
54
    T& operator()(Iterator start, Iterator last)
    {
        return m_data[m_shape.index(start, last)];
    }

Paul's avatar
Paul committed
55
56
57
    T& operator[](std::size_t i)
    {
        assert(!this->empty() && i < this->size());
Paul's avatar
Paul committed
58
        return m_data[m_shape.index(i)];
Paul's avatar
Paul committed
59
60
61
62
63
    }

    const T& operator[](std::size_t i) const
    {
        assert(!this->empty() && i < this->size());
Paul's avatar
Paul committed
64
        return m_data[m_shape.index(i)];
Paul's avatar
Paul committed
65
66
67
68
69
    }

    T& front()
    {
        assert(!this->empty());
Paul's avatar
Paul committed
70
        return m_data[0];
Paul's avatar
Paul committed
71
72
73
74
75
    }

    const T& front() const
    {
        assert(!this->empty());
Paul's avatar
Paul committed
76
        return m_data[0];
Paul's avatar
Paul committed
77
78
79
80
81
    }

    T& back()
    {
        assert(!this->empty());
Paul's avatar
Paul committed
82
        return m_data[m_shape.index(this->size() - 1)];
Paul's avatar
Paul committed
83
84
85
86
87
    }

    const T& back() const
    {
        assert(!this->empty());
Paul's avatar
Paul committed
88
        return m_data[m_shape.index(this->size() - 1)];
Paul's avatar
Paul committed
89
90
91
92
93
    }

    // TODO: Add iterators so it can handle nonpacked tensors
    T* begin()
    {
Paul's avatar
Paul committed
94
95
        assert(this->m_shape.packed());
        return m_data;
Paul's avatar
Paul committed
96
97
98
99
    }

    T* end()
    {
Paul's avatar
Paul committed
100
        assert(this->m_shape.packed());
Paul's avatar
Paul committed
101
        if(this->empty())
Paul's avatar
Paul committed
102
            return m_data;
Paul's avatar
Paul committed
103
        else
Paul's avatar
Paul committed
104
            return m_data + this->size();
Paul's avatar
Paul committed
105
106
107
108
    }

    const T* begin() const
    {
Paul's avatar
Paul committed
109
110
        assert(this->m_shape.packed());
        return m_data;
Paul's avatar
Paul committed
111
112
113
114
    }

    const T* end() const
    {
Paul's avatar
Paul committed
115
        assert(this->m_shape.packed());
Paul's avatar
Paul committed
116
        if(this->empty())
Paul's avatar
Paul committed
117
            return m_data;
Paul's avatar
Paul committed
118
        else
Paul's avatar
Paul committed
119
            return m_data + this->size();
Paul's avatar
Paul committed
120
121
    }

Paul's avatar
Paul committed
122
123
124
125
126
    friend std::ostream& operator<<(std::ostream& os, const tensor_view<T>& x)
    {
        if(!x.empty())
        {
            os << x.front();
Paul's avatar
Paul committed
127
            for(std::size_t i = 1; i < x.m_shape.elements(); i++)
Paul's avatar
Paul committed
128
            {
Paul's avatar
Paul committed
129
                os << ", " << x.m_data[x.m_shape.index(i)];
Paul's avatar
Paul committed
130
131
132
133
134
            }
        }
        return os;
    }

Paul's avatar
Paul committed
135
    private:
Paul's avatar
Paul committed
136
137
    T* m_data;
    shape m_shape;
Paul's avatar
Paul committed
138
139
};

Paul's avatar
Paul committed
140
template <class T, class U>
141
142
143
144
145
146
147
148
149
150
151
152
153
154
bool operator==(const tensor_view<T>& x, const tensor_view<U>& y)
{
    if(x.get_shape() == y.get_shape())
    {
        for(std::size_t i = 0; i < x.get_shape().elements(); i++)
        {
            if(!float_equal(x[i], y[i]))
                return false;
        }
        return true;
    }
    return false;
}

Paul's avatar
Paul committed
155
156
157
158
159
template <class T, class U>
bool operator!=(const tensor_view<T>& x, const tensor_view<U>& y)
{
    return !(x == y);
}
160

Paul's avatar
Paul committed
161
template <class T>
Paul's avatar
Paul committed
162
163
164
165
166
tensor_view<T> make_view(shape s, T* data)
{
    return {s, data};
}

Paul's avatar
Paul committed
167
} // namespace migraph
Paul's avatar
Paul committed
168
169

#endif