literal.hpp 3.78 KB
Newer Older
Paul's avatar
Paul committed
1
2
#ifndef MIGRAPH_GUARD_MIGRAPHLIB_LITERAL_HPP
#define MIGRAPH_GUARD_MIGRAPHLIB_LITERAL_HPP
Paul's avatar
Paul committed
3

Paul's avatar
Paul committed
4
#include <migraph/shape.hpp>
Paul's avatar
Paul committed
5
#include <migraph/shape_for_each.hpp>
Paul's avatar
Paul committed
6
7
8
#include <migraph/argument.hpp>
#include <migraph/tensor_view.hpp>
#include <migraph/raw_data.hpp>
9
#include <migraph/make_shared_array.hpp>
10
#include <migraph/config.hpp>
Paul's avatar
Paul committed
11

Paul's avatar
Paul committed
12
13
#include <memory>

14
namespace migraph { inline namespace MIGRAPH_INLINE_NS {
Paul's avatar
Paul committed
15

Paul's avatar
Paul committed
16
17
18
19
/**
 * @brief Represents a raw literal
 * @details This stores the literal has a raw buffer that is owned by this class
 */
Paul's avatar
Paul committed
20
struct literal : raw_data<literal>
Paul's avatar
Paul committed
21
{
Paul's avatar
Paul committed
22
    literal() {}
Paul's avatar
Paul committed
23

Paul's avatar
Paul committed
24
    template <class U, class T = deduce<U>>
Paul's avatar
Paul committed
25
    literal(U x) : buffer(make_shared_array<char>(sizeof(T))), m_shape(shape::get_type<T>{})
Paul's avatar
Paul committed
26
    {
Paul's avatar
Paul committed
27
        static_assert(std::is_trivially_copyable<T>{}, "Literals can only be trivial types");
Paul's avatar
Paul committed
28
        *(reinterpret_cast<T*>(buffer.get())) = x;
Paul's avatar
Paul committed
29
30
    }

Paul's avatar
Paul committed
31
    template <class T>
Paul's avatar
Paul committed
32
    literal(const shape& s, const std::vector<T>& x)
33
        : buffer(make_shared_array<char>(s.bytes())), m_shape(s)
Paul's avatar
Paul committed
34
    {
Paul's avatar
Paul committed
35
        static_assert(std::is_trivially_copyable<T>{}, "Literals can only be trivial types");
Paul's avatar
Paul committed
36
        fill(x.begin(), x.end());
Paul's avatar
Paul committed
37
38
    }

Paul's avatar
Paul committed
39
    template <class T>
Paul's avatar
Paul committed
40
    literal(const shape& s, const std::initializer_list<T>& x)
41
        : buffer(make_shared_array<char>(s.bytes())), m_shape(s)
Paul's avatar
Paul committed
42
    {
Paul's avatar
Paul committed
43
        static_assert(std::is_trivially_copyable<T>{}, "Literals can only be trivial types");
Paul's avatar
Paul committed
44
        fill(x.begin(), x.end());
Paul's avatar
Paul committed
45
46
    }

Paul's avatar
Paul committed
47
    template <class Iterator>
Paul's avatar
Paul committed
48
    literal(const shape& s, Iterator start, Iterator end)
49
        : buffer(make_shared_array<char>(s.bytes())), m_shape(s)
Paul's avatar
Paul committed
50
    {
Paul's avatar
Paul committed
51
        fill(start, end);
Paul's avatar
Paul committed
52
53
    }

54
    literal(const shape& s, const char* x) : buffer(make_shared_array<char>(s.bytes())), m_shape(s)
Paul's avatar
Paul committed
55
56
57
    {
        std::copy(x, x + s.bytes(), buffer.get());
    }
Paul's avatar
Paul committed
58

Paul's avatar
Paul committed
59
    /// Whether data is available
Paul's avatar
Paul committed
60
    bool empty() const { return this->buffer == nullptr; }
Paul's avatar
Paul committed
61

Paul's avatar
Paul committed
62
    /// Provides a raw pointer to the data
Paul's avatar
Paul committed
63
    const char* data() const { return this->buffer.get(); }
Paul's avatar
Paul committed
64

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

Paul's avatar
Paul committed
67
    /// Convert the data to an argument
Paul's avatar
Paul committed
68
69
    argument get_argument() const
    {
Paul's avatar
Paul committed
70
        std::vector<char> b(buffer.get(), buffer.get() + m_shape.bytes());
Paul's avatar
Paul committed
71
        return {m_shape, [b]() mutable { return b.data(); }};
Paul's avatar
Paul committed
72
73
    }

Paul's avatar
Paul committed
74
    private:
Paul's avatar
Paul committed
75
    std::shared_ptr<char> buffer;
Paul's avatar
Paul committed
76
    shape m_shape;
Paul's avatar
Paul committed
77
78
79
80

    template <class Iterator>
    void fill(Iterator start, Iterator end)
    {
Paul's avatar
Paul committed
81
        if(m_shape.standard())
Paul's avatar
Paul committed
82
        {
Paul's avatar
Paul committed
83
            m_shape.visit_type([&](auto as) { std::copy(start, end, as.from(buffer.get())); });
Paul's avatar
Paul committed
84
85
86
87
88
        }
        else
        {
            auto it = start;
            m_shape.visit_type([&](auto as) {
Paul's avatar
Paul committed
89
                auto output = make_view(m_shape, as.from(buffer.get()));
Paul's avatar
Paul committed
90
91
                shape_for_each(output.get_shape(), [&](const auto& idx) {
                    output(idx.begin(), idx.end()) = *it;
Paul's avatar
Paul committed
92
                    it++;
Paul's avatar
Paul committed
93
94
95
96
                });
            });
        }
    }
Paul's avatar
Paul committed
97
98
};

Paul's avatar
Paul committed
99
template <class F>
Paul's avatar
Paul committed
100
101
102
103
104
literal transform(literal l, F f)
{
    literal result;
    l.visit([&](auto x) {
        using type = std::remove_cv_t<typename decltype(x)::value_type>;
Paul's avatar
Paul committed
105
        std::vector<type> output(x.size(), type(0));
Paul's avatar
Paul committed
106
107
108
109
110
111
        std::transform(x.begin(), x.end(), output.begin(), f);
        result = literal{l.get_shape(), output};
    });
    return result;
}

Paul's avatar
Paul committed
112
113
114
115
116
117
118
template <class F>
literal transform(literal l1, literal l2, F f)
{
    assert(l1.get_shape() == l2.get_shape());
    literal result;
    visit_all(l1, l2)([&](auto x, auto y) {
        using type = std::remove_cv_t<typename decltype(x)::value_type>;
Paul's avatar
Paul committed
119
        std::vector<type> output(x.size(), type(0));
Paul's avatar
Paul committed
120
121
122
123
124
125
        std::transform(x.begin(), x.end(), y.begin(), output.begin(), f);
        result = literal{l1.get_shape(), output};
    });
    return result;
}

126
} // inline namespace MIGRAPH_INLINE_NS
Paul's avatar
Paul committed
127
} // namespace migraph
Paul's avatar
Paul committed
128
129

#endif