eliminate_allocation_test.cpp 3.88 KB
Newer Older
Paul's avatar
Paul committed
1
2
#include <migraphx/eliminate_allocation.hpp>
#include <migraphx/dead_code_elimination.hpp>
3
4
#include <migraphx/check_shapes.hpp>
#include <migraphx/argument.hpp>
Paul's avatar
Paul committed
5
6
7
8
9
10
11
#include <basic_ops.hpp>
#include <test.hpp>

struct eliminate_allocation_target
{
    std::size_t align = 32;
    std::string name() const { return "eliminate_allocation"; }
Paul's avatar
Paul committed
12
    std::vector<migraphx::pass> get_passes(migraphx::context&) const
Paul's avatar
Paul committed
13
    {
Paul's avatar
Paul committed
14
15
        return {migraphx::eliminate_allocation{"allocate", align},
                migraphx::dead_code_elimination{}};
Paul's avatar
Paul committed
16
    }
Paul's avatar
Paul committed
17
    migraphx::context get_context() const { return {}; }
Paul's avatar
Paul committed
18
19
20
21
};

struct allocate
{
Paul's avatar
Paul committed
22
    migraphx::shape s{};
Paul's avatar
Paul committed
23
    std::string name() const { return "allocate"; }
Paul's avatar
Paul committed
24
    migraphx::shape compute_shape(const std::vector<migraphx::shape>& inputs) const
Paul's avatar
Paul committed
25
    {
Paul's avatar
Paul committed
26
        migraphx::check_shapes{inputs}.has(0);
Paul's avatar
Paul committed
27
28
        return s;
    }
Paul's avatar
Paul committed
29
    migraphx::argument compute(migraphx::context&,
Paul's avatar
Paul committed
30
31
                               const migraphx::shape& output_shape,
                               const std::vector<migraphx::argument>&) const
Paul's avatar
Paul committed
32
33
34
35
36
    {
        return {output_shape};
    }
};

Paul's avatar
Paul committed
37
TEST_CASE(basic)
Paul's avatar
Paul committed
38
{
Paul's avatar
Paul committed
39
40
    migraphx::program p;
    auto a1 = p.add_instruction(allocate{migraphx::shape{migraphx::shape::float_type, {8}}});
Paul's avatar
Paul committed
41
42
    auto p1 = p.add_instruction(pass_op{}, a1);

Paul's avatar
Paul committed
43
    auto a2 = p.add_instruction(allocate{migraphx::shape{migraphx::shape::float_type, {40}}});
Paul's avatar
Paul committed
44
45
    auto p2 = p.add_instruction(pass_op{}, a2, p1);

Paul's avatar
Paul committed
46
    auto a3 = p.add_instruction(allocate{migraphx::shape{migraphx::shape::float_type, {200}}});
Paul's avatar
Paul committed
47
48
49
    p.add_instruction(pass_op{}, a3, p2);

    p.compile(eliminate_allocation_target{});
Paul's avatar
Paul committed
50
    EXPECT(p.get_shape() == migraphx::shape{migraphx::shape::float_type, {200}});
Paul's avatar
Paul committed
51
    EXPECT(p.get_parameter_shape("memory").bytes() == (8 * 4 + 40 * 4 + 200 * 4));
Paul's avatar
Paul committed
52
53
}

Paul's avatar
Paul committed
54
TEST_CASE(aligned)
Paul's avatar
Paul committed
55
{
Paul's avatar
Paul committed
56
57
    migraphx::program p;
    auto a1 = p.add_instruction(allocate{migraphx::shape{migraphx::shape::float_type, {1}}});
Paul's avatar
Paul committed
58
59
    auto p1 = p.add_instruction(pass_op{}, a1);

Paul's avatar
Paul committed
60
    auto a2 = p.add_instruction(allocate{migraphx::shape{migraphx::shape::float_type, {2}}});
Paul's avatar
Paul committed
61
62
    auto p2 = p.add_instruction(pass_op{}, a2, p1);

Paul's avatar
Paul committed
63
    auto a3 = p.add_instruction(allocate{migraphx::shape{migraphx::shape::float_type, {200}}});
Paul's avatar
Paul committed
64
65
66
    p.add_instruction(pass_op{}, a3, p2);

    p.compile(eliminate_allocation_target{});
Paul's avatar
Paul committed
67
    EXPECT(p.get_shape() == migraphx::shape{migraphx::shape::float_type, {200}});
Paul's avatar
Paul committed
68
    EXPECT(p.get_parameter_shape("memory").bytes() == (32 + 32 + 200 * 4));
Paul's avatar
Paul committed
69
70
}

Paul's avatar
Paul committed
71
TEST_CASE(unaligned)
Paul's avatar
Paul committed
72
{
Paul's avatar
Paul committed
73
74
    migraphx::program p;
    auto a1 = p.add_instruction(allocate{migraphx::shape{migraphx::shape::float_type, {1}}});
Paul's avatar
Paul committed
75
76
    auto p1 = p.add_instruction(pass_op{}, a1);

Paul's avatar
Paul committed
77
    auto a2 = p.add_instruction(allocate{migraphx::shape{migraphx::shape::float_type, {2}}});
Paul's avatar
Paul committed
78
79
    auto p2 = p.add_instruction(pass_op{}, a2, p1);

Paul's avatar
Paul committed
80
    auto a3 = p.add_instruction(allocate{migraphx::shape{migraphx::shape::float_type, {200}}});
Paul's avatar
Paul committed
81
82
83
    p.add_instruction(pass_op{}, a3, p2);

    p.compile(eliminate_allocation_target{1});
Paul's avatar
Paul committed
84
    EXPECT(p.get_shape() == migraphx::shape{migraphx::shape::float_type, {200}});
Paul's avatar
Paul committed
85
    EXPECT(p.get_parameter_shape("memory").bytes() == (1 * 4 + 2 * 4 + 200 * 4));
Paul's avatar
Paul committed
86
87
}

Paul's avatar
Paul committed
88
TEST_CASE(float_aligned)
Paul's avatar
Paul committed
89
{
Paul's avatar
Paul committed
90
91
    migraphx::program p;
    auto a1 = p.add_instruction(allocate{migraphx::shape{migraphx::shape::float_type, {1}}});
Paul's avatar
Paul committed
92
93
    auto p1 = p.add_instruction(pass_op{}, a1);

Paul's avatar
Paul committed
94
    auto a2 = p.add_instruction(allocate{migraphx::shape{migraphx::shape::float_type, {2}}});
Paul's avatar
Paul committed
95
96
    auto p2 = p.add_instruction(pass_op{}, a2, p1);

Paul's avatar
Paul committed
97
    auto a3 = p.add_instruction(allocate{migraphx::shape{migraphx::shape::float_type, {200}}});
Paul's avatar
Paul committed
98
99
100
    p.add_instruction(pass_op{}, a3, p2);

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

Paul's avatar
Paul committed
105
106
int main(int argc, const char* argv[])
{
Paul's avatar
Paul committed
107
    setenv("MIGRAPHX_DISABLE_MEMORY_COLORING", "1", 1);
Paul's avatar
Paul committed
108
109
    test::run(argc, argv);
}