literal.hpp 3.17 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>
Paul's avatar
Paul committed
10

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

Paul's avatar
Paul committed
13
namespace migraph {
Paul's avatar
Paul committed
14

Paul's avatar
Paul committed
15
16
17
18
/**
 * @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
19
struct literal : raw_data<literal>
Paul's avatar
Paul committed
20
{
Paul's avatar
Paul committed
21
    literal() {}
Paul's avatar
Paul committed
22

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

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

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

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

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

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

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

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

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

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

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

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

Paul's avatar
Paul committed
111
} // namespace migraph
Paul's avatar
Paul committed
112
113

#endif