simplify_algebra_test.cpp 100 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
33
#include <migraphx/make_op.hpp>

Paul's avatar
Paul committed
34
35
#include <test.hpp>

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

Paul's avatar
Paul committed
41
TEST_CASE(simplify_add1)
Paul's avatar
Paul committed
42
{
Paul Fultz II's avatar
Paul Fultz II committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
    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
68
69
}

Paul's avatar
Paul committed
70
TEST_CASE(simplify_add2)
Paul's avatar
Paul committed
71
{
Paul Fultz II's avatar
Paul Fultz II committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
    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
97
98
}

Paul's avatar
Paul committed
99
TEST_CASE(simplify_add3)
Paul's avatar
Paul committed
100
{
Paul Fultz II's avatar
Paul Fultz II committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
    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
124
125
}

126
127
128
129
130
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
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
158
    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);
159
160
161
162
163
164
165
166
}

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
167
168
169
170
171
172
173
174
175
176
177
        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;
178
    };
Paul Fultz II's avatar
Paul Fultz II committed
179
180
    migraphx::module m1 = create_program();
    run_pass(m1);
181

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

Paul's avatar
Paul committed
186
// TODO: Add test case
187
// TEST_CASE(simplify_add4)
Paul's avatar
Paul committed
188
189
void simplify_add4()
{
Paul Fultz II's avatar
Paul Fultz II committed
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
    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
215
216
}

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

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

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

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

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

Paul's avatar
Paul committed
335
336
TEST_CASE(simplify_mul_add)
{
Paul Fultz II's avatar
Paul Fultz II committed
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
    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
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
387
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
388
389
{
    auto b = migraphx::op::broadcast{1, {2, 1, 4, 5}};
Paul Fultz II's avatar
Paul Fultz II committed
390
    migraphx::module m1;
Paul's avatar
Paul committed
391
    {
Paul Fultz II's avatar
Paul Fultz II committed
392
393
394
395
396
397
        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
398
    }
Paul Fultz II's avatar
Paul Fultz II committed
399
    run_pass(m1);
Paul's avatar
Paul committed
400

Paul Fultz II's avatar
Paul Fultz II committed
401
    migraphx::module m2;
Paul's avatar
Paul committed
402
    {
Paul Fultz II's avatar
Paul Fultz II committed
403
404
405
406
407
        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
408
    }
Paul Fultz II's avatar
Paul Fultz II committed
409
    EXPECT(m1 == m2);
Paul's avatar
Paul committed
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
436
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);
}

437
438
TEST_CASE(simplify_add_conv1)
{
Paul Fultz II's avatar
Paul Fultz II committed
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
    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);
455
456
457
458
}

TEST_CASE(simplify_add_conv_no_fusion_7x7_diff_strides)
{
Paul Fultz II's avatar
Paul Fultz II committed
459
460
461
462
463
464
465
466
467
    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(
468
        migraphx::make_op("convolution", {{"padding", {0, 0}}, {"stride", {3, 3}}}), y, v);
Paul Fultz II's avatar
Paul Fultz II committed
469
470
471
472
473
    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());
474
    // No fusion
Paul Fultz II's avatar
Paul Fultz II committed
475
476
    EXPECT(std::count_if(
               m.begin(), m.end(), [](auto&& ins) { return ins.name() == "convolution"; }) == 2);
477
478
479
480
}

TEST_CASE(simplify_add_conv_1x1_diff_strides1)
{
Paul Fultz II's avatar
Paul Fultz II committed
481
482
483
484
485
486
487
488
489
    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(
490
        migraphx::make_op("convolution", {{"padding", {0, 0}}, {"stride", {2, 2}}}), y, v);
Paul Fultz II's avatar
Paul Fultz II committed
491
492
493
494
495
496
497
    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);
498
499
500
501
}

TEST_CASE(simplify_add_conv_1x1_diff_strides2)
{
Paul Fultz II's avatar
Paul Fultz II committed
502
503
504
505
506
507
508
509
    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(
510
        migraphx::make_op("convolution", {{"padding", {0, 0}}, {"stride", {2, 2}}}), x, w);
Paul Fultz II's avatar
Paul Fultz II committed
511
512
513
514
515
516
517
518
    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);
519
520
521
522
}

TEST_CASE(simplify_add_conv_1x1_diff_strides_odd)
{
Paul Fultz II's avatar
Paul Fultz II committed
523
524
    migraphx::module m;
    auto x = m.add_parameter("x", {migraphx::shape::float_type, {1, 54, 83, 83}});
525
    auto w =
Paul Fultz II's avatar
Paul Fultz II committed
526
527
        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}});
528
    auto v =
Paul Fultz II's avatar
Paul Fultz II committed
529
530
531
        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(
532
        migraphx::make_op("convolution", {{"padding", {0, 0}}, {"stride", {2, 2}}}), y, v);
Paul Fultz II's avatar
Paul Fultz II committed
533
534
535
536
537
538
539
    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);
540
541
542
543
}

TEST_CASE(simplify_add_conv_no_fusion_asymetrical_strides1)
{
Paul Fultz II's avatar
Paul Fultz II committed
544
545
546
547
548
549
550
551
    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(
552
        migraphx::make_op("convolution", {{"padding", {0, 0}}, {"stride", {2, 1}}}), x, w);
Paul Fultz II's avatar
Paul Fultz II committed
553
554
555
556
557
558
    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());
559
    // No fusion
Paul Fultz II's avatar
Paul Fultz II committed
560
561
    EXPECT(std::count_if(
               m.begin(), m.end(), [](auto&& ins) { return ins.name() == "convolution"; }) == 2);
562
563
564
565
}

TEST_CASE(simplify_add_conv_no_fusion_asymetrical_strides2)
{
Paul Fultz II's avatar
Paul Fultz II committed
566
567
568
569
570
571
572
573
574
    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(
575
        migraphx::make_op("convolution", {{"padding", {0, 0}}, {"stride", {2, 1}}}), y, v);
Paul Fultz II's avatar
Paul Fultz II committed
576
577
578
579
580
    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());
581
    // No fusion
Paul Fultz II's avatar
Paul Fultz II committed
582
583
    EXPECT(std::count_if(
               m.begin(), m.end(), [](auto&& ins) { return ins.name() == "convolution"; }) == 2);
584
585
}

586
587
588
TEST_CASE(simplify_concat_add_relu)
{
    auto s = migraphx::shape{migraphx::shape::int32_type, {1}};
Paul Fultz II's avatar
Paul Fultz II committed
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
616
    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);
617
618
}

619
620
621
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
622
623
624
625
626
627
628
629
630
631
632
    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);
633
        auto concat =
Paul Fultz II's avatar
Paul Fultz II committed
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
            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());
654
655
656
657
658
}

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
659
    migraphx::module m1;
660
    {
661
        auto b    = migraphx::op::broadcast{1, {2, 1, 4, 5}};
Paul Fultz II's avatar
Paul Fultz II committed
662
663
664
665
666
667
668
        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);
669
        auto concat =
Paul Fultz II's avatar
Paul Fultz II committed
670
671
            m1.add_instruction(migraphx::make_op("concat", {{"axis", 1}}), sum, oneb, twob);
        m1.add_instruction(pass_op{}, concat);
672
    }
Paul Fultz II's avatar
Paul Fultz II committed
673
    run_pass(m1);
674

Paul Fultz II's avatar
Paul Fultz II committed
675
    migraphx::module m2;
676
677
    {
        auto b       = migraphx::op::broadcast{1, {2, 2, 4, 5}};
Paul Fultz II's avatar
Paul Fultz II committed
678
679
680
681
682
683
684
685
686
687
688
        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());
689
690
}

691
692
693
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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
    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;
713
714
    {
        auto b        = migraphx::op::broadcast{1, {2, 2, 4, 5}};
Paul Fultz II's avatar
Paul Fultz II committed
715
716
717
718
719
720
721
722
723
724
725
726
        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);
727
728
729
730
731
}

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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
    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;
751
752
    {
        auto b       = migraphx::op::broadcast{1, {2, 1, 4, 5}};
Paul Fultz II's avatar
Paul Fultz II committed
753
754
755
756
757
758
759
760
761
762
763
764
765
        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);
766
767
}

768
769
TEST_CASE(simplify_div_const)
{
Paul Fultz II's avatar
Paul Fultz II committed
770
    migraphx::module m1;
771
    {
Paul Fultz II's avatar
Paul Fultz II committed
772
773
774
        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);
775
    }
Paul Fultz II's avatar
Paul Fultz II committed
776
    run_pass(m1);
777

Paul Fultz II's avatar
Paul Fultz II committed
778
    migraphx::module m2;
779
    {
Paul Fultz II's avatar
Paul Fultz II committed
780
781
782
783
        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);
784
    }
Paul Fultz II's avatar
Paul Fultz II committed
785
    EXPECT(m1 == m2);
786
787
788
789
}

TEST_CASE(simplify_sub_const)
{
Paul Fultz II's avatar
Paul Fultz II committed
790
    migraphx::module m1;
791
    {
Paul Fultz II's avatar
Paul Fultz II committed
792
793
794
        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);
795
    }
Paul Fultz II's avatar
Paul Fultz II committed
796
    run_pass(m1);
797

Paul Fultz II's avatar
Paul Fultz II committed
798
    migraphx::module m2;
799
    {
Paul Fultz II's avatar
Paul Fultz II committed
800
801
802
803
        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);
804
    }
Paul Fultz II's avatar
Paul Fultz II committed
805
    EXPECT(m1 == m2);
806
807
}

kahmed10's avatar
kahmed10 committed
808
809
TEST_CASE(simplify_rsqrt)
{
Paul Fultz II's avatar
Paul Fultz II committed
810
    migraphx::module m1;
kahmed10's avatar
kahmed10 committed
811
    {
Paul Fultz II's avatar
Paul Fultz II committed
812
813
814
        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
815
    }
Paul Fultz II's avatar
Paul Fultz II committed
816
    run_pass(m1);
kahmed10's avatar
kahmed10 committed
817

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

TEST_CASE(simplify_rsqrt_multi_use)
{
Paul Fultz II's avatar
Paul Fultz II committed
828
    migraphx::module m1;
kahmed10's avatar
kahmed10 committed
829
    {
Paul Fultz II's avatar
Paul Fultz II committed
830
831
832
833
834
        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
835
    }
Paul Fultz II's avatar
Paul Fultz II committed
836
    migraphx::module m2{m1};
kahmed10's avatar
kahmed10 committed
837

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

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

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

Paul Fultz II's avatar
Paul Fultz II committed
864
    migraphx::module m2;
865
    {
Paul Fultz II's avatar
Paul Fultz II committed
866
867
868
869
        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);
870
    }
Paul Fultz II's avatar
Paul Fultz II committed
871
    EXPECT(m1 == m2);
872
873
874
875
876
877
}

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

Paul Fultz II's avatar
Paul Fultz II committed
905
    migraphx::module m2;
906
    {
Paul Fultz II's avatar
Paul Fultz II committed
907
908
909
910
        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);
911
912
    }

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

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

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

951
952
953
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
954
    migraphx::module m1;
955
956
    {
        auto b     = migraphx::op::broadcast{1, {3, 1, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
957
958
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
959
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
960
        auto y = m1.add_instruction(
961
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
        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;
    {
977
        auto b       = migraphx::op::broadcast{1, {3, 2, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
978
979
980
981
982
983
984
985
        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(
986
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), relu);
Paul Fultz II's avatar
Paul Fultz II committed
987
        auto y = m2.add_instruction(
988
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), relu);
Paul Fultz II's avatar
Paul Fultz II committed
989
990
        auto add = m2.add_instruction(migraphx::make_op("add"), x, y);
        m2.add_instruction(pass_op{}, add);
991
    }
Paul Fultz II's avatar
Paul Fultz II committed
992
    EXPECT(m1.sort() == m2.sort());
993
994
}

Shucai Xiao's avatar
Shucai Xiao committed
995
996
997
998
999
1000
1001
1002
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
1003
        auto y = m1.add_instruction(
Shucai Xiao's avatar
Shucai Xiao committed
1004
1005
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);

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

kahmed10's avatar
kahmed10 committed
1009
1010
        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
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
1046
        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
1047
1048
        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
1049
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), rmn);
kahmed10's avatar
kahmed10 committed
1050
1051
        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
1052
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), rmx);
kahmed10's avatar
kahmed10 committed
1053
        auto slc2 = m2.add_instruction(
Shucai Xiao's avatar
Shucai Xiao committed
1054
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), rmn);
kahmed10's avatar
kahmed10 committed
1055
        auto slc3 = m2.add_instruction(
Shucai Xiao's avatar
Shucai Xiao committed
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
1086
            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
1087
        auto y    = m2.add_instruction(
Shucai Xiao's avatar
Shucai Xiao committed
1088
1089
            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
1090
1091
        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
1092
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), rms);
kahmed10's avatar
kahmed10 committed
1093
        auto slc1 = m2.add_instruction(
Shucai Xiao's avatar
Shucai Xiao committed
1094
1095
1096
1097
1098
1099
1100
1101
            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());
}

1102
1103
1104
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
1105
    migraphx::module m1;
1106
    {
1107
1108
        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
1109
1110
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
1111
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1112
        auto y = m1.add_instruction(
1113
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
        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;
    {
1131
        auto b       = migraphx::op::broadcast{1, {3, 2, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1132
1133
1134
1135
1136
1137
1138
1139
1140
        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(
1141
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {4}}}), rsp);
Paul Fultz II's avatar
Paul Fultz II committed
1142
        auto slc2 = m2.add_instruction(
1143
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {4}}, {"ends", {8}}}), rsp);
Paul Fultz II's avatar
Paul Fultz II committed
1144
1145
        auto add = m2.add_instruction(migraphx::make_op("add"), slc1, slc2);
        m2.add_instruction(pass_op{}, add);
1146
    }
Paul Fultz II's avatar
Paul Fultz II committed
1147
    EXPECT(m1.sort() == m2.sort());
1148
1149
1150
1151
1152
}

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
1153
    migraphx::module m1;
1154
    {
1155
        auto r     = migraphx::op::reshape{{3, 2, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1156
1157
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
1158
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1159
        auto y = m1.add_instruction(
1160
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1161
1162
        auto one  = m1.add_literal(1);
        auto oneb = m1.add_instruction(
1163
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {3, 1, 4, 2}}}), one);
Paul Fultz II's avatar
Paul Fultz II committed
1164
1165
        auto two  = m1.add_literal(2);
        auto twob = m1.add_instruction(
1166
            migraphx::make_op("broadcast", {{"axis", 3}, {"out_lens", {3, 2, 4, 1}}}), two);
Paul Fultz II's avatar
Paul Fultz II committed
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
        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());
1180
1181
1182
1183
1184
}

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
1185
    migraphx::module m1;
1186
1187
    {
        auto b     = migraphx::op::broadcast{1, {3, 1, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1188
1189
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
1190
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {2}}, {"ends", {3}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1191
        auto y = m1.add_instruction(
1192
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
        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());
1208
1209
1210
1211
1212
}

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
1213
    migraphx::module m1;
1214
1215
    {
        auto b     = migraphx::op::broadcast{1, {3, 1, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1216
1217
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
1218
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {2}}, {"ends", {3}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1219
        auto y = m1.add_instruction(
1220
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
        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());
1236
1237
1238
1239
1240
}

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
1241
    migraphx::module m1;
1242
1243
    {
        auto b     = migraphx::op::broadcast{1, {3, 1, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1244
1245
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
1246
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1247
        auto y = m1.add_instruction(
1248
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
        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());
1264
1265
1266
1267
1268
}

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
1269
    migraphx::module m1;
1270
    {
1271
        auto b     = migraphx::op::broadcast{1, {3, 1, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1272
1273
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
1274
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1275
        auto y = m1.add_instruction(
1276
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
        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);
1287
    }
Paul Fultz II's avatar
Paul Fultz II committed
1288
    run_pass(m1);
1289

Paul Fultz II's avatar
Paul Fultz II committed
1290
    migraphx::module m2;
1291
1292
    {
        auto b       = migraphx::op::broadcast{1, {3, 2, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
        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());
1303
1304
1305
1306
1307
}

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
1308
    migraphx::module m1;
1309
1310
    {
        auto b     = migraphx::op::broadcast{1, {3, 1, 4, 3}};
Paul Fultz II's avatar
Paul Fultz II committed
1311
1312
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
1313
1314
            migraphx::make_op("slice", {{"axes", {1, 3}}, {"starts", {0, 0}}, {"ends", {1, 3}}}),
            input);
Paul Fultz II's avatar
Paul Fultz II committed
1315
        auto y = m1.add_instruction(
1316
1317
            migraphx::make_op("slice", {{"axes", {1, 3}}, {"starts", {1, 3}}, {"ends", {2, 6}}}),
            input);
Paul Fultz II's avatar
Paul Fultz II committed
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
        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());
1332
1333
1334
1335
1336
}

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
1337
    migraphx::module m1;
1338
1339
    {
        auto b     = migraphx::op::broadcast{1, {3, 1, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1340
1341
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
1342
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1343
        auto y = m1.add_instruction(
1344
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
        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;
    {
1361
        auto b     = migraphx::op::broadcast{1, {3, 2, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1362
1363
        auto input = m2.add_parameter("input", s);
        auto slice = m2.add_instruction(
1364
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1365
1366
1367
1368
1369
1370
1371
        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(
1372
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), relu);
Paul Fultz II's avatar
Paul Fultz II committed
1373
        auto y = m2.add_instruction(
1374
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), relu);
Paul Fultz II's avatar
Paul Fultz II committed
1375
1376
1377
        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);
1378
    }
Paul Fultz II's avatar
Paul Fultz II committed
1379
    EXPECT(m1.sort() == m2.sort());
1380
1381
1382
1383
1384
}

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
1385
    migraphx::module m1;
1386
1387
    {
        auto b     = migraphx::op::broadcast{1, {3, 1, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1388
1389
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
1390
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1391
        auto y = m1.add_instruction(
1392
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
        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;
    {
1410
        auto b     = migraphx::op::broadcast{1, {3, 2, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1411
1412
        auto input = m2.add_parameter("input", s);
        auto slice = m2.add_instruction(
1413
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1414
1415
1416
1417
1418
1419
1420
1421
        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(
1422
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), relu);
Paul Fultz II's avatar
Paul Fultz II committed
1423
        auto y = m2.add_instruction(
1424
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), relu);
Paul Fultz II's avatar
Paul Fultz II committed
1425
1426
1427
        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);
1428
    }
Paul Fultz II's avatar
Paul Fultz II committed
1429
    EXPECT(m1.sort() == m2.sort());
1430
1431
1432
1433
1434
}

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
1435
    migraphx::module m1;
1436
    {
Paul Fultz II's avatar
Paul Fultz II committed
1437
1438
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
1439
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1440
        auto y = m1.add_instruction(
1441
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1442
1443
        auto sum = m1.add_instruction(migraphx::make_op("add"), x, y);
        m1.add_instruction(pass_op{}, sum);
1444
    }
Paul Fultz II's avatar
Paul Fultz II committed
1445
1446
1447
    migraphx::module m2 = m1;
    run_pass(m1);
    EXPECT(m1.sort() == m2.sort());
1448
1449
1450
1451
1452
}

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
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
    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(
1473
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {2}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
1474
        auto y = m2.add_instruction(
1475
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {2}}, {"ends", {4}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
1476
1477
        auto sum = m2.add_instruction(migraphx::make_op("add"), x, y);
        m2.add_instruction(pass_op{}, sum);
1478
    }
Paul Fultz II's avatar
Paul Fultz II committed
1479
    EXPECT(m1.sort() == m2.sort());
1480
1481
1482
1483
1484
}

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
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
    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(
1503
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {2}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
1504
        auto y = m2.add_instruction(
1505
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {2}}, {"ends", {4}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
1506
1507
        auto sum = m2.add_instruction(migraphx::make_op("add"), x, y);
        m2.add_instruction(pass_op{}, sum);
1508
    }
Paul Fultz II's avatar
Paul Fultz II committed
1509
    EXPECT(m1.sort() == m2.sort());
1510
1511
1512
1513
1514
}

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
1515
    migraphx::module m1;
1516
    {
Paul Fultz II's avatar
Paul Fultz II committed
1517
1518
1519
1520
1521
1522
1523
        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);
1524
1525
    }

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

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
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
    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(
1555
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {12}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
1556
        auto y = m2.add_instruction(
1557
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {12}}, {"ends", {24}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
1558
1559
        auto sum = m2.add_instruction(migraphx::make_op("add"), x, y);
        m2.add_instruction(pass_op{}, sum);
1560
    }
Paul Fultz II's avatar
Paul Fultz II committed
1561
    EXPECT(m1.sort() == m2.sort());
1562
1563
}

1564
1565
1566
1567
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
1568
    migraphx::module m1;
1569
    {
Paul Fultz II's avatar
Paul Fultz II committed
1570
1571
1572
1573
        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(
1574
1575
1576
1577
1578
            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
1579
        auto conv2 = m1.add_instruction(
1580
1581
1582
1583
1584
            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
1585
        m1.add_instruction(pass_op{}, conv1, conv2);
1586
    }
Paul Fultz II's avatar
Paul Fultz II committed
1587
1588
    migraphx::module m2 = m1;
    run_pass(m1);
1589

Paul Fultz II's avatar
Paul Fultz II committed
1590
    EXPECT(m1.sort() == m2.sort());
1591
1592
1593
}

TEST_CASE(simplify_conv_horiz_grouped)
1594
1595
1596
1597
{
    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
1598
1599
1600
1601
1602
1603
1604
    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));
1605
        auto convx =
Paul Fultz II's avatar
Paul Fultz II committed
1606
            m1.add_instruction(migraphx::make_op("convolution", {{"padding", {1, 1}}}), input, a);
1607
        auto convy =
Paul Fultz II's avatar
Paul Fultz II committed
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
            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(
1629
            migraphx::make_op("convolution", {{"padding", {1, 1}}}), input, concat1);
Paul Fultz II's avatar
Paul Fultz II committed
1630
        auto convx = m2.add_instruction(
1631
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {6}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
1632
        auto convy = m2.add_instruction(
1633
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {6}}, {"ends", {12}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
1634
1635
1636
        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(
1637
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {0}}, {"ends", {64}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
1638
        auto doty = m2.add_instruction(
1639
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {64}}, {"ends", {128}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
1640
1641
1642
        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);
1643
    }
Paul Fultz II's avatar
Paul Fultz II committed
1644
    EXPECT(m1.sort() == m2.sort());
1645
1646
}

1647
TEST_CASE(simplify_conv_horiz_grouped_extra1)
1648
1649
1650
1651
{
    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
1652
1653
1654
1655
1656
1657
1658
1659
    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));
1660
        auto convx =
Paul Fultz II's avatar
Paul Fultz II committed
1661
            m1.add_instruction(migraphx::make_op("convolution", {{"padding", {1, 1}}}), input, a);
1662
        auto convy =
Paul Fultz II's avatar
Paul Fultz II committed
1663
1664
1665
1666
1667
1668
            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);
1669
        auto sum3    = sqdiffx;
Paul Fultz II's avatar
Paul Fultz II committed
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
        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(
1687
            migraphx::make_op("convolution", {{"padding", {1, 1}}}), input, concat1);
Paul Fultz II's avatar
Paul Fultz II committed
1688
        auto convx = m2.add_instruction(
1689
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {6}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
1690
        auto convy = m2.add_instruction(
1691
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {6}}, {"ends", {12}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
1692
1693
1694
        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(
1695
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {0}}, {"ends", {64}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
1696
        auto doty = m2.add_instruction(
1697
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {64}}, {"ends", {128}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
1698
1699
        auto sum2    = m2.add_instruction(migraphx::make_op("add"), dotx, doty);
        auto sqdiffx = m2.add_instruction(migraphx::make_op("sqdiff"), input, e);
1700
        auto sum3    = sqdiffx;
Paul Fultz II's avatar
Paul Fultz II committed
1701
1702
1703
        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);
1704
    }
Paul Fultz II's avatar
Paul Fultz II committed
1705
    EXPECT(m1.sort() == m2.sort());
1706
1707
}

1708
TEST_CASE(simplify_conv_horiz_grouped_extra2)
1709
1710
1711
1712
{
    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
1713
1714
1715
1716
1717
1718
1719
1720
1721
    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));
1722
        auto convx =
Paul Fultz II's avatar
Paul Fultz II committed
1723
            m1.add_instruction(migraphx::make_op("convolution", {{"padding", {1, 1}}}), input, a);
1724
        auto convy =
Paul Fultz II's avatar
Paul Fultz II committed
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
            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(
1751
            migraphx::make_op("convolution", {{"padding", {1, 1}}}), input, concat1);
Paul Fultz II's avatar
Paul Fultz II committed
1752
        auto convx = m2.add_instruction(
1753
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {6}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
1754
        auto convy = m2.add_instruction(
1755
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {6}}, {"ends", {12}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
1756
1757
1758
        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(
1759
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {0}}, {"ends", {64}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
1760
        auto doty = m2.add_instruction(
1761
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {64}}, {"ends", {128}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
1762
1763
1764
1765
1766
1767
1768
1769
1770
        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());
1771
1772
}

1773
1774
TEST_CASE(simplify_mul_slice_conv_horiz_fusion)
{
Paul Fultz II's avatar
Paul Fultz II committed
1775
    migraphx::module m1;
1776
    {
Paul Fultz II's avatar
Paul Fultz II committed
1777
1778
        auto x = m1.add_parameter("x", {migraphx::shape::int32_type, {1, 1024, 17, 17}});
        auto w = m1.add_literal(
1779
            migraphx::generate_literal({migraphx::shape::int32_type, {768, 1024, 1, 1}}));
Paul Fultz II's avatar
Paul Fultz II committed
1780
1781
        auto conv   = m1.add_instruction(migraphx::make_op("convolution"), x, w);
        auto slice1 = m1.add_instruction(
1782
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {384}}}), conv);
1783
        auto a1 =
Paul Fultz II's avatar
Paul Fultz II committed
1784
1785
            m1.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {384}}, 1));
        auto b1 = m1.add_instruction(
1786
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 384, 17, 17}}}), a1);
Paul Fultz II's avatar
Paul Fultz II committed
1787
        auto mul = m1.add_instruction(migraphx::make_op("mul"), slice1, b1);
1788
        auto a2 =
Paul Fultz II's avatar
Paul Fultz II committed
1789
1790
            m1.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {384}}, 2));
        auto b2 = m1.add_instruction(
1791
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 384, 17, 17}}}), a2);
Paul Fultz II's avatar
Paul Fultz II committed
1792
        auto add1 = m1.add_instruction(migraphx::make_op("add"), mul, b2);
1793
        auto a3 =
Paul Fultz II's avatar
Paul Fultz II committed
1794
1795
            m1.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {384}}, 3));
        auto b3 = m1.add_instruction(
1796
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 384, 17, 17}}}), a3);
Paul Fultz II's avatar
Paul Fultz II committed
1797
        auto slice2 = m1.add_instruction(
1798
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {384}}, {"ends", {768}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
1799
1800
        auto add2 = m1.add_instruction(migraphx::make_op("add"), slice2, b3);
        m1.add_instruction(pass_op{}, add1, add2);
1801
    }
Paul Fultz II's avatar
Paul Fultz II committed
1802
    run_pass(m1);
1803

Paul Fultz II's avatar
Paul Fultz II committed
1804
    migraphx::module m2;
1805
    {
Paul Fultz II's avatar
Paul Fultz II committed
1806
1807
        auto x = m2.add_parameter("x", {migraphx::shape::int32_type, {1, 1024, 17, 17}});
        auto w = m2.add_literal(
1808
            migraphx::generate_literal({migraphx::shape::int32_type, {768, 1024, 1, 1}}));
Paul Fultz II's avatar
Paul Fultz II committed
1809
        auto wslice1 = m2.add_instruction(
1810
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {384}}}), w);
1811
        auto a1 =
Paul Fultz II's avatar
Paul Fultz II committed
1812
1813
            m2.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {384}}, 1));
        auto b1 = m2.add_instruction(
1814
            migraphx::make_op("broadcast", {{"axis", 0}, {"out_lens", {384, 1024, 1, 1}}}), a1);
Paul Fultz II's avatar
Paul Fultz II committed
1815
1816
        auto mul     = m2.add_instruction(migraphx::make_op("mul"), b1, wslice1);
        auto wslice2 = m2.add_instruction(
1817
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {384}}, {"ends", {768}}}), w);
Paul Fultz II's avatar
Paul Fultz II committed
1818
1819
        auto concat1 = m2.add_instruction(migraphx::make_op("concat", {{"axis", 0}}), mul, wslice2);
        auto conv    = m2.add_instruction(migraphx::make_op("convolution"), x, concat1);
1820
        auto a2 =
Paul Fultz II's avatar
Paul Fultz II committed
1821
            m2.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {384}}, 2));
1822
        auto a3 =
Paul Fultz II's avatar
Paul Fultz II committed
1823
1824
1825
            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(
1826
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 768, 17, 17}}}), concat2);
Paul Fultz II's avatar
Paul Fultz II committed
1827
1828
        auto add    = m2.add_instruction(migraphx::make_op("add"), conv, b4);
        auto slice1 = m2.add_instruction(
1829
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {384}}}), add);
Paul Fultz II's avatar
Paul Fultz II committed
1830
        auto slice2 = m2.add_instruction(
1831
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {384}}, {"ends", {768}}}), add);
Paul Fultz II's avatar
Paul Fultz II committed
1832
        m2.add_instruction(pass_op{}, slice1, slice2);
1833
    }
Paul Fultz II's avatar
Paul Fultz II committed
1834
    EXPECT(m1.sort() == m2.sort());
1835
}
1836
1837
1838
1839
TEST_CASE(reorder_reshape_slice)
{
    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
1840
1841
    auto create_m1             = [&](std::size_t batch_size) {
        migraphx::module m1;
1842
        auto s     = migraphx::shape{migraphx::shape::float_type, {batch_size, 128, 1920}};
Paul Fultz II's avatar
Paul Fultz II committed
1843
1844
        auto input = m1.add_parameter("input", s);
        auto slc0  = m1.add_instruction(
1845
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {640}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1846
        auto slc1 = m1.add_instruction(
1847
1848
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {640}}, {"ends", {1280}}}),
            input);
Paul Fultz II's avatar
Paul Fultz II committed
1849
        auto slc2 = m1.add_instruction(
1850
1851
1852
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {1280}}, {"ends", {1920}}}),
            input);

Paul Fultz II's avatar
Paul Fultz II committed
1853
1854
1855
        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);
1856
1857

        std::vector<int64_t> lens = {static_cast<int64_t>(batch_size), 128, 10, 64};
Paul Fultz II's avatar
Paul Fultz II committed
1858
1859
1860
        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);
1861

1862
1863
1864
        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);
1865

Paul Fultz II's avatar
Paul Fultz II committed
1866
1867
1868
        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});
1869

Paul Fultz II's avatar
Paul Fultz II committed
1870
        return m1;
1871
1872
    };

Paul Fultz II's avatar
Paul Fultz II committed
1873
1874
    auto create_m2 = [&](std::size_t batch_size) {
        migraphx::module m2;
1875
        auto s     = migraphx::shape{migraphx::shape::float_type, {batch_size, 128, 1920}};
Paul Fultz II's avatar
Paul Fultz II committed
1876
        auto input = m2.add_parameter("input", s);
1877
        std::vector<int64_t> lens = {static_cast<int64_t>(batch_size), 128, 30, 64};
Paul Fultz II's avatar
Paul Fultz II committed
1878
        auto r = m2.add_instruction(migraphx::make_op("reshape", {{"dims", lens}}), input);
1879

Paul Fultz II's avatar
Paul Fultz II committed
1880
        auto slc0 = m2.add_instruction(
1881
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {10}}}), r);
Paul Fultz II's avatar
Paul Fultz II committed
1882
        auto slc1 = m2.add_instruction(
1883
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {10}}, {"ends", {20}}}), r);
Paul Fultz II's avatar
Paul Fultz II committed
1884
        auto slc2 = m2.add_instruction(
1885
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {20}}, {"ends", {30}}}), r);
1886

1887
1888
1889
1890
1891
1892
        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);
1893

Paul Fultz II's avatar
Paul Fultz II committed
1894
1895
1896
        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});
1897

Paul Fultz II's avatar
Paul Fultz II committed
1898
        return m2;
1899
1900
1901
    };

    auto test = [&](std::size_t batch_size) {
Paul Fultz II's avatar
Paul Fultz II committed
1902
1903
1904
1905
        auto m1 = create_m1(batch_size);
        run_pass(m1);
        auto m2 = create_m2(batch_size);
        EXPECT(m1.sort() == m2.sort());
1906
1907
1908
1909
1910
1911
1912
    };

    test(1);
    test(4);
    test(8);
}

1913
TEST_CASE(reorder_reshape_slice_move_axis1)
1914
{
Paul Fultz II's avatar
Paul Fultz II committed
1915
1916
1917
    auto create_m1 = [](std::size_t batch_size) {
        migraphx::module m1;
        auto s = migraphx::shape{migraphx::shape::float_type, {batch_size, 256, 96}};
1918
1919
        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
1920
1921
        auto input                 = m1.add_parameter("input", s);
        auto slc0                  = m1.add_instruction(
1922
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {32}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1923
        auto slc1 = m1.add_instruction(
1924
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {32}}, {"ends", {64}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1925
        auto slc2 = m1.add_instruction(
1926
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {64}}, {"ends", {96}}}), input);
1927

Paul Fultz II's avatar
Paul Fultz II committed
1928
1929
1930
        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);
1931

1932
        std::vector<int64_t> lens = {static_cast<int64_t>(batch_size), 64, 4, 32};
Paul Fultz II's avatar
Paul Fultz II committed
1933
1934
1935
        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);
1936

1937
1938
1939
        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);
1940

Paul Fultz II's avatar
Paul Fultz II committed
1941
1942
1943
        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});
1944

Paul Fultz II's avatar
Paul Fultz II committed
1945
        return m1;
1946
1947
    };

Paul Fultz II's avatar
Paul Fultz II committed
1948
1949
1950
    auto create_m2 = [](std::size_t batch_size) {
        migraphx::module m;
        auto s = migraphx::shape{migraphx::shape::float_type, {batch_size, 256, 96}};
1951
1952
        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
1953
        auto input                 = m.add_parameter("input", s);
1954
        std::vector<int64_t> lens  = {static_cast<int64_t>(batch_size), 64, 4, 96};
Paul Fultz II's avatar
Paul Fultz II committed
1955
1956
        auto rsp  = m.add_instruction(migraphx::make_op("reshape", {{"dims", lens}}), input);
        auto slc0 = m.add_instruction(
1957
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {0}}, {"ends", {32}}}), rsp);
1958
        auto t0 = m.add_instruction(migraphx::make_op("transpose", {{"permutation", perm0}}), slc0);
Paul Fultz II's avatar
Paul Fultz II committed
1959
        auto slc1 = m.add_instruction(
1960
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {32}}, {"ends", {64}}}), rsp);
1961
        auto t1 = m.add_instruction(migraphx::make_op("transpose", {{"permutation", perm0}}), slc1);
Paul Fultz II's avatar
Paul Fultz II committed
1962
        auto slc2 = m.add_instruction(
1963
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {64}}, {"ends", {96}}}), rsp);
1964
        auto t2 = m.add_instruction(migraphx::make_op("transpose", {{"permutation", perm1}}), slc2);
1965

Paul Fultz II's avatar
Paul Fultz II committed
1966
1967
1968
        auto sum = m.add_instruction(migraphx::make_op("add"), t0, t1);
        auto ret = m.add_instruction(migraphx::make_op("dot"), sum, t2);
        m.add_return({ret});
1969

Paul Fultz II's avatar
Paul Fultz II committed
1970
        return m;
1971
1972
    };

1973
    auto test = [&](std::size_t batch_size) {
Paul Fultz II's avatar
Paul Fultz II committed
1974
1975
1976
1977
        auto m1 = create_m1(batch_size);
        auto m2 = create_m2(batch_size);
        run_pass(m1);
        EXPECT(m1.sort() == m2.sort());
1978
1979
1980
1981
1982
1983
    };

    test(4);
    test(8);
}

1984
1985
TEST_CASE(reorder_reshape_slice_move_axis2)
{
Paul Fultz II's avatar
Paul Fultz II committed
1986
1987
    auto create_m1 = [] {
        migraphx::module m1;
1988
        migraphx::shape s{migraphx::shape::float_type, {128, 96}};
Paul Fultz II's avatar
Paul Fultz II committed
1989
1990
        auto input = m1.add_parameter("input", s);
        auto slc0  = m1.add_instruction(
1991
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {32}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1992
        auto slc1 = m1.add_instruction(
1993
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {32}}, {"ends", {64}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1994
        auto slc2 = m1.add_instruction(
1995
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {64}}, {"ends", {96}}}), input);
1996

Paul Fultz II's avatar
Paul Fultz II committed
1997
1998
1999
        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);
2000
2001

        std::vector<int64_t> lens = {1, 16, 8, 32};
Paul Fultz II's avatar
Paul Fultz II committed
2002
2003
2004
        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);
2005

Paul Fultz II's avatar
Paul Fultz II committed
2006
2007
2008
        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});
2009

Paul Fultz II's avatar
Paul Fultz II committed
2010
        return m1;
2011
2012
    };

Paul Fultz II's avatar
Paul Fultz II committed
2013
2014
    auto create_m2 = [] {
        migraphx::module m;
2015
        auto s                    = migraphx::shape{migraphx::shape::float_type, {128, 96}};
Paul Fultz II's avatar
Paul Fultz II committed
2016
        auto input                = m.add_parameter("input", s);
2017
        std::vector<int64_t> lens = {1, 16, 8, 96};
Paul Fultz II's avatar
Paul Fultz II committed
2018
2019
        auto rsp  = m.add_instruction(migraphx::make_op("reshape", {{"dims", lens}}), input);
        auto slc0 = m.add_instruction(
2020
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {0}}, {"ends", {32}}}), rsp);
Paul Fultz II's avatar
Paul Fultz II committed
2021
        auto slc1 = m.add_instruction(
2022
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {32}}, {"ends", {64}}}), rsp);
Paul Fultz II's avatar
Paul Fultz II committed
2023
        auto slc2 = m.add_instruction(
2024
2025
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {64}}, {"ends", {96}}}), rsp);

Paul Fultz II's avatar
Paul Fultz II committed
2026
2027
2028
        auto sum = m.add_instruction(migraphx::make_op("add"), slc0, slc1);
        auto ret = m.add_instruction(migraphx::make_op("mul"), sum, slc2);
        m.add_return({ret});
2029

Paul Fultz II's avatar
Paul Fultz II committed
2030
        return m;
2031
2032
    };

Paul Fultz II's avatar
Paul Fultz II committed
2033
2034
2035
2036
    auto m1 = create_m1();
    auto m2 = create_m2();
    run_pass(m1);
    EXPECT(m1.sort() == m2.sort());
2037
2038
2039
2040
2041
}

TEST_CASE(reorder_reshape_slice_not_apply)
{
    auto create_p = [] {
Paul Fultz II's avatar
Paul Fultz II committed
2042
        migraphx::module m;
2043
        migraphx::shape s{migraphx::shape::float_type, {128, 96}};
Paul Fultz II's avatar
Paul Fultz II committed
2044
2045
        auto input = m.add_parameter("input", s);
        auto slc0  = m.add_instruction(
2046
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {32}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2047
        auto slc1 = m.add_instruction(
2048
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {32}}, {"ends", {64}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2049
        auto slc2 = m.add_instruction(
2050
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {64}}, {"ends", {96}}}), input);
2051

Paul Fultz II's avatar
Paul Fultz II committed
2052
2053
2054
        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);
2055
2056

        std::vector<int64_t> lens = {1, 16, 16, 16};
Paul Fultz II's avatar
Paul Fultz II committed
2057
2058
2059
        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);
2060

Paul Fultz II's avatar
Paul Fultz II committed
2061
2062
2063
        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});
2064

Paul Fultz II's avatar
Paul Fultz II committed
2065
        return m;
2066
2067
    };

Paul Fultz II's avatar
Paul Fultz II committed
2068
2069
2070
2071
    auto m1 = create_p();
    auto m2 = m1;
    run_pass(m1);
    EXPECT(m1.sort() == m2.sort());
2072
2073
}

2074
2075
TEST_CASE(reorder_reshape_slice_diff_dims)
{
Paul Fultz II's avatar
Paul Fultz II committed
2076
2077
2078
    auto create_m1 = [](std::size_t batch_size) {
        migraphx::module m1;
        auto s = migraphx::shape{migraphx::shape::float_type, {batch_size, 96, 96}};
2079
2080
        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
2081
2082
        auto input                 = m1.add_parameter("input", s);
        auto slc0                  = m1.add_instruction(
2083
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {32}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2084
        auto slc1 = m1.add_instruction(
2085
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {32}}, {"ends", {64}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2086
        auto slc2 = m1.add_instruction(
2087
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {64}}, {"ends", {96}}}), input);
2088

Paul Fultz II's avatar
Paul Fultz II committed
2089
2090
2091
        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);
2092
2093
2094

        std::vector<int64_t> lens  = {static_cast<int64_t>(batch_size), 32, 3, 32};
        std::vector<int64_t> lens1 = {static_cast<int64_t>(batch_size), 48, 2, 32};
Paul Fultz II's avatar
Paul Fultz II committed
2095
2096
2097
        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);
2098

Paul Fultz II's avatar
Paul Fultz II committed
2099
        m1.add_return({r0, r1, r2});
2100

Paul Fultz II's avatar
Paul Fultz II committed
2101
        return m1;
2102
2103
2104
    };

    auto test = [&](std::size_t batch_size) {
Paul Fultz II's avatar
Paul Fultz II committed
2105
2106
2107
2108
        auto m1 = create_m1(batch_size);
        auto m2 = m1;
        run_pass(m1);
        EXPECT(m1.sort() == m2.sort());
2109
2110
2111
2112
2113
2114
2115
2116
2117
    };

    test(4);
    test(8);
}

TEST_CASE(reorder_slice_trans)
{
    std::vector<int64_t> perm = {0, 2, 1};
Paul Fultz II's avatar
Paul Fultz II committed
2118
2119
    auto create_m1            = [&](std::size_t batch_size) {
        migraphx::module m1;
2120
        auto s     = migraphx::shape{migraphx::shape::float_type, {batch_size, 128, 1920}};
Paul Fultz II's avatar
Paul Fultz II committed
2121
2122
        auto input = m1.add_parameter("input", s);
        auto slc0  = m1.add_instruction(
2123
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {640}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2124
        auto slc1 = m1.add_instruction(
2125
2126
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {640}}, {"ends", {1280}}}),
            input);
Paul Fultz II's avatar
Paul Fultz II committed
2127
        auto slc2 = m1.add_instruction(
2128
2129
2130
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {1280}}, {"ends", {1920}}}),
            input);

2131
2132
2133
        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);
2134

Paul Fultz II's avatar
Paul Fultz II committed
2135
2136
2137
        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});
2138

Paul Fultz II's avatar
Paul Fultz II committed
2139
        return m1;
2140
2141
    };

Paul Fultz II's avatar
Paul Fultz II committed
2142
2143
    auto create_m2 = [&](std::size_t batch_size) {
        migraphx::module m2;
2144
        auto s     = migraphx::shape{migraphx::shape::float_type, {batch_size, 128, 1920}};
Paul Fultz II's avatar
Paul Fultz II committed
2145
        auto input = m2.add_parameter("input", s);
2146
        auto r = m2.add_instruction(migraphx::make_op("transpose", {{"permutation", perm}}), input);
2147

Paul Fultz II's avatar
Paul Fultz II committed
2148
        auto slc0 = m2.add_instruction(
2149
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {640}}}), r);
Paul Fultz II's avatar
Paul Fultz II committed
2150
        auto slc1 = m2.add_instruction(
2151
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {640}}, {"ends", {1280}}}), r);
Paul Fultz II's avatar
Paul Fultz II committed
2152
        auto slc2 = m2.add_instruction(
2153
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1280}}, {"ends", {1920}}}), r);
2154

Paul Fultz II's avatar
Paul Fultz II committed
2155
2156
2157
        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});
2158

Paul Fultz II's avatar
Paul Fultz II committed
2159
        return m2;
2160
2161
2162
    };

    auto test = [&](std::size_t batch_size) {
Paul Fultz II's avatar
Paul Fultz II committed
2163
2164
2165
2166
        auto m1 = create_m1(batch_size);
        run_pass(m1);
        auto m2 = create_m2(batch_size);
        EXPECT(m1.sort() == m2.sort());
2167
2168
2169
2170
2171
2172
2173
2174
    };

    test(1);
    test(8);
}

TEST_CASE(reorder_slice_trans_diff_perm)
{
Paul Fultz II's avatar
Paul Fultz II committed
2175
2176
2177
    auto create_m1 = [](std::size_t batch_size) {
        migraphx::module m1;
        auto s = migraphx::shape{migraphx::shape::float_type, {batch_size, 128, 1920}};
2178
2179
        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
2180
2181
        auto input                 = m1.add_parameter("input", s);
        auto slc0                  = m1.add_instruction(
2182
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {640}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2183
        auto slc1 = m1.add_instruction(
2184
2185
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {640}}, {"ends", {1280}}}),
            input);
Paul Fultz II's avatar
Paul Fultz II committed
2186
        auto slc2 = m1.add_instruction(
2187
2188
2189
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {1280}}, {"ends", {1920}}}),
            input);

2190
2191
2192
2193
2194
2195
        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);
2196

Paul Fultz II's avatar
Paul Fultz II committed
2197
2198
2199
        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});
2200

Paul Fultz II's avatar
Paul Fultz II committed
2201
        return m1;
2202
2203
2204
    };

    auto test = [&](std::size_t batch_size) {
Paul Fultz II's avatar
Paul Fultz II committed
2205
2206
2207
2208
        auto m1 = create_m1(batch_size);
        run_pass(m1);
        auto m2 = m1;
        EXPECT(m1.sort() == m2.sort());
2209
2210
2211
2212
2213
2214
    };

    test(1);
    test(4);
}

Shucai Xiao's avatar
Shucai Xiao committed
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
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
2243
int main(int argc, const char* argv[]) { test::run(argc, argv); }