eliminate_allocation_test.cpp 4.92 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
Paul's avatar
Paul committed
24
25
#include <migraphx/eliminate_allocation.hpp>
#include <migraphx/dead_code_elimination.hpp>
26
#include <migraphx/pass_manager.hpp>
27
28
#include <migraphx/check_shapes.hpp>
#include <migraphx/argument.hpp>
Paul's avatar
Paul committed
29
30
31
#include <basic_ops.hpp>
#include <test.hpp>

Paul Fultz II's avatar
Paul Fultz II committed
32
void run_pass(migraphx::module& m, std::size_t align = 32)
Paul's avatar
Paul committed
33
{
34
    migraphx::run_passes(
Paul Fultz II's avatar
Paul Fultz II committed
35
        m, {migraphx::eliminate_allocation{"allocate", align}, migraphx::dead_code_elimination{}});
36
}
Paul's avatar
Paul committed
37
38
39

struct allocate
{
Paul's avatar
Paul committed
40
    migraphx::shape s{};
41
42
43
44
45
46

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

Paul's avatar
Paul committed
48
    std::string name() const { return "allocate"; }
Paul's avatar
Paul committed
49
    migraphx::shape compute_shape(const std::vector<migraphx::shape>& inputs) const
Paul's avatar
Paul committed
50
    {
51
        migraphx::check_shapes{inputs, *this}.has(0);
Paul's avatar
Paul committed
52
53
        return s;
    }
Paul's avatar
Paul committed
54
    migraphx::argument compute(migraphx::context&,
Paul's avatar
Paul committed
55
56
                               const migraphx::shape& output_shape,
                               const std::vector<migraphx::argument>&) const
Paul's avatar
Paul committed
57
58
59
60
61
    {
        return {output_shape};
    }
};

Paul's avatar
Paul committed
62
TEST_CASE(basic)
Paul's avatar
Paul committed
63
{
Paul Fultz II's avatar
Paul Fultz II committed
64
    migraphx::module m;
Paul's avatar
Paul committed
65

Paul Fultz II's avatar
Paul Fultz II committed
66
67
    auto a1 = m.add_instruction(allocate{migraphx::shape{migraphx::shape::float_type, {8}}});
    auto m1 = m.add_instruction(pass_op{}, a1);
Paul's avatar
Paul committed
68

Paul Fultz II's avatar
Paul Fultz II committed
69
70
    auto a2 = m.add_instruction(allocate{migraphx::shape{migraphx::shape::float_type, {40}}});
    auto m2 = m.add_instruction(pass_op{}, a2, m1);
71

Paul Fultz II's avatar
Paul Fultz II committed
72
73
    auto a3 = m.add_instruction(allocate{migraphx::shape{migraphx::shape::float_type, {200}}});
    m.add_instruction(pass_op{}, a3, m2);
Paul's avatar
Paul committed
74

Paul Fultz II's avatar
Paul Fultz II committed
75
76
77
    run_pass(m);
    EXPECT(m.get_output_shapes().back() == migraphx::shape{migraphx::shape::float_type, {200}});
    EXPECT(m.get_parameter_shape("memory").bytes() == (8 * 4 + 40 * 4 + 200 * 4));
Paul's avatar
Paul committed
78
79
}

Paul's avatar
Paul committed
80
TEST_CASE(aligned)
Paul's avatar
Paul committed
81
{
Paul Fultz II's avatar
Paul Fultz II committed
82
    migraphx::module m;
Paul's avatar
Paul committed
83

Paul Fultz II's avatar
Paul Fultz II committed
84
85
    auto a1 = m.add_instruction(allocate{migraphx::shape{migraphx::shape::float_type, {1}}});
    auto m1 = m.add_instruction(pass_op{}, a1);
86

Paul Fultz II's avatar
Paul Fultz II committed
87
88
    auto a2 = m.add_instruction(allocate{migraphx::shape{migraphx::shape::float_type, {2}}});
    auto m2 = m.add_instruction(pass_op{}, a2, m1);
Paul's avatar
Paul committed
89

Paul Fultz II's avatar
Paul Fultz II committed
90
91
    auto a3 = m.add_instruction(allocate{migraphx::shape{migraphx::shape::float_type, {200}}});
    m.add_instruction(pass_op{}, a3, m2);
Paul's avatar
Paul committed
92

Paul Fultz II's avatar
Paul Fultz II committed
93
94
95
    run_pass(m);
    EXPECT(m.get_output_shapes().back() == migraphx::shape{migraphx::shape::float_type, {200}});
    EXPECT(m.get_parameter_shape("memory").bytes() == (32 + 32 + 200 * 4));
Paul's avatar
Paul committed
96
97
}

Paul's avatar
Paul committed
98
TEST_CASE(unaligned)
Paul's avatar
Paul committed
99
{
Paul Fultz II's avatar
Paul Fultz II committed
100
    migraphx::module m;
Paul's avatar
Paul committed
101

Paul Fultz II's avatar
Paul Fultz II committed
102
103
    auto a1 = m.add_instruction(allocate{migraphx::shape{migraphx::shape::float_type, {1}}});
    auto m1 = m.add_instruction(pass_op{}, a1);
Paul's avatar
Paul committed
104

Paul Fultz II's avatar
Paul Fultz II committed
105
106
    auto a2 = m.add_instruction(allocate{migraphx::shape{migraphx::shape::float_type, {2}}});
    auto m2 = m.add_instruction(pass_op{}, a2, m1);
107

Paul Fultz II's avatar
Paul Fultz II committed
108
109
    auto a3 = m.add_instruction(allocate{migraphx::shape{migraphx::shape::float_type, {200}}});
    m.add_instruction(pass_op{}, a3, m2);
Paul's avatar
Paul committed
110

Paul Fultz II's avatar
Paul Fultz II committed
111
112
113
    run_pass(m, 1);
    EXPECT(m.get_output_shapes().back() == migraphx::shape{migraphx::shape::float_type, {200}});
    EXPECT(m.get_parameter_shape("memory").bytes() == (1 * 4 + 2 * 4 + 200 * 4));
Paul's avatar
Paul committed
114
115
}

Paul's avatar
Paul committed
116
TEST_CASE(float_aligned)
Paul's avatar
Paul committed
117
{
Paul Fultz II's avatar
Paul Fultz II committed
118
    migraphx::module m;
Paul's avatar
Paul committed
119

Paul Fultz II's avatar
Paul Fultz II committed
120
121
    auto a1 = m.add_instruction(allocate{migraphx::shape{migraphx::shape::float_type, {1}}});
    auto m1 = m.add_instruction(pass_op{}, a1);
122

Paul Fultz II's avatar
Paul Fultz II committed
123
124
    auto a2 = m.add_instruction(allocate{migraphx::shape{migraphx::shape::float_type, {2}}});
    auto m2 = m.add_instruction(pass_op{}, a2, m1);
Paul's avatar
Paul committed
125

Paul Fultz II's avatar
Paul Fultz II committed
126
127
    auto a3 = m.add_instruction(allocate{migraphx::shape{migraphx::shape::float_type, {200}}});
    m.add_instruction(pass_op{}, a3, m2);
Paul's avatar
Paul committed
128

Paul Fultz II's avatar
Paul Fultz II committed
129
130
131
    run_pass(m, 4);
    EXPECT(m.get_output_shapes().back() == migraphx::shape{migraphx::shape::float_type, {200}});
    EXPECT(m.get_parameter_shape("memory").bytes() == (1 * 4 + 2 * 4 + 200 * 4));
Paul's avatar
Paul committed
132
133
}

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