eliminate_allocation_test.cpp 3.85 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
14
        return {migraphx::eliminate_allocation{"allocate", align},
                migraphx::dead_code_elimination{}};
Paul's avatar
Paul committed
15
    }
Paul's avatar
Paul committed
16
    migraphx::context get_context() const { return {}; }
Paul's avatar
Paul committed
17
18
19
20
};

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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