"sgl-router/src/vscode:/vscode.git/clone" did not exist on "f2d5c4920e3bbb9ac4883e03718aad64f426b6b6"
propagate_constant_test.cpp 5.97 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
#include <migraphx/propagate_constant.hpp>
Paul's avatar
Paul committed
25
#include <migraphx/dead_code_elimination.hpp>
26
#include <migraphx/pass_manager.hpp>
Paul's avatar
Paul committed
27
#include <basic_ops.hpp>
28
29
#include <migraphx/make_op.hpp>

Paul's avatar
Paul committed
30
31
#include <test.hpp>

Paul Fultz II's avatar
Paul Fultz II committed
32
void run_pass(migraphx::module& m)
Paul's avatar
Paul committed
33
{
Paul Fultz II's avatar
Paul Fultz II committed
34
    migraphx::run_passes(m, {migraphx::propagate_constant{}, migraphx::dead_code_elimination{}});
35
}
Paul's avatar
Paul committed
36

Paul's avatar
Paul committed
37
TEST_CASE(const_add)
Paul's avatar
Paul committed
38
{
Paul Fultz II's avatar
Paul Fultz II committed
39
40
41
42
43
44
    migraphx::module m1;
    auto one = m1.add_literal(1);
    auto two = m1.add_literal(2);
    auto sum = m1.add_instruction(migraphx::make_op("add"), one, two);
    m1.add_instruction(pass_op{}, sum);
    run_pass(m1);
Paul's avatar
Paul committed
45

Paul Fultz II's avatar
Paul Fultz II committed
46
47
48
49
    migraphx::module m2;
    auto total = m2.add_literal(3);
    m2.add_instruction(pass_op{}, total);
    EXPECT(m1 == m2);
Paul's avatar
Paul committed
50
51
}

Paul's avatar
Paul committed
52
TEST_CASE(const_add_parameter)
Paul's avatar
Paul committed
53
{
Paul Fultz II's avatar
Paul Fultz II committed
54
55
56
57
58
59
    migraphx::module m1;
    auto one = m1.add_parameter("one", {migraphx::shape::int32_type, {1}});
    auto two = m1.add_literal(2);
    auto sum = m1.add_instruction(migraphx::make_op("add"), one, two);
    m1.add_instruction(pass_op{}, sum);
    run_pass(m1);
Paul's avatar
Paul committed
60

Paul Fultz II's avatar
Paul Fultz II committed
61
62
63
64
    migraphx::module m2;
    auto total = m2.add_literal(3);
    m2.add_instruction(pass_op{}, total);
    EXPECT(m1 != m2);
Paul's avatar
Paul committed
65
66
}

Paul's avatar
Paul committed
67
TEST_CASE(const_multiadd)
Paul's avatar
Paul committed
68
{
Paul Fultz II's avatar
Paul Fultz II committed
69
70
71
72
73
74
75
    migraphx::module m1;
    auto one  = m1.add_literal(1);
    auto two  = m1.add_literal(2);
    auto sum1 = m1.add_instruction(migraphx::make_op("add"), one, two);
    auto sum2 = m1.add_instruction(migraphx::make_op("add"), sum1, two);
    m1.add_instruction(pass_op{}, sum2);
    run_pass(m1);
Paul's avatar
Paul committed
76

Paul Fultz II's avatar
Paul Fultz II committed
77
78
79
80
    migraphx::module m2;
    auto total = m2.add_literal(5);
    m2.add_instruction(pass_op{}, total);
    EXPECT(m1 == m2);
Paul's avatar
Paul committed
81
82
}

Paul's avatar
Paul committed
83
TEST_CASE(const_add_mul)
Paul's avatar
Paul committed
84
{
Paul Fultz II's avatar
Paul Fultz II committed
85
86
87
88
89
90
91
92
    migraphx::module m1;
    auto one  = m1.add_literal(1);
    auto two  = m1.add_literal(2);
    auto mul  = m1.add_instruction(migraphx::make_op("mul"), two, two);
    auto sum1 = m1.add_instruction(migraphx::make_op("add"), one, mul);
    auto sum2 = m1.add_instruction(migraphx::make_op("add"), sum1, two);
    m1.add_instruction(pass_op{}, sum2);
    run_pass(m1);
Paul's avatar
Paul committed
93

Paul Fultz II's avatar
Paul Fultz II committed
94
95
96
97
    migraphx::module m2;
    auto total = m2.add_literal(7);
    m2.add_instruction(pass_op{}, total);
    EXPECT(m1 == m2);
Paul's avatar
Paul committed
98
99
}

Paul's avatar
Paul committed
100
101
TEST_CASE(const_add_scalar)
{
Paul Fultz II's avatar
Paul Fultz II committed
102
103
104
105
106
107
108
109
    migraphx::module m1;
    auto one = m1.add_instruction(migraphx::make_op("scalar", {{"scalar_bcst_dims", {2, 2}}}),
                                  m1.add_literal(1));
    auto two = m1.add_instruction(migraphx::make_op("scalar", {{"scalar_bcst_dims", {2, 2}}}),
                                  m1.add_literal(2));
    auto sum = m1.add_instruction(migraphx::make_op("add"), one, two);
    m1.add_instruction(pass_op{}, sum);
    run_pass(m1);
Paul's avatar
Paul committed
110

Paul Fultz II's avatar
Paul Fultz II committed
111
    migraphx::module m2;
Paul's avatar
Paul committed
112
    auto total =
Paul Fultz II's avatar
Paul Fultz II committed
113
114
115
        m2.add_literal(migraphx::literal{{migraphx::shape::int32_type, {2, 2}}, {3, 3, 3, 3}});
    m2.add_instruction(pass_op{}, total);
    EXPECT(m1 == m2);
Paul's avatar
Paul committed
116
117
118
119
}

TEST_CASE(const_scalar)
{
Paul Fultz II's avatar
Paul Fultz II committed
120
    migraphx::module m1;
Paul's avatar
Paul committed
121
    {
Paul Fultz II's avatar
Paul Fultz II committed
122
123
124
        auto one = m1.add_instruction(migraphx::make_op("scalar", {{"scalar_bcst_dims", {2, 2}}}),
                                      m1.add_literal(1));
        m1.add_instruction(pass_op{}, one);
Paul's avatar
Paul committed
125
    }
Paul Fultz II's avatar
Paul Fultz II committed
126
    run_pass(m1);
Paul's avatar
Paul committed
127

Paul Fultz II's avatar
Paul Fultz II committed
128
    migraphx::module m2;
Paul's avatar
Paul committed
129
    {
Paul Fultz II's avatar
Paul Fultz II committed
130
131
132
        auto one = m2.add_instruction(migraphx::make_op("scalar", {{"scalar_bcst_dims", {2, 2}}}),
                                      m2.add_literal(1));
        m2.add_instruction(pass_op{}, one);
Paul's avatar
Paul committed
133
    }
Paul Fultz II's avatar
Paul Fultz II committed
134
    EXPECT(m1 == m2);
Paul's avatar
Paul committed
135
136
}

137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
TEST_CASE(const_dot)
{
    migraphx::module m1;
    {
        migraphx::shape s{migraphx::shape::float_type, {2, 2}};
        std::vector<float> vec = {1.0f, 2.0f, 1.0f, 2.0f};

        auto l  = m1.add_literal(migraphx::literal(s, vec));
        auto dl = m1.add_instruction(migraphx::make_op("dot"), l, l);
        auto x  = m1.add_parameter("x", s);
        auto r  = m1.add_instruction(migraphx::make_op("add"), dl, x);
        m1.add_return({r});
    }

    run_pass(m1);

    migraphx::module m2;
    {
        migraphx::shape s{migraphx::shape::float_type, {2, 2}};
        std::vector<float> vec = {3.0f, 6.0f, 3.0f, 6.0f};

        auto x = m2.add_parameter("x", s);
        auto l = m2.add_literal(migraphx::literal(s, vec));
        auto r = m2.add_instruction(migraphx::make_op("add"), l, x);
        m2.add_return({r});
    }
    EXPECT(m1 == m2);
}

166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
TEST_CASE(last_const)
{
    const std::vector<float> vec = {1.0f, 2.0f, 1.0f, 2.0f};
    migraphx::module m1;
    {
        migraphx::shape s{migraphx::shape::float_type, {2, 2}};
        auto l = m1.add_literal(migraphx::literal(s, vec));
        m1.add_instruction(
            migraphx::make_op("convert", {{"target_type", migraphx::shape::half_type}}), l);
    }

    run_pass(m1);

    migraphx::module m2;
    {
        migraphx::shape s{migraphx::shape::half_type, {2, 2}};
        auto l = m2.add_literal(migraphx::literal(s, vec));
        m2.add_instruction(migraphx::make_op("identity"), l);
    }
    EXPECT(m1 == m2);
}

Paul's avatar
Paul committed
188
int main(int argc, const char* argv[]) { test::run(argc, argv); }