eliminate_allocation_test.cpp 3.72 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
#include <migraph/eliminate_allocation.hpp>
#include <migraph/dead_code_elimination.hpp>
#include <migraph/operators.hpp>
#include <basic_ops.hpp>
#include <test.hpp>

struct eliminate_allocation_target
{
    std::size_t align = 32;
    std::string name() const { return "eliminate_allocation"; }
    std::vector<migraph::pass> get_passes(migraph::context&) const
    {
        return {migraph::eliminate_allocation{"allocate", align}, migraph::dead_code_elimination{}};
    }
    migraph::context get_context() const { return {}; }
};

struct allocate
{
    migraph::shape s{};
    std::string name() const { return "allocate"; }
    migraph::shape compute_shape(const std::vector<migraph::shape>& inputs) const
    {
        migraph::check_shapes{inputs}.has(0);
        return s;
    }
Paul's avatar
Paul committed
27
28
29
    migraph::argument compute(migraph::context&,
                              const migraph::shape& output_shape,
                              const std::vector<migraph::argument>&) const
Paul's avatar
Paul committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
    {
        return {output_shape};
    }
};

void basic()
{
    migraph::program p;
    auto a1 = p.add_instruction(allocate{migraph::shape{migraph::shape::float_type, {8}}});
    auto p1 = p.add_instruction(pass_op{}, a1);

    auto a2 = p.add_instruction(allocate{migraph::shape{migraph::shape::float_type, {40}}});
    auto p2 = p.add_instruction(pass_op{}, a2, p1);

    auto a3 = p.add_instruction(allocate{migraph::shape{migraph::shape::float_type, {200}}});
    p.add_instruction(pass_op{}, a3, p2);

    p.compile(eliminate_allocation_target{});
    EXPECT(p.get_shape() == migraph::shape{migraph::shape::float_type, {200}});
Paul's avatar
Paul committed
49
    EXPECT(p.get_parameter_shape("memory").bytes() == (8 * 4 + 40 * 4 + 200 * 4));
Paul's avatar
Paul committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
}

void aligned()
{
    migraph::program p;
    auto a1 = p.add_instruction(allocate{migraph::shape{migraph::shape::float_type, {1}}});
    auto p1 = p.add_instruction(pass_op{}, a1);

    auto a2 = p.add_instruction(allocate{migraph::shape{migraph::shape::float_type, {2}}});
    auto p2 = p.add_instruction(pass_op{}, a2, p1);

    auto a3 = p.add_instruction(allocate{migraph::shape{migraph::shape::float_type, {200}}});
    p.add_instruction(pass_op{}, a3, p2);

    p.compile(eliminate_allocation_target{});
    EXPECT(p.get_shape() == migraph::shape{migraph::shape::float_type, {200}});
Paul's avatar
Paul committed
66
    EXPECT(p.get_parameter_shape("memory").bytes() == (32 + 32 + 200 * 4));
Paul's avatar
Paul committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
}

void unaligned()
{
    migraph::program p;
    auto a1 = p.add_instruction(allocate{migraph::shape{migraph::shape::float_type, {1}}});
    auto p1 = p.add_instruction(pass_op{}, a1);

    auto a2 = p.add_instruction(allocate{migraph::shape{migraph::shape::float_type, {2}}});
    auto p2 = p.add_instruction(pass_op{}, a2, p1);

    auto a3 = p.add_instruction(allocate{migraph::shape{migraph::shape::float_type, {200}}});
    p.add_instruction(pass_op{}, a3, p2);

    p.compile(eliminate_allocation_target{1});
    EXPECT(p.get_shape() == migraph::shape{migraph::shape::float_type, {200}});
Paul's avatar
Paul committed
83
    EXPECT(p.get_parameter_shape("memory").bytes() == (1 * 4 + 2 * 4 + 200 * 4));
Paul's avatar
Paul committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
}

void float_aligned()
{
    migraph::program p;
    auto a1 = p.add_instruction(allocate{migraph::shape{migraph::shape::float_type, {1}}});
    auto p1 = p.add_instruction(pass_op{}, a1);

    auto a2 = p.add_instruction(allocate{migraph::shape{migraph::shape::float_type, {2}}});
    auto p2 = p.add_instruction(pass_op{}, a2, p1);

    auto a3 = p.add_instruction(allocate{migraph::shape{migraph::shape::float_type, {200}}});
    p.add_instruction(pass_op{}, a3, p2);

    p.compile(eliminate_allocation_target{4});
    EXPECT(p.get_shape() == migraph::shape{migraph::shape::float_type, {200}});
Paul's avatar
Paul committed
100
    EXPECT(p.get_parameter_shape("memory").bytes() == (1 * 4 + 2 * 4 + 200 * 4));
Paul's avatar
Paul committed
101
102
103
104
105
106
107
108
109
}

int main()
{
    basic();
    aligned();
    unaligned();
    float_aligned();
}