"...datasets/SuperGLUE_BoolQ/SuperGLUE_BoolQ_ppl_314797.py" did not exist on "7d346000bb8f1f7611f88dc8e003bdf8c9ae3ece"
raw_data.hpp 1.83 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
6
7
8
9
10
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

#ifndef RTG_GUARD_RAW_DATA_HPP
#define RTG_GUARD_RAW_DATA_HPP

namespace rtg {

template<class Derived>
struct raw_data
{
    friend bool operator==(const Derived& x, const Derived& y)
    {
        auto&& xshape = x.get_shape();
        auto&& yshape = y.get_shape();
        bool result = x.empty() && y.empty();
        if(not result && xshape == yshape)
        {
            auto&& xbuffer = x.data();
            auto&& ybuffer = y.data();
            // TODO: Dont use tensor view for single values
            xshape.visit_type([&](auto as) {
                auto xview = make_view(xshape, as.from(xbuffer));
                auto yview = make_view(yshape, as.from(ybuffer));
                result = xview == yview;
            });
        }
        return result;
    }

    friend bool operator!=(const Derived& x, const Derived& y)
    {
        return !(x == y);
    }

    template<class Visitor>
    void visit_at(Visitor v, std::size_t n=0) const
    {
        auto && s = static_cast<const Derived&>(*this).get_shape();
        auto && buffer = static_cast<const Derived&>(*this).data();
        s.visit_type([&](auto as) {
            v(*(as.from(buffer)+s.index(n)));
        });
    }

    template<class Visitor>
    void visit(Visitor v) const
    {
        auto && s = static_cast<const Derived&>(*this).get_shape();
        auto && buffer = static_cast<const Derived&>(*this).data();
        s.visit_type([&](auto as) {
            v(make_view(this->s, as.from(buffer)));
        });
    }

    bool single() const
    {
        auto && s = static_cast<const Derived&>(*this).get_shape();
        return this->s.elements() == 1;
    }

    template<class T>
    T at(std::size_t n=0) const
    {
        T result;
        this->visit_at([&](auto x) {
            result = x;
        });
        return result;
    }
};

} // namespace rtg

#endif