shape_test.cpp 4.28 KB
Newer Older
Paul's avatar
Paul committed
1

Paul's avatar
Paul committed
2
#include <migraph/shape.hpp>
Paul's avatar
Paul committed
3
4
5
#include <array>
#include <algorithm>
#include <numeric>
Paul's avatar
Paul committed
6
7
8
9
#include "test.hpp"

void test_shape_assign()
{
Paul's avatar
Paul committed
10
11
    migraph::shape s1{migraph::shape::float_type, {100, 32, 8, 8}};
    migraph::shape s2 = s1; // NOLINT
Paul's avatar
Paul committed
12
13
14
15
    EXPECT(s1 == s2);
    EXPECT(!(s1 != s2));
}

Paul's avatar
Paul committed
16
17
18
void test_shape_packed_default()
{
    migraph::shape s{migraph::shape::float_type, {2, 2}};
Paul's avatar
Paul committed
19
    EXPECT(s.standard());
Paul's avatar
Paul committed
20
    EXPECT(s.packed());
Paul's avatar
Paul committed
21
22
    EXPECT(not s.transposed());
    EXPECT(not s.broadcasted());
Paul's avatar
Paul committed
23
24
25
26
27
}

void test_shape_packed()
{
    migraph::shape s{migraph::shape::float_type, {2, 2}, {2, 1}};
Paul's avatar
Paul committed
28
    EXPECT(s.standard());
Paul's avatar
Paul committed
29
    EXPECT(s.packed());
Paul's avatar
Paul committed
30
31
    EXPECT(not s.transposed());
    EXPECT(not s.broadcasted());
Paul's avatar
Paul committed
32
33
34
35
36
}

void test_shape_transposed()
{
    migraph::shape s{migraph::shape::float_type, {2, 2}, {1, 2}};
Paul's avatar
Paul committed
37
38
39
40
41
42
43
44
45
46
    EXPECT(not s.standard());
    EXPECT(s.packed());
    EXPECT(s.transposed());
    EXPECT(not s.broadcasted());
}

void test_shape_broadcasted()
{
    migraph::shape s{migraph::shape::float_type, {2, 2}, {1, 0}};
    EXPECT(not s.standard());
Paul's avatar
Paul committed
47
    EXPECT(not s.packed());
Paul's avatar
Paul committed
48
49
    EXPECT(not s.transposed());
    EXPECT(s.broadcasted());
Paul's avatar
Paul committed
50
51
}

Paul's avatar
Paul committed
52
53
void test_shape_default()
{
Paul's avatar
Paul committed
54
55
    migraph::shape s1{};
    migraph::shape s2{};
Paul's avatar
Paul committed
56
57
58
59
    EXPECT(s1 == s2);
    EXPECT(!(s1 != s2));
}

Paul's avatar
Paul committed
60
61
void test_shape4()
{
Paul's avatar
Paul committed
62
    migraph::shape s{migraph::shape::float_type, {100, 32, 8, 8}};
Paul's avatar
Paul committed
63
    EXPECT(s.standard());
Paul's avatar
Paul committed
64
    EXPECT(s.packed());
Paul's avatar
Paul committed
65
66
    EXPECT(not s.transposed());
    EXPECT(not s.broadcasted());
Paul's avatar
Paul committed
67
    EXPECT(s.type() == migraph::shape::float_type);
Paul's avatar
Paul committed
68
69
70
71
72
73
74
75
    EXPECT(s.lens()[0] == 100);
    EXPECT(s.lens()[1] == 32);
    EXPECT(s.lens()[2] == 8);
    EXPECT(s.lens()[3] == 8);
    EXPECT(s.strides()[0] == s.lens()[1] * s.strides()[1]);
    EXPECT(s.strides()[1] == s.lens()[2] * s.strides()[2]);
    EXPECT(s.strides()[2] == s.lens()[3] * s.strides()[3]);
    EXPECT(s.strides()[3] == 1);
Paul's avatar
Paul committed
76
77
    EXPECT(s.elements() == 100 * 32 * 8 * 8);
    EXPECT(s.bytes() == 100 * 32 * 8 * 8 * sizeof(float));
Paul's avatar
Paul committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    EXPECT(s.index({0, 0, 0, 0}) == 0);
    EXPECT(s.index({0, 0, 0, 1}) == 1);
    EXPECT(s.index({0, 0, 0, 0}) == s.index(0));
    EXPECT(s.index({0, 0, 0, 1}) == s.index(1));
    EXPECT(s.index({0, 0, 1, 0}) == s.index(8));
    EXPECT(s.index({0, 1, 0, 0}) == s.index(8 * 8));
    EXPECT(s.index({1, 0, 0, 0}) == s.index(8 * 8 * 32));
    EXPECT(s.index(0) == 0);
    EXPECT(s.index(1) == 1);
    EXPECT(s.index(8) == 8);
    EXPECT(s.index(8 * 8) == 8 * 8);
    EXPECT(s.index(8 * 8 * 32) == 8 * 8 * 32);
    EXPECT(s.index(s.elements() - 1) == s.elements() - 1);
}

void test_shape4_nonpacked()
{
    std::vector<std::size_t> lens       = {100, 32, 8, 8};
Paul's avatar
Paul committed
96
97
    std::array<std::size_t, 4> offsets  = {{5, 10, 0, 6}};
    std::array<std::size_t, 4> adj_lens = {{0, 0, 0, 0}};
Paul's avatar
Paul committed
98
99
100
101
102
103

    std::transform(
        lens.begin(), lens.end(), offsets.begin(), adj_lens.begin(), std::plus<size_t>());
    // adj_lens should be: { 105, 42, 8, 14 }
    std::vector<std::size_t> strides(4);
    strides.back() = 1;
Paul's avatar
Paul committed
104
105
106
107
    std::partial_sum(adj_lens.rbegin(),
                     adj_lens.rend() - 1,
                     strides.rbegin() + 1,
                     std::multiplies<std::size_t>());
Paul's avatar
Paul committed
108

Paul's avatar
Paul committed
109
    migraph::shape s{migraph::shape::float_type, lens, strides};
Paul's avatar
Paul committed
110
111
112
113
    EXPECT(not s.standard());
    EXPECT(not s.packed());
    EXPECT(not s.transposed());
    EXPECT(not s.broadcasted());
Paul's avatar
Paul committed
114
    EXPECT(s.type() == migraph::shape::float_type);
Paul's avatar
Paul committed
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
    EXPECT(s.lens()[0] == 100);
    EXPECT(s.lens()[1] == 32);
    EXPECT(s.lens()[2] == 8);
    EXPECT(s.lens()[3] == 8);
    EXPECT(s.strides()[0] == 4704);
    EXPECT(s.strides()[1] == 112);
    EXPECT(s.strides()[2] == 14);
    EXPECT(s.strides()[3] == 1);
    EXPECT(s.elements() == 100 * 32 * 8 * 8);
    EXPECT(s.bytes() == sizeof(float) * 469274);

    EXPECT(s.index(0) == 0);
    EXPECT(s.index(1) == 1);
    EXPECT(s.index({0, 0, 0, 0}) == 0);
    EXPECT(s.index({0, 0, 0, 1}) == s.index(1));
    // TODO: Fix these tests
    // EXPECT(s.index({0, 0, 1, 0}) == s.index(8));
    // EXPECT(s.index({0, 1, 0, 0}) == s.index(8 * 8));
    // EXPECT(s.index({1, 0, 0, 0}) == s.index(8 * 8 * 32));
    // EXPECT(s.index(s.elements() - 1) == 469273);
Paul's avatar
Paul committed
135
136
}

Paul's avatar
Paul committed
137
138
int main()
{
Paul's avatar
Paul committed
139
    test_shape_assign();
Paul's avatar
Paul committed
140
141
142
    test_shape_packed_default();
    test_shape_packed();
    test_shape_transposed();
Paul's avatar
Paul committed
143
    test_shape_broadcasted();
Paul's avatar
Paul committed
144
    test_shape_default();
Paul's avatar
Paul committed
145
    test_shape4();
Paul's avatar
Paul committed
146
    test_shape4_nonpacked();
Paul's avatar
Paul committed
147
}