eliminate_allocation_test.cpp 4.02 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{};
23
24
25
26
27
28

    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
        return migraphx::pack(f(self.s, "shape"));
    }
Paul's avatar
Paul committed
29

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

Paul's avatar
Paul committed
44
TEST_CASE(basic)
Paul's avatar
Paul committed
45
{
Paul's avatar
Paul committed
46
47
    migraphx::program p;
    auto a1 = p.add_instruction(allocate{migraphx::shape{migraphx::shape::float_type, {8}}});
Paul's avatar
Paul committed
48
49
    auto p1 = p.add_instruction(pass_op{}, a1);

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

Paul's avatar
Paul committed
53
    auto a3 = p.add_instruction(allocate{migraphx::shape{migraphx::shape::float_type, {200}}});
Paul's avatar
Paul committed
54
55
56
    p.add_instruction(pass_op{}, a3, p2);

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

Paul's avatar
Paul committed
61
TEST_CASE(aligned)
Paul's avatar
Paul committed
62
{
Paul's avatar
Paul committed
63
64
    migraphx::program p;
    auto a1 = p.add_instruction(allocate{migraphx::shape{migraphx::shape::float_type, {1}}});
Paul's avatar
Paul committed
65
66
    auto p1 = p.add_instruction(pass_op{}, a1);

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

Paul's avatar
Paul committed
70
    auto a3 = p.add_instruction(allocate{migraphx::shape{migraphx::shape::float_type, {200}}});
Paul's avatar
Paul committed
71
72
73
    p.add_instruction(pass_op{}, a3, p2);

    p.compile(eliminate_allocation_target{});
Paul's avatar
Paul committed
74
    EXPECT(p.get_shape() == migraphx::shape{migraphx::shape::float_type, {200}});
Paul's avatar
Paul committed
75
    EXPECT(p.get_parameter_shape("memory").bytes() == (32 + 32 + 200 * 4));
Paul's avatar
Paul committed
76
77
}

Paul's avatar
Paul committed
78
TEST_CASE(unaligned)
Paul's avatar
Paul committed
79
{
Paul's avatar
Paul committed
80
81
    migraphx::program p;
    auto a1 = p.add_instruction(allocate{migraphx::shape{migraphx::shape::float_type, {1}}});
Paul's avatar
Paul committed
82
83
    auto p1 = p.add_instruction(pass_op{}, a1);

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

Paul's avatar
Paul committed
87
    auto a3 = p.add_instruction(allocate{migraphx::shape{migraphx::shape::float_type, {200}}});
Paul's avatar
Paul committed
88
89
90
    p.add_instruction(pass_op{}, a3, p2);

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

Paul's avatar
Paul committed
95
TEST_CASE(float_aligned)
Paul's avatar
Paul committed
96
{
Paul's avatar
Paul committed
97
98
    migraphx::program p;
    auto a1 = p.add_instruction(allocate{migraphx::shape{migraphx::shape::float_type, {1}}});
Paul's avatar
Paul committed
99
100
    auto p1 = p.add_instruction(pass_op{}, a1);

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

Paul's avatar
Paul committed
104
    auto a3 = p.add_instruction(allocate{migraphx::shape{migraphx::shape::float_type, {200}}});
Paul's avatar
Paul committed
105
106
107
    p.add_instruction(pass_op{}, a3, p2);

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

Paul's avatar
Paul committed
112
113
int main(int argc, const char* argv[])
{
Paul's avatar
Paul committed
114
    setenv("MIGRAPHX_DISABLE_MEMORY_COLORING", "1", 1);
Paul's avatar
Paul committed
115
116
    test::run(argc, argv);
}