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

9
void run_pass(migraphx::program& p, std::size_t align = 32)
Paul's avatar
Paul committed
10
{
11
12
13
    migraphx::run_passes(
        p, {migraphx::eliminate_allocation{"allocate", align}, migraphx::dead_code_elimination{}});
}
Paul's avatar
Paul committed
14
15
16

struct allocate
{
Paul's avatar
Paul committed
17
    migraphx::shape s{};
18
19
20
21
22
23

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

107
int main(int argc, const char* argv[]) { test::run(argc, argv); }