"vscode:/vscode.git/clone" did not exist on "c822c48dd38d89c7b9bc7bcbdf9da8543f515b84"
eliminate_contiguous_test.cpp 9.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_contiguous.hpp>
#include <migraphx/dead_code_elimination.hpp>
26
#include <migraphx/pass_manager.hpp>
27
#include <migraphx/instruction.hpp>
28
#include <basic_ops.hpp>
29
30
#include <migraphx/make_op.hpp>

31
#include <pointwise.hpp>
32
33
#include <test.hpp>

Paul Fultz II's avatar
Paul Fultz II committed
34
void run_pass(migraphx::module& m)
35
{
36
37
    migraphx::run_passes(
        m, {migraphx::eliminate_contiguous{"contiguous"}, migraphx::dead_code_elimination{}});
38
}
39

Paul's avatar
Paul committed
40
TEST_CASE(standard_op)
41
{
Paul Fultz II's avatar
Paul Fultz II committed
42
43
44
    migraphx::module m;

    auto l = m.add_parameter("x", {migraphx::shape::float_type, {2, 2}});
45
    auto t = m.add_instruction(migraphx::make_op("transpose", {{"permutation", {1, 0}}}), l);
Paul Fultz II's avatar
Paul Fultz II committed
46
47
48
49
50
    auto c = m.add_instruction(migraphx::make_op("contiguous"), t);
    m.add_instruction(pass_standard_op{}, c);
    auto count = std::distance(m.begin(), m.end());
    run_pass(m);
    EXPECT(std::distance(m.begin(), m.end()) == count);
51
52
}

Paul's avatar
Paul committed
53
TEST_CASE(standard_op_const)
54
{
Paul Fultz II's avatar
Paul Fultz II committed
55
56
57
    migraphx::module m;

    auto l = m.add_literal(get_2x2());
58
    auto t = m.add_instruction(migraphx::make_op("transpose", {{"permutation", {1, 0}}}), l);
Paul Fultz II's avatar
Paul Fultz II committed
59
60
61
62
    auto c = m.add_instruction(migraphx::make_op("contiguous"), t);
    m.add_instruction(pass_standard_op{}, c);
    run_pass(m);
    EXPECT(std::distance(m.begin(), m.end()) == 2);
Paul's avatar
Paul committed
63
64
65
66
}

TEST_CASE(non_standard_op)
{
Paul Fultz II's avatar
Paul Fultz II committed
67
68
69
    migraphx::module m;

    auto l = m.add_parameter("x", {migraphx::shape::float_type, {2, 2}});
70
    auto t = m.add_instruction(migraphx::make_op("transpose", {{"permutation", {1, 0}}}), l);
Paul Fultz II's avatar
Paul Fultz II committed
71
72
73
74
75
    auto c = m.add_instruction(migraphx::make_op("contiguous"), t);
    m.add_instruction(pass_op{}, c);
    auto count = std::distance(m.begin(), m.end());
    run_pass(m);
    EXPECT(std::distance(m.begin(), m.end()) == count);
76
77
}

Paul's avatar
Paul committed
78
79
TEST_CASE(non_standard_op_const)
{
Paul Fultz II's avatar
Paul Fultz II committed
80
81
82
    migraphx::module m;

    auto l = m.add_literal(get_2x2());
83
    auto t = m.add_instruction(migraphx::make_op("transpose", {{"permutation", {1, 0}}}), l);
Paul Fultz II's avatar
Paul Fultz II committed
84
85
86
87
    auto c = m.add_instruction(migraphx::make_op("contiguous"), t);
    m.add_instruction(pass_op{}, c);
    run_pass(m);
    EXPECT(std::distance(m.begin(), m.end()) == 2);
Paul's avatar
Paul committed
88
89
}

Paul Fultz II's avatar
Paul Fultz II committed
90
TEST_CASE(transpose_gem)
91
{
Paul Fultz II's avatar
Paul Fultz II committed
92
93
94
    migraphx::module m;

    auto l  = m.add_literal(get_2x2());
95
    auto t  = m.add_instruction(migraphx::make_op("transpose", {{"permutation", {1, 0}}}), l);
Paul Fultz II's avatar
Paul Fultz II committed
96
97
98
99
100
101
    auto c  = m.add_instruction(migraphx::make_op("contiguous"), t);
    auto ic = m.add_instruction(migraphx::make_op("identity"), c);
    m.add_instruction(migraphx::make_op("dot"), ic, l);
    auto count = std::distance(m.begin(), m.end());
    run_pass(m);
    EXPECT(std::distance(m.begin(), m.end()) == (count - 1));
102
103
}

104
105
TEST_CASE(transpose_standard_op)
{
Paul Fultz II's avatar
Paul Fultz II committed
106
107
108
    migraphx::module m;

    auto l  = m.add_parameter("x", {migraphx::shape::float_type, {2, 2}});
109
    auto t  = m.add_instruction(migraphx::make_op("transpose", {{"permutation", {1, 0}}}), l);
Paul Fultz II's avatar
Paul Fultz II committed
110
111
112
113
114
115
    auto c  = m.add_instruction(migraphx::make_op("contiguous"), t);
    auto sn = m.add_instruction(migraphx::make_op("sin"), c);
    m.add_instruction(pass_standard_op{}, sn);
    auto count = std::distance(m.begin(), m.end());
    run_pass(m);
    EXPECT(std::distance(m.begin(), m.end()) == count);
116
117
}

Paul's avatar
Paul committed
118
119
TEST_CASE(transpose_standard_op_const)
{
Paul Fultz II's avatar
Paul Fultz II committed
120
121
122
    migraphx::module m;

    auto l  = m.add_literal(get_2x2());
123
    auto t  = m.add_instruction(migraphx::make_op("transpose", {{"permutation", {1, 0}}}), l);
Paul Fultz II's avatar
Paul Fultz II committed
124
125
126
127
128
    auto c  = m.add_instruction(migraphx::make_op("contiguous"), t);
    auto sn = m.add_instruction(migraphx::make_op("sin"), c);
    m.add_instruction(pass_standard_op{}, sn);
    run_pass(m);
    EXPECT(std::distance(m.begin(), m.end()) == 3);
Paul's avatar
Paul committed
129
130
}

131
132
TEST_CASE(no_packed_unary_op)
{
Paul Fultz II's avatar
Paul Fultz II committed
133
    migraphx::module m;
134

Paul Fultz II's avatar
Paul Fultz II committed
135
136
    auto l = m.add_literal(get_2x2());
    auto t = m.add_instruction(
137
        migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), l);
Paul Fultz II's avatar
Paul Fultz II committed
138
139
140
141
142
143
    auto c  = m.add_instruction(migraphx::make_op("contiguous"), t);
    auto sn = m.add_instruction(migraphx::make_op("sin"), c);
    m.add_instruction(pass_standard_op{}, sn);
    auto count = std::distance(m.begin(), m.end());
    run_pass(m);
    EXPECT(std::distance(m.begin(), m.end()) == count - 1);
144
145
}

146
147
TEST_CASE(non_standard_return_input)
{
Paul Fultz II's avatar
Paul Fultz II committed
148
149
150
    migraphx::module m;

    auto l  = m.add_literal(get_2x2());
151
    auto tl = m.add_instruction(migraphx::make_op("transpose", {{"permutation", {1, 0}}}), l);
Paul Fultz II's avatar
Paul Fultz II committed
152
153
154
155
156
    auto c  = m.add_instruction(migraphx::make_op("contiguous"), tl);
    m.add_return({c});
    auto count = std::distance(m.begin(), m.end());
    run_pass(m);
    EXPECT(std::distance(m.begin(), m.end()) == count);
157
158
}

Khalique's avatar
Khalique committed
159
160
161
162
163
TEST_CASE(non_standard_flatten_op)
{
    migraphx::module m;

    auto l = m.add_parameter("x", {migraphx::shape::float_type, {2, 6, 6, 6}});
Khalique's avatar
Khalique committed
164
165
    auto t = m.add_instruction(
        migraphx::make_op("slice", {{"axes", {2, 3}}, {"starts", {1, 1}}, {"ends", {6, 6}}}), l);
Khalique's avatar
Khalique committed
166
167
168
169
170
171
172
    auto c = m.add_instruction(migraphx::make_op("contiguous"), t);
    m.add_instruction(migraphx::make_op("flatten"), c);
    auto count = std::distance(m.begin(), m.end());
    run_pass(m);
    EXPECT(std::distance(m.begin(), m.end()) == count);
}

Khalique's avatar
Khalique committed
173
174
175
176
177
178
179
180
181
182
183
184
185
186
TEST_CASE(standard_flatten_op)
{
    migraphx::module m;

    auto l = m.add_parameter("x", {migraphx::shape::float_type, {2, 6, 6, 6}});
    auto t = m.add_instruction(
        migraphx::make_op("slice", {{"axes", {0, 1}}, {"starts", {1, 1}}, {"ends", {6, 6}}}), l);
    auto c = m.add_instruction(migraphx::make_op("contiguous"), t);
    m.add_instruction(migraphx::make_op("flatten"), c);
    auto count = std::distance(m.begin(), m.end());
    run_pass(m);
    EXPECT(std::distance(m.begin(), m.end()) == (count - 1));
}

187
188
189
190
191
192
193
194
195
196
197
198
TEST_CASE(contiguous_pointwise)
{
    migraphx::shape s{migraphx::shape::float_type, {2, 3, 8, 8}};
    migraphx::program p;
    auto* mm = p.get_main_module();
    {
        auto x  = mm->add_parameter("x", s);
        auto y  = mm->add_parameter("y", migraphx::shape{migraphx::shape::float_type, {3}});
        auto yb = mm->add_instruction(
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {2, 3, 8, 8}}}), y);
        auto yc  = mm->add_instruction(migraphx::make_op("contiguous"), yb);
        auto add = add_pointwise(p, "main:pointwise0", {x, yc}, single_pointwise("add"));
199
200
        auto cadd = mm->add_instruction(migraphx::make_op("contiguous"), add);
        mm->add_instruction(pass_op{}, cadd);
201
202
203
    }
    auto count = std::distance(mm->begin(), mm->end());
    run_pass(*mm);
204
    EXPECT(std::distance(mm->begin(), mm->end()) == (count - 2));
205
206
207
208
    EXPECT(std::none_of(
        mm->begin(), mm->end(), [](auto&& ins) { return ins.name() == "contiguous"; }));
}

209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
TEST_CASE(contiguous_nhwc_pointwise)
{
    auto s =
        migraphx::shape::from_permutation(migraphx::shape::float_type, {2, 3, 8, 8}, {0, 2, 3, 1});
    migraphx::program p1;
    {
        auto* mm = p1.get_main_module();
        auto x   = mm->add_parameter("x", s);
        auto y   = mm->add_parameter("y", migraphx::shape{migraphx::shape::float_type, {3}});
        auto yb  = mm->add_instruction(
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {2, 3, 8, 8}}}), y);
        auto yc   = mm->add_instruction(migraphx::make_op("contiguous"), yb);
        auto add  = add_pointwise(p1, "main:pointwise0", {x, yc}, single_pointwise("add"));
        auto cadd = mm->add_instruction(migraphx::make_op("contiguous"), add);
        mm->add_instruction(pass_op{}, cadd);
    }
    run_pass(*p1.get_main_module());
    migraphx::program p2;
    {
        auto* mm = p2.get_main_module();
        auto x   = mm->add_parameter("x", s);
        auto y   = mm->add_parameter("y", migraphx::shape{migraphx::shape::float_type, {3}});
        auto yb  = mm->add_instruction(
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {2, 3, 8, 8}}}), y);
        auto add  = add_pointwise(p2, "main:pointwise0", {x, yb}, single_pointwise("add"));
        auto cadd = mm->add_instruction(migraphx::make_op("contiguous"), add);
        mm->add_instruction(pass_op{}, cadd);
    }
    EXPECT(p1 == p2);
}

240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
TEST_CASE(slice_contiguous)
{
    migraphx::module m;

    migraphx::shape s{migraphx::shape::float_type, {4, 2}};
    auto x  = m.add_parameter("x", s);
    auto t  = m.add_instruction(migraphx::make_op("transpose", {{"permutation", {1, 0}}}), x);
    auto c  = m.add_instruction(migraphx::make_op("contiguous"), t);
    auto s1 = m.add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {1}}}), c);
    auto s2 = m.add_instruction(
        migraphx::make_op("slice", {{"axes", {0}}, {"starts", {1}}, {"ends", {2}}}), c);
    auto c1 = m.add_instruction(migraphx::make_op("contiguous"), s1);
    auto c2 = m.add_instruction(migraphx::make_op("contiguous"), s2);
    m.add_instruction(pass_standard_op{}, c1, c2);
    run_pass(m);
    EXPECT(std::count_if(
               m.begin(), m.end(), [](auto&& ins) { return ins.name() == "contiguous"; }) == 1);
}

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