simplify_algebra_test.cpp 90.8 KB
Newer Older
Paul's avatar
Paul committed
1
2
#include <migraphx/simplify_algebra.hpp>
#include <migraphx/dead_code_elimination.hpp>
3
#include <migraphx/pass_manager.hpp>
Paul's avatar
Paul committed
4
#include <migraphx/operators.hpp>
Paul's avatar
Paul committed
5
6
7
#include <migraphx/generate.hpp>
#include <migraphx/ranges.hpp>
#include <migraphx/instruction.hpp>
Paul's avatar
Paul committed
8
#include <basic_ops.hpp>
9
10
#include <migraphx/make_op.hpp>

Paul's avatar
Paul committed
11
12
#include <test.hpp>

Paul Fultz II's avatar
Paul Fultz II committed
13
void run_pass(migraphx::module& m)
Paul's avatar
Paul committed
14
{
Paul Fultz II's avatar
Paul Fultz II committed
15
    migraphx::run_passes(m, {migraphx::simplify_algebra{}, migraphx::dead_code_elimination{}});
16
}
Paul's avatar
Paul committed
17

Paul's avatar
Paul committed
18
TEST_CASE(simplify_add1)
Paul's avatar
Paul committed
19
{
Paul Fultz II's avatar
Paul Fultz II committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
    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
45
46
}

Paul's avatar
Paul committed
47
TEST_CASE(simplify_add2)
Paul's avatar
Paul committed
48
{
Paul Fultz II's avatar
Paul Fultz II committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
    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
74
75
}

Paul's avatar
Paul committed
76
TEST_CASE(simplify_add3)
Paul's avatar
Paul committed
77
{
Paul Fultz II's avatar
Paul Fultz II committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    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
101
102
}

103
104
105
106
107
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
    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);
136
137
138
139
140
141
142
143
}

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
144
145
146
147
148
149
150
151
152
153
154
        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;
155
    };
Paul Fultz II's avatar
Paul Fultz II committed
156
157
    migraphx::module m1 = create_program();
    run_pass(m1);
158

Paul Fultz II's avatar
Paul Fultz II committed
159
160
    migraphx::module m2 = create_program();
    EXPECT(m1 == m2);
161
162
}

Paul's avatar
Paul committed
163
// TODO: Add test case
164
// TEST_CASE(simplify_add4)
Paul's avatar
Paul committed
165
166
void simplify_add4()
{
Paul Fultz II's avatar
Paul Fultz II committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
    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
192
193
}

Paul's avatar
Paul committed
194
195
TEST_CASE(simplify_mul_conv1)
{
Paul Fultz II's avatar
Paul Fultz II committed
196
197
198
199
200
    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(
201
202
203
204
        migraphx::make_op("convolution",
                          {{"padding", {1, 1}}, {"stride", {2, 2}}, {"dilation", {1, 1}}}),
        x,
        w);
Paul Fultz II's avatar
Paul Fultz II committed
205
206
    auto a = m.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {256}}));
    auto b = m.add_instruction(
207
        migraphx::make_op("broadcast", {{"axis", 1}, {"dims", {1, 256, 14, 14}}}), a);
Paul Fultz II's avatar
Paul Fultz II committed
208
209
    auto mul = m.add_instruction(migraphx::make_op("mul"), conv, b);
    m.add_instruction(pass_op{}, mul);
Paul's avatar
Paul committed
210
    EXPECT(conv->outputs().front()->name() == "mul");
Paul Fultz II's avatar
Paul Fultz II committed
211
212
213
    run_pass(m);
    auto new_conv =
        std::find_if(m.begin(), m.end(), [](auto&& ins) { return ins.name() == "convolution"; });
Paul's avatar
Paul committed
214
215
216
    EXPECT(new_conv->outputs().front()->name() != "mul");
}

217
218
TEST_CASE(simplify_mul_slice_conv1)
{
Paul Fultz II's avatar
Paul Fultz II committed
219
    migraphx::module m1;
220
    {
Paul Fultz II's avatar
Paul Fultz II committed
221
222
        auto x = m1.add_parameter("x", {migraphx::shape::int32_type, {1, 1024, 17, 17}});
        auto w = m1.add_literal(
223
            migraphx::generate_literal({migraphx::shape::int32_type, {768, 1024, 1, 1}}));
Paul Fultz II's avatar
Paul Fultz II committed
224
225
        auto conv   = m1.add_instruction(migraphx::make_op("convolution"), x, w);
        auto slice1 = m1.add_instruction(
226
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {384}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
227
228
        auto a = m1.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {384}}));
        auto b = m1.add_instruction(
229
            migraphx::make_op("broadcast", {{"axis", 1}, {"dims", {1, 384, 17, 17}}}), a);
Paul Fultz II's avatar
Paul Fultz II committed
230
231
        auto mul    = m1.add_instruction(migraphx::make_op("mul"), slice1, b);
        auto slice2 = m1.add_instruction(
232
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {384}}, {"ends", {768}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
233
234
        auto add = m1.add_instruction(migraphx::make_op("add"), mul, slice2);
        m1.add_instruction(pass_op{}, add);
235
    }
Paul Fultz II's avatar
Paul Fultz II committed
236
    run_pass(m1);
237

Paul Fultz II's avatar
Paul Fultz II committed
238
    migraphx::module m2;
239
    {
Paul Fultz II's avatar
Paul Fultz II committed
240
241
        auto x = m2.add_parameter("x", {migraphx::shape::int32_type, {1, 1024, 17, 17}});
        auto w = m2.add_literal(
242
            migraphx::generate_literal({migraphx::shape::int32_type, {768, 1024, 1, 1}}));
Paul Fultz II's avatar
Paul Fultz II committed
243
        auto wslice1 = m2.add_instruction(
244
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {384}}}), w);
Paul Fultz II's avatar
Paul Fultz II committed
245
246
        auto a = m2.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {384}}));
        auto b = m2.add_instruction(
247
            migraphx::make_op("broadcast", {{"axis", 0}, {"dims", {384, 1024, 1, 1}}}), a);
Paul Fultz II's avatar
Paul Fultz II committed
248
249
        auto mul     = m2.add_instruction(migraphx::make_op("mul"), b, wslice1);
        auto wslice2 = m2.add_instruction(
250
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {384}}, {"ends", {768}}}), w);
Paul Fultz II's avatar
Paul Fultz II committed
251
252
253
        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(
254
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {384}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
255
        auto slice2 = m2.add_instruction(
256
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {384}}, {"ends", {768}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
257
258
        auto add = m2.add_instruction(migraphx::make_op("add"), slice1, slice2);
        m2.add_instruction(pass_op{}, add);
259
    }
Paul Fultz II's avatar
Paul Fultz II committed
260
    EXPECT(m1 == m2);
261
262
263
264
}

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

TEST_CASE(simplify_mul_slice_conv_not_all_slice)
{
Paul Fultz II's avatar
Paul Fultz II committed
289
    migraphx::module m1;
290
    {
Paul Fultz II's avatar
Paul Fultz II committed
291
292
        auto x = m1.add_parameter("x", {migraphx::shape::int32_type, {1, 1024, 17, 17}});
        auto w = m1.add_literal(
293
            migraphx::generate_literal({migraphx::shape::int32_type, {768, 1024, 1, 1}}));
Paul Fultz II's avatar
Paul Fultz II committed
294
295
        auto conv   = m1.add_instruction(migraphx::make_op("convolution"), x, w);
        auto slice1 = m1.add_instruction(
296
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {384}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
297
298
        auto a = m1.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {384}}));
        auto b = m1.add_instruction(
299
            migraphx::make_op("broadcast", {{"axis", 1}, {"dims", {1, 384, 17, 17}}}), a);
Paul Fultz II's avatar
Paul Fultz II committed
300
301
        auto mul = m1.add_instruction(migraphx::make_op("mul"), slice1, b);
        auto c   = m1.add_literal(
302
            migraphx::generate_literal({migraphx::shape::int32_type, {1, 768, 17, 17}}));
Paul Fultz II's avatar
Paul Fultz II committed
303
304
305
        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);
306
    }
Paul Fultz II's avatar
Paul Fultz II committed
307
308
309
    migraphx::module m2 = m1;
    run_pass(m1);
    EXPECT(m1 == m2);
310
311
}

Paul's avatar
Paul committed
312
313
TEST_CASE(simplify_mul_add)
{
Paul Fultz II's avatar
Paul Fultz II committed
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
    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
336
337
}

Paul's avatar
Paul committed
338
339
340
TEST_CASE(simplify_inner_broadcast)
{
    auto b = migraphx::op::broadcast{1, {2, 1, 4, 5}};
Paul Fultz II's avatar
Paul Fultz II committed
341
    migraphx::module m1;
Paul's avatar
Paul committed
342
    {
Paul Fultz II's avatar
Paul Fultz II committed
343
344
345
346
347
348
        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
349
    }
Paul Fultz II's avatar
Paul Fultz II committed
350
    run_pass(m1);
Paul's avatar
Paul committed
351

Paul Fultz II's avatar
Paul Fultz II committed
352
    migraphx::module m2;
Paul's avatar
Paul committed
353
    {
Paul Fultz II's avatar
Paul Fultz II committed
354
355
356
357
358
        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
359
    }
Paul Fultz II's avatar
Paul Fultz II committed
360
    EXPECT(m1 == m2);
Paul's avatar
Paul committed
361
362
}

363
364
TEST_CASE(simplify_add_conv1)
{
Paul Fultz II's avatar
Paul Fultz II committed
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
    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);
381
382
383
384
}

TEST_CASE(simplify_add_conv_no_fusion_7x7_diff_strides)
{
Paul Fultz II's avatar
Paul Fultz II committed
385
386
387
388
389
390
391
392
393
    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(
394
        migraphx::make_op("convolution", {{"padding", {0, 0}}, {"stride", {3, 3}}}), y, v);
Paul Fultz II's avatar
Paul Fultz II committed
395
396
397
398
399
    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());
400
    // No fusion
Paul Fultz II's avatar
Paul Fultz II committed
401
402
    EXPECT(std::count_if(
               m.begin(), m.end(), [](auto&& ins) { return ins.name() == "convolution"; }) == 2);
403
404
405
406
}

TEST_CASE(simplify_add_conv_1x1_diff_strides1)
{
Paul Fultz II's avatar
Paul Fultz II committed
407
408
409
410
411
412
413
414
415
    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(
416
        migraphx::make_op("convolution", {{"padding", {0, 0}}, {"stride", {2, 2}}}), y, v);
Paul Fultz II's avatar
Paul Fultz II committed
417
418
419
420
421
422
423
    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);
424
425
426
427
}

TEST_CASE(simplify_add_conv_1x1_diff_strides2)
{
Paul Fultz II's avatar
Paul Fultz II committed
428
429
430
431
432
433
434
435
    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(
436
        migraphx::make_op("convolution", {{"padding", {0, 0}}, {"stride", {2, 2}}}), x, w);
Paul Fultz II's avatar
Paul Fultz II committed
437
438
439
440
441
442
443
444
    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);
445
446
447
448
}

TEST_CASE(simplify_add_conv_1x1_diff_strides_odd)
{
Paul Fultz II's avatar
Paul Fultz II committed
449
450
    migraphx::module m;
    auto x = m.add_parameter("x", {migraphx::shape::float_type, {1, 54, 83, 83}});
451
    auto w =
Paul Fultz II's avatar
Paul Fultz II committed
452
453
        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}});
454
    auto v =
Paul Fultz II's avatar
Paul Fultz II committed
455
456
457
        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(
458
        migraphx::make_op("convolution", {{"padding", {0, 0}}, {"stride", {2, 2}}}), y, v);
Paul Fultz II's avatar
Paul Fultz II committed
459
460
461
462
463
464
465
    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);
466
467
468
469
}

TEST_CASE(simplify_add_conv_no_fusion_asymetrical_strides1)
{
Paul Fultz II's avatar
Paul Fultz II committed
470
471
472
473
474
475
476
477
    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(
478
        migraphx::make_op("convolution", {{"padding", {0, 0}}, {"stride", {2, 1}}}), x, w);
Paul Fultz II's avatar
Paul Fultz II committed
479
480
481
482
483
484
    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());
485
    // No fusion
Paul Fultz II's avatar
Paul Fultz II committed
486
487
    EXPECT(std::count_if(
               m.begin(), m.end(), [](auto&& ins) { return ins.name() == "convolution"; }) == 2);
488
489
490
491
}

TEST_CASE(simplify_add_conv_no_fusion_asymetrical_strides2)
{
Paul Fultz II's avatar
Paul Fultz II committed
492
493
494
495
496
497
498
499
500
    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(
501
        migraphx::make_op("convolution", {{"padding", {0, 0}}, {"stride", {2, 1}}}), y, v);
Paul Fultz II's avatar
Paul Fultz II committed
502
503
504
505
506
    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());
507
    // No fusion
Paul Fultz II's avatar
Paul Fultz II committed
508
509
    EXPECT(std::count_if(
               m.begin(), m.end(), [](auto&& ins) { return ins.name() == "convolution"; }) == 2);
510
511
}

512
513
514
TEST_CASE(simplify_concat_add_relu)
{
    auto s = migraphx::shape{migraphx::shape::int32_type, {1}};
Paul Fultz II's avatar
Paul Fultz II committed
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
    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);
543
544
}

545
546
547
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
548
549
550
551
552
553
554
555
556
557
558
    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);
559
        auto concat =
Paul Fultz II's avatar
Paul Fultz II committed
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
            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());
580
581
582
583
584
}

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
585
    migraphx::module m1;
586
    {
587
        auto b    = migraphx::op::broadcast{1, {2, 1, 4, 5}};
Paul Fultz II's avatar
Paul Fultz II committed
588
589
590
591
592
593
594
        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);
595
        auto concat =
Paul Fultz II's avatar
Paul Fultz II committed
596
597
            m1.add_instruction(migraphx::make_op("concat", {{"axis", 1}}), sum, oneb, twob);
        m1.add_instruction(pass_op{}, concat);
598
    }
Paul Fultz II's avatar
Paul Fultz II committed
599
    run_pass(m1);
600

Paul Fultz II's avatar
Paul Fultz II committed
601
    migraphx::module m2;
602
603
    {
        auto b       = migraphx::op::broadcast{1, {2, 2, 4, 5}};
Paul Fultz II's avatar
Paul Fultz II committed
604
605
606
607
608
609
610
611
612
613
614
        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());
615
616
}

617
618
619
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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
    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;
639
640
    {
        auto b        = migraphx::op::broadcast{1, {2, 2, 4, 5}};
Paul Fultz II's avatar
Paul Fultz II committed
641
642
643
644
645
646
647
648
649
650
651
652
        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);
653
654
655
656
657
}

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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
    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;
677
678
    {
        auto b       = migraphx::op::broadcast{1, {2, 1, 4, 5}};
Paul Fultz II's avatar
Paul Fultz II committed
679
680
681
682
683
684
685
686
687
688
689
690
691
        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);
692
693
}

694
695
TEST_CASE(simplify_div_const)
{
Paul Fultz II's avatar
Paul Fultz II committed
696
    migraphx::module m1;
697
    {
Paul Fultz II's avatar
Paul Fultz II committed
698
699
700
        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);
701
    }
Paul Fultz II's avatar
Paul Fultz II committed
702
    run_pass(m1);
703

Paul Fultz II's avatar
Paul Fultz II committed
704
    migraphx::module m2;
705
    {
Paul Fultz II's avatar
Paul Fultz II committed
706
707
708
709
        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);
710
    }
Paul Fultz II's avatar
Paul Fultz II committed
711
    EXPECT(m1 == m2);
712
713
714
715
}

TEST_CASE(simplify_sub_const)
{
Paul Fultz II's avatar
Paul Fultz II committed
716
    migraphx::module m1;
717
    {
Paul Fultz II's avatar
Paul Fultz II committed
718
719
720
        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);
721
    }
Paul Fultz II's avatar
Paul Fultz II committed
722
    run_pass(m1);
723

Paul Fultz II's avatar
Paul Fultz II committed
724
    migraphx::module m2;
725
    {
Paul Fultz II's avatar
Paul Fultz II committed
726
727
728
729
        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);
730
    }
Paul Fultz II's avatar
Paul Fultz II committed
731
    EXPECT(m1 == m2);
732
733
}

kahmed10's avatar
kahmed10 committed
734
735
TEST_CASE(simplify_rsqrt)
{
Paul Fultz II's avatar
Paul Fultz II committed
736
    migraphx::module m1;
kahmed10's avatar
kahmed10 committed
737
    {
Paul Fultz II's avatar
Paul Fultz II committed
738
739
740
        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
741
    }
Paul Fultz II's avatar
Paul Fultz II committed
742
    run_pass(m1);
kahmed10's avatar
kahmed10 committed
743

Paul Fultz II's avatar
Paul Fultz II committed
744
    migraphx::module m2;
kahmed10's avatar
kahmed10 committed
745
    {
Paul Fultz II's avatar
Paul Fultz II committed
746
747
        auto x = m2.add_parameter("x", {migraphx::shape::int32_type, {1}});
        m2.add_instruction(migraphx::make_op("rsqrt"), x);
kahmed10's avatar
kahmed10 committed
748
    }
Paul Fultz II's avatar
Paul Fultz II committed
749
    EXPECT(m1 == m2);
kahmed10's avatar
kahmed10 committed
750
751
752
753
}

TEST_CASE(simplify_rsqrt_multi_use)
{
Paul Fultz II's avatar
Paul Fultz II committed
754
    migraphx::module m1;
kahmed10's avatar
kahmed10 committed
755
    {
Paul Fultz II's avatar
Paul Fultz II committed
756
757
758
759
760
        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
761
    }
Paul Fultz II's avatar
Paul Fultz II committed
762
    migraphx::module m2{m1};
kahmed10's avatar
kahmed10 committed
763

Paul Fultz II's avatar
Paul Fultz II committed
764
765
    run_pass(m1);
    EXPECT(m1 == m2);
kahmed10's avatar
kahmed10 committed
766
767
}

768
769
770
771
TEST_CASE(simplify_slice_concat)
{
    auto s = migraphx::shape{migraphx::shape::float_type, {256}};

Paul Fultz II's avatar
Paul Fultz II committed
772
    migraphx::module m1;
773
    {
Paul Fultz II's avatar
Paul Fultz II committed
774
775
776
        auto x       = m1.add_parameter("x", s);
        auto y       = m1.add_parameter("y", s);
        auto xslice1 = m1.add_instruction(
777
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {128}}}), x);
Paul Fultz II's avatar
Paul Fultz II committed
778
        auto xslice2 = m1.add_instruction(
779
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {128}}, {"ends", {256}}}), x);
Paul Fultz II's avatar
Paul Fultz II committed
780
        auto yslice1 = m1.add_instruction(
781
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {128}}}), y);
Paul Fultz II's avatar
Paul Fultz II committed
782
        auto yslice2 = m1.add_instruction(
783
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {128}}, {"ends", {256}}}), y);
Paul Fultz II's avatar
Paul Fultz II committed
784
        auto concat = m1.add_instruction(
785
            migraphx::make_op("concat", {{"axis", 0}}), xslice1, xslice2, yslice1, yslice2);
Paul Fultz II's avatar
Paul Fultz II committed
786
        m1.add_instruction(pass_op{}, concat);
787
    }
Paul Fultz II's avatar
Paul Fultz II committed
788
    run_pass(m1);
789

Paul Fultz II's avatar
Paul Fultz II committed
790
    migraphx::module m2;
791
    {
Paul Fultz II's avatar
Paul Fultz II committed
792
793
794
795
        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);
796
    }
Paul Fultz II's avatar
Paul Fultz II committed
797
    EXPECT(m1 == m2);
798
799
800
801
802
803
}

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
804
    migraphx::module m1;
805
    {
Paul Fultz II's avatar
Paul Fultz II committed
806
807
808
        auto x       = m1.add_parameter("x", s);
        auto y       = m1.add_parameter("y", s);
        auto xslice1 = m1.add_instruction(
809
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {64}}}), x);
Paul Fultz II's avatar
Paul Fultz II committed
810
        auto xslice2 = m1.add_instruction(
811
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {64}}, {"ends", {192}}}), x);
Paul Fultz II's avatar
Paul Fultz II committed
812
        auto xslice3 = m1.add_instruction(
813
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {192}}, {"ends", {256}}}), x);
Paul Fultz II's avatar
Paul Fultz II committed
814
        auto yslice1 = m1.add_instruction(
815
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {64}}}), y);
Paul Fultz II's avatar
Paul Fultz II committed
816
        auto yslice2 = m1.add_instruction(
817
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {64}}, {"ends", {192}}}), y);
Paul Fultz II's avatar
Paul Fultz II committed
818
        auto yslice3 = m1.add_instruction(
819
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {192}}, {"ends", {256}}}), y);
Paul Fultz II's avatar
Paul Fultz II committed
820
821
822
823
824
825
826
827
        auto concat = m1.add_instruction(migraphx::make_op("concat", {{"axis", 0}}),
                                         xslice1,
                                         xslice2,
                                         xslice3,
                                         yslice1,
                                         yslice2,
                                         yslice3);
        m1.add_instruction(pass_op{}, concat);
828
    }
Paul Fultz II's avatar
Paul Fultz II committed
829
    run_pass(m1);
830

Paul Fultz II's avatar
Paul Fultz II committed
831
    migraphx::module m2;
832
    {
Paul Fultz II's avatar
Paul Fultz II committed
833
834
835
836
        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);
837
838
    }

Paul Fultz II's avatar
Paul Fultz II committed
839
    EXPECT(m1 == m2);
840
841
842
843
844
845
}

TEST_CASE(simplify_slice_concat_flipped)
{
    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", {64}}}), x);
Paul Fultz II's avatar
Paul Fultz II committed
852
        auto xslice2 = m1.add_instruction(
853
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {192}}, {"ends", {256}}}), x);
Paul Fultz II's avatar
Paul Fultz II committed
854
        auto xslice3 = m1.add_instruction(
855
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {64}}, {"ends", {192}}}), x);
Paul Fultz II's avatar
Paul Fultz II committed
856
        auto yslice1 = m1.add_instruction(
857
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {64}}}), y);
Paul Fultz II's avatar
Paul Fultz II committed
858
        auto yslice2 = m1.add_instruction(
859
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {192}}, {"ends", {256}}}), y);
Paul Fultz II's avatar
Paul Fultz II committed
860
        auto yslice3 = m1.add_instruction(
861
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {64}}, {"ends", {192}}}), y);
Paul Fultz II's avatar
Paul Fultz II committed
862
863
864
865
866
867
868
869
870
871
872
873
874
        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);
875
876
}

877
878
879
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
880
    migraphx::module m1;
881
882
    {
        auto b     = migraphx::op::broadcast{1, {3, 1, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
883
884
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
885
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
886
        auto y = m1.add_instruction(
887
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
        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;
    {
903
        auto b       = migraphx::op::broadcast{1, {3, 2, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
904
905
906
907
908
909
910
911
        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(
912
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), relu);
Paul Fultz II's avatar
Paul Fultz II committed
913
        auto y = m2.add_instruction(
914
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), relu);
Paul Fultz II's avatar
Paul Fultz II committed
915
916
        auto add = m2.add_instruction(migraphx::make_op("add"), x, y);
        m2.add_instruction(pass_op{}, add);
917
    }
Paul Fultz II's avatar
Paul Fultz II committed
918
    EXPECT(m1.sort() == m2.sort());
919
920
921
922
923
}

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
924
    migraphx::module m1;
925
    {
926
927
        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
928
929
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
930
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
931
        auto y = m1.add_instruction(
932
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
        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;
    {
950
        auto b       = migraphx::op::broadcast{1, {3, 2, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
951
952
953
954
955
956
957
958
959
        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(
960
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {4}}}), rsp);
Paul Fultz II's avatar
Paul Fultz II committed
961
        auto slc2 = m2.add_instruction(
962
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {4}}, {"ends", {8}}}), rsp);
Paul Fultz II's avatar
Paul Fultz II committed
963
964
        auto add = m2.add_instruction(migraphx::make_op("add"), slc1, slc2);
        m2.add_instruction(pass_op{}, add);
965
    }
Paul Fultz II's avatar
Paul Fultz II committed
966
    EXPECT(m1.sort() == m2.sort());
967
968
969
970
971
}

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
972
    migraphx::module m1;
973
    {
974
        auto r     = migraphx::op::reshape{{3, 2, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
975
976
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
977
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
978
        auto y = m1.add_instruction(
979
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
980
981
        auto one  = m1.add_literal(1);
        auto oneb = m1.add_instruction(
982
            migraphx::make_op("broadcast", {{"axis", 1}, {"dims", {3, 1, 4, 2}}}), one);
Paul Fultz II's avatar
Paul Fultz II committed
983
984
        auto two  = m1.add_literal(2);
        auto twob = m1.add_instruction(
985
            migraphx::make_op("broadcast", {{"axis", 3}, {"dims", {3, 2, 4, 1}}}), two);
Paul Fultz II's avatar
Paul Fultz II committed
986
987
988
989
990
991
992
993
994
995
996
997
998
        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());
999
1000
1001
1002
1003
}

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
1004
    migraphx::module m1;
1005
1006
    {
        auto b     = migraphx::op::broadcast{1, {3, 1, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1007
1008
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
1009
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {2}}, {"ends", {3}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1010
        auto y = m1.add_instruction(
1011
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
        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());
1027
1028
1029
1030
1031
}

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
1032
    migraphx::module m1;
1033
1034
    {
        auto b     = migraphx::op::broadcast{1, {3, 1, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1035
1036
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
1037
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {2}}, {"ends", {3}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1038
        auto y = m1.add_instruction(
1039
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
        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());
1055
1056
1057
1058
1059
}

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
1060
    migraphx::module m1;
1061
1062
    {
        auto b     = migraphx::op::broadcast{1, {3, 1, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1063
1064
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
1065
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1066
        auto y = m1.add_instruction(
1067
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
        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());
1083
1084
1085
1086
1087
}

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
1088
    migraphx::module m1;
1089
    {
1090
        auto b     = migraphx::op::broadcast{1, {3, 1, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1091
1092
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
1093
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1094
        auto y = m1.add_instruction(
1095
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
        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);
1106
    }
Paul Fultz II's avatar
Paul Fultz II committed
1107
    run_pass(m1);
1108

Paul Fultz II's avatar
Paul Fultz II committed
1109
    migraphx::module m2;
1110
1111
    {
        auto b       = migraphx::op::broadcast{1, {3, 2, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
        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());
1122
1123
1124
1125
1126
}

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
1127
    migraphx::module m1;
1128
1129
    {
        auto b     = migraphx::op::broadcast{1, {3, 1, 4, 3}};
Paul Fultz II's avatar
Paul Fultz II committed
1130
1131
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
1132
1133
            migraphx::make_op("slice", {{"axes", {1, 3}}, {"starts", {0, 0}}, {"ends", {1, 3}}}),
            input);
Paul Fultz II's avatar
Paul Fultz II committed
1134
        auto y = m1.add_instruction(
1135
1136
            migraphx::make_op("slice", {{"axes", {1, 3}}, {"starts", {1, 3}}, {"ends", {2, 6}}}),
            input);
Paul Fultz II's avatar
Paul Fultz II committed
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
        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());
1151
1152
1153
1154
1155
}

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
1156
    migraphx::module m1;
1157
1158
    {
        auto b     = migraphx::op::broadcast{1, {3, 1, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1159
1160
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
1161
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1162
        auto y = m1.add_instruction(
1163
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
        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;
    {
1180
        auto b     = migraphx::op::broadcast{1, {3, 2, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1181
1182
        auto input = m2.add_parameter("input", s);
        auto slice = m2.add_instruction(
1183
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1184
1185
1186
1187
1188
1189
1190
        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(
1191
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), relu);
Paul Fultz II's avatar
Paul Fultz II committed
1192
        auto y = m2.add_instruction(
1193
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), relu);
Paul Fultz II's avatar
Paul Fultz II committed
1194
1195
1196
        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);
1197
    }
Paul Fultz II's avatar
Paul Fultz II committed
1198
    EXPECT(m1.sort() == m2.sort());
1199
1200
1201
1202
1203
}

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
1204
    migraphx::module m1;
1205
1206
    {
        auto b     = migraphx::op::broadcast{1, {3, 1, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1207
1208
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
1209
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1210
        auto y = m1.add_instruction(
1211
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
        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;
    {
1229
        auto b     = migraphx::op::broadcast{1, {3, 2, 4}};
Paul Fultz II's avatar
Paul Fultz II committed
1230
1231
        auto input = m2.add_parameter("input", s);
        auto slice = m2.add_instruction(
1232
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1233
1234
1235
1236
1237
1238
1239
1240
        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(
1241
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), relu);
Paul Fultz II's avatar
Paul Fultz II committed
1242
        auto y = m2.add_instruction(
1243
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), relu);
Paul Fultz II's avatar
Paul Fultz II committed
1244
1245
1246
        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);
1247
    }
Paul Fultz II's avatar
Paul Fultz II committed
1248
    EXPECT(m1.sort() == m2.sort());
1249
1250
1251
1252
1253
}

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
1254
    migraphx::module m1;
1255
    {
Paul Fultz II's avatar
Paul Fultz II committed
1256
1257
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
1258
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1259
        auto y = m1.add_instruction(
1260
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1261
1262
        auto sum = m1.add_instruction(migraphx::make_op("add"), x, y);
        m1.add_instruction(pass_op{}, sum);
1263
    }
Paul Fultz II's avatar
Paul Fultz II committed
1264
1265
1266
    migraphx::module m2 = m1;
    run_pass(m1);
    EXPECT(m1.sort() == m2.sort());
1267
1268
1269
1270
1271
}

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
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
    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(
1292
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {2}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
1293
        auto y = m2.add_instruction(
1294
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {2}}, {"ends", {4}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
1295
1296
        auto sum = m2.add_instruction(migraphx::make_op("add"), x, y);
        m2.add_instruction(pass_op{}, sum);
1297
    }
Paul Fultz II's avatar
Paul Fultz II committed
1298
    EXPECT(m1.sort() == m2.sort());
1299
1300
1301
1302
1303
}

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
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
    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(
1322
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {2}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
1323
        auto y = m2.add_instruction(
1324
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {2}}, {"ends", {4}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
1325
1326
        auto sum = m2.add_instruction(migraphx::make_op("add"), x, y);
        m2.add_instruction(pass_op{}, sum);
1327
    }
Paul Fultz II's avatar
Paul Fultz II committed
1328
    EXPECT(m1.sort() == m2.sort());
1329
1330
1331
1332
1333
}

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
1334
    migraphx::module m1;
1335
    {
Paul Fultz II's avatar
Paul Fultz II committed
1336
1337
1338
1339
1340
1341
1342
        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);
1343
1344
    }

Paul Fultz II's avatar
Paul Fultz II committed
1345
1346
1347
    migraphx::module m2 = m1;
    run_pass(m1);
    EXPECT(m1.sort() == m2.sort());
1348
1349
1350
1351
1352
1353
}

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
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
    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(
1374
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {12}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
1375
        auto y = m2.add_instruction(
1376
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {12}}, {"ends", {24}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
1377
1378
        auto sum = m2.add_instruction(migraphx::make_op("add"), x, y);
        m2.add_instruction(pass_op{}, sum);
1379
    }
Paul Fultz II's avatar
Paul Fultz II committed
1380
    EXPECT(m1.sort() == m2.sort());
1381
1382
}

1383
1384
1385
1386
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
1387
    migraphx::module m1;
1388
    {
Paul Fultz II's avatar
Paul Fultz II committed
1389
1390
1391
1392
        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(
1393
1394
1395
1396
1397
            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
1398
        auto conv2 = m1.add_instruction(
1399
1400
1401
1402
1403
            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
1404
        m1.add_instruction(pass_op{}, conv1, conv2);
1405
    }
Paul Fultz II's avatar
Paul Fultz II committed
1406
1407
    migraphx::module m2 = m1;
    run_pass(m1);
1408

Paul Fultz II's avatar
Paul Fultz II committed
1409
    EXPECT(m1.sort() == m2.sort());
1410
1411
1412
}

TEST_CASE(simplify_conv_horiz_grouped)
1413
1414
1415
1416
{
    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
1417
1418
1419
1420
1421
1422
1423
    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));
1424
        auto convx =
Paul Fultz II's avatar
Paul Fultz II committed
1425
            m1.add_instruction(migraphx::make_op("convolution", {{"padding", {1, 1}}}), input, a);
1426
        auto convy =
Paul Fultz II's avatar
Paul Fultz II committed
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
            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(
1448
            migraphx::make_op("convolution", {{"padding", {1, 1}}}), input, concat1);
Paul Fultz II's avatar
Paul Fultz II committed
1449
        auto convx = m2.add_instruction(
1450
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {6}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
1451
        auto convy = m2.add_instruction(
1452
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {6}}, {"ends", {12}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
1453
1454
1455
        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(
1456
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {0}}, {"ends", {64}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
1457
        auto doty = m2.add_instruction(
1458
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {64}}, {"ends", {128}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
1459
1460
1461
        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);
1462
    }
Paul Fultz II's avatar
Paul Fultz II committed
1463
    EXPECT(m1.sort() == m2.sort());
1464
1465
}

1466
TEST_CASE(simplify_conv_horiz_grouped_extra1)
1467
1468
1469
1470
{
    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
1471
1472
1473
1474
1475
1476
1477
1478
    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));
1479
        auto convx =
Paul Fultz II's avatar
Paul Fultz II committed
1480
            m1.add_instruction(migraphx::make_op("convolution", {{"padding", {1, 1}}}), input, a);
1481
        auto convy =
Paul Fultz II's avatar
Paul Fultz II committed
1482
1483
1484
1485
1486
1487
            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);
1488
        auto sum3    = sqdiffx;
Paul Fultz II's avatar
Paul Fultz II committed
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
        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(
1506
            migraphx::make_op("convolution", {{"padding", {1, 1}}}), input, concat1);
Paul Fultz II's avatar
Paul Fultz II committed
1507
        auto convx = m2.add_instruction(
1508
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {6}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
1509
        auto convy = m2.add_instruction(
1510
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {6}}, {"ends", {12}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
1511
1512
1513
        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(
1514
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {0}}, {"ends", {64}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
1515
        auto doty = m2.add_instruction(
1516
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {64}}, {"ends", {128}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
1517
1518
        auto sum2    = m2.add_instruction(migraphx::make_op("add"), dotx, doty);
        auto sqdiffx = m2.add_instruction(migraphx::make_op("sqdiff"), input, e);
1519
        auto sum3    = sqdiffx;
Paul Fultz II's avatar
Paul Fultz II committed
1520
1521
1522
        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);
1523
    }
Paul Fultz II's avatar
Paul Fultz II committed
1524
    EXPECT(m1.sort() == m2.sort());
1525
1526
}

1527
TEST_CASE(simplify_conv_horiz_grouped_extra2)
1528
1529
1530
1531
{
    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
1532
1533
1534
1535
1536
1537
1538
1539
1540
    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));
1541
        auto convx =
Paul Fultz II's avatar
Paul Fultz II committed
1542
            m1.add_instruction(migraphx::make_op("convolution", {{"padding", {1, 1}}}), input, a);
1543
        auto convy =
Paul Fultz II's avatar
Paul Fultz II committed
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
            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(
1570
            migraphx::make_op("convolution", {{"padding", {1, 1}}}), input, concat1);
Paul Fultz II's avatar
Paul Fultz II committed
1571
        auto convx = m2.add_instruction(
1572
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {6}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
1573
        auto convy = m2.add_instruction(
1574
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {6}}, {"ends", {12}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
1575
1576
1577
        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(
1578
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {0}}, {"ends", {64}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
1579
        auto doty = m2.add_instruction(
1580
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {64}}, {"ends", {128}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
1581
1582
1583
1584
1585
1586
1587
1588
1589
        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());
1590
1591
}

1592
1593
TEST_CASE(simplify_mul_slice_conv_horiz_fusion)
{
Paul Fultz II's avatar
Paul Fultz II committed
1594
    migraphx::module m1;
1595
    {
Paul Fultz II's avatar
Paul Fultz II committed
1596
1597
        auto x = m1.add_parameter("x", {migraphx::shape::int32_type, {1, 1024, 17, 17}});
        auto w = m1.add_literal(
1598
            migraphx::generate_literal({migraphx::shape::int32_type, {768, 1024, 1, 1}}));
Paul Fultz II's avatar
Paul Fultz II committed
1599
1600
        auto conv   = m1.add_instruction(migraphx::make_op("convolution"), x, w);
        auto slice1 = m1.add_instruction(
1601
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {384}}}), conv);
1602
        auto a1 =
Paul Fultz II's avatar
Paul Fultz II committed
1603
1604
            m1.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {384}}, 1));
        auto b1 = m1.add_instruction(
1605
            migraphx::make_op("broadcast", {{"axis", 1}, {"dims", {1, 384, 17, 17}}}), a1);
Paul Fultz II's avatar
Paul Fultz II committed
1606
        auto mul = m1.add_instruction(migraphx::make_op("mul"), slice1, b1);
1607
        auto a2 =
Paul Fultz II's avatar
Paul Fultz II committed
1608
1609
            m1.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {384}}, 2));
        auto b2 = m1.add_instruction(
1610
            migraphx::make_op("broadcast", {{"axis", 1}, {"dims", {1, 384, 17, 17}}}), a2);
Paul Fultz II's avatar
Paul Fultz II committed
1611
        auto add1 = m1.add_instruction(migraphx::make_op("add"), mul, b2);
1612
        auto a3 =
Paul Fultz II's avatar
Paul Fultz II committed
1613
1614
            m1.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {384}}, 3));
        auto b3 = m1.add_instruction(
1615
            migraphx::make_op("broadcast", {{"axis", 1}, {"dims", {1, 384, 17, 17}}}), a3);
Paul Fultz II's avatar
Paul Fultz II committed
1616
        auto slice2 = m1.add_instruction(
1617
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {384}}, {"ends", {768}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
1618
1619
        auto add2 = m1.add_instruction(migraphx::make_op("add"), slice2, b3);
        m1.add_instruction(pass_op{}, add1, add2);
1620
    }
Paul Fultz II's avatar
Paul Fultz II committed
1621
    run_pass(m1);
1622

Paul Fultz II's avatar
Paul Fultz II committed
1623
    migraphx::module m2;
1624
    {
Paul Fultz II's avatar
Paul Fultz II committed
1625
1626
        auto x = m2.add_parameter("x", {migraphx::shape::int32_type, {1, 1024, 17, 17}});
        auto w = m2.add_literal(
1627
            migraphx::generate_literal({migraphx::shape::int32_type, {768, 1024, 1, 1}}));
Paul Fultz II's avatar
Paul Fultz II committed
1628
        auto wslice1 = m2.add_instruction(
1629
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {384}}}), w);
1630
        auto a1 =
Paul Fultz II's avatar
Paul Fultz II committed
1631
1632
            m2.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {384}}, 1));
        auto b1 = m2.add_instruction(
1633
            migraphx::make_op("broadcast", {{"axis", 0}, {"dims", {384, 1024, 1, 1}}}), a1);
Paul Fultz II's avatar
Paul Fultz II committed
1634
1635
        auto mul     = m2.add_instruction(migraphx::make_op("mul"), b1, wslice1);
        auto wslice2 = m2.add_instruction(
1636
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {384}}, {"ends", {768}}}), w);
Paul Fultz II's avatar
Paul Fultz II committed
1637
1638
        auto concat1 = m2.add_instruction(migraphx::make_op("concat", {{"axis", 0}}), mul, wslice2);
        auto conv    = m2.add_instruction(migraphx::make_op("convolution"), x, concat1);
1639
        auto a2 =
Paul Fultz II's avatar
Paul Fultz II committed
1640
            m2.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {384}}, 2));
1641
        auto a3 =
Paul Fultz II's avatar
Paul Fultz II committed
1642
1643
1644
            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(
1645
            migraphx::make_op("broadcast", {{"axis", 1}, {"dims", {1, 768, 17, 17}}}), concat2);
Paul Fultz II's avatar
Paul Fultz II committed
1646
1647
        auto add    = m2.add_instruction(migraphx::make_op("add"), conv, b4);
        auto slice1 = m2.add_instruction(
1648
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {384}}}), add);
Paul Fultz II's avatar
Paul Fultz II committed
1649
        auto slice2 = m2.add_instruction(
1650
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {384}}, {"ends", {768}}}), add);
Paul Fultz II's avatar
Paul Fultz II committed
1651
        m2.add_instruction(pass_op{}, slice1, slice2);
1652
    }
Paul Fultz II's avatar
Paul Fultz II committed
1653
    EXPECT(m1.sort() == m2.sort());
1654
}
1655
1656
1657
1658
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
1659
1660
    auto create_m1             = [&](std::size_t batch_size) {
        migraphx::module m1;
1661
        auto s     = migraphx::shape{migraphx::shape::float_type, {batch_size, 128, 1920}};
Paul Fultz II's avatar
Paul Fultz II committed
1662
1663
        auto input = m1.add_parameter("input", s);
        auto slc0  = m1.add_instruction(
1664
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {640}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1665
        auto slc1 = m1.add_instruction(
1666
1667
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {640}}, {"ends", {1280}}}),
            input);
Paul Fultz II's avatar
Paul Fultz II committed
1668
        auto slc2 = m1.add_instruction(
1669
1670
1671
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {1280}}, {"ends", {1920}}}),
            input);

Paul Fultz II's avatar
Paul Fultz II committed
1672
1673
1674
        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);
1675
1676

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

Paul Fultz II's avatar
Paul Fultz II committed
1681
1682
1683
        auto t0 = m1.add_instruction(migraphx::make_op("transpose", {{"dims", perm0}}), r0);
        auto t1 = m1.add_instruction(migraphx::make_op("transpose", {{"dims", perm0}}), r1);
        auto t2 = m1.add_instruction(migraphx::make_op("transpose", {{"dims", perm1}}), r2);
1684

Paul Fultz II's avatar
Paul Fultz II committed
1685
1686
1687
        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});
1688

Paul Fultz II's avatar
Paul Fultz II committed
1689
        return m1;
1690
1691
    };

Paul Fultz II's avatar
Paul Fultz II committed
1692
1693
    auto create_m2 = [&](std::size_t batch_size) {
        migraphx::module m2;
1694
        auto s     = migraphx::shape{migraphx::shape::float_type, {batch_size, 128, 1920}};
Paul Fultz II's avatar
Paul Fultz II committed
1695
        auto input = m2.add_parameter("input", s);
1696
        std::vector<int64_t> lens = {static_cast<int64_t>(batch_size), 128, 30, 64};
Paul Fultz II's avatar
Paul Fultz II committed
1697
        auto r = m2.add_instruction(migraphx::make_op("reshape", {{"dims", lens}}), input);
1698

Paul Fultz II's avatar
Paul Fultz II committed
1699
        auto slc0 = m2.add_instruction(
1700
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {10}}}), r);
Paul Fultz II's avatar
Paul Fultz II committed
1701
        auto slc1 = m2.add_instruction(
1702
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {10}}, {"ends", {20}}}), r);
Paul Fultz II's avatar
Paul Fultz II committed
1703
        auto slc2 = m2.add_instruction(
1704
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {20}}, {"ends", {30}}}), r);
1705

Paul Fultz II's avatar
Paul Fultz II committed
1706
1707
1708
        auto t0 = m2.add_instruction(migraphx::make_op("transpose", {{"dims", perm0}}), slc0);
        auto t1 = m2.add_instruction(migraphx::make_op("transpose", {{"dims", perm0}}), slc1);
        auto t2 = m2.add_instruction(migraphx::make_op("transpose", {{"dims", perm1}}), slc2);
1709

Paul Fultz II's avatar
Paul Fultz II committed
1710
1711
1712
        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});
1713

Paul Fultz II's avatar
Paul Fultz II committed
1714
        return m2;
1715
1716
1717
    };

    auto test = [&](std::size_t batch_size) {
Paul Fultz II's avatar
Paul Fultz II committed
1718
1719
1720
1721
        auto m1 = create_m1(batch_size);
        run_pass(m1);
        auto m2 = create_m2(batch_size);
        EXPECT(m1.sort() == m2.sort());
1722
1723
1724
1725
1726
1727
1728
    };

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

1729
TEST_CASE(reorder_reshape_slice_move_axis1)
1730
{
Paul Fultz II's avatar
Paul Fultz II committed
1731
1732
1733
    auto create_m1 = [](std::size_t batch_size) {
        migraphx::module m1;
        auto s = migraphx::shape{migraphx::shape::float_type, {batch_size, 256, 96}};
1734
1735
        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
1736
1737
        auto input                 = m1.add_parameter("input", s);
        auto slc0                  = m1.add_instruction(
1738
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {32}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1739
        auto slc1 = m1.add_instruction(
1740
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {32}}, {"ends", {64}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1741
        auto slc2 = m1.add_instruction(
1742
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {64}}, {"ends", {96}}}), input);
1743

Paul Fultz II's avatar
Paul Fultz II committed
1744
1745
1746
        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);
1747

1748
        std::vector<int64_t> lens = {static_cast<int64_t>(batch_size), 64, 4, 32};
Paul Fultz II's avatar
Paul Fultz II committed
1749
1750
1751
        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);
1752

Paul Fultz II's avatar
Paul Fultz II committed
1753
1754
1755
        auto t0 = m1.add_instruction(migraphx::make_op("transpose", {{"dims", perm0}}), r0);
        auto t1 = m1.add_instruction(migraphx::make_op("transpose", {{"dims", perm0}}), r1);
        auto t2 = m1.add_instruction(migraphx::make_op("transpose", {{"dims", perm1}}), r2);
1756

Paul Fultz II's avatar
Paul Fultz II committed
1757
1758
1759
        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});
1760

Paul Fultz II's avatar
Paul Fultz II committed
1761
        return m1;
1762
1763
    };

Paul Fultz II's avatar
Paul Fultz II committed
1764
1765
1766
    auto create_m2 = [](std::size_t batch_size) {
        migraphx::module m;
        auto s = migraphx::shape{migraphx::shape::float_type, {batch_size, 256, 96}};
1767
1768
        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
1769
        auto input                 = m.add_parameter("input", s);
1770
        std::vector<int64_t> lens  = {static_cast<int64_t>(batch_size), 64, 4, 96};
Paul Fultz II's avatar
Paul Fultz II committed
1771
1772
        auto rsp  = m.add_instruction(migraphx::make_op("reshape", {{"dims", lens}}), input);
        auto slc0 = m.add_instruction(
1773
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {0}}, {"ends", {32}}}), rsp);
Paul Fultz II's avatar
Paul Fultz II committed
1774
1775
        auto t0   = m.add_instruction(migraphx::make_op("transpose", {{"dims", perm0}}), slc0);
        auto slc1 = m.add_instruction(
1776
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {32}}, {"ends", {64}}}), rsp);
Paul Fultz II's avatar
Paul Fultz II committed
1777
1778
        auto t1   = m.add_instruction(migraphx::make_op("transpose", {{"dims", perm0}}), slc1);
        auto slc2 = m.add_instruction(
1779
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {64}}, {"ends", {96}}}), rsp);
Paul Fultz II's avatar
Paul Fultz II committed
1780
        auto t2 = m.add_instruction(migraphx::make_op("transpose", {{"dims", perm1}}), slc2);
1781

Paul Fultz II's avatar
Paul Fultz II committed
1782
1783
1784
        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});
1785

Paul Fultz II's avatar
Paul Fultz II committed
1786
        return m;
1787
1788
    };

1789
    auto test = [&](std::size_t batch_size) {
Paul Fultz II's avatar
Paul Fultz II committed
1790
1791
1792
1793
        auto m1 = create_m1(batch_size);
        auto m2 = create_m2(batch_size);
        run_pass(m1);
        EXPECT(m1.sort() == m2.sort());
1794
1795
1796
1797
1798
1799
    };

    test(4);
    test(8);
}

1800
1801
TEST_CASE(reorder_reshape_slice_move_axis2)
{
Paul Fultz II's avatar
Paul Fultz II committed
1802
1803
    auto create_m1 = [] {
        migraphx::module m1;
1804
        migraphx::shape s{migraphx::shape::float_type, {128, 96}};
Paul Fultz II's avatar
Paul Fultz II committed
1805
1806
        auto input = m1.add_parameter("input", s);
        auto slc0  = m1.add_instruction(
1807
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {32}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1808
        auto slc1 = m1.add_instruction(
1809
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {32}}, {"ends", {64}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1810
        auto slc2 = m1.add_instruction(
1811
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {64}}, {"ends", {96}}}), input);
1812

Paul Fultz II's avatar
Paul Fultz II committed
1813
1814
1815
        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);
1816
1817

        std::vector<int64_t> lens = {1, 16, 8, 32};
Paul Fultz II's avatar
Paul Fultz II committed
1818
1819
1820
        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);
1821

Paul Fultz II's avatar
Paul Fultz II committed
1822
1823
1824
        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});
1825

Paul Fultz II's avatar
Paul Fultz II committed
1826
        return m1;
1827
1828
    };

Paul Fultz II's avatar
Paul Fultz II committed
1829
1830
    auto create_m2 = [] {
        migraphx::module m;
1831
        auto s                    = migraphx::shape{migraphx::shape::float_type, {128, 96}};
Paul Fultz II's avatar
Paul Fultz II committed
1832
        auto input                = m.add_parameter("input", s);
1833
        std::vector<int64_t> lens = {1, 16, 8, 96};
Paul Fultz II's avatar
Paul Fultz II committed
1834
1835
        auto rsp  = m.add_instruction(migraphx::make_op("reshape", {{"dims", lens}}), input);
        auto slc0 = m.add_instruction(
1836
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {0}}, {"ends", {32}}}), rsp);
Paul Fultz II's avatar
Paul Fultz II committed
1837
        auto slc1 = m.add_instruction(
1838
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {32}}, {"ends", {64}}}), rsp);
Paul Fultz II's avatar
Paul Fultz II committed
1839
        auto slc2 = m.add_instruction(
1840
1841
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {64}}, {"ends", {96}}}), rsp);

Paul Fultz II's avatar
Paul Fultz II committed
1842
1843
1844
        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});
1845

Paul Fultz II's avatar
Paul Fultz II committed
1846
        return m;
1847
1848
    };

Paul Fultz II's avatar
Paul Fultz II committed
1849
1850
1851
1852
    auto m1 = create_m1();
    auto m2 = create_m2();
    run_pass(m1);
    EXPECT(m1.sort() == m2.sort());
1853
1854
1855
1856
1857
}

TEST_CASE(reorder_reshape_slice_not_apply)
{
    auto create_p = [] {
Paul Fultz II's avatar
Paul Fultz II committed
1858
        migraphx::module m;
1859
        migraphx::shape s{migraphx::shape::float_type, {128, 96}};
Paul Fultz II's avatar
Paul Fultz II committed
1860
1861
        auto input = m.add_parameter("input", s);
        auto slc0  = m.add_instruction(
1862
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {32}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1863
        auto slc1 = m.add_instruction(
1864
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {32}}, {"ends", {64}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1865
        auto slc2 = m.add_instruction(
1866
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {64}}, {"ends", {96}}}), input);
1867

Paul Fultz II's avatar
Paul Fultz II committed
1868
1869
1870
        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);
1871
1872

        std::vector<int64_t> lens = {1, 16, 16, 16};
Paul Fultz II's avatar
Paul Fultz II committed
1873
1874
1875
        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);
1876

Paul Fultz II's avatar
Paul Fultz II committed
1877
1878
1879
        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});
1880

Paul Fultz II's avatar
Paul Fultz II committed
1881
        return m;
1882
1883
    };

Paul Fultz II's avatar
Paul Fultz II committed
1884
1885
1886
1887
    auto m1 = create_p();
    auto m2 = m1;
    run_pass(m1);
    EXPECT(m1.sort() == m2.sort());
1888
1889
}

1890
1891
TEST_CASE(reorder_reshape_slice_diff_dims)
{
Paul Fultz II's avatar
Paul Fultz II committed
1892
1893
1894
    auto create_m1 = [](std::size_t batch_size) {
        migraphx::module m1;
        auto s = migraphx::shape{migraphx::shape::float_type, {batch_size, 96, 96}};
1895
1896
        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
1897
1898
        auto input                 = m1.add_parameter("input", s);
        auto slc0                  = m1.add_instruction(
1899
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {32}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1900
        auto slc1 = m1.add_instruction(
1901
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {32}}, {"ends", {64}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1902
        auto slc2 = m1.add_instruction(
1903
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {64}}, {"ends", {96}}}), input);
1904

Paul Fultz II's avatar
Paul Fultz II committed
1905
1906
1907
        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);
1908
1909
1910

        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
1911
1912
1913
        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);
1914

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

Paul Fultz II's avatar
Paul Fultz II committed
1917
        return m1;
1918
1919
1920
    };

    auto test = [&](std::size_t batch_size) {
Paul Fultz II's avatar
Paul Fultz II committed
1921
1922
1923
1924
        auto m1 = create_m1(batch_size);
        auto m2 = m1;
        run_pass(m1);
        EXPECT(m1.sort() == m2.sort());
1925
1926
1927
1928
1929
1930
1931
1932
1933
    };

    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
1934
1935
    auto create_m1            = [&](std::size_t batch_size) {
        migraphx::module m1;
1936
        auto s     = migraphx::shape{migraphx::shape::float_type, {batch_size, 128, 1920}};
Paul Fultz II's avatar
Paul Fultz II committed
1937
1938
        auto input = m1.add_parameter("input", s);
        auto slc0  = m1.add_instruction(
1939
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {640}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1940
        auto slc1 = m1.add_instruction(
1941
1942
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {640}}, {"ends", {1280}}}),
            input);
Paul Fultz II's avatar
Paul Fultz II committed
1943
        auto slc2 = m1.add_instruction(
1944
1945
1946
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {1280}}, {"ends", {1920}}}),
            input);

Paul Fultz II's avatar
Paul Fultz II committed
1947
1948
1949
        auto t0 = m1.add_instruction(migraphx::make_op("transpose", {{"dims", perm}}), slc0);
        auto t1 = m1.add_instruction(migraphx::make_op("transpose", {{"dims", perm}}), slc1);
        auto t2 = m1.add_instruction(migraphx::make_op("transpose", {{"dims", perm}}), slc2);
1950

Paul Fultz II's avatar
Paul Fultz II committed
1951
1952
1953
        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});
1954

Paul Fultz II's avatar
Paul Fultz II committed
1955
        return m1;
1956
1957
    };

Paul Fultz II's avatar
Paul Fultz II committed
1958
1959
    auto create_m2 = [&](std::size_t batch_size) {
        migraphx::module m2;
1960
        auto s     = migraphx::shape{migraphx::shape::float_type, {batch_size, 128, 1920}};
Paul Fultz II's avatar
Paul Fultz II committed
1961
1962
        auto input = m2.add_parameter("input", s);
        auto r     = m2.add_instruction(migraphx::make_op("transpose", {{"dims", perm}}), input);
1963

Paul Fultz II's avatar
Paul Fultz II committed
1964
        auto slc0 = m2.add_instruction(
1965
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {640}}}), r);
Paul Fultz II's avatar
Paul Fultz II committed
1966
        auto slc1 = m2.add_instruction(
1967
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {640}}, {"ends", {1280}}}), r);
Paul Fultz II's avatar
Paul Fultz II committed
1968
        auto slc2 = m2.add_instruction(
1969
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1280}}, {"ends", {1920}}}), r);
1970

Paul Fultz II's avatar
Paul Fultz II committed
1971
1972
1973
        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});
1974

Paul Fultz II's avatar
Paul Fultz II committed
1975
        return m2;
1976
1977
1978
    };

    auto test = [&](std::size_t batch_size) {
Paul Fultz II's avatar
Paul Fultz II committed
1979
1980
1981
1982
        auto m1 = create_m1(batch_size);
        run_pass(m1);
        auto m2 = create_m2(batch_size);
        EXPECT(m1.sort() == m2.sort());
1983
1984
1985
1986
1987
1988
1989
1990
    };

    test(1);
    test(8);
}

TEST_CASE(reorder_slice_trans_diff_perm)
{
Paul Fultz II's avatar
Paul Fultz II committed
1991
1992
1993
    auto create_m1 = [](std::size_t batch_size) {
        migraphx::module m1;
        auto s = migraphx::shape{migraphx::shape::float_type, {batch_size, 128, 1920}};
1994
1995
        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
1996
1997
        auto input                 = m1.add_parameter("input", s);
        auto slc0                  = m1.add_instruction(
1998
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {640}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1999
        auto slc1 = m1.add_instruction(
2000
2001
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {640}}, {"ends", {1280}}}),
            input);
Paul Fultz II's avatar
Paul Fultz II committed
2002
        auto slc2 = m1.add_instruction(
2003
2004
2005
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {1280}}, {"ends", {1920}}}),
            input);

Paul Fultz II's avatar
Paul Fultz II committed
2006
2007
2008
        auto t0 = m1.add_instruction(migraphx::make_op("transpose", {{"dims", perm0}}), slc0);
        auto t1 = m1.add_instruction(migraphx::make_op("transpose", {{"dims", perm0}}), slc1);
        auto t2 = m1.add_instruction(migraphx::make_op("transpose", {{"dims", perm1}}), slc2);
2009

Paul Fultz II's avatar
Paul Fultz II committed
2010
2011
2012
        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});
2013

Paul Fultz II's avatar
Paul Fultz II committed
2014
        return m1;
2015
2016
2017
    };

    auto test = [&](std::size_t batch_size) {
Paul Fultz II's avatar
Paul Fultz II committed
2018
2019
2020
2021
        auto m1 = create_m1(batch_size);
        run_pass(m1);
        auto m2 = m1;
        EXPECT(m1.sort() == m2.sort());
2022
2023
2024
2025
2026
2027
    };

    test(1);
    test(4);
}

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