eliminate_allocation_test.cpp 3.83 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
#include <migraphx/eliminate_allocation.hpp>
#include <migraphx/dead_code_elimination.hpp>
#include <migraphx/operators.hpp>
Paul's avatar
Paul committed
4
5
6
7
8
9
10
#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
11
    std::vector<migraphx::pass> get_passes(migraphx::context&) const
Paul's avatar
Paul committed
12
    {
Paul's avatar
Paul committed
13
        return {migraphx::eliminate_allocation{"allocate", align}, migraphx::dead_code_elimination{}};
Paul's avatar
Paul committed
14
    }
Paul's avatar
Paul committed
15
    migraphx::context get_context() const { return {}; }
Paul's avatar
Paul committed
16
17
18
19
};

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

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

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

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

    p.compile(eliminate_allocation_target{});
Paul's avatar
Paul committed
48
    EXPECT(p.get_shape() == migraphx::shape{migraphx::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
}

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

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

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

    p.compile(eliminate_allocation_target{});
Paul's avatar
Paul committed
65
    EXPECT(p.get_shape() == migraphx::shape{migraphx::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
}

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

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

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

    p.compile(eliminate_allocation_target{1});
Paul's avatar
Paul committed
82
    EXPECT(p.get_shape() == migraphx::shape{migraphx::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
}

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

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

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

    p.compile(eliminate_allocation_target{4});
Paul's avatar
Paul committed
99
    EXPECT(p.get_shape() == migraphx::shape{migraphx::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
}

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