"src/targets/vscode:/vscode.git/clone" did not exist on "ee29e1162f1a87683f4164ef193ffbbc1dd3a243"
simplify_algebra_test.cpp 102 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/simplify_algebra.hpp>
#include <migraphx/dead_code_elimination.hpp>
26
#include <migraphx/pass_manager.hpp>
Paul's avatar
Paul committed
27
#include <migraphx/operators.hpp>
Paul's avatar
Paul committed
28
29
30
#include <migraphx/generate.hpp>
#include <migraphx/ranges.hpp>
#include <migraphx/instruction.hpp>
Paul's avatar
Paul committed
31
#include <basic_ops.hpp>
32
#include <migraphx/make_op.hpp>
Paul's avatar
Paul committed
33
34
#include <test.hpp>

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

Paul's avatar
Paul committed
40
TEST_CASE(simplify_add1)
Paul's avatar
Paul committed
41
{
Paul Fultz II's avatar
Paul Fultz II committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
    migraphx::module m1;
    {
        auto x    = m1.add_parameter("x", {migraphx::shape::int32_type, {1}});
        auto y    = m1.add_parameter("y", {migraphx::shape::int32_type, {1}});
        auto one  = m1.add_literal(1);
        auto two  = m1.add_literal(2);
        auto sum1 = m1.add_instruction(migraphx::make_op("add"), x, one);
        auto sum2 = m1.add_instruction(migraphx::make_op("add"), y, two);
        auto sum3 = m1.add_instruction(migraphx::make_op("add"), sum1, sum2);
        m1.add_instruction(pass_op{}, sum3);
    }
    run_pass(m1);

    migraphx::module m2;
    {
        auto x    = m2.add_parameter("x", {migraphx::shape::int32_type, {1}});
        auto y    = m2.add_parameter("y", {migraphx::shape::int32_type, {1}});
        auto one  = m2.add_literal(1);
        auto two  = m2.add_literal(2);
        auto sum1 = m2.add_instruction(migraphx::make_op("add"), one, two);
        auto sum2 = m2.add_instruction(migraphx::make_op("add"), x, y);
        auto sum3 = m2.add_instruction(migraphx::make_op("add"), sum2, sum1);
        m2.add_instruction(pass_op{}, sum3);
    }
    EXPECT(m1 == m2);
Paul's avatar
Paul committed
67
68
}

Paul's avatar
Paul committed
69
TEST_CASE(simplify_add2)
Paul's avatar
Paul committed
70
{
Paul Fultz II's avatar
Paul Fultz II committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    migraphx::module m1;
    {
        auto x    = m1.add_parameter("x", {migraphx::shape::int32_type, {1}});
        auto y    = m1.add_parameter("y", {migraphx::shape::int32_type, {1}});
        auto one  = m1.add_literal(1);
        auto two  = m1.add_literal(2);
        auto sum1 = m1.add_instruction(migraphx::make_op("add"), one, x);
        auto sum2 = m1.add_instruction(migraphx::make_op("add"), two, y);
        auto sum3 = m1.add_instruction(migraphx::make_op("add"), sum1, sum2);
        m1.add_instruction(pass_op{}, sum3);
    }
    run_pass(m1);

    migraphx::module m2;
    {
        auto x    = m2.add_parameter("x", {migraphx::shape::int32_type, {1}});
        auto y    = m2.add_parameter("y", {migraphx::shape::int32_type, {1}});
        auto one  = m2.add_literal(1);
        auto two  = m2.add_literal(2);
        auto sum1 = m2.add_instruction(migraphx::make_op("add"), one, two);
        auto sum2 = m2.add_instruction(migraphx::make_op("add"), x, y);
        auto sum3 = m2.add_instruction(migraphx::make_op("add"), sum2, sum1);
        m2.add_instruction(pass_op{}, sum3);
    }
    EXPECT(m1 == m2);
Paul's avatar
Paul committed
96
97
}

Paul's avatar
Paul committed
98
TEST_CASE(simplify_add3)
Paul's avatar
Paul committed
99
{
Paul Fultz II's avatar
Paul Fultz II committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
    migraphx::module m1;
    {
        auto x    = m1.add_parameter("x", {migraphx::shape::int32_type, {1}});
        auto one  = m1.add_literal(1);
        auto two  = m1.add_literal(2);
        auto sum1 = m1.add_instruction(migraphx::make_op("add"), one, x);
        auto sum2 = m1.add_instruction(migraphx::make_op("add"), one, two);
        auto sum3 = m1.add_instruction(migraphx::make_op("add"), sum1, sum2);
        m1.add_instruction(pass_op{}, sum3);
    }
    run_pass(m1);

    migraphx::module m2;
    {
        auto x    = m2.add_parameter("x", {migraphx::shape::int32_type, {1}});
        auto one  = m2.add_literal(1);
        auto two  = m2.add_literal(2);
        auto sum1 = m2.add_instruction(migraphx::make_op("add"), one, two);
        auto sum2 = m2.add_instruction(migraphx::make_op("add"), one, sum1);
        auto sum3 = m2.add_instruction(migraphx::make_op("add"), x, sum2);
        m2.add_instruction(pass_op{}, sum3);
    }
    EXPECT(m1 == m2);
Paul's avatar
Paul committed
123
124
}

125
126
127
128
129
TEST_CASE(simplify_add_broadcast1)
{
    migraphx::shape inner{migraphx::shape::int32_type, {2}};
    migraphx::shape outer{migraphx::shape::int32_type, {1, 2, 3, 3}};
    migraphx::op::broadcast b{1, {1, 2, 3, 3}};
Paul Fultz II's avatar
Paul Fultz II committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
    migraphx::module m1;
    {
        auto x    = m1.add_parameter("x", outer);
        auto y    = m1.add_parameter("y", outer);
        auto one  = m1.add_literal({inner, {1, 1}});
        auto oneb = m1.add_instruction(b, one);
        auto two  = m1.add_literal({inner, {2, 2}});
        auto twob = m1.add_instruction(b, two);
        auto sum1 = m1.add_instruction(migraphx::make_op("add"), x, oneb);
        auto sum2 = m1.add_instruction(migraphx::make_op("add"), y, twob);
        auto sum3 = m1.add_instruction(migraphx::make_op("add"), sum1, sum2);
        m1.add_instruction(pass_op{}, sum3);
    }
    run_pass(m1);

    migraphx::module m2;
    {
        auto x     = m2.add_parameter("x", outer);
        auto y     = m2.add_parameter("y", outer);
        auto one   = m2.add_literal({inner, {1, 1}});
        auto two   = m2.add_literal({inner, {2, 2}});
        auto sum1  = m2.add_instruction(migraphx::make_op("add"), one, two);
        auto sum1b = m2.add_instruction(b, sum1);
        auto sum2  = m2.add_instruction(migraphx::make_op("add"), x, y);
        auto sum3  = m2.add_instruction(migraphx::make_op("add"), sum2, sum1b);
        m2.add_instruction(pass_op{}, sum3);
    }
    EXPECT(m1 == m2);
158
159
160
161
162
163
164
165
}

TEST_CASE(simplify_add_broadcast2)
{
    migraphx::shape inner{migraphx::shape::int32_type, {2}};
    migraphx::shape outer{migraphx::shape::int32_type, {1, 2, 3, 3}};
    migraphx::op::broadcast b{1, {1, 2, 3, 3}};
    auto create_program = [&] {
Paul Fultz II's avatar
Paul Fultz II committed
166
167
168
169
170
171
172
173
174
175
176
        migraphx::module m;
        auto x    = m.add_parameter("x", outer);
        auto y    = m.add_parameter("y", outer);
        auto one  = m.add_literal({inner, {1, 1}});
        auto oneb = m.add_instruction(b, one);
        auto two  = m.add_literal({outer, {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}});
        auto sum1 = m.add_instruction(migraphx::make_op("add"), x, y);
        auto sum2 = m.add_instruction(migraphx::make_op("add"), oneb, two);
        auto sum3 = m.add_instruction(migraphx::make_op("add"), sum2, sum1);
        m.add_instruction(pass_op{}, sum3);
        return m;
177
    };
Paul Fultz II's avatar
Paul Fultz II committed
178
179
    migraphx::module m1 = create_program();
    run_pass(m1);
180

Paul Fultz II's avatar
Paul Fultz II committed
181
182
    migraphx::module m2 = create_program();
    EXPECT(m1 == m2);
183
184
}

Paul's avatar
Paul committed
185
// TODO: Add test case
186
// TEST_CASE(simplify_add4)
Paul's avatar
Paul committed
187
188
void simplify_add4()
{
Paul Fultz II's avatar
Paul Fultz II committed
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
    migraphx::module m1;
    {
        auto x    = m1.add_parameter("x", {migraphx::shape::int32_type, {1}});
        auto y    = m1.add_parameter("y", {migraphx::shape::int32_type, {1}});
        auto one  = m1.add_literal(1);
        auto two  = m1.add_literal(2);
        auto sum1 = m1.add_instruction(migraphx::make_op("add"), one, x);
        auto sum2 = m1.add_instruction(migraphx::make_op("add"), sum1, y);
        auto sum3 = m1.add_instruction(migraphx::make_op("add"), sum2, two);
        m1.add_instruction(pass_op{}, sum3);
    }
    run_pass(m1);

    migraphx::module m2;
    {
        auto x    = m2.add_parameter("x", {migraphx::shape::int32_type, {1}});
        auto y    = m2.add_parameter("y", {migraphx::shape::int32_type, {1}});
        auto one  = m2.add_literal(1);
        auto two  = m2.add_literal(2);
        auto sum1 = m2.add_instruction(migraphx::make_op("add"), one, two);
        auto sum2 = m2.add_instruction(migraphx::make_op("add"), x, y);
        auto sum3 = m2.add_instruction(migraphx::make_op("add"), sum2, sum1);
        m2.add_instruction(pass_op{}, sum3);
    }
    EXPECT(m1 == m2);
Paul's avatar
Paul committed
214
215
}

Paul's avatar
Paul committed
216
217
TEST_CASE(simplify_mul_conv1)
{
Paul Fultz II's avatar
Paul Fultz II committed
218
219
220
221
222
    migraphx::module m;
    auto x = m.add_parameter("x", {migraphx::shape::int32_type, {1, 128, 28, 28}});
    auto w =
        m.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {256, 128, 3, 3}}));
    auto conv = m.add_instruction(
223
224
225
226
        migraphx::make_op("convolution",
                          {{"padding", {1, 1}}, {"stride", {2, 2}}, {"dilation", {1, 1}}}),
        x,
        w);
Paul Fultz II's avatar
Paul Fultz II committed
227
228
    auto a = m.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {256}}));
    auto b = m.add_instruction(
229
        migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 256, 14, 14}}}), a);
Paul Fultz II's avatar
Paul Fultz II committed
230
231
    auto mul = m.add_instruction(migraphx::make_op("mul"), conv, b);
    m.add_instruction(pass_op{}, mul);
Paul's avatar
Paul committed
232
    EXPECT(conv->outputs().front()->name() == "mul");
Paul Fultz II's avatar
Paul Fultz II committed
233
234
235
    run_pass(m);
    auto new_conv =
        std::find_if(m.begin(), m.end(), [](auto&& ins) { return ins.name() == "convolution"; });
Paul's avatar
Paul committed
236
237
238
    EXPECT(new_conv->outputs().front()->name() != "mul");
}

239
240
TEST_CASE(simplify_mul_slice_conv1)
{
Paul Fultz II's avatar
Paul Fultz II committed
241
    migraphx::module m1;
242
    {
Paul Fultz II's avatar
Paul Fultz II committed
243
244
        auto x = m1.add_parameter("x", {migraphx::shape::int32_type, {1, 1024, 17, 17}});
        auto w = m1.add_literal(
245
            migraphx::generate_literal({migraphx::shape::int32_type, {768, 1024, 1, 1}}));
Paul Fultz II's avatar
Paul Fultz II committed
246
247
        auto conv   = m1.add_instruction(migraphx::make_op("convolution"), x, w);
        auto slice1 = m1.add_instruction(
248
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {384}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
249
250
        auto a = m1.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {384}}));
        auto b = m1.add_instruction(
251
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 384, 17, 17}}}), a);
Paul Fultz II's avatar
Paul Fultz II committed
252
253
        auto mul    = m1.add_instruction(migraphx::make_op("mul"), slice1, b);
        auto slice2 = m1.add_instruction(
254
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {384}}, {"ends", {768}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
255
256
        auto add = m1.add_instruction(migraphx::make_op("add"), mul, slice2);
        m1.add_instruction(pass_op{}, add);
257
    }
Paul Fultz II's avatar
Paul Fultz II committed
258
    run_pass(m1);
259

Paul Fultz II's avatar
Paul Fultz II committed
260
    migraphx::module m2;
261
    {
Paul Fultz II's avatar
Paul Fultz II committed
262
263
        auto x = m2.add_parameter("x", {migraphx::shape::int32_type, {1, 1024, 17, 17}});
        auto w = m2.add_literal(
264
            migraphx::generate_literal({migraphx::shape::int32_type, {768, 1024, 1, 1}}));
Paul Fultz II's avatar
Paul Fultz II committed
265
        auto wslice1 = m2.add_instruction(
266
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {384}}}), w);
Paul Fultz II's avatar
Paul Fultz II committed
267
268
        auto a = m2.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {384}}));
        auto b = m2.add_instruction(
269
            migraphx::make_op("broadcast", {{"axis", 0}, {"out_lens", {384, 1024, 1, 1}}}), a);
Paul Fultz II's avatar
Paul Fultz II committed
270
271
        auto mul     = m2.add_instruction(migraphx::make_op("mul"), b, wslice1);
        auto wslice2 = m2.add_instruction(
272
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {384}}, {"ends", {768}}}), w);
Paul Fultz II's avatar
Paul Fultz II committed
273
274
275
        auto concat = m2.add_instruction(migraphx::make_op("concat", {{"axis", 0}}), mul, wslice2);
        auto conv   = m2.add_instruction(migraphx::make_op("convolution"), x, concat);
        auto slice1 = m2.add_instruction(
276
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {384}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
277
        auto slice2 = m2.add_instruction(
278
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {384}}, {"ends", {768}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
279
280
        auto add = m2.add_instruction(migraphx::make_op("add"), slice1, slice2);
        m2.add_instruction(pass_op{}, add);
281
    }
Paul Fultz II's avatar
Paul Fultz II committed
282
    EXPECT(m1 == m2);
283
284
285
286
}

TEST_CASE(simplify_mul_slice_conv_overlapping_slice)
{
Paul Fultz II's avatar
Paul Fultz II committed
287
    migraphx::module m1;
288
    {
Paul Fultz II's avatar
Paul Fultz II committed
289
290
        auto x = m1.add_parameter("x", {migraphx::shape::int32_type, {1, 1024, 17, 17}});
        auto w = m1.add_literal(
291
            migraphx::generate_literal({migraphx::shape::int32_type, {768, 1024, 1, 1}}));
Paul Fultz II's avatar
Paul Fultz II committed
292
293
        auto conv   = m1.add_instruction(migraphx::make_op("convolution"), x, w);
        auto slice1 = m1.add_instruction(
294
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {384}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
295
296
        auto a = m1.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {384}}));
        auto b = m1.add_instruction(
297
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 384, 17, 17}}}), a);
Paul Fultz II's avatar
Paul Fultz II committed
298
299
        auto mul    = m1.add_instruction(migraphx::make_op("mul"), slice1, b);
        auto slice2 = m1.add_instruction(
300
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {383}}, {"ends", {767}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
301
302
        auto add = m1.add_instruction(migraphx::make_op("add"), mul, slice2);
        m1.add_instruction(pass_op{}, add);
303
    }
Paul Fultz II's avatar
Paul Fultz II committed
304
305
306
    migraphx::module m2 = m1;
    run_pass(m1);
    EXPECT(m1 == m2);
307
308
309
310
}

TEST_CASE(simplify_mul_slice_conv_not_all_slice)
{
Paul Fultz II's avatar
Paul Fultz II committed
311
    migraphx::module m1;
312
    {
Paul Fultz II's avatar
Paul Fultz II committed
313
314
        auto x = m1.add_parameter("x", {migraphx::shape::int32_type, {1, 1024, 17, 17}});
        auto w = m1.add_literal(
315
            migraphx::generate_literal({migraphx::shape::int32_type, {768, 1024, 1, 1}}));
Paul Fultz II's avatar
Paul Fultz II committed
316
317
        auto conv   = m1.add_instruction(migraphx::make_op("convolution"), x, w);
        auto slice1 = m1.add_instruction(
318
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {384}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
319
320
        auto a = m1.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {384}}));
        auto b = m1.add_instruction(
321
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 384, 17, 17}}}), a);
Paul Fultz II's avatar
Paul Fultz II committed
322
323
        auto mul = m1.add_instruction(migraphx::make_op("mul"), slice1, b);
        auto c   = m1.add_literal(
324
            migraphx::generate_literal({migraphx::shape::int32_type, {1, 768, 17, 17}}));
Paul Fultz II's avatar
Paul Fultz II committed
325
326
327
        auto add    = m1.add_instruction(migraphx::make_op("add"), conv, c);
        auto concat = m1.add_instruction(migraphx::make_op("concat", {{"axis", 1}}), mul, add);
        m1.add_instruction(pass_op{}, concat);
328
    }
Paul Fultz II's avatar
Paul Fultz II committed
329
330
331
    migraphx::module m2 = m1;
    run_pass(m1);
    EXPECT(m1 == m2);
332
333
}

Paul's avatar
Paul committed
334
335
TEST_CASE(simplify_mul_add)
{
Paul Fultz II's avatar
Paul Fultz II committed
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
    migraphx::module m1;
    {
        auto x   = m1.add_parameter("x", {migraphx::shape::int32_type, {1}});
        auto one = m1.add_literal(1);
        auto two = m1.add_literal(2);
        auto sum = m1.add_instruction(migraphx::make_op("add"), one, x);
        auto mul = m1.add_instruction(migraphx::make_op("mul"), sum, two);
        m1.add_instruction(pass_op{}, mul);
    }
    run_pass(m1);

    migraphx::module m2;
    {
        auto x    = m2.add_parameter("x", {migraphx::shape::int32_type, {1}});
        auto one  = m2.add_literal(1);
        auto two  = m2.add_literal(2);
        auto mul1 = m2.add_instruction(migraphx::make_op("mul"), two, x);
        auto mul2 = m2.add_instruction(migraphx::make_op("mul"), two, one);
        auto sum  = m2.add_instruction(migraphx::make_op("add"), mul1, mul2);
        m2.add_instruction(pass_op{}, sum);
    }
    EXPECT(m1 == m2);
Paul's avatar
Paul committed
358
359
}

360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
TEST_CASE(simplify_dot_add)
{
    migraphx::module m1;
    {
        auto x   = m1.add_parameter("x", {migraphx::shape::float_type, {2, 2}});
        auto one = m1.add_literal(get_2x2());
        auto two = m1.add_literal(get_2x2(1));
        auto sum = m1.add_instruction(migraphx::make_op("add"), one, x);
        auto dot = m1.add_instruction(migraphx::make_op("dot"), sum, two);
        m1.add_instruction(pass_op{}, dot);
    }
    run_pass(m1);

    migraphx::module m2;
    {
        auto x    = m2.add_parameter("x", {migraphx::shape::float_type, {2, 2}});
        auto one  = m2.add_literal(get_2x2());
        auto two  = m2.add_literal(get_2x2(1));
        auto dot1 = m2.add_instruction(migraphx::make_op("dot"), x, two);
        auto dot2 = m2.add_instruction(migraphx::make_op("dot"), one, two);
        auto sum  = m2.add_instruction(migraphx::make_op("add"), dot1, dot2);
        m2.add_instruction(pass_op{}, sum);
    }
    EXPECT(m1 == m2);
}

TEST_CASE(simplify_inner_broadcast1)
Paul's avatar
Paul committed
387
388
{
    auto b = migraphx::op::broadcast{1, {2, 1, 4, 5}};
Paul Fultz II's avatar
Paul Fultz II committed
389
    migraphx::module m1;
Paul's avatar
Paul committed
390
    {
Paul Fultz II's avatar
Paul Fultz II committed
391
392
393
394
395
396
        auto x   = m1.add_parameter("x", {migraphx::shape::int32_type, {1}});
        auto y   = m1.add_parameter("y", {migraphx::shape::int32_type, {1}});
        auto xb  = m1.add_instruction(b, x);
        auto yb  = m1.add_instruction(b, y);
        auto sum = m1.add_instruction(migraphx::make_op("add"), xb, yb);
        m1.add_instruction(pass_op{}, sum);
Paul's avatar
Paul committed
397
    }
Paul Fultz II's avatar
Paul Fultz II committed
398
    run_pass(m1);
Paul's avatar
Paul committed
399

Paul Fultz II's avatar
Paul Fultz II committed
400
    migraphx::module m2;
Paul's avatar
Paul committed
401
    {
Paul Fultz II's avatar
Paul Fultz II committed
402
403
404
405
406
        auto x    = m2.add_parameter("x", {migraphx::shape::int32_type, {1}});
        auto y    = m2.add_parameter("y", {migraphx::shape::int32_type, {1}});
        auto sum  = m2.add_instruction(migraphx::make_op("add"), x, y);
        auto sumb = m2.add_instruction(b, sum);
        m2.add_instruction(pass_op{}, sumb);
Paul's avatar
Paul committed
407
    }
Paul Fultz II's avatar
Paul Fultz II committed
408
    EXPECT(m1 == m2);
Paul's avatar
Paul committed
409
410
}

411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
TEST_CASE(simplify_inner_broadcast2)
{
    auto b = migraphx::op::multibroadcast{{2, 1, 4, 5}};
    migraphx::module m1;
    {
        auto x   = m1.add_parameter("x", {migraphx::shape::int32_type, {1, 1, 1, 1}});
        auto y   = m1.add_parameter("y", {migraphx::shape::int32_type, {1, 1, 1, 1}});
        auto xb  = m1.add_instruction(b, x);
        auto yb  = m1.add_instruction(b, y);
        auto sum = m1.add_instruction(migraphx::make_op("add"), xb, yb);
        m1.add_instruction(pass_op{}, sum);
    }
    run_pass(m1);

    migraphx::module m2;
    {
        auto x    = m2.add_parameter("x", {migraphx::shape::int32_type, {1, 1, 1, 1}});
        auto y    = m2.add_parameter("y", {migraphx::shape::int32_type, {1, 1, 1, 1}});
        auto sum  = m2.add_instruction(migraphx::make_op("add"), x, y);
        auto sumb = m2.add_instruction(b, sum);
        m2.add_instruction(pass_op{}, sumb);
    }
    EXPECT(m1 == m2);
}

436
437
TEST_CASE(simplify_add_conv1)
{
Paul Fultz II's avatar
Paul Fultz II committed
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
    migraphx::module m;
    auto x = m.add_parameter("x", {migraphx::shape::float_type, {1, 128, 28, 28}});
    auto w =
        m.add_literal(migraphx::generate_literal({migraphx::shape::float_type, {256, 128, 3, 3}}));
    auto y = m.add_parameter("y", {migraphx::shape::float_type, {1, 128, 28, 28}});
    auto v =
        m.add_literal(migraphx::generate_literal({migraphx::shape::float_type, {256, 128, 3, 3}}));
    auto conv1 = m.add_instruction(migraphx::make_op("convolution"), x, w);
    auto conv2 = m.add_instruction(migraphx::make_op("convolution"), y, v);
    auto sum   = m.add_instruction(migraphx::make_op("add"), conv1, conv2);
    m.add_instruction(pass_op{}, sum);
    auto s = m.get_output_shapes().back();
    run_pass(m);
    EXPECT(s == m.get_output_shapes().back());
    EXPECT(std::count_if(
               m.begin(), m.end(), [](auto&& ins) { return ins.name() == "convolution"; }) == 1);
454
455
456
457
}

TEST_CASE(simplify_add_conv_no_fusion_7x7_diff_strides)
{
Paul Fultz II's avatar
Paul Fultz II committed
458
459
460
461
462
463
464
465
466
    migraphx::module m;
    auto x = m.add_parameter("x", {migraphx::shape::float_type, {1, 128, 14, 14}});
    auto w =
        m.add_literal(migraphx::generate_literal({migraphx::shape::float_type, {256, 128, 7, 7}}));
    auto y = m.add_parameter("y", {migraphx::shape::float_type, {1, 128, 28, 28}});
    auto v =
        m.add_literal(migraphx::generate_literal({migraphx::shape::float_type, {256, 128, 7, 7}}));
    auto conv1 = m.add_instruction(migraphx::make_op("convolution"), x, w);
    auto conv2 = m.add_instruction(
467
        migraphx::make_op("convolution", {{"padding", {0, 0}}, {"stride", {3, 3}}}), y, v);
Paul Fultz II's avatar
Paul Fultz II committed
468
469
470
471
472
    auto sum = m.add_instruction(migraphx::make_op("add"), conv1, conv2);
    m.add_instruction(pass_op{}, sum);
    auto s = m.get_output_shapes().back();
    run_pass(m);
    EXPECT(s == m.get_output_shapes().back());
473
    // No fusion
Paul Fultz II's avatar
Paul Fultz II committed
474
475
    EXPECT(std::count_if(
               m.begin(), m.end(), [](auto&& ins) { return ins.name() == "convolution"; }) == 2);
476
477
478
479
}

TEST_CASE(simplify_add_conv_1x1_diff_strides1)
{
Paul Fultz II's avatar
Paul Fultz II committed
480
481
482
483
484
485
486
487
488
    migraphx::module m;
    auto x = m.add_parameter("x", {migraphx::shape::float_type, {1, 128, 14, 14}});
    auto w =
        m.add_literal(migraphx::generate_literal({migraphx::shape::float_type, {256, 128, 1, 1}}));
    auto y = m.add_parameter("y", {migraphx::shape::float_type, {1, 128, 28, 28}});
    auto v =
        m.add_literal(migraphx::generate_literal({migraphx::shape::float_type, {256, 128, 1, 1}}));
    auto conv1 = m.add_instruction(migraphx::make_op("convolution"), x, w);
    auto conv2 = m.add_instruction(
489
        migraphx::make_op("convolution", {{"padding", {0, 0}}, {"stride", {2, 2}}}), y, v);
Paul Fultz II's avatar
Paul Fultz II committed
490
491
492
493
494
495
496
    auto sum = m.add_instruction(migraphx::make_op("add"), conv1, conv2);
    m.add_instruction(pass_op{}, sum);
    auto s = m.get_output_shapes().back();
    run_pass(m);
    EXPECT(s == m.get_output_shapes().back());
    EXPECT(std::count_if(
               m.begin(), m.end(), [](auto&& ins) { return ins.name() == "convolution"; }) == 1);
497
498
499
500
}

TEST_CASE(simplify_add_conv_1x1_diff_strides2)
{
Paul Fultz II's avatar
Paul Fultz II committed
501
502
503
504
505
506
507
508
    migraphx::module m;
    auto x = m.add_parameter("x", {migraphx::shape::float_type, {1, 128, 28, 28}});
    auto w =
        m.add_literal(migraphx::generate_literal({migraphx::shape::float_type, {256, 128, 1, 1}}));
    auto y = m.add_parameter("y", {migraphx::shape::float_type, {1, 128, 14, 14}});
    auto v =
        m.add_literal(migraphx::generate_literal({migraphx::shape::float_type, {256, 128, 1, 1}}));
    auto conv1 = m.add_instruction(
509
        migraphx::make_op("convolution", {{"padding", {0, 0}}, {"stride", {2, 2}}}), x, w);
Paul Fultz II's avatar
Paul Fultz II committed
510
511
512
513
514
515
516
517
    auto conv2 = m.add_instruction(migraphx::make_op("convolution"), y, v);
    auto sum   = m.add_instruction(migraphx::make_op("add"), conv1, conv2);
    m.add_instruction(pass_op{}, sum);
    auto s = m.get_output_shapes().back();
    run_pass(m);
    EXPECT(s == m.get_output_shapes().back());
    EXPECT(std::count_if(
               m.begin(), m.end(), [](auto&& ins) { return ins.name() == "convolution"; }) == 1);
518
519
520
521
}

TEST_CASE(simplify_add_conv_1x1_diff_strides_odd)
{
Paul Fultz II's avatar
Paul Fultz II committed
522
523
    migraphx::module m;
    auto x = m.add_parameter("x", {migraphx::shape::float_type, {1, 54, 83, 83}});
524
    auto w =
Paul Fultz II's avatar
Paul Fultz II committed
525
526
        m.add_literal(migraphx::generate_literal({migraphx::shape::float_type, {54, 54, 1, 1}}));
    auto y = m.add_parameter("y", {migraphx::shape::float_type, {1, 54, 165, 165}});
527
    auto v =
Paul Fultz II's avatar
Paul Fultz II committed
528
529
530
        m.add_literal(migraphx::generate_literal({migraphx::shape::float_type, {54, 54, 1, 1}}));
    auto conv1 = m.add_instruction(migraphx::make_op("convolution"), x, w);
    auto conv2 = m.add_instruction(
531
        migraphx::make_op("convolution", {{"padding", {0, 0}}, {"stride", {2, 2}}}), y, v);
Paul Fultz II's avatar
Paul Fultz II committed
532
533
534
535
536
537
538
    auto sum = m.add_instruction(migraphx::make_op("add"), conv1, conv2);
    m.add_instruction(pass_op{}, sum);
    auto s = m.get_output_shapes().back();
    run_pass(m);
    EXPECT(s == m.get_output_shapes().back());
    EXPECT(std::count_if(
               m.begin(), m.end(), [](auto&& ins) { return ins.name() == "convolution"; }) == 1);
539
540
541
542
}

TEST_CASE(simplify_add_conv_no_fusion_asymetrical_strides1)
{
Paul Fultz II's avatar
Paul Fultz II committed
543
544
545
546
547
548
549
550
    migraphx::module m;
    auto x = m.add_parameter("x", {migraphx::shape::float_type, {1, 128, 28, 14}});
    auto w =
        m.add_literal(migraphx::generate_literal({migraphx::shape::float_type, {256, 128, 1, 1}}));
    auto y = m.add_parameter("y", {migraphx::shape::float_type, {1, 128, 14, 14}});
    auto v =
        m.add_literal(migraphx::generate_literal({migraphx::shape::float_type, {256, 128, 1, 1}}));
    auto conv1 = m.add_instruction(
551
        migraphx::make_op("convolution", {{"padding", {0, 0}}, {"stride", {2, 1}}}), x, w);
Paul Fultz II's avatar
Paul Fultz II committed
552
553
554
555
556
557
    auto conv2 = m.add_instruction(migraphx::make_op("convolution"), y, v);
    auto sum   = m.add_instruction(migraphx::make_op("add"), conv1, conv2);
    m.add_instruction(pass_op{}, sum);
    auto s = m.get_output_shapes().back();
    run_pass(m);
    EXPECT(s == m.get_output_shapes().back());
558
    // No fusion
Paul Fultz II's avatar
Paul Fultz II committed
559
560
    EXPECT(std::count_if(
               m.begin(), m.end(), [](auto&& ins) { return ins.name() == "convolution"; }) == 2);
561
562
563
564
}

TEST_CASE(simplify_add_conv_no_fusion_asymetrical_strides2)
{
Paul Fultz II's avatar
Paul Fultz II committed
565
566
567
568
569
570
571
572
573
    migraphx::module m;
    auto x = m.add_parameter("x", {migraphx::shape::float_type, {1, 128, 14, 14}});
    auto w =
        m.add_literal(migraphx::generate_literal({migraphx::shape::float_type, {256, 128, 1, 1}}));
    auto y = m.add_parameter("y", {migraphx::shape::float_type, {1, 128, 28, 14}});
    auto v =
        m.add_literal(migraphx::generate_literal({migraphx::shape::float_type, {256, 128, 1, 1}}));
    auto conv1 = m.add_instruction(migraphx::make_op("convolution"), x, w);
    auto conv2 = m.add_instruction(
574
        migraphx::make_op("convolution", {{"padding", {0, 0}}, {"stride", {2, 1}}}), y, v);
Paul Fultz II's avatar
Paul Fultz II committed
575
576
577
578
579
    auto sum = m.add_instruction(migraphx::make_op("add"), conv1, conv2);
    m.add_instruction(pass_op{}, sum);
    auto s = m.get_output_shapes().back();
    run_pass(m);
    EXPECT(s == m.get_output_shapes().back());
580
    // No fusion
Paul Fultz II's avatar
Paul Fultz II committed
581
582
    EXPECT(std::count_if(
               m.begin(), m.end(), [](auto&& ins) { return ins.name() == "convolution"; }) == 2);
583
584
}

585
586
587
TEST_CASE(simplify_concat_add_relu)
{
    auto s = migraphx::shape{migraphx::shape::int32_type, {1}};
Paul Fultz II's avatar
Paul Fultz II committed
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
    migraphx::module m1;
    {
        auto x      = m1.add_parameter("x", s);
        auto y      = m1.add_parameter("y", s);
        auto one    = m1.add_literal({s, {1}});
        auto two    = m1.add_literal({s, {2}});
        auto sum1   = m1.add_instruction(migraphx::make_op("add"), x, one);
        auto relu1  = m1.add_instruction(migraphx::make_op("relu"), sum1);
        auto sum2   = m1.add_instruction(migraphx::make_op("add"), y, two);
        auto relu2  = m1.add_instruction(migraphx::make_op("relu"), sum2);
        auto concat = m1.add_instruction(migraphx::make_op("concat", {{"axis", 0}}), relu1, relu2);
        m1.add_instruction(pass_op{}, concat);
    }
    run_pass(m1);

    migraphx::module m2;
    {
        auto x       = m2.add_parameter("x", s);
        auto y       = m2.add_parameter("y", s);
        auto one     = m2.add_literal({s, {1}});
        auto two     = m2.add_literal({s, {2}});
        auto concat1 = m2.add_instruction(migraphx::make_op("concat", {{"axis", 0}}), x, y);
        auto concat2 = m2.add_instruction(migraphx::make_op("concat", {{"axis", 0}}), one, two);
        auto sum     = m2.add_instruction(migraphx::make_op("add"), concat1, concat2);
        auto relu    = m2.add_instruction(migraphx::make_op("relu"), sum);
        m2.add_instruction(pass_op{}, relu);
    }
    EXPECT(m1 == m2);
616
617
}

618
619
620
TEST_CASE(simplify_concat_add_relu_partial)
{
    auto s = migraphx::shape{migraphx::shape::int32_type, {1}};
Paul Fultz II's avatar
Paul Fultz II committed
621
622
623
624
625
626
627
628
629
630
631
    migraphx::module m1;
    {
        auto x     = m1.add_parameter("x", s);
        auto y     = m1.add_parameter("y", s);
        auto one   = m1.add_literal({s, {1}});
        auto two   = m1.add_literal({s, {2}});
        auto sum1  = m1.add_instruction(migraphx::make_op("add"), x, one);
        auto relu1 = m1.add_instruction(migraphx::make_op("relu"), sum1);
        auto sum2  = m1.add_instruction(migraphx::make_op("add"), y, two);
        auto relu2 = m1.add_instruction(migraphx::make_op("relu"), sum2);
        auto sum3  = m1.add_instruction(migraphx::make_op("add"), x, y);
632
        auto concat =
Paul Fultz II's avatar
Paul Fultz II committed
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
            m1.add_instruction(migraphx::make_op("concat", {{"axis", 0}}), sum3, relu1, relu2);
        m1.add_instruction(pass_op{}, concat);
    }
    run_pass(m1);

    migraphx::module m2;
    {
        auto x       = m2.add_parameter("x", s);
        auto y       = m2.add_parameter("y", s);
        auto one     = m2.add_literal({s, {1}});
        auto two     = m2.add_literal({s, {2}});
        auto concat1 = m2.add_instruction(migraphx::make_op("concat", {{"axis", 0}}), x, y);
        auto concat2 = m2.add_instruction(migraphx::make_op("concat", {{"axis", 0}}), one, two);
        auto sum1    = m2.add_instruction(migraphx::make_op("add"), concat1, concat2);
        auto relu    = m2.add_instruction(migraphx::make_op("relu"), sum1);
        auto sum2    = m2.add_instruction(migraphx::make_op("add"), x, y);
        auto concat  = m2.add_instruction(migraphx::make_op("concat", {{"axis", 0}}), sum2, relu);
        m2.add_instruction(pass_op{}, concat);
    }
    EXPECT(m1.sort() == m2.sort());
653
654
655
656
657
}

TEST_CASE(simplify_concat_add_relu_partial_broadcast)
{
    auto s = migraphx::shape{migraphx::shape::int32_type, {2, 1, 4, 5}};
Paul Fultz II's avatar
Paul Fultz II committed
658
    migraphx::module m1;
659
    {
660
        auto b    = migraphx::op::broadcast{1, {2, 1, 4, 5}};
Paul Fultz II's avatar
Paul Fultz II committed
661
662
663
664
665
666
667
        auto x    = m1.add_parameter("x", s);
        auto y    = m1.add_parameter("y", s);
        auto one  = m1.add_literal(1);
        auto oneb = m1.add_instruction(b, one);
        auto two  = m1.add_literal(2);
        auto twob = m1.add_instruction(b, two);
        auto sum  = m1.add_instruction(migraphx::make_op("add"), x, y);
668
        auto concat =
Paul Fultz II's avatar
Paul Fultz II committed
669
670
            m1.add_instruction(migraphx::make_op("concat", {{"axis", 1}}), sum, oneb, twob);
        m1.add_instruction(pass_op{}, concat);
671
    }
Paul Fultz II's avatar
Paul Fultz II committed
672
    run_pass(m1);
673

Paul Fultz II's avatar
Paul Fultz II committed
674
    migraphx::module m2;
675
676
    {
        auto b       = migraphx::op::broadcast{1, {2, 2, 4, 5}};
Paul Fultz II's avatar
Paul Fultz II committed
677
678
679
680
681
682
683
684
685
686
687
        auto x       = m2.add_parameter("x", s);
        auto y       = m2.add_parameter("y", s);
        auto one     = m2.add_literal(1);
        auto two     = m2.add_literal(2);
        auto concat1 = m2.add_instruction(migraphx::make_op("concat", {{"axis", 0}}), one, two);
        auto concatb = m2.add_instruction(b, concat1);
        auto sum     = m2.add_instruction(migraphx::make_op("add"), x, y);
        auto concat2 = m2.add_instruction(migraphx::make_op("concat", {{"axis", 1}}), sum, concatb);
        m2.add_instruction(pass_op{}, concat2);
    }
    EXPECT(m1.sort() == m2.sort());
688
689
}

690
691
692
TEST_CASE(simplify_concat_add_relu_broadcast_different_axis)
{
    auto s = migraphx::shape{migraphx::shape::int32_type, {2, 1, 4, 5}};
Paul Fultz II's avatar
Paul Fultz II committed
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
    migraphx::module m1;
    {
        auto b      = migraphx::op::broadcast{1, {2, 1, 4, 5}};
        auto x      = m1.add_parameter("x", s);
        auto y      = m1.add_parameter("y", s);
        auto one    = m1.add_literal(1);
        auto oneb   = m1.add_instruction(b, one);
        auto two    = m1.add_literal(2);
        auto twob   = m1.add_instruction(b, two);
        auto sum1   = m1.add_instruction(migraphx::make_op("add"), x, oneb);
        auto relu1  = m1.add_instruction(migraphx::make_op("relu"), sum1);
        auto sum2   = m1.add_instruction(migraphx::make_op("add"), y, twob);
        auto relu2  = m1.add_instruction(migraphx::make_op("relu"), sum2);
        auto concat = m1.add_instruction(migraphx::make_op("concat", {{"axis", 1}}), relu1, relu2);
        m1.add_instruction(pass_op{}, concat);
    }
    run_pass(m1);

    migraphx::module m2;
712
713
    {
        auto b        = migraphx::op::broadcast{1, {2, 2, 4, 5}};
Paul Fultz II's avatar
Paul Fultz II committed
714
715
716
717
718
719
720
721
722
723
724
725
        auto x        = m2.add_parameter("x", s);
        auto y        = m2.add_parameter("y", s);
        auto one      = m2.add_literal(1);
        auto two      = m2.add_literal(2);
        auto concat1  = m2.add_instruction(migraphx::make_op("concat", {{"axis", 1}}), x, y);
        auto concat2  = m2.add_instruction(migraphx::make_op("concat", {{"axis", 0}}), one, two);
        auto concat2b = m2.add_instruction(b, concat2);
        auto sum      = m2.add_instruction(migraphx::make_op("add"), concat1, concat2b);
        auto relu     = m2.add_instruction(migraphx::make_op("relu"), sum);
        m2.add_instruction(pass_op{}, relu);
    }
    EXPECT(m1 == m2);
726
727
728
729
730
}

TEST_CASE(simplify_concat_add_relu_broadcast_same_axis)
{
    auto s = migraphx::shape{migraphx::shape::int32_type, {2, 1, 4, 5}};
Paul Fultz II's avatar
Paul Fultz II committed
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
    migraphx::module m1;
    {
        auto b      = migraphx::op::broadcast{1, {2, 1, 4, 5}};
        auto x      = m1.add_parameter("x", s);
        auto y      = m1.add_parameter("y", s);
        auto one    = m1.add_literal(1);
        auto oneb   = m1.add_instruction(b, one);
        auto two    = m1.add_literal(2);
        auto twob   = m1.add_instruction(b, two);
        auto sum1   = m1.add_instruction(migraphx::make_op("add"), x, oneb);
        auto relu1  = m1.add_instruction(migraphx::make_op("relu"), sum1);
        auto sum2   = m1.add_instruction(migraphx::make_op("add"), y, twob);
        auto relu2  = m1.add_instruction(migraphx::make_op("relu"), sum2);
        auto concat = m1.add_instruction(migraphx::make_op("concat", {{"axis", 0}}), relu1, relu2);
        m1.add_instruction(pass_op{}, concat);
    }
    run_pass(m1);

    migraphx::module m2;
750
751
    {
        auto b       = migraphx::op::broadcast{1, {2, 1, 4, 5}};
Paul Fultz II's avatar
Paul Fultz II committed
752
753
754
755
756
757
758
759
760
761
762
763
764
        auto x       = m2.add_parameter("x", s);
        auto y       = m2.add_parameter("y", s);
        auto one     = m2.add_literal(1);
        auto oneb    = m2.add_instruction(b, one);
        auto two     = m2.add_literal(2);
        auto twob    = m2.add_instruction(b, two);
        auto concat1 = m2.add_instruction(migraphx::make_op("concat", {{"axis", 0}}), x, y);
        auto concat2 = m2.add_instruction(migraphx::make_op("concat", {{"axis", 0}}), oneb, twob);
        auto sum     = m2.add_instruction(migraphx::make_op("add"), concat1, concat2);
        auto relu    = m2.add_instruction(migraphx::make_op("relu"), sum);
        m2.add_instruction(pass_op{}, relu);
    }
    EXPECT(m1 == m2);
765
766
}

767
768
TEST_CASE(simplify_div_const)
{
Paul Fultz II's avatar
Paul Fultz II committed
769
    migraphx::module m1;
770
    {
Paul Fultz II's avatar
Paul Fultz II committed
771
772
773
        auto x   = m1.add_parameter("x", {migraphx::shape::int32_type, {1}});
        auto two = m1.add_literal(2);
        m1.add_instruction(migraphx::make_op("div"), x, two);
774
    }
Paul Fultz II's avatar
Paul Fultz II committed
775
    run_pass(m1);
776

Paul Fultz II's avatar
Paul Fultz II committed
777
    migraphx::module m2;
778
    {
Paul Fultz II's avatar
Paul Fultz II committed
779
780
781
782
        auto x     = m2.add_parameter("x", {migraphx::shape::int32_type, {1}});
        auto two   = m2.add_literal(2);
        auto recip = m2.insert_instruction(std::next(two), migraphx::make_op("recip"), two);
        m2.add_instruction(migraphx::make_op("mul"), x, recip);
783
    }
Paul Fultz II's avatar
Paul Fultz II committed
784
    EXPECT(m1 == m2);
785
786
787
788
}

TEST_CASE(simplify_sub_const)
{
Paul Fultz II's avatar
Paul Fultz II committed
789
    migraphx::module m1;
790
    {
Paul Fultz II's avatar
Paul Fultz II committed
791
792
793
        auto x   = m1.add_parameter("x", {migraphx::shape::int32_type, {1}});
        auto two = m1.add_literal(2);
        m1.add_instruction(migraphx::make_op("sub"), x, two);
794
    }
Paul Fultz II's avatar
Paul Fultz II committed
795
    run_pass(m1);
796

Paul Fultz II's avatar
Paul Fultz II committed
797
    migraphx::module m2;
798
    {
Paul Fultz II's avatar
Paul Fultz II committed
799
800
801
802
        auto x   = m2.add_parameter("x", {migraphx::shape::int32_type, {1}});
        auto two = m2.add_literal(2);
        auto neg = m2.insert_instruction(std::next(two), migraphx::make_op("neg"), two);
        m2.add_instruction(migraphx::make_op("add"), x, neg);
803
    }
Paul Fultz II's avatar
Paul Fultz II committed
804
    EXPECT(m1 == m2);
805
806
}

kahmed10's avatar
kahmed10 committed
807
808
TEST_CASE(simplify_rsqrt)
{
Paul Fultz II's avatar
Paul Fultz II committed
809
    migraphx::module m1;
kahmed10's avatar
kahmed10 committed
810
    {
Paul Fultz II's avatar
Paul Fultz II committed
811
812
813
        auto x    = m1.add_parameter("x", {migraphx::shape::int32_type, {1}});
        auto sqrt = m1.add_instruction(migraphx::make_op("sqrt"), x);
        m1.add_instruction(migraphx::make_op("recip"), sqrt);
kahmed10's avatar
kahmed10 committed
814
    }
Paul Fultz II's avatar
Paul Fultz II committed
815
    run_pass(m1);
kahmed10's avatar
kahmed10 committed
816

Paul Fultz II's avatar
Paul Fultz II committed
817
    migraphx::module m2;
kahmed10's avatar
kahmed10 committed
818
    {
Paul Fultz II's avatar
Paul Fultz II committed
819
820
        auto x = m2.add_parameter("x", {migraphx::shape::int32_type, {1}});
        m2.add_instruction(migraphx::make_op("rsqrt"), x);
kahmed10's avatar
kahmed10 committed
821
    }
Paul Fultz II's avatar
Paul Fultz II committed
822
    EXPECT(m1 == m2);
kahmed10's avatar
kahmed10 committed
823
824
825
826
}

TEST_CASE(simplify_rsqrt_multi_use)
{
Paul Fultz II's avatar
Paul Fultz II committed
827
    migraphx::module m1;
kahmed10's avatar
kahmed10 committed
828
    {
Paul Fultz II's avatar
Paul Fultz II committed
829
830
831
832
833
        auto x     = m1.add_parameter("x", {migraphx::shape::int32_type, {1}});
        auto sqrt  = m1.add_instruction(migraphx::make_op("sqrt"), x);
        auto add   = m1.add_instruction(migraphx::make_op("add"), sqrt, sqrt);
        auto rsqrt = m1.add_instruction(migraphx::make_op("recip"), sqrt);
        m1.add_instruction(migraphx::make_op("add"), rsqrt, add);
kahmed10's avatar
kahmed10 committed
834
    }
Paul Fultz II's avatar
Paul Fultz II committed
835
    migraphx::module m2{m1};
kahmed10's avatar
kahmed10 committed
836

Paul Fultz II's avatar
Paul Fultz II committed
837
838
    run_pass(m1);
    EXPECT(m1 == m2);
kahmed10's avatar
kahmed10 committed
839
840
}

841
842
843
844
TEST_CASE(simplify_slice_concat)
{
    auto s = migraphx::shape{migraphx::shape::float_type, {256}};

Paul Fultz II's avatar
Paul Fultz II committed
845
    migraphx::module m1;
846
    {
Paul Fultz II's avatar
Paul Fultz II committed
847
848
849
        auto x       = m1.add_parameter("x", s);
        auto y       = m1.add_parameter("y", s);
        auto xslice1 = m1.add_instruction(
850
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {128}}}), x);
Paul Fultz II's avatar
Paul Fultz II committed
851
        auto xslice2 = m1.add_instruction(
852
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {128}}, {"ends", {256}}}), x);
Paul Fultz II's avatar
Paul Fultz II committed
853
        auto yslice1 = m1.add_instruction(
854
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {128}}}), y);
Paul Fultz II's avatar
Paul Fultz II committed
855
        auto yslice2 = m1.add_instruction(
856
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {128}}, {"ends", {256}}}), y);
Paul Fultz II's avatar
Paul Fultz II committed
857
        auto concat = m1.add_instruction(
858
            migraphx::make_op("concat", {{"axis", 0}}), xslice1, xslice2, yslice1, yslice2);
Paul Fultz II's avatar
Paul Fultz II committed
859
        m1.add_instruction(pass_op{}, concat);
860
    }
Paul Fultz II's avatar
Paul Fultz II committed
861
    run_pass(m1);
862

Paul Fultz II's avatar
Paul Fultz II committed
863
    migraphx::module m2;
864
    {
Paul Fultz II's avatar
Paul Fultz II committed
865
866
867
868
        auto x      = m2.add_parameter("x", s);
        auto y      = m2.add_parameter("y", s);
        auto concat = m2.add_instruction(migraphx::make_op("concat", {{"axis", 0}}), x, y);
        m2.add_instruction(pass_op{}, concat);
869
    }
Paul Fultz II's avatar
Paul Fultz II committed
870
    EXPECT(m1 == m2);
871
872
873
874
875
876
}

TEST_CASE(simplify_slice_concat_non_uniform)
{
    auto s = migraphx::shape{migraphx::shape::float_type, {256}};

Paul Fultz II's avatar
Paul Fultz II committed
877
    migraphx::module m1;
878
    {
Paul Fultz II's avatar
Paul Fultz II committed
879
880
881
        auto x       = m1.add_parameter("x", s);
        auto y       = m1.add_parameter("y", s);
        auto xslice1 = m1.add_instruction(
882
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {64}}}), x);
Paul Fultz II's avatar
Paul Fultz II committed
883
        auto xslice2 = m1.add_instruction(
884
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {64}}, {"ends", {192}}}), x);
Paul Fultz II's avatar
Paul Fultz II committed
885
        auto xslice3 = m1.add_instruction(
886
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {192}}, {"ends", {256}}}), x);
Paul Fultz II's avatar
Paul Fultz II committed
887
        auto yslice1 = m1.add_instruction(
888
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {64}}}), y);
Paul Fultz II's avatar
Paul Fultz II committed
889
        auto yslice2 = m1.add_instruction(
890
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {64}}, {"ends", {192}}}), y);
Paul Fultz II's avatar
Paul Fultz II committed
891
        auto yslice3 = m1.add_instruction(
892
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {192}}, {"ends", {256}}}), y);
Paul Fultz II's avatar
Paul Fultz II committed
893
894
895
896
897
898
899
900
        auto concat = m1.add_instruction(migraphx::make_op("concat", {{"axis", 0}}),
                                         xslice1,
                                         xslice2,
                                         xslice3,
                                         yslice1,
                                         yslice2,
                                         yslice3);
        m1.add_instruction(pass_op{}, concat);
901
    }
Paul Fultz II's avatar
Paul Fultz II committed
902
    run_pass(m1);
903

Paul Fultz II's avatar
Paul Fultz II committed
904
    migraphx::module m2;
905
    {
Paul Fultz II's avatar
Paul Fultz II committed
906
907
908
909
        auto x      = m2.add_parameter("x", s);
        auto y      = m2.add_parameter("y", s);
        auto concat = m2.add_instruction(migraphx::make_op("concat", {{"axis", 0}}), x, y);
        m2.add_instruction(pass_op{}, concat);
910
911
    }

Paul Fultz II's avatar
Paul Fultz II committed
912
    EXPECT(m1 == m2);
913
914
915
916
917
918
}

TEST_CASE(simplify_slice_concat_flipped)
{
    auto s = migraphx::shape{migraphx::shape::float_type, {256}};

Paul Fultz II's avatar
Paul Fultz II committed
919
    migraphx::module m1;
920
    {
Paul Fultz II's avatar
Paul Fultz II committed
921
922
923
        auto x       = m1.add_parameter("x", s);
        auto y       = m1.add_parameter("y", s);
        auto xslice1 = m1.add_instruction(
924
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {64}}}), x);
Paul Fultz II's avatar
Paul Fultz II committed
925
        auto xslice2 = m1.add_instruction(
926
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {192}}, {"ends", {256}}}), x);
Paul Fultz II's avatar
Paul Fultz II committed
927
        auto xslice3 = m1.add_instruction(
928
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {64}}, {"ends", {192}}}), x);
Paul Fultz II's avatar
Paul Fultz II committed
929
        auto yslice1 = m1.add_instruction(
930
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {64}}}), y);
Paul Fultz II's avatar
Paul Fultz II committed
931
        auto yslice2 = m1.add_instruction(
932
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {192}}, {"ends", {256}}}), y);
Paul Fultz II's avatar
Paul Fultz II committed
933
        auto yslice3 = m1.add_instruction(
934
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {64}}, {"ends", {192}}}), y);
Paul Fultz II's avatar
Paul Fultz II committed
935
936
937
938
939
940
941
942
943
944
945
946
947
        auto concat = m1.add_instruction(migraphx::make_op("concat", {{"axis", 0}}),
                                         xslice1,
                                         xslice2,
                                         xslice3,
                                         yslice1,
                                         yslice2,
                                         yslice3);
        m1.add_instruction(pass_op{}, concat);
    }
    migraphx::module m2 = m1;
    run_pass(m1);

    EXPECT(m1 == m2);
948
949
}

950
951
952
TEST_CASE(simplify_split_add_relu)
{
    auto s = migraphx::shape{migraphx::shape::int32_type, {3, 2, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
953
    migraphx::module m1;
954
955
    {
        auto b     = migraphx::op::broadcast{1, {3, 1, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
956
957
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
958
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
959
        auto y = m1.add_instruction(
960
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
        auto one   = m1.add_literal(1);
        auto oneb  = m1.add_instruction(b, one);
        auto two   = m1.add_literal(2);
        auto twob  = m1.add_instruction(b, two);
        auto sum1  = m1.add_instruction(migraphx::make_op("add"), x, oneb);
        auto relu1 = m1.add_instruction(migraphx::make_op("relu"), sum1);
        auto sum2  = m1.add_instruction(migraphx::make_op("add"), y, twob);
        auto relu2 = m1.add_instruction(migraphx::make_op("relu"), sum2);
        auto add   = m1.add_instruction(migraphx::make_op("add"), relu1, relu2);
        m1.add_instruction(pass_op{}, add);
    }
    run_pass(m1);

    migraphx::module m2;
    {
976
        auto b       = migraphx::op::broadcast{1, {3, 2, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
977
978
979
980
981
982
983
984
        auto input   = m2.add_parameter("input", s);
        auto one     = m2.add_literal(1);
        auto two     = m2.add_literal(2);
        auto concat  = m2.add_instruction(migraphx::make_op("concat", {{"axis", 0}}), one, two);
        auto concatb = m2.add_instruction(b, concat);
        auto sum     = m2.add_instruction(migraphx::make_op("add"), input, concatb);
        auto relu    = m2.add_instruction(migraphx::make_op("relu"), sum);
        auto x       = m2.add_instruction(
985
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), relu);
Paul Fultz II's avatar
Paul Fultz II committed
986
        auto y = m2.add_instruction(
987
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), relu);
Paul Fultz II's avatar
Paul Fultz II committed
988
989
        auto add = m2.add_instruction(migraphx::make_op("add"), x, y);
        m2.add_instruction(pass_op{}, add);
990
    }
Paul Fultz II's avatar
Paul Fultz II committed
991
    EXPECT(m1.sort() == m2.sort());
992
993
}

Shucai Xiao's avatar
Shucai Xiao committed
994
995
996
997
998
999
1000
1001
TEST_CASE(simplify_split_reduce0)
{
    auto s = migraphx::shape{migraphx::shape::int32_type, {3, 2, 4}};
    migraphx::module m1;
    {
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
kahmed10's avatar
kahmed10 committed
1002
        auto y = m1.add_instruction(
Shucai Xiao's avatar
Shucai Xiao committed
1003
1004
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);

kahmed10's avatar
kahmed10 committed
1005
1006
        auto one = m1.add_literal(1);
        auto two = m1.add_literal(2);
Shucai Xiao's avatar
Shucai Xiao committed
1007

kahmed10's avatar
kahmed10 committed
1008
1009
        auto arx   = m1.add_instruction(migraphx::make_op("contiguous"), x);
        auto ary   = m1.add_instruction(migraphx::make_op("contiguous"), y);
Shucai Xiao's avatar
Shucai Xiao committed
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
        auto rmax0 = m1.add_instruction(migraphx::make_op("reduce_sum", {{"axes", {0, 1}}}), x);
        auto rmin0 = m1.add_instruction(migraphx::make_op("reduce_mean", {{"axes", {0, 1}}}), x);
        auto rmax1 = m1.add_instruction(migraphx::make_op("gather", {{"axis", 1}}), arx, one);
        auto rmin1 = m1.add_instruction(migraphx::make_op("gather", {{"axis", 1}}), ary, two);
        auto rmax2 = m1.add_instruction(migraphx::make_op("reduce_sum", {{"axes", {0, 1}}}), y);
        auto rmin2 = m1.add_instruction(migraphx::make_op("reduce_mean", {{"axes", {0, 1}}}), y);
        m1.add_return({rmax0, rmin0, rmax1, rmin1, rmax2, rmin2});
    }

    migraphx::module m2 = m1;
    run_pass(m1);
    EXPECT(m1.sort() == m2.sort());
}

TEST_CASE(simplify_split_reduce1)
{
    auto s = migraphx::shape{migraphx::shape::int32_type, {3, 2, 4}};
    migraphx::module m1;
    {
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
        auto y = m1.add_instruction(
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);

        auto rmax0 = m1.add_instruction(migraphx::make_op("reduce_sum", {{"axes", {0, 2}}}), x);
        auto rmin0 = m1.add_instruction(migraphx::make_op("reduce_mean", {{"axes", {0, 2}}}), x);
        auto rmax2 = m1.add_instruction(migraphx::make_op("reduce_sum", {{"axes", {0, 2}}}), y);
        auto rmin2 = m1.add_instruction(migraphx::make_op("reduce_mean", {{"axes", {0, 2}}}), y);
        m1.add_return({rmax0, rmin0, rmax2, rmin2});
    }

    migraphx::module m2;
    {
        auto input = m2.add_parameter("input", s);

kahmed10's avatar
kahmed10 committed
1046
1047
        auto rmn  = m2.add_instruction(migraphx::make_op("reduce_mean", {{"axes", {0, 2}}}), input);
        auto slc0 = m2.add_instruction(
Shucai Xiao's avatar
Shucai Xiao committed
1048
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), rmn);
kahmed10's avatar
kahmed10 committed
1049
1050
        auto rmx  = m2.add_instruction(migraphx::make_op("reduce_sum", {{"axes", {0, 2}}}), input);
        auto slc1 = m2.add_instruction(
Shucai Xiao's avatar
Shucai Xiao committed
1051
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), rmx);
kahmed10's avatar
kahmed10 committed
1052
        auto slc2 = m2.add_instruction(
Shucai Xiao's avatar
Shucai Xiao committed
1053
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), rmn);
kahmed10's avatar
kahmed10 committed
1054
        auto slc3 = m2.add_instruction(
Shucai Xiao's avatar
Shucai Xiao committed
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), rmx);
        m2.add_return({slc3, slc2, slc1, slc0});
    }

    run_pass(m1);
    EXPECT(m1.sort() == m2.sort());
}

TEST_CASE(simplify_split_reduce2)
{
    auto s = migraphx::shape{migraphx::shape::int32_type, {3, 2, 4}};
    migraphx::module m1;
    {
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
        auto y = m1.add_instruction(
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);
        auto rmax0 = m1.add_instruction(migraphx::make_op("reduce_sum", {{"axes", {0, 2}}}), x);
        auto rmin0 = m1.add_instruction(migraphx::make_op("reduce_mean", {{"axes", {0, 1}}}), x);
        auto rmax2 = m1.add_instruction(migraphx::make_op("reduce_sum", {{"axes", {0, 2}}}), y);
        auto rmin2 = m1.add_instruction(migraphx::make_op("reduce_mean", {{"axes", {0, 1}}}), y);
        m1.add_return({rmax0, rmin0, rmax2, rmin2});
    }

    migraphx::module m2;
    {
        auto input = m2.add_parameter("input", s);
        auto x     = m2.add_instruction(
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);
        auto rmn1 = m2.add_instruction(migraphx::make_op("reduce_mean", {{"axes", {0, 1}}}), x);
kahmed10's avatar
kahmed10 committed
1086
        auto y    = m2.add_instruction(
Shucai Xiao's avatar
Shucai Xiao committed
1087
1088
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
        auto rmn2 = m2.add_instruction(migraphx::make_op("reduce_mean", {{"axes", {0, 1}}}), y);
kahmed10's avatar
kahmed10 committed
1089
1090
        auto rms  = m2.add_instruction(migraphx::make_op("reduce_sum", {{"axes", {0, 2}}}), input);
        auto slc0 = m2.add_instruction(
Shucai Xiao's avatar
Shucai Xiao committed
1091
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), rms);
kahmed10's avatar
kahmed10 committed
1092
        auto slc1 = m2.add_instruction(
Shucai Xiao's avatar
Shucai Xiao committed
1093
1094
1095
1096
1097
1098
1099
1100
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), rms);
        m2.add_return({slc1, rmn2, slc0, rmn1});
    }

    run_pass(m1);
    EXPECT(m1.sort() == m2.sort());
}

1101
1102
1103
TEST_CASE(simplify_split_add_relu_reshape)
{
    auto s = migraphx::shape{migraphx::shape::int32_type, {3, 2, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1104
    migraphx::module m1;
1105
    {
1106
1107
        auto b     = migraphx::op::broadcast{1, {3, 1, 4}};
        auto r     = migraphx::op::reshape{{3, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1108
1109
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
1110
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1111
        auto y = m1.add_instruction(
1112
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
        auto one      = m1.add_literal(1);
        auto oneb     = m1.add_instruction(b, one);
        auto two      = m1.add_literal(2);
        auto twob     = m1.add_instruction(b, two);
        auto sum1     = m1.add_instruction(migraphx::make_op("add"), x, oneb);
        auto relu1    = m1.add_instruction(migraphx::make_op("relu"), sum1);
        auto reshape1 = m1.add_instruction(r, relu1);
        auto sum2     = m1.add_instruction(migraphx::make_op("add"), y, twob);
        auto relu2    = m1.add_instruction(migraphx::make_op("relu"), sum2);
        auto reshape2 = m1.add_instruction(r, relu2);
        auto add      = m1.add_instruction(migraphx::make_op("add"), reshape1, reshape2);
        m1.add_instruction(pass_op{}, add);
    }
    run_pass(m1);

    migraphx::module m2;
    {
1130
        auto b       = migraphx::op::broadcast{1, {3, 2, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1131
1132
1133
1134
1135
1136
1137
1138
1139
        auto input   = m2.add_parameter("input", s);
        auto one     = m2.add_literal(1);
        auto two     = m2.add_literal(2);
        auto concat  = m2.add_instruction(migraphx::make_op("concat", {{"axis", 0}}), one, two);
        auto concatb = m2.add_instruction(b, concat);
        auto sum     = m2.add_instruction(migraphx::make_op("add"), input, concatb);
        auto relu    = m2.add_instruction(migraphx::make_op("relu"), sum);
        auto rsp     = m2.add_instruction(migraphx::make_op("reshape", {{"dims", {3, 8}}}), relu);
        auto slc1    = m2.add_instruction(
1140
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {4}}}), rsp);
Paul Fultz II's avatar
Paul Fultz II committed
1141
        auto slc2 = m2.add_instruction(
1142
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {4}}, {"ends", {8}}}), rsp);
Paul Fultz II's avatar
Paul Fultz II committed
1143
1144
        auto add = m2.add_instruction(migraphx::make_op("add"), slc1, slc2);
        m2.add_instruction(pass_op{}, add);
1145
    }
Paul Fultz II's avatar
Paul Fultz II committed
1146
    EXPECT(m1.sort() == m2.sort());
1147
1148
1149
1150
1151
}

TEST_CASE(simplify_slice_different_axis)
{
    auto s = migraphx::shape{migraphx::shape::int32_type, {3, 2, 4, 2}};
Paul Fultz II's avatar
Paul Fultz II committed
1152
    migraphx::module m1;
1153
    {
1154
        auto r     = migraphx::op::reshape{{3, 2, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1155
1156
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
1157
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1158
        auto y = m1.add_instruction(
1159
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1160
1161
        auto one  = m1.add_literal(1);
        auto oneb = m1.add_instruction(
1162
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {3, 1, 4, 2}}}), one);
Paul Fultz II's avatar
Paul Fultz II committed
1163
1164
        auto two  = m1.add_literal(2);
        auto twob = m1.add_instruction(
1165
            migraphx::make_op("broadcast", {{"axis", 3}, {"out_lens", {3, 2, 4, 1}}}), two);
Paul Fultz II's avatar
Paul Fultz II committed
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
        auto sum1     = m1.add_instruction(migraphx::make_op("add"), x, oneb);
        auto relu1    = m1.add_instruction(migraphx::make_op("relu"), sum1);
        auto reshape1 = m1.add_instruction(r, relu1);
        auto sum2     = m1.add_instruction(migraphx::make_op("add"), y, twob);
        auto relu2    = m1.add_instruction(migraphx::make_op("relu"), sum2);
        auto reshape2 = m1.add_instruction(r, relu2);
        auto add      = m1.add_instruction(migraphx::make_op("add"), reshape1, reshape2);
        m1.add_instruction(pass_op{}, add);
    }
    migraphx::module m2 = m1;
    run_pass(m1);

    EXPECT(m1.sort() == m2.sort());
1179
1180
1181
1182
1183
}

TEST_CASE(simplify_slice_missing_begining_slice)
{
    auto s = migraphx::shape{migraphx::shape::int32_type, {3, 3, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1184
    migraphx::module m1;
1185
1186
    {
        auto b     = migraphx::op::broadcast{1, {3, 1, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1187
1188
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
1189
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {2}}, {"ends", {3}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1190
        auto y = m1.add_instruction(
1191
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
        auto one   = m1.add_literal(1);
        auto oneb  = m1.add_instruction(b, one);
        auto two   = m1.add_literal(2);
        auto twob  = m1.add_instruction(b, two);
        auto sum1  = m1.add_instruction(migraphx::make_op("add"), x, oneb);
        auto relu1 = m1.add_instruction(migraphx::make_op("relu"), sum1);
        auto sum2  = m1.add_instruction(migraphx::make_op("add"), y, twob);
        auto relu2 = m1.add_instruction(migraphx::make_op("relu"), sum2);
        auto add   = m1.add_instruction(migraphx::make_op("add"), relu1, relu2);
        m1.add_instruction(pass_op{}, add);
    }
    migraphx::module m2 = m1;
    run_pass(m1);

    EXPECT(m1.sort() == m2.sort());
1207
1208
1209
1210
1211
}

TEST_CASE(simplify_slice_missing_middle_slice)
{
    auto s = migraphx::shape{migraphx::shape::int32_type, {3, 3, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1212
    migraphx::module m1;
1213
1214
    {
        auto b     = migraphx::op::broadcast{1, {3, 1, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1215
1216
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
1217
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {2}}, {"ends", {3}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1218
        auto y = m1.add_instruction(
1219
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
        auto one   = m1.add_literal(1);
        auto oneb  = m1.add_instruction(b, one);
        auto two   = m1.add_literal(2);
        auto twob  = m1.add_instruction(b, two);
        auto sum1  = m1.add_instruction(migraphx::make_op("add"), x, oneb);
        auto relu1 = m1.add_instruction(migraphx::make_op("relu"), sum1);
        auto sum2  = m1.add_instruction(migraphx::make_op("add"), y, twob);
        auto relu2 = m1.add_instruction(migraphx::make_op("relu"), sum2);
        auto add   = m1.add_instruction(migraphx::make_op("add"), relu1, relu2);
        m1.add_instruction(pass_op{}, add);
    }
    migraphx::module m2 = m1;
    run_pass(m1);

    EXPECT(m1.sort() == m2.sort());
1235
1236
1237
1238
1239
}

TEST_CASE(simplify_slice_missing_end_slice)
{
    auto s = migraphx::shape{migraphx::shape::int32_type, {3, 3, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1240
    migraphx::module m1;
1241
1242
    {
        auto b     = migraphx::op::broadcast{1, {3, 1, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1243
1244
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
1245
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1246
        auto y = m1.add_instruction(
1247
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
        auto one   = m1.add_literal(1);
        auto oneb  = m1.add_instruction(b, one);
        auto two   = m1.add_literal(2);
        auto twob  = m1.add_instruction(b, two);
        auto sum1  = m1.add_instruction(migraphx::make_op("add"), x, oneb);
        auto relu1 = m1.add_instruction(migraphx::make_op("relu"), sum1);
        auto sum2  = m1.add_instruction(migraphx::make_op("add"), y, twob);
        auto relu2 = m1.add_instruction(migraphx::make_op("relu"), sum2);
        auto add   = m1.add_instruction(migraphx::make_op("add"), relu1, relu2);
        m1.add_instruction(pass_op{}, add);
    }
    migraphx::module m2 = m1;
    run_pass(m1);

    EXPECT(m1.sort() == m2.sort());
1263
1264
1265
1266
1267
}

TEST_CASE(simplify_split_add_relu_concat_same_axis)
{
    auto s = migraphx::shape{migraphx::shape::int32_type, {3, 2, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1268
    migraphx::module m1;
1269
    {
1270
        auto b     = migraphx::op::broadcast{1, {3, 1, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1271
1272
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
1273
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1274
        auto y = m1.add_instruction(
1275
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
        auto one    = m1.add_literal(1);
        auto oneb   = m1.add_instruction(b, one);
        auto two    = m1.add_literal(2);
        auto twob   = m1.add_instruction(b, two);
        auto sum1   = m1.add_instruction(migraphx::make_op("add"), x, oneb);
        auto relu1  = m1.add_instruction(migraphx::make_op("relu"), sum1);
        auto sum2   = m1.add_instruction(migraphx::make_op("add"), y, twob);
        auto relu2  = m1.add_instruction(migraphx::make_op("relu"), sum2);
        auto concat = m1.add_instruction(migraphx::make_op("concat", {{"axis", 1}}), relu1, relu2);
        m1.add_instruction(pass_op{}, concat);
1286
    }
Paul Fultz II's avatar
Paul Fultz II committed
1287
    run_pass(m1);
1288

Paul Fultz II's avatar
Paul Fultz II committed
1289
    migraphx::module m2;
1290
1291
    {
        auto b       = migraphx::op::broadcast{1, {3, 2, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
        auto input   = m2.add_parameter("input", s);
        auto one     = m2.add_literal(1);
        auto two     = m2.add_literal(2);
        auto concat  = m2.add_instruction(migraphx::make_op("concat", {{"axis", 0}}), one, two);
        auto concatb = m2.add_instruction(b, concat);
        auto sum     = m2.add_instruction(migraphx::make_op("add"), input, concatb);
        auto relu    = m2.add_instruction(migraphx::make_op("relu"), sum);
        m2.add_instruction(pass_op{}, relu);
    }
    EXPECT(m1.sort() == m2.sort());
1302
1303
1304
1305
1306
}

TEST_CASE(simplify_split_add_relu_multi_axes)
{
    auto s = migraphx::shape{migraphx::shape::int32_type, {3, 2, 4, 6}};
Paul Fultz II's avatar
Paul Fultz II committed
1307
    migraphx::module m1;
1308
1309
    {
        auto b     = migraphx::op::broadcast{1, {3, 1, 4, 3}};
Paul Fultz II's avatar
Paul Fultz II committed
1310
1311
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
1312
1313
            migraphx::make_op("slice", {{"axes", {1, 3}}, {"starts", {0, 0}}, {"ends", {1, 3}}}),
            input);
Paul Fultz II's avatar
Paul Fultz II committed
1314
        auto y = m1.add_instruction(
1315
1316
            migraphx::make_op("slice", {{"axes", {1, 3}}, {"starts", {1, 3}}, {"ends", {2, 6}}}),
            input);
Paul Fultz II's avatar
Paul Fultz II committed
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
        auto one   = m1.add_literal(1);
        auto oneb  = m1.add_instruction(b, one);
        auto two   = m1.add_literal(2);
        auto twob  = m1.add_instruction(b, two);
        auto sum1  = m1.add_instruction(migraphx::make_op("add"), x, oneb);
        auto relu1 = m1.add_instruction(migraphx::make_op("relu"), sum1);
        auto sum2  = m1.add_instruction(migraphx::make_op("add"), y, twob);
        auto relu2 = m1.add_instruction(migraphx::make_op("relu"), sum2);
        auto add   = m1.add_instruction(migraphx::make_op("add"), relu1, relu2);
        m1.add_instruction(pass_op{}, add);
    }
    migraphx::module m2 = m1;
    run_pass(m1);
    EXPECT(m1.sort() == m2.sort());
1331
1332
1333
1334
1335
}

TEST_CASE(simplify_split_add_relu_used_multiple_split1)
{
    auto s = migraphx::shape{migraphx::shape::int32_type, {3, 2, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1336
    migraphx::module m1;
1337
1338
    {
        auto b     = migraphx::op::broadcast{1, {3, 1, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1339
1340
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
1341
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1342
        auto y = m1.add_instruction(
1343
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
        auto one   = m1.add_literal(1);
        auto oneb  = m1.add_instruction(b, one);
        auto two   = m1.add_literal(2);
        auto twob  = m1.add_instruction(b, two);
        auto sum1  = m1.add_instruction(migraphx::make_op("add"), x, oneb);
        auto relu1 = m1.add_instruction(migraphx::make_op("relu"), sum1);
        auto sum2  = m1.add_instruction(migraphx::make_op("add"), y, twob);
        auto relu2 = m1.add_instruction(migraphx::make_op("relu"), sum2);
        auto add1  = m1.add_instruction(migraphx::make_op("add"), relu1, relu2);
        auto add2  = m1.add_instruction(migraphx::make_op("add"), x, add1);
        m1.add_instruction(pass_op{}, add2);
    }
    run_pass(m1);

    migraphx::module m2;
    {
1360
        auto b     = migraphx::op::broadcast{1, {3, 2, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1361
1362
        auto input = m2.add_parameter("input", s);
        auto slice = m2.add_instruction(
1363
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1364
1365
1366
1367
1368
1369
1370
        auto one     = m2.add_literal(1);
        auto two     = m2.add_literal(2);
        auto concat  = m2.add_instruction(migraphx::make_op("concat", {{"axis", 0}}), one, two);
        auto concatb = m2.add_instruction(b, concat);
        auto sum     = m2.add_instruction(migraphx::make_op("add"), input, concatb);
        auto relu    = m2.add_instruction(migraphx::make_op("relu"), sum);
        auto x       = m2.add_instruction(
1371
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), relu);
Paul Fultz II's avatar
Paul Fultz II committed
1372
        auto y = m2.add_instruction(
1373
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), relu);
Paul Fultz II's avatar
Paul Fultz II committed
1374
1375
1376
        auto add1 = m2.add_instruction(migraphx::make_op("add"), x, y);
        auto add2 = m2.add_instruction(migraphx::make_op("add"), slice, add1);
        m2.add_instruction(pass_op{}, add2);
1377
    }
Paul Fultz II's avatar
Paul Fultz II committed
1378
    EXPECT(m1.sort() == m2.sort());
1379
1380
1381
1382
1383
}

TEST_CASE(simplify_split_add_relu_used_multiple_split2)
{
    auto s = migraphx::shape{migraphx::shape::int32_type, {3, 2, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1384
    migraphx::module m1;
1385
1386
    {
        auto b     = migraphx::op::broadcast{1, {3, 1, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1387
1388
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
1389
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1390
        auto y = m1.add_instruction(
1391
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
        auto z     = m1.add_instruction(migraphx::make_op("relu"), x);
        auto one   = m1.add_literal(1);
        auto oneb  = m1.add_instruction(b, one);
        auto two   = m1.add_literal(2);
        auto twob  = m1.add_instruction(b, two);
        auto sum1  = m1.add_instruction(migraphx::make_op("add"), x, oneb);
        auto relu1 = m1.add_instruction(migraphx::make_op("relu"), sum1);
        auto sum2  = m1.add_instruction(migraphx::make_op("add"), y, twob);
        auto relu2 = m1.add_instruction(migraphx::make_op("relu"), sum2);
        auto add1  = m1.add_instruction(migraphx::make_op("add"), relu1, relu2);
        auto add2  = m1.add_instruction(migraphx::make_op("add"), z, add1);
        m1.add_instruction(pass_op{}, add2);
    }
    run_pass(m1);

    migraphx::module m2;
    {
1409
        auto b     = migraphx::op::broadcast{1, {3, 2, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1410
1411
        auto input = m2.add_parameter("input", s);
        auto slice = m2.add_instruction(
1412
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1413
1414
1415
1416
1417
1418
1419
1420
        auto z       = m2.add_instruction(migraphx::make_op("relu"), slice);
        auto one     = m2.add_literal(1);
        auto two     = m2.add_literal(2);
        auto concat  = m2.add_instruction(migraphx::make_op("concat", {{"axis", 0}}), one, two);
        auto concatb = m2.add_instruction(b, concat);
        auto sum     = m2.add_instruction(migraphx::make_op("add"), input, concatb);
        auto relu    = m2.add_instruction(migraphx::make_op("relu"), sum);
        auto x       = m2.add_instruction(
1421
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), relu);
Paul Fultz II's avatar
Paul Fultz II committed
1422
        auto y = m2.add_instruction(
1423
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), relu);
Paul Fultz II's avatar
Paul Fultz II committed
1424
1425
1426
        auto add1 = m2.add_instruction(migraphx::make_op("add"), x, y);
        auto add2 = m2.add_instruction(migraphx::make_op("add"), z, add1);
        m2.add_instruction(pass_op{}, add2);
1427
    }
Paul Fultz II's avatar
Paul Fultz II committed
1428
    EXPECT(m1.sort() == m2.sort());
1429
1430
1431
1432
1433
}

TEST_CASE(simplify_split_between_add)
{
    auto s = migraphx::shape{migraphx::shape::int32_type, {3, 2, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1434
    migraphx::module m1;
1435
    {
Paul Fultz II's avatar
Paul Fultz II committed
1436
1437
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
1438
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1439
        auto y = m1.add_instruction(
1440
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1441
1442
        auto sum = m1.add_instruction(migraphx::make_op("add"), x, y);
        m1.add_instruction(pass_op{}, sum);
1443
    }
Paul Fultz II's avatar
Paul Fultz II committed
1444
1445
1446
    migraphx::module m2 = m1;
    run_pass(m1);
    EXPECT(m1.sort() == m2.sort());
1447
1448
1449
1450
1451
}

TEST_CASE(simplify_dot_horiz)
{
    auto s = migraphx::shape{migraphx::shape::int32_type, {3, 2, 2}};
Paul Fultz II's avatar
Paul Fultz II committed
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
    migraphx::module m1;
    {
        auto input = m1.add_parameter("input", s);
        auto a     = m1.add_literal(migraphx::generate_literal(s, 0));
        auto b     = m1.add_literal(migraphx::generate_literal(s, 1));
        auto x     = m1.add_instruction(migraphx::make_op("dot"), input, a);
        auto y     = m1.add_instruction(migraphx::make_op("dot"), input, b);
        auto sum   = m1.add_instruction(migraphx::make_op("add"), x, y);
        m1.add_instruction(pass_op{}, sum);
    }
    run_pass(m1);

    migraphx::module m2;
    {
        auto input  = m2.add_parameter("input", s);
        auto a      = m2.add_literal(migraphx::generate_literal(s, 0));
        auto b      = m2.add_literal(migraphx::generate_literal(s, 1));
        auto concat = m2.add_instruction(migraphx::make_op("concat", {{"axis", 2}}), a, b);
        auto dot    = m2.add_instruction(migraphx::make_op("dot"), input, concat);
        auto x      = m2.add_instruction(
1472
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {2}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
1473
        auto y = m2.add_instruction(
1474
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {2}}, {"ends", {4}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
1475
1476
        auto sum = m2.add_instruction(migraphx::make_op("add"), x, y);
        m2.add_instruction(pass_op{}, sum);
1477
    }
Paul Fultz II's avatar
Paul Fultz II committed
1478
    EXPECT(m1.sort() == m2.sort());
1479
1480
1481
1482
1483
}

TEST_CASE(simplify_dot_horiz_same_constant)
{
    auto s = migraphx::shape{migraphx::shape::int32_type, {3, 2, 2}};
Paul Fultz II's avatar
Paul Fultz II committed
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
    migraphx::module m1;
    {
        auto input = m1.add_parameter("input", s);
        auto a     = m1.add_literal(migraphx::generate_literal(s, 0));
        auto x     = m1.add_instruction(migraphx::make_op("dot"), input, a);
        auto y     = m1.add_instruction(migraphx::make_op("dot"), input, a);
        auto sum   = m1.add_instruction(migraphx::make_op("add"), x, y);
        m1.add_instruction(pass_op{}, sum);
    }
    run_pass(m1);

    migraphx::module m2;
    {
        auto input  = m2.add_parameter("input", s);
        auto a      = m2.add_literal(migraphx::generate_literal(s, 0));
        auto concat = m2.add_instruction(migraphx::make_op("concat", {{"axis", 2}}), a, a);
        auto dot    = m2.add_instruction(migraphx::make_op("dot"), input, concat);
        auto x      = m2.add_instruction(
1502
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {2}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
1503
        auto y = m2.add_instruction(
1504
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {2}}, {"ends", {4}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
1505
1506
        auto sum = m2.add_instruction(migraphx::make_op("add"), x, y);
        m2.add_instruction(pass_op{}, sum);
1507
    }
Paul Fultz II's avatar
Paul Fultz II committed
1508
    EXPECT(m1.sort() == m2.sort());
1509
1510
1511
1512
1513
}

TEST_CASE(simplify_dot_horiz_flipped)
{
    auto s = migraphx::shape{migraphx::shape::int32_type, {3, 2, 2}};
Paul Fultz II's avatar
Paul Fultz II committed
1514
    migraphx::module m1;
1515
    {
Paul Fultz II's avatar
Paul Fultz II committed
1516
1517
1518
1519
1520
1521
1522
        auto input = m1.add_parameter("input", s);
        auto a     = m1.add_literal(migraphx::generate_literal(s, 0));
        auto b     = m1.add_literal(migraphx::generate_literal(s, 1));
        auto x     = m1.add_instruction(migraphx::make_op("dot"), input, a);
        auto y     = m1.add_instruction(migraphx::make_op("dot"), b, input);
        auto sum   = m1.add_instruction(migraphx::make_op("add"), x, y);
        m1.add_instruction(pass_op{}, sum);
1523
1524
    }

Paul Fultz II's avatar
Paul Fultz II committed
1525
1526
1527
    migraphx::module m2 = m1;
    run_pass(m1);
    EXPECT(m1.sort() == m2.sort());
1528
1529
}

1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
// test if contiguous is added as necessary for reshapes
TEST_CASE(simplify_dot_horiz_reshape)
{
    auto s = migraphx::shape{migraphx::shape::int32_type, {3, 4, 4}};
    migraphx::module m1;
    {
        auto input = m1.add_parameter("input", s);
        auto a     = m1.add_literal(migraphx::generate_literal(s, 0));
        auto b     = m1.add_literal(migraphx::generate_literal(s, 1));
        auto x     = m1.add_instruction(migraphx::make_op("dot"), input, a);
        auto y     = m1.add_instruction(migraphx::make_op("dot"), input, b);
        auto x_rsp = m1.add_instruction(migraphx::make_op("reshape", {{"dims", {3, 4, 2, 2}}}), x);
        auto y_rsp =
            m1.add_instruction(migraphx::make_op("unsqueeze", {{"axes", {2}}, {"steps", {2}}}), y);
        auto sum = m1.add_instruction(migraphx::make_op("add"), {x_rsp, y_rsp});
        m1.add_instruction(pass_op{}, sum);
    }
    run_pass(m1);

    migraphx::module m2;
    {
        auto input  = m2.add_parameter("input", s);
        auto a      = m2.add_literal(migraphx::generate_literal(s, 0));
        auto b      = m2.add_literal(migraphx::generate_literal(s, 1));
        auto concat = m2.add_instruction(migraphx::make_op("concat", {{"axis", 2}}), a, b);
        auto dot    = m2.add_instruction(migraphx::make_op("dot"), input, concat);
        auto x      = m2.add_instruction(
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {4}}}), dot);
        auto y = m2.add_instruction(
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {4}}, {"ends", {8}}}), dot);
        auto x_cont = m2.add_instruction(migraphx::make_op("contiguous"), x);
        auto x_rsp =
            m2.add_instruction(migraphx::make_op("reshape", {{"dims", {3, 4, 2, 2}}}), x_cont);
        auto y_rsp =
            m2.add_instruction(migraphx::make_op("unsqueeze", {{"axes", {2}}, {"steps", {2}}}), y);
        auto sum = m2.add_instruction(migraphx::make_op("add"), {x_rsp, y_rsp});
        m2.add_instruction(pass_op{}, sum);
    }

    EXPECT(m1.sort() == m2.sort());
}

1572
1573
1574
1575
TEST_CASE(simplify_conv_horiz)
{
    auto s  = migraphx::shape{migraphx::shape::int32_type, {8, 3, 64, 64}};
    auto ws = migraphx::shape{migraphx::shape::int32_type, {12, 3, 3, 3}};
Paul Fultz II's avatar
Paul Fultz II committed
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
    migraphx::module m1;
    {
        auto input = m1.add_parameter("input", s);
        auto a     = m1.add_literal(migraphx::generate_literal(ws, 0));
        auto b     = m1.add_literal(migraphx::generate_literal(ws, 1));
        auto x     = m1.add_instruction(migraphx::make_op("convolution"), input, a);
        auto y     = m1.add_instruction(migraphx::make_op("convolution"), input, b);
        auto sum   = m1.add_instruction(migraphx::make_op("add"), x, y);
        m1.add_instruction(pass_op{}, sum);
    }
    run_pass(m1);

    migraphx::module m2;
    {
        auto input  = m2.add_parameter("input", s);
        auto a      = m2.add_literal(migraphx::generate_literal(ws, 0));
        auto b      = m2.add_literal(migraphx::generate_literal(ws, 1));
        auto concat = m2.add_instruction(migraphx::make_op("concat", {{"axis", 0}}), a, b);
        auto conv   = m2.add_instruction(migraphx::make_op("convolution"), input, concat);
        auto x      = m2.add_instruction(
1596
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {12}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
1597
        auto y = m2.add_instruction(
1598
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {12}}, {"ends", {24}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
1599
1600
        auto sum = m2.add_instruction(migraphx::make_op("add"), x, y);
        m2.add_instruction(pass_op{}, sum);
1601
    }
Paul Fultz II's avatar
Paul Fultz II committed
1602
    EXPECT(m1.sort() == m2.sort());
1603
1604
}

1605
1606
1607
1608
TEST_CASE(simplify_group_conv_horiz)
{
    auto s  = migraphx::shape{migraphx::shape::int32_type, {1, 32, 111, 111}};
    auto ws = migraphx::shape{migraphx::shape::int32_type, {32, 1, 7, 7}};
Paul Fultz II's avatar
Paul Fultz II committed
1609
    migraphx::module m1;
1610
    {
Paul Fultz II's avatar
Paul Fultz II committed
1611
1612
1613
1614
        auto x     = m1.add_parameter("x", s);
        auto w1    = m1.add_literal(migraphx::generate_literal(ws, 1));
        auto w2    = m1.add_literal(migraphx::generate_literal(ws, 2));
        auto conv1 = m1.add_instruction(
1615
1616
1617
1618
1619
            migraphx::make_op(
                "convolution",
                {{"padding", {3, 3}}, {"stride", {2, 2}}, {"dilation", {1, 1}}, {"group", 32}}),
            x,
            w1);
Paul Fultz II's avatar
Paul Fultz II committed
1620
        auto conv2 = m1.add_instruction(
1621
1622
1623
1624
1625
            migraphx::make_op(
                "convolution",
                {{"padding", {3, 3}}, {"stride", {2, 2}}, {"dilation", {1, 1}}, {"group", 32}}),
            x,
            w2);
Paul Fultz II's avatar
Paul Fultz II committed
1626
        m1.add_instruction(pass_op{}, conv1, conv2);
1627
    }
Paul Fultz II's avatar
Paul Fultz II committed
1628
1629
    migraphx::module m2 = m1;
    run_pass(m1);
1630

Paul Fultz II's avatar
Paul Fultz II committed
1631
    EXPECT(m1.sort() == m2.sort());
1632
1633
1634
}

TEST_CASE(simplify_conv_horiz_grouped)
1635
1636
1637
1638
{
    auto s   = migraphx::shape{migraphx::shape::int32_type, {8, 6, 64, 64}};
    auto ws1 = migraphx::shape{migraphx::shape::int32_type, {6, 6, 3, 3}};
    auto ws2 = migraphx::shape{migraphx::shape::int32_type, {8, 6, 64, 64}};
Paul Fultz II's avatar
Paul Fultz II committed
1639
1640
1641
1642
1643
1644
1645
    migraphx::module m1;
    {
        auto input = m1.add_parameter("input", s);
        auto a     = m1.add_literal(migraphx::generate_literal(ws1, 0));
        auto b     = m1.add_literal(migraphx::generate_literal(ws1, 1));
        auto c     = m1.add_literal(migraphx::generate_literal(ws2, 2));
        auto d     = m1.add_literal(migraphx::generate_literal(ws2, 3));
1646
        auto convx =
Paul Fultz II's avatar
Paul Fultz II committed
1647
            m1.add_instruction(migraphx::make_op("convolution", {{"padding", {1, 1}}}), input, a);
1648
        auto convy =
Paul Fultz II's avatar
Paul Fultz II committed
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
            m1.add_instruction(migraphx::make_op("convolution", {{"padding", {1, 1}}}), input, b);
        auto dotx = m1.add_instruction(migraphx::make_op("dot"), input, c);
        auto doty = m1.add_instruction(migraphx::make_op("dot"), input, d);
        auto sum1 = m1.add_instruction(migraphx::make_op("add"), convx, convy);
        auto sum2 = m1.add_instruction(migraphx::make_op("add"), dotx, doty);
        auto sum3 = m1.add_instruction(migraphx::make_op("add"), sum1, sum2);

        m1.add_instruction(pass_op{}, sum3);
    }
    run_pass(m1);

    migraphx::module m2;
    {
        auto input   = m2.add_parameter("input", s);
        auto a       = m2.add_literal(migraphx::generate_literal(ws1, 0));
        auto b       = m2.add_literal(migraphx::generate_literal(ws1, 1));
        auto c       = m2.add_literal(migraphx::generate_literal(ws2, 2));
        auto d       = m2.add_literal(migraphx::generate_literal(ws2, 3));
        auto concat1 = m2.add_instruction(migraphx::make_op("concat", {{"axis", 0}}), a, b);
        auto concat2 = m2.add_instruction(migraphx::make_op("concat", {{"axis", 3}}), c, d);
        auto conv    = m2.add_instruction(
1670
            migraphx::make_op("convolution", {{"padding", {1, 1}}}), input, concat1);
Paul Fultz II's avatar
Paul Fultz II committed
1671
        auto convx = m2.add_instruction(
1672
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {6}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
1673
        auto convy = m2.add_instruction(
1674
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {6}}, {"ends", {12}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
1675
1676
1677
        auto sum1 = m2.add_instruction(migraphx::make_op("add"), convx, convy);
        auto dot  = m2.add_instruction(migraphx::make_op("dot"), input, concat2);
        auto dotx = m2.add_instruction(
1678
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {0}}, {"ends", {64}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
1679
        auto doty = m2.add_instruction(
1680
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {64}}, {"ends", {128}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
1681
1682
1683
        auto sum2 = m2.add_instruction(migraphx::make_op("add"), dotx, doty);
        auto sum3 = m2.add_instruction(migraphx::make_op("add"), sum1, sum2);
        m2.add_instruction(pass_op{}, sum3);
1684
    }
Paul Fultz II's avatar
Paul Fultz II committed
1685
    EXPECT(m1.sort() == m2.sort());
1686
1687
}

1688
TEST_CASE(simplify_conv_horiz_grouped_extra1)
1689
1690
1691
1692
{
    auto s   = migraphx::shape{migraphx::shape::int32_type, {8, 6, 64, 64}};
    auto ws1 = migraphx::shape{migraphx::shape::int32_type, {6, 6, 3, 3}};
    auto ws2 = migraphx::shape{migraphx::shape::int32_type, {8, 6, 64, 64}};
Paul Fultz II's avatar
Paul Fultz II committed
1693
1694
1695
1696
1697
1698
1699
1700
    migraphx::module m1;
    {
        auto input = m1.add_parameter("input", s);
        auto a     = m1.add_literal(migraphx::generate_literal(ws1, 0));
        auto b     = m1.add_literal(migraphx::generate_literal(ws1, 1));
        auto c     = m1.add_literal(migraphx::generate_literal(ws2, 2));
        auto d     = m1.add_literal(migraphx::generate_literal(ws2, 3));
        auto e     = m1.add_literal(migraphx::generate_literal(s, 4));
1701
        auto convx =
Paul Fultz II's avatar
Paul Fultz II committed
1702
            m1.add_instruction(migraphx::make_op("convolution", {{"padding", {1, 1}}}), input, a);
1703
        auto convy =
Paul Fultz II's avatar
Paul Fultz II committed
1704
1705
1706
1707
1708
1709
            m1.add_instruction(migraphx::make_op("convolution", {{"padding", {1, 1}}}), input, b);
        auto dotx    = m1.add_instruction(migraphx::make_op("dot"), input, c);
        auto doty    = m1.add_instruction(migraphx::make_op("dot"), input, d);
        auto sqdiffx = m1.add_instruction(migraphx::make_op("sqdiff"), input, e);
        auto sum1    = m1.add_instruction(migraphx::make_op("add"), convx, convy);
        auto sum2    = m1.add_instruction(migraphx::make_op("add"), dotx, doty);
1710
        auto sum3    = sqdiffx;
Paul Fultz II's avatar
Paul Fultz II committed
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
        auto sum4    = m1.add_instruction(migraphx::make_op("add"), sum1, sum2);
        auto sum5    = m1.add_instruction(migraphx::make_op("add"), sum4, sum3);
        m1.add_instruction(pass_op{}, sum5);
    }
    run_pass(m1);

    migraphx::module m2;
    {
        auto input   = m2.add_parameter("input", s);
        auto a       = m2.add_literal(migraphx::generate_literal(ws1, 0));
        auto b       = m2.add_literal(migraphx::generate_literal(ws1, 1));
        auto c       = m2.add_literal(migraphx::generate_literal(ws2, 2));
        auto d       = m2.add_literal(migraphx::generate_literal(ws2, 3));
        auto e       = m2.add_literal(migraphx::generate_literal(s, 4));
        auto concat1 = m2.add_instruction(migraphx::make_op("concat", {{"axis", 0}}), a, b);
        auto concat2 = m2.add_instruction(migraphx::make_op("concat", {{"axis", 3}}), c, d);
        auto conv    = m2.add_instruction(
1728
            migraphx::make_op("convolution", {{"padding", {1, 1}}}), input, concat1);
Paul Fultz II's avatar
Paul Fultz II committed
1729
        auto convx = m2.add_instruction(
1730
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {6}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
1731
        auto convy = m2.add_instruction(
1732
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {6}}, {"ends", {12}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
1733
1734
1735
        auto sum1 = m2.add_instruction(migraphx::make_op("add"), convx, convy);
        auto dot  = m2.add_instruction(migraphx::make_op("dot"), input, concat2);
        auto dotx = m2.add_instruction(
1736
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {0}}, {"ends", {64}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
1737
        auto doty = m2.add_instruction(
1738
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {64}}, {"ends", {128}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
1739
1740
        auto sum2    = m2.add_instruction(migraphx::make_op("add"), dotx, doty);
        auto sqdiffx = m2.add_instruction(migraphx::make_op("sqdiff"), input, e);
1741
        auto sum3    = sqdiffx;
Paul Fultz II's avatar
Paul Fultz II committed
1742
1743
1744
        auto sum4    = m2.add_instruction(migraphx::make_op("add"), sum1, sum2);
        auto sum5    = m2.add_instruction(migraphx::make_op("add"), sum4, sum3);
        m2.add_instruction(pass_op{}, sum5);
1745
    }
Paul Fultz II's avatar
Paul Fultz II committed
1746
    EXPECT(m1.sort() == m2.sort());
1747
1748
}

1749
TEST_CASE(simplify_conv_horiz_grouped_extra2)
1750
1751
1752
1753
{
    auto s   = migraphx::shape{migraphx::shape::int32_type, {8, 6, 64, 64}};
    auto ws1 = migraphx::shape{migraphx::shape::int32_type, {6, 6, 3, 3}};
    auto ws2 = migraphx::shape{migraphx::shape::int32_type, {8, 6, 64, 64}};
Paul Fultz II's avatar
Paul Fultz II committed
1754
1755
1756
1757
1758
1759
1760
1761
1762
    migraphx::module m1;
    {
        auto input = m1.add_parameter("input", s);
        auto a     = m1.add_literal(migraphx::generate_literal(ws1, 0));
        auto b     = m1.add_literal(migraphx::generate_literal(ws1, 1));
        auto c     = m1.add_literal(migraphx::generate_literal(ws2, 2));
        auto d     = m1.add_literal(migraphx::generate_literal(ws2, 3));
        auto e     = m1.add_literal(migraphx::generate_literal(s, 4));
        auto f     = m1.add_literal(migraphx::generate_literal(s, 5));
1763
        auto convx =
Paul Fultz II's avatar
Paul Fultz II committed
1764
            m1.add_instruction(migraphx::make_op("convolution", {{"padding", {1, 1}}}), input, a);
1765
        auto convy =
Paul Fultz II's avatar
Paul Fultz II committed
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
            m1.add_instruction(migraphx::make_op("convolution", {{"padding", {1, 1}}}), input, b);
        auto dotx    = m1.add_instruction(migraphx::make_op("dot"), input, c);
        auto doty    = m1.add_instruction(migraphx::make_op("dot"), input, d);
        auto sqdiffx = m1.add_instruction(migraphx::make_op("sqdiff"), input, e);
        auto sqdiffy = m1.add_instruction(migraphx::make_op("sqdiff"), input, f);
        auto sum1    = m1.add_instruction(migraphx::make_op("add"), convx, convy);
        auto sum2    = m1.add_instruction(migraphx::make_op("add"), dotx, doty);
        auto sum3    = m1.add_instruction(migraphx::make_op("add"), sqdiffx, sqdiffy);
        auto sum4    = m1.add_instruction(migraphx::make_op("add"), sum1, sum2);
        auto sum5    = m1.add_instruction(migraphx::make_op("add"), sum4, sum3);
        m1.add_instruction(pass_op{}, sum5);
    }
    run_pass(m1);

    migraphx::module m2;
    {
        auto input   = m2.add_parameter("input", s);
        auto a       = m2.add_literal(migraphx::generate_literal(ws1, 0));
        auto b       = m2.add_literal(migraphx::generate_literal(ws1, 1));
        auto c       = m2.add_literal(migraphx::generate_literal(ws2, 2));
        auto d       = m2.add_literal(migraphx::generate_literal(ws2, 3));
        auto e       = m2.add_literal(migraphx::generate_literal(s, 4));
        auto f       = m2.add_literal(migraphx::generate_literal(s, 5));
        auto concat1 = m2.add_instruction(migraphx::make_op("concat", {{"axis", 0}}), a, b);
        auto concat2 = m2.add_instruction(migraphx::make_op("concat", {{"axis", 3}}), c, d);
        auto conv    = m2.add_instruction(
1792
            migraphx::make_op("convolution", {{"padding", {1, 1}}}), input, concat1);
Paul Fultz II's avatar
Paul Fultz II committed
1793
        auto convx = m2.add_instruction(
1794
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {6}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
1795
        auto convy = m2.add_instruction(
1796
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {6}}, {"ends", {12}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
1797
1798
1799
        auto sum1 = m2.add_instruction(migraphx::make_op("add"), convx, convy);
        auto dot  = m2.add_instruction(migraphx::make_op("dot"), input, concat2);
        auto dotx = m2.add_instruction(
1800
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {0}}, {"ends", {64}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
1801
        auto doty = m2.add_instruction(
1802
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {64}}, {"ends", {128}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
1803
1804
1805
1806
1807
1808
1809
1810
1811
        auto sum2    = m2.add_instruction(migraphx::make_op("add"), dotx, doty);
        auto sqdiffx = m2.add_instruction(migraphx::make_op("sqdiff"), input, e);
        auto sqdiffy = m2.add_instruction(migraphx::make_op("sqdiff"), input, f);
        auto sum3    = m2.add_instruction(migraphx::make_op("add"), sqdiffx, sqdiffy);
        auto sum4    = m2.add_instruction(migraphx::make_op("add"), sum1, sum2);
        auto sum5    = m2.add_instruction(migraphx::make_op("add"), sum4, sum3);
        m2.add_instruction(pass_op{}, sum5);
    }
    EXPECT(m1.sort() == m2.sort());
1812
1813
}

1814
1815
TEST_CASE(simplify_mul_slice_conv_horiz_fusion)
{
Paul Fultz II's avatar
Paul Fultz II committed
1816
    migraphx::module m1;
1817
    {
Paul Fultz II's avatar
Paul Fultz II committed
1818
1819
        auto x = m1.add_parameter("x", {migraphx::shape::int32_type, {1, 1024, 17, 17}});
        auto w = m1.add_literal(
1820
            migraphx::generate_literal({migraphx::shape::int32_type, {768, 1024, 1, 1}}));
Paul Fultz II's avatar
Paul Fultz II committed
1821
1822
        auto conv   = m1.add_instruction(migraphx::make_op("convolution"), x, w);
        auto slice1 = m1.add_instruction(
1823
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {384}}}), conv);
1824
        auto a1 =
Paul Fultz II's avatar
Paul Fultz II committed
1825
1826
            m1.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {384}}, 1));
        auto b1 = m1.add_instruction(
1827
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 384, 17, 17}}}), a1);
Paul Fultz II's avatar
Paul Fultz II committed
1828
        auto mul = m1.add_instruction(migraphx::make_op("mul"), slice1, b1);
1829
        auto a2 =
Paul Fultz II's avatar
Paul Fultz II committed
1830
1831
            m1.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {384}}, 2));
        auto b2 = m1.add_instruction(
1832
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 384, 17, 17}}}), a2);
Paul Fultz II's avatar
Paul Fultz II committed
1833
        auto add1 = m1.add_instruction(migraphx::make_op("add"), mul, b2);
1834
        auto a3 =
Paul Fultz II's avatar
Paul Fultz II committed
1835
1836
            m1.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {384}}, 3));
        auto b3 = m1.add_instruction(
1837
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 384, 17, 17}}}), a3);
Paul Fultz II's avatar
Paul Fultz II committed
1838
        auto slice2 = m1.add_instruction(
1839
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {384}}, {"ends", {768}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
1840
1841
        auto add2 = m1.add_instruction(migraphx::make_op("add"), slice2, b3);
        m1.add_instruction(pass_op{}, add1, add2);
1842
    }
Paul Fultz II's avatar
Paul Fultz II committed
1843
    run_pass(m1);
1844

Paul Fultz II's avatar
Paul Fultz II committed
1845
    migraphx::module m2;
1846
    {
Paul Fultz II's avatar
Paul Fultz II committed
1847
1848
        auto x = m2.add_parameter("x", {migraphx::shape::int32_type, {1, 1024, 17, 17}});
        auto w = m2.add_literal(
1849
            migraphx::generate_literal({migraphx::shape::int32_type, {768, 1024, 1, 1}}));
Paul Fultz II's avatar
Paul Fultz II committed
1850
        auto wslice1 = m2.add_instruction(
1851
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {384}}}), w);
1852
        auto a1 =
Paul Fultz II's avatar
Paul Fultz II committed
1853
1854
            m2.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {384}}, 1));
        auto b1 = m2.add_instruction(
1855
            migraphx::make_op("broadcast", {{"axis", 0}, {"out_lens", {384, 1024, 1, 1}}}), a1);
Paul Fultz II's avatar
Paul Fultz II committed
1856
1857
        auto mul     = m2.add_instruction(migraphx::make_op("mul"), b1, wslice1);
        auto wslice2 = m2.add_instruction(
1858
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {384}}, {"ends", {768}}}), w);
Paul Fultz II's avatar
Paul Fultz II committed
1859
1860
        auto concat1 = m2.add_instruction(migraphx::make_op("concat", {{"axis", 0}}), mul, wslice2);
        auto conv    = m2.add_instruction(migraphx::make_op("convolution"), x, concat1);
1861
        auto a2 =
Paul Fultz II's avatar
Paul Fultz II committed
1862
            m2.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {384}}, 2));
1863
        auto a3 =
Paul Fultz II's avatar
Paul Fultz II committed
1864
1865
1866
            m2.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {384}}, 3));
        auto concat2 = m2.add_instruction(migraphx::make_op("concat"), a2, a3);
        auto b4      = m2.add_instruction(
1867
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 768, 17, 17}}}), concat2);
Paul Fultz II's avatar
Paul Fultz II committed
1868
1869
        auto add    = m2.add_instruction(migraphx::make_op("add"), conv, b4);
        auto slice1 = m2.add_instruction(
1870
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {384}}}), add);
Paul Fultz II's avatar
Paul Fultz II committed
1871
        auto slice2 = m2.add_instruction(
1872
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {384}}, {"ends", {768}}}), add);
Paul Fultz II's avatar
Paul Fultz II committed
1873
        m2.add_instruction(pass_op{}, slice1, slice2);
1874
    }
Paul Fultz II's avatar
Paul Fultz II committed
1875
    EXPECT(m1.sort() == m2.sort());
1876
}
1877
1878
1879

template <std::size_t BS, bool TransposeInput>
void reorder_reshape_slice()
1880
1881
1882
{
    std::vector<int64_t> perm0 = {0, 2, 1, 3};
    std::vector<int64_t> perm1 = {0, 2, 3, 1};
1883
1884
1885
1886
1887
1888
1889
    migraphx::module m1;
    {
        auto s = migraphx::shape{migraphx::shape::float_type, {BS, 128, 1920}};
        if(TransposeInput)
        {
            s = migraphx::shape{migraphx::shape::float_type, {BS, 128, 1920}, {165120, 1, 128}};
        }
Paul Fultz II's avatar
Paul Fultz II committed
1890
1891
        auto input = m1.add_parameter("input", s);
        auto slc0  = m1.add_instruction(
1892
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {640}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1893
        auto slc1 = m1.add_instruction(
1894
1895
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {640}}, {"ends", {1280}}}),
            input);
Paul Fultz II's avatar
Paul Fultz II committed
1896
        auto slc2 = m1.add_instruction(
1897
1898
1899
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {1280}}, {"ends", {1920}}}),
            input);

Paul Fultz II's avatar
Paul Fultz II committed
1900
1901
1902
        auto c0 = m1.add_instruction(migraphx::make_op("contiguous"), slc0);
        auto c1 = m1.add_instruction(migraphx::make_op("contiguous"), slc1);
        auto c2 = m1.add_instruction(migraphx::make_op("contiguous"), slc2);
1903

1904
        std::vector<int64_t> lens = {static_cast<int64_t>(BS), 128, 10, 64};
Paul Fultz II's avatar
Paul Fultz II committed
1905
1906
1907
        auto r0 = m1.add_instruction(migraphx::make_op("reshape", {{"dims", lens}}), c0);
        auto r1 = m1.add_instruction(migraphx::make_op("reshape", {{"dims", lens}}), c1);
        auto r2 = m1.add_instruction(migraphx::make_op("reshape", {{"dims", lens}}), c2);
1908

1909
1910
1911
        auto t0 = m1.add_instruction(migraphx::make_op("transpose", {{"permutation", perm0}}), r0);
        auto t1 = m1.add_instruction(migraphx::make_op("transpose", {{"permutation", perm0}}), r1);
        auto t2 = m1.add_instruction(migraphx::make_op("transpose", {{"permutation", perm1}}), r2);
1912

Paul Fultz II's avatar
Paul Fultz II committed
1913
1914
1915
        auto sum = m1.add_instruction(migraphx::make_op("add"), t0, t1);
        auto ret = m1.add_instruction(migraphx::make_op("dot"), sum, t2);
        m1.add_return({ret});
1916
1917
    };

1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
    migraphx::module m2;
    {
        auto s = migraphx::shape{migraphx::shape::float_type, {BS, 128, 1920}};
        if(TransposeInput)
        {
            s = migraphx::shape{migraphx::shape::float_type, {BS, 128, 1920}, {165120, 1, 128}};
        }
        auto input     = m2.add_parameter("input", s);
        auto rsp_input = input;
        if(TransposeInput)
        {
            rsp_input = m2.add_instruction(migraphx::make_op("contiguous"), {input});
        }
        std::vector<int64_t> lens = {static_cast<int64_t>(BS), 128, 30, 64};
        auto r = m2.add_instruction(migraphx::make_op("reshape", {{"dims", lens}}), rsp_input);
1933

Paul Fultz II's avatar
Paul Fultz II committed
1934
        auto slc0 = m2.add_instruction(
1935
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {10}}}), r);
Paul Fultz II's avatar
Paul Fultz II committed
1936
        auto slc1 = m2.add_instruction(
1937
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {10}}, {"ends", {20}}}), r);
Paul Fultz II's avatar
Paul Fultz II committed
1938
        auto slc2 = m2.add_instruction(
1939
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {20}}, {"ends", {30}}}), r);
1940

1941
1942
1943
1944
1945
1946
        auto t0 =
            m2.add_instruction(migraphx::make_op("transpose", {{"permutation", perm0}}), slc0);
        auto t1 =
            m2.add_instruction(migraphx::make_op("transpose", {{"permutation", perm0}}), slc1);
        auto t2 =
            m2.add_instruction(migraphx::make_op("transpose", {{"permutation", perm1}}), slc2);
1947

Paul Fultz II's avatar
Paul Fultz II committed
1948
1949
1950
        auto sum = m2.add_instruction(migraphx::make_op("add"), t0, t1);
        auto ret = m2.add_instruction(migraphx::make_op("dot"), sum, t2);
        m2.add_return({ret});
1951
    };
1952
1953
    run_pass(m1);
    EXPECT(m1.sort() == m2.sort());
1954
1955
}

1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
TEST_CASE_REGISTER(reorder_reshape_slice<1, true>); // test if contiguous is added as necessary if
                                                    // input is transposed
TEST_CASE_REGISTER(reorder_reshape_slice<4, true>);
TEST_CASE_REGISTER(reorder_reshape_slice<8, true>);
TEST_CASE_REGISTER(reorder_reshape_slice<1, false>);
TEST_CASE_REGISTER(reorder_reshape_slice<4, false>);
TEST_CASE_REGISTER(reorder_reshape_slice<8, false>);

template <std::size_t BS>
void reorder_reshape_slice_move_axis1()
1966
{
1967
1968
1969
    migraphx::module m1;
    {
        auto s                     = migraphx::shape{migraphx::shape::float_type, {BS, 256, 96}};
1970
1971
        std::vector<int64_t> perm0 = {0, 2, 1, 3};
        std::vector<int64_t> perm1 = {0, 2, 3, 1};
Paul Fultz II's avatar
Paul Fultz II committed
1972
1973
        auto input                 = m1.add_parameter("input", s);
        auto slc0                  = m1.add_instruction(
1974
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {32}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1975
        auto slc1 = m1.add_instruction(
1976
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {32}}, {"ends", {64}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1977
        auto slc2 = m1.add_instruction(
1978
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {64}}, {"ends", {96}}}), input);
1979

Paul Fultz II's avatar
Paul Fultz II committed
1980
1981
1982
        auto c0 = m1.add_instruction(migraphx::make_op("contiguous"), slc0);
        auto c1 = m1.add_instruction(migraphx::make_op("contiguous"), slc1);
        auto c2 = m1.add_instruction(migraphx::make_op("contiguous"), slc2);
1983

1984
        std::vector<int64_t> lens = {static_cast<int64_t>(BS), 64, 4, 32};
Paul Fultz II's avatar
Paul Fultz II committed
1985
1986
1987
        auto r0 = m1.add_instruction(migraphx::make_op("reshape", {{"dims", lens}}), c0);
        auto r1 = m1.add_instruction(migraphx::make_op("reshape", {{"dims", lens}}), c1);
        auto r2 = m1.add_instruction(migraphx::make_op("reshape", {{"dims", lens}}), c2);
1988

1989
1990
1991
        auto t0 = m1.add_instruction(migraphx::make_op("transpose", {{"permutation", perm0}}), r0);
        auto t1 = m1.add_instruction(migraphx::make_op("transpose", {{"permutation", perm0}}), r1);
        auto t2 = m1.add_instruction(migraphx::make_op("transpose", {{"permutation", perm1}}), r2);
1992

Paul Fultz II's avatar
Paul Fultz II committed
1993
1994
1995
        auto sum = m1.add_instruction(migraphx::make_op("add"), t0, t1);
        auto ret = m1.add_instruction(migraphx::make_op("dot"), sum, t2);
        m1.add_return({ret});
1996
1997
    };

1998
1999
2000
    migraphx::module m2;
    {
        auto s                     = migraphx::shape{migraphx::shape::float_type, {BS, 256, 96}};
2001
2002
        std::vector<int64_t> perm0 = {0, 2, 1, 3};
        std::vector<int64_t> perm1 = {0, 2, 3, 1};
2003
2004
2005
2006
        auto input                 = m2.add_parameter("input", s);
        std::vector<int64_t> lens  = {static_cast<int64_t>(BS), 64, 4, 96};
        auto rsp  = m2.add_instruction(migraphx::make_op("reshape", {{"dims", lens}}), input);
        auto slc0 = m2.add_instruction(
2007
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {0}}, {"ends", {32}}}), rsp);
2008
2009
2010
        auto t0 =
            m2.add_instruction(migraphx::make_op("transpose", {{"permutation", perm0}}), slc0);
        auto slc1 = m2.add_instruction(
2011
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {32}}, {"ends", {64}}}), rsp);
2012
2013
2014
        auto t1 =
            m2.add_instruction(migraphx::make_op("transpose", {{"permutation", perm0}}), slc1);
        auto slc2 = m2.add_instruction(
2015
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {64}}, {"ends", {96}}}), rsp);
2016
2017
        auto t2 =
            m2.add_instruction(migraphx::make_op("transpose", {{"permutation", perm1}}), slc2);
2018

2019
2020
2021
        auto sum = m2.add_instruction(migraphx::make_op("add"), t0, t1);
        auto ret = m2.add_instruction(migraphx::make_op("dot"), sum, t2);
        m2.add_return({ret});
2022
2023
    };

2024
2025
    run_pass(m1);
    EXPECT(m1.sort() == m2.sort());
2026
2027
}

2028
2029
2030
TEST_CASE_REGISTER(reorder_reshape_slice_move_axis1<4>);
TEST_CASE_REGISTER(reorder_reshape_slice_move_axis1<8>);

2031
2032
TEST_CASE(reorder_reshape_slice_move_axis2)
{
2033
2034
    migraphx::module m1;
    {
2035
        migraphx::shape s{migraphx::shape::float_type, {128, 96}};
Paul Fultz II's avatar
Paul Fultz II committed
2036
2037
        auto input = m1.add_parameter("input", s);
        auto slc0  = m1.add_instruction(
2038
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {32}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2039
        auto slc1 = m1.add_instruction(
2040
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {32}}, {"ends", {64}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2041
        auto slc2 = m1.add_instruction(
2042
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {64}}, {"ends", {96}}}), input);
2043

Paul Fultz II's avatar
Paul Fultz II committed
2044
2045
2046
        auto c0 = m1.add_instruction(migraphx::make_op("contiguous"), slc0);
        auto c1 = m1.add_instruction(migraphx::make_op("contiguous"), slc1);
        auto c2 = m1.add_instruction(migraphx::make_op("contiguous"), slc2);
2047
2048

        std::vector<int64_t> lens = {1, 16, 8, 32};
Paul Fultz II's avatar
Paul Fultz II committed
2049
2050
2051
        auto r0 = m1.add_instruction(migraphx::make_op("reshape", {{"dims", lens}}), c0);
        auto r1 = m1.add_instruction(migraphx::make_op("reshape", {{"dims", lens}}), c1);
        auto r2 = m1.add_instruction(migraphx::make_op("reshape", {{"dims", lens}}), c2);
2052

Paul Fultz II's avatar
Paul Fultz II committed
2053
2054
2055
        auto sum = m1.add_instruction(migraphx::make_op("add"), r0, r1);
        auto ret = m1.add_instruction(migraphx::make_op("mul"), sum, r2);
        m1.add_return({ret});
2056
2057
    };

2058
2059
    migraphx::module m2;
    {
2060
        auto s                    = migraphx::shape{migraphx::shape::float_type, {128, 96}};
2061
        auto input                = m2.add_parameter("input", s);
2062
        std::vector<int64_t> lens = {1, 16, 8, 96};
2063
2064
        auto rsp  = m2.add_instruction(migraphx::make_op("reshape", {{"dims", lens}}), input);
        auto slc0 = m2.add_instruction(
2065
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {0}}, {"ends", {32}}}), rsp);
2066
        auto slc1 = m2.add_instruction(
2067
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {32}}, {"ends", {64}}}), rsp);
2068
        auto slc2 = m2.add_instruction(
2069
2070
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {64}}, {"ends", {96}}}), rsp);

2071
2072
2073
        auto sum = m2.add_instruction(migraphx::make_op("add"), slc0, slc1);
        auto ret = m2.add_instruction(migraphx::make_op("mul"), sum, slc2);
        m2.add_return({ret});
2074
2075
    };

Paul Fultz II's avatar
Paul Fultz II committed
2076
2077
    run_pass(m1);
    EXPECT(m1.sort() == m2.sort());
2078
2079
2080
2081
2082
}

TEST_CASE(reorder_reshape_slice_not_apply)
{
    auto create_p = [] {
Paul Fultz II's avatar
Paul Fultz II committed
2083
        migraphx::module m;
2084
        migraphx::shape s{migraphx::shape::float_type, {128, 96}};
Paul Fultz II's avatar
Paul Fultz II committed
2085
2086
        auto input = m.add_parameter("input", s);
        auto slc0  = m.add_instruction(
2087
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {32}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2088
        auto slc1 = m.add_instruction(
2089
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {32}}, {"ends", {64}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2090
        auto slc2 = m.add_instruction(
2091
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {64}}, {"ends", {96}}}), input);
2092

Paul Fultz II's avatar
Paul Fultz II committed
2093
2094
2095
        auto c0 = m.add_instruction(migraphx::make_op("contiguous"), slc0);
        auto c1 = m.add_instruction(migraphx::make_op("contiguous"), slc1);
        auto c2 = m.add_instruction(migraphx::make_op("contiguous"), slc2);
2096
2097

        std::vector<int64_t> lens = {1, 16, 16, 16};
Paul Fultz II's avatar
Paul Fultz II committed
2098
2099
2100
        auto r0 = m.add_instruction(migraphx::make_op("reshape", {{"dims", lens}}), c0);
        auto r1 = m.add_instruction(migraphx::make_op("reshape", {{"dims", lens}}), c1);
        auto r2 = m.add_instruction(migraphx::make_op("reshape", {{"dims", lens}}), c2);
2101

Paul Fultz II's avatar
Paul Fultz II committed
2102
2103
2104
        auto sum = m.add_instruction(migraphx::make_op("add"), r0, r1);
        auto ret = m.add_instruction(migraphx::make_op("mul"), sum, r2);
        m.add_return({ret});
2105

Paul Fultz II's avatar
Paul Fultz II committed
2106
        return m;
2107
2108
    };

Paul Fultz II's avatar
Paul Fultz II committed
2109
2110
2111
2112
    auto m1 = create_p();
    auto m2 = m1;
    run_pass(m1);
    EXPECT(m1.sort() == m2.sort());
2113
2114
}

2115
2116
template <std::size_t BS>
void reorder_reshape_slice_diff_dims()
2117
{
2118
2119
2120
2121
2122
    migraphx::module m1;
    {
        auto s     = migraphx::shape{migraphx::shape::float_type, {BS, 96, 96}};
        auto input = m1.add_parameter("input", s);
        auto slc0  = m1.add_instruction(
2123
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {32}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2124
        auto slc1 = m1.add_instruction(
2125
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {32}}, {"ends", {64}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2126
        auto slc2 = m1.add_instruction(
2127
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {64}}, {"ends", {96}}}), input);
2128

Paul Fultz II's avatar
Paul Fultz II committed
2129
2130
2131
        auto c0 = m1.add_instruction(migraphx::make_op("contiguous"), slc0);
        auto c1 = m1.add_instruction(migraphx::make_op("contiguous"), slc1);
        auto c2 = m1.add_instruction(migraphx::make_op("contiguous"), slc2);
2132

2133
2134
        std::vector<int64_t> lens  = {static_cast<int64_t>(BS), 32, 3, 32};
        std::vector<int64_t> lens1 = {static_cast<int64_t>(BS), 48, 2, 32};
Paul Fultz II's avatar
Paul Fultz II committed
2135
2136
2137
        auto r0 = m1.add_instruction(migraphx::make_op("reshape", {{"dims", lens}}), c0);
        auto r1 = m1.add_instruction(migraphx::make_op("reshape", {{"dims", lens}}), c1);
        auto r2 = m1.add_instruction(migraphx::make_op("reshape", {{"dims", lens1}}), c2);
2138

Paul Fultz II's avatar
Paul Fultz II committed
2139
        m1.add_return({r0, r1, r2});
2140
2141
    };

2142
2143
2144
    auto m2 = m1;
    run_pass(m1);
    EXPECT(m1.sort() == m2.sort());
2145
2146
}

2147
2148
2149
2150
2151
TEST_CASE_REGISTER(reorder_reshape_slice_diff_dims<4>);
TEST_CASE_REGISTER(reorder_reshape_slice_diff_dims<8>);

template <std::size_t BS>
void reorder_slice_trans()
2152
2153
{
    std::vector<int64_t> perm = {0, 2, 1};
2154
2155
2156
2157

    migraphx::module m1;
    {
        auto s     = migraphx::shape{migraphx::shape::float_type, {BS, 128, 1920}};
Paul Fultz II's avatar
Paul Fultz II committed
2158
2159
        auto input = m1.add_parameter("input", s);
        auto slc0  = m1.add_instruction(
2160
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {640}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2161
        auto slc1 = m1.add_instruction(
2162
2163
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {640}}, {"ends", {1280}}}),
            input);
Paul Fultz II's avatar
Paul Fultz II committed
2164
        auto slc2 = m1.add_instruction(
2165
2166
2167
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {1280}}, {"ends", {1920}}}),
            input);

2168
2169
2170
        auto t0 = m1.add_instruction(migraphx::make_op("transpose", {{"permutation", perm}}), slc0);
        auto t1 = m1.add_instruction(migraphx::make_op("transpose", {{"permutation", perm}}), slc1);
        auto t2 = m1.add_instruction(migraphx::make_op("transpose", {{"permutation", perm}}), slc2);
2171

Paul Fultz II's avatar
Paul Fultz II committed
2172
2173
2174
        auto sum = m1.add_instruction(migraphx::make_op("add"), t0, t1);
        auto ret = m1.add_instruction(migraphx::make_op("mul"), sum, t2);
        m1.add_return({ret});
2175
2176
    };

2177
2178
2179
    migraphx::module m2;
    {
        auto s     = migraphx::shape{migraphx::shape::float_type, {BS, 128, 1920}};
Paul Fultz II's avatar
Paul Fultz II committed
2180
        auto input = m2.add_parameter("input", s);
2181
        auto r = m2.add_instruction(migraphx::make_op("transpose", {{"permutation", perm}}), input);
2182

Paul Fultz II's avatar
Paul Fultz II committed
2183
        auto slc0 = m2.add_instruction(
2184
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {640}}}), r);
Paul Fultz II's avatar
Paul Fultz II committed
2185
        auto slc1 = m2.add_instruction(
2186
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {640}}, {"ends", {1280}}}), r);
Paul Fultz II's avatar
Paul Fultz II committed
2187
        auto slc2 = m2.add_instruction(
2188
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1280}}, {"ends", {1920}}}), r);
2189

Paul Fultz II's avatar
Paul Fultz II committed
2190
2191
2192
        auto sum = m2.add_instruction(migraphx::make_op("add"), slc0, slc1);
        auto ret = m2.add_instruction(migraphx::make_op("mul"), sum, slc2);
        m2.add_return({ret});
2193
2194
    };

2195
2196
    run_pass(m1);
    EXPECT(m1.sort() == m2.sort());
2197
2198
}

2199
2200
2201
2202
2203
TEST_CASE_REGISTER(reorder_slice_trans<1>);
TEST_CASE_REGISTER(reorder_slice_trans<8>);

template <std::size_t BS>
void reorder_slice_trans_diff_perm()
2204
{
2205
2206
2207
    migraphx::module m1;
    {
        auto s                     = migraphx::shape{migraphx::shape::float_type, {BS, 128, 1920}};
2208
2209
        std::vector<int64_t> perm0 = {0, 2, 1};
        std::vector<int64_t> perm1 = {0, 1, 2};
Paul Fultz II's avatar
Paul Fultz II committed
2210
2211
        auto input                 = m1.add_parameter("input", s);
        auto slc0                  = m1.add_instruction(
2212
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {640}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2213
        auto slc1 = m1.add_instruction(
2214
2215
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {640}}, {"ends", {1280}}}),
            input);
Paul Fultz II's avatar
Paul Fultz II committed
2216
        auto slc2 = m1.add_instruction(
2217
2218
2219
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {1280}}, {"ends", {1920}}}),
            input);

2220
2221
2222
2223
2224
2225
        auto t0 =
            m1.add_instruction(migraphx::make_op("transpose", {{"permutation", perm0}}), slc0);
        auto t1 =
            m1.add_instruction(migraphx::make_op("transpose", {{"permutation", perm0}}), slc1);
        auto t2 =
            m1.add_instruction(migraphx::make_op("transpose", {{"permutation", perm1}}), slc2);
2226

Paul Fultz II's avatar
Paul Fultz II committed
2227
2228
2229
        auto sum = m1.add_instruction(migraphx::make_op("add"), t0, t1);
        auto ret = m1.add_instruction(migraphx::make_op("dot"), sum, t2);
        m1.add_return({ret});
2230
2231
    };

2232
2233
2234
    run_pass(m1);
    auto m2 = m1;
    EXPECT(m1.sort() == m2.sort());
2235
2236
}

2237
2238
2239
TEST_CASE_REGISTER(reorder_slice_trans_diff_perm<1>);
TEST_CASE_REGISTER(reorder_slice_trans_diff_perm<4>);

Shucai Xiao's avatar
Shucai Xiao committed
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
TEST_CASE(reorder_slice_ins_deps)
{
    auto create_module = [] {
        migraphx::module m;
        migraphx::shape sx{migraphx::shape::float_type, {4, 2}};
        migraphx::shape sy{migraphx::shape::float_type, {2, 2}};
        std::vector<float> datax = {0, 1, 2, 3, 4, 5, 6, 7};
        std::vector<float> datay = {0, 1, 2, 3};
        auto inx                 = m.add_literal(migraphx::literal(sx, datax));
        auto iny                 = m.add_literal(migraphx::literal(sy, datay));
        auto slc0                = m.add_instruction(
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {2}}}), inx);
        auto slc1 = m.add_instruction(
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {2}}, {"ends", {4}}}), inx);
        auto n0 = m.add_instruction(migraphx::make_op("neg"), slc0);
        auto a0 = m.add_instruction(migraphx::make_op("add"), n0, slc1);
        auto m0 = m.add_instruction(migraphx::make_op("mul"), a0, iny);
        auto r  = m.add_instruction(migraphx::make_op("add"), m0, slc0);
        m.add_return({r});

        return m;
    };

    auto m = create_module();
    run_pass(m);
    EXPECT(m == create_module());
}

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