simplify_algebra_test.cpp 154 KB
Newer Older
1
2
3
/*
 * The MIT License (MIT)
 *
4
 * Copyright (c) 2015-2023 Advanced Micro Devices, Inc. All rights reserved.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
Paul's avatar
Paul committed
24
25
#include <migraphx/simplify_algebra.hpp>
#include <migraphx/dead_code_elimination.hpp>
26
#include <migraphx/pass_manager.hpp>
27
#include <migraphx/permutation.hpp>
Paul's avatar
Paul committed
28
29
30
#include <migraphx/generate.hpp>
#include <migraphx/ranges.hpp>
#include <migraphx/instruction.hpp>
Paul's avatar
Paul committed
31
#include <basic_ops.hpp>
32
#include <migraphx/make_op.hpp>
Paul's avatar
Paul committed
33
34
#include <test.hpp>

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

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

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

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

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

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

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

125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
TEST_CASE(simplify_zero_add_constant)
{
    migraphx::module m1;
    {
        auto x    = m1.add_parameter("x", {migraphx::shape::int32_type, {1}});
        auto zero = m1.add_literal(0);
        m1.add_instruction(migraphx::make_op("add"), zero, x);
    }
    run_pass(m1);

    migraphx::module m2;
    {
        auto x = m2.add_parameter("x", {migraphx::shape::int32_type, {1}});
        m2.add_instruction(migraphx::make_op("identity"), x);
    }

    migraphx::module m3;
    {
        auto x    = m3.add_parameter("x", {migraphx::shape::int32_type, {1}});
        auto zero = m3.add_literal(0);
        m3.add_instruction(migraphx::make_op("add"), x, zero);
    }
    run_pass(m3);

    EXPECT((m1 == m2) && (m2 == m3));
}

152
153
154
155
TEST_CASE(simplify_add_broadcast1)
{
    migraphx::shape inner{migraphx::shape::int32_type, {2}};
    migraphx::shape outer{migraphx::shape::int32_type, {1, 2, 3, 3}};
156
    auto b = migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 2, 3, 3}}});
Paul Fultz II's avatar
Paul Fultz II committed
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
    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);
185
186
187
188
189
190
}

TEST_CASE(simplify_add_broadcast2)
{
    migraphx::shape inner{migraphx::shape::int32_type, {2}};
    migraphx::shape outer{migraphx::shape::int32_type, {1, 2, 3, 3}};
191
    auto b              = migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 2, 3, 3}}});
192
    auto create_program = [&] {
Paul Fultz II's avatar
Paul Fultz II committed
193
194
195
196
197
198
199
200
201
202
203
        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;
204
    };
Paul Fultz II's avatar
Paul Fultz II committed
205
206
    migraphx::module m1 = create_program();
    run_pass(m1);
207

Paul Fultz II's avatar
Paul Fultz II committed
208
209
    migraphx::module m2 = create_program();
    EXPECT(m1 == m2);
210
211
}

Paul's avatar
Paul committed
212
// TODO: Add test case
213
// TEST_CASE(simplify_add4)
Paul's avatar
Paul committed
214
215
void simplify_add4()
{
Paul Fultz II's avatar
Paul Fultz II committed
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
    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
241
242
}

Paul's avatar
Paul committed
243
244
TEST_CASE(simplify_mul_conv1)
{
Paul Fultz II's avatar
Paul Fultz II committed
245
246
247
248
249
    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(
250
251
252
253
        migraphx::make_op("convolution",
                          {{"padding", {1, 1}}, {"stride", {2, 2}}, {"dilation", {1, 1}}}),
        x,
        w);
Paul Fultz II's avatar
Paul Fultz II committed
254
255
    auto a = m.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {256}}));
    auto b = m.add_instruction(
256
        migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 256, 14, 14}}}), a);
Paul Fultz II's avatar
Paul Fultz II committed
257
258
    auto mul = m.add_instruction(migraphx::make_op("mul"), conv, b);
    m.add_instruction(pass_op{}, mul);
Paul's avatar
Paul committed
259
    EXPECT(conv->outputs().front()->name() == "mul");
Paul Fultz II's avatar
Paul Fultz II committed
260
261
262
    run_pass(m);
    auto new_conv =
        std::find_if(m.begin(), m.end(), [](auto&& ins) { return ins.name() == "convolution"; });
Paul's avatar
Paul committed
263
264
265
    EXPECT(new_conv->outputs().front()->name() != "mul");
}

266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
TEST_CASE(simplify_mul_conv2)
{
    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(
        migraphx::make_op("convolution",
                          {{"padding", {1, 1}}, {"stride", {2, 2}}, {"dilation", {1, 1}}}),
        x,
        w);
    auto a      = m.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {256}}));
    auto unsq_a = m.add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1, 2}}}), a);
    auto b      = m.add_instruction(
        migraphx::make_op("multibroadcast", {{"out_lens", {1, 256, 14, 14}}}), unsq_a);
    auto mul = m.add_instruction(migraphx::make_op("mul"), conv, b);
    m.add_instruction(pass_op{}, mul);
    EXPECT(conv->outputs().front()->name() == "mul");
    run_pass(m);
    auto new_conv =
        std::find_if(m.begin(), m.end(), [](auto&& ins) { return ins.name() == "convolution"; });
    EXPECT(new_conv->outputs().front()->name() != "mul");
}

// len = 1 case
TEST_CASE(simplify_mul_conv3)
{
    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(
        migraphx::make_op("convolution",
                          {{"padding", {1, 1}}, {"stride", {2, 2}}, {"dilation", {1, 1}}}),
        x,
        w);
    auto a = m.add_literal(
        migraphx::generate_literal({migraphx::shape::int32_type, {256, 1, 1}, {1, 18, 1}}));
    auto b =
        m.add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {1, 256, 14, 14}}}), a);
    auto mul = m.add_instruction(migraphx::make_op("mul"), conv, b);
    m.add_instruction(pass_op{}, mul);
    EXPECT(conv->outputs().front()->name() == "mul");
    run_pass(m);
    auto new_conv =
        std::find_if(m.begin(), m.end(), [](auto&& ins) { return ins.name() == "convolution"; });
    EXPECT(new_conv->outputs().front()->name() != "mul");
}

// Previously broadcasted literal case, should skip
TEST_CASE(simplify_mul_conv_skip1)
{
    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(
        migraphx::make_op("convolution",
                          {{"padding", {1, 1}}, {"stride", {2, 2}}, {"dilation", {1, 1}}}),
        x,
        w);
    auto a = m.add_literal(
        migraphx::generate_literal({migraphx::shape::int32_type, {256, 14, 14}, {1, 0, 0}}));
    auto b = m.add_instruction(
        migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 256, 14, 14}}}), a);
    auto mul = m.add_instruction(migraphx::make_op("mul"), conv, b);
    m.add_instruction(pass_op{}, mul);
    EXPECT(conv->outputs().front()->name() == "mul");
    run_pass(m);
    auto new_conv =
        std::find_if(m.begin(), m.end(), [](auto&& ins) { return ins.name() == "convolution"; });
    EXPECT(new_conv->outputs().front()->name() == "mul");
}

// Another previously broadcasted literal case, should skip
TEST_CASE(simplify_mul_conv_skip2)
{
    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(
        migraphx::make_op("convolution",
                          {{"padding", {1, 1}}, {"stride", {2, 2}}, {"dilation", {1, 1}}}),
        x,
        w);
    auto a = m.add_literal(
        migraphx::generate_literal({migraphx::shape::int32_type, {256, 14, 14}, {1, 0, 0}}));
    auto b =
        m.add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {1, 256, 14, 14}}}), a);
    auto mul = m.add_instruction(migraphx::make_op("mul"), conv, b);
    m.add_instruction(pass_op{}, mul);
    EXPECT(conv->outputs().front()->name() == "mul");
    run_pass(m);
    auto new_conv =
        std::find_if(m.begin(), m.end(), [](auto&& ins) { return ins.name() == "convolution"; });
    EXPECT(new_conv->outputs().front()->name() == "mul");
}

365
366
TEST_CASE(simplify_mul_slice_conv1)
{
Paul Fultz II's avatar
Paul Fultz II committed
367
    migraphx::module m1;
368
    {
Paul Fultz II's avatar
Paul Fultz II committed
369
370
        auto x = m1.add_parameter("x", {migraphx::shape::int32_type, {1, 1024, 17, 17}});
        auto w = m1.add_literal(
371
            migraphx::generate_literal({migraphx::shape::int32_type, {768, 1024, 1, 1}}));
Paul Fultz II's avatar
Paul Fultz II committed
372
373
        auto conv   = m1.add_instruction(migraphx::make_op("convolution"), x, w);
        auto slice1 = m1.add_instruction(
374
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {384}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
375
376
        auto a = m1.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {384}}));
        auto b = m1.add_instruction(
377
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 384, 17, 17}}}), a);
Paul Fultz II's avatar
Paul Fultz II committed
378
379
        auto mul    = m1.add_instruction(migraphx::make_op("mul"), slice1, b);
        auto slice2 = m1.add_instruction(
380
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {384}}, {"ends", {768}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
381
382
        auto add = m1.add_instruction(migraphx::make_op("add"), mul, slice2);
        m1.add_instruction(pass_op{}, add);
383
    }
Paul Fultz II's avatar
Paul Fultz II committed
384
    run_pass(m1);
385

Paul Fultz II's avatar
Paul Fultz II committed
386
    migraphx::module m2;
387
    {
Paul Fultz II's avatar
Paul Fultz II committed
388
389
        auto x = m2.add_parameter("x", {migraphx::shape::int32_type, {1, 1024, 17, 17}});
        auto w = m2.add_literal(
390
            migraphx::generate_literal({migraphx::shape::int32_type, {768, 1024, 1, 1}}));
Paul Fultz II's avatar
Paul Fultz II committed
391
        auto wslice1 = m2.add_instruction(
392
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {384}}}), w);
Paul Fultz II's avatar
Paul Fultz II committed
393
394
        auto a = m2.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {384}}));
        auto b = m2.add_instruction(
395
            migraphx::make_op("broadcast", {{"axis", 0}, {"out_lens", {384, 1024, 1, 1}}}), a);
Paul Fultz II's avatar
Paul Fultz II committed
396
397
        auto mul     = m2.add_instruction(migraphx::make_op("mul"), b, wslice1);
        auto wslice2 = m2.add_instruction(
398
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {384}}, {"ends", {768}}}), w);
Paul Fultz II's avatar
Paul Fultz II committed
399
400
401
        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(
402
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {384}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
403
        auto slice2 = m2.add_instruction(
404
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {384}}, {"ends", {768}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
405
406
        auto add = m2.add_instruction(migraphx::make_op("add"), slice1, slice2);
        m2.add_instruction(pass_op{}, add);
407
    }
Paul Fultz II's avatar
Paul Fultz II committed
408
    EXPECT(m1 == m2);
409
410
411
412
}

TEST_CASE(simplify_mul_slice_conv_overlapping_slice)
{
Paul Fultz II's avatar
Paul Fultz II committed
413
    migraphx::module m1;
414
    {
Paul Fultz II's avatar
Paul Fultz II committed
415
416
        auto x = m1.add_parameter("x", {migraphx::shape::int32_type, {1, 1024, 17, 17}});
        auto w = m1.add_literal(
417
            migraphx::generate_literal({migraphx::shape::int32_type, {768, 1024, 1, 1}}));
Paul Fultz II's avatar
Paul Fultz II committed
418
419
        auto conv   = m1.add_instruction(migraphx::make_op("convolution"), x, w);
        auto slice1 = m1.add_instruction(
420
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {384}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
421
422
        auto a = m1.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {384}}));
        auto b = m1.add_instruction(
423
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 384, 17, 17}}}), a);
Paul Fultz II's avatar
Paul Fultz II committed
424
425
        auto mul    = m1.add_instruction(migraphx::make_op("mul"), slice1, b);
        auto slice2 = m1.add_instruction(
426
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {383}}, {"ends", {767}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
427
428
        auto add = m1.add_instruction(migraphx::make_op("add"), mul, slice2);
        m1.add_instruction(pass_op{}, add);
429
    }
Paul Fultz II's avatar
Paul Fultz II committed
430
431
432
    migraphx::module m2 = m1;
    run_pass(m1);
    EXPECT(m1 == m2);
433
434
435
436
}

TEST_CASE(simplify_mul_slice_conv_not_all_slice)
{
Paul Fultz II's avatar
Paul Fultz II committed
437
    migraphx::module m1;
438
    {
Paul Fultz II's avatar
Paul Fultz II committed
439
440
        auto x = m1.add_parameter("x", {migraphx::shape::int32_type, {1, 1024, 17, 17}});
        auto w = m1.add_literal(
441
            migraphx::generate_literal({migraphx::shape::int32_type, {768, 1024, 1, 1}}));
Paul Fultz II's avatar
Paul Fultz II committed
442
443
        auto conv   = m1.add_instruction(migraphx::make_op("convolution"), x, w);
        auto slice1 = m1.add_instruction(
444
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {384}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
445
446
        auto a = m1.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {384}}));
        auto b = m1.add_instruction(
447
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 384, 17, 17}}}), a);
Paul Fultz II's avatar
Paul Fultz II committed
448
449
        auto mul = m1.add_instruction(migraphx::make_op("mul"), slice1, b);
        auto c   = m1.add_literal(
450
            migraphx::generate_literal({migraphx::shape::int32_type, {1, 768, 17, 17}}));
Paul Fultz II's avatar
Paul Fultz II committed
451
452
453
        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);
454
    }
Paul Fultz II's avatar
Paul Fultz II committed
455
456
457
    migraphx::module m2 = m1;
    run_pass(m1);
    EXPECT(m1 == m2);
458
459
}

Paul's avatar
Paul committed
460
461
TEST_CASE(simplify_mul_add)
{
Paul Fultz II's avatar
Paul Fultz II committed
462
463
464
    migraphx::module m1;
    {
        auto x   = m1.add_parameter("x", {migraphx::shape::int32_type, {1}});
465
        auto one = m1.add_literal(3);
Paul Fultz II's avatar
Paul Fultz II committed
466
467
468
469
470
471
472
473
474
475
        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}});
476
        auto one  = m2.add_literal(3);
Paul Fultz II's avatar
Paul Fultz II committed
477
478
479
480
481
482
483
        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
484
485
}

486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
TEST_CASE(simplify_dot_add)
{
    migraphx::module m1;
    {
        auto x   = m1.add_parameter("x", {migraphx::shape::float_type, {2, 2}});
        auto one = m1.add_literal(get_2x2());
        auto two = m1.add_literal(get_2x2(1));
        auto sum = m1.add_instruction(migraphx::make_op("add"), one, x);
        auto dot = m1.add_instruction(migraphx::make_op("dot"), sum, two);
        m1.add_instruction(pass_op{}, dot);
    }
    run_pass(m1);

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

512
513
514
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
TEST_CASE(simplify_conv_add)
{
    migraphx::shape s{migraphx::shape::float_type, {1, 3, 32, 32}};
    migraphx::shape ws{migraphx::shape::float_type, {4, 3, 3, 3}};
    migraphx::module m1;
    {
        auto x    = m1.add_parameter("x", s);
        auto c    = m1.add_literal(migraphx::generate_literal(s, 1));
        auto w    = m1.add_literal(migraphx::generate_literal(ws, 2));
        auto sum  = m1.add_instruction(migraphx::make_op("add"), c, x);
        auto conv = m1.add_instruction(migraphx::make_op("convolution"), sum, w);
        m1.add_instruction(pass_op{}, conv);
    }
    run_pass(m1);

    migraphx::module m2;
    {
        auto x     = m2.add_parameter("x", s);
        auto c     = m2.add_literal(migraphx::generate_literal(s, 1));
        auto w     = m2.add_literal(migraphx::generate_literal(ws, 2));
        auto conv1 = m2.add_instruction(migraphx::make_op("convolution"), c, w);
        auto conv2 = m2.add_instruction(migraphx::make_op("convolution"), x, w);
        auto sum   = m2.add_instruction(migraphx::make_op("add"), conv1, conv2);
        m2.add_instruction(pass_op{}, sum);
    }
    EXPECT(m1 == m2);
}

540
TEST_CASE(simplify_inner_broadcast1)
Paul's avatar
Paul committed
541
{
542
    auto b = migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {2, 1, 4, 5}}});
Paul Fultz II's avatar
Paul Fultz II committed
543
    migraphx::module m1;
Paul's avatar
Paul committed
544
    {
Paul Fultz II's avatar
Paul Fultz II committed
545
546
547
548
549
550
        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
551
    }
Paul Fultz II's avatar
Paul Fultz II committed
552
    run_pass(m1);
Paul's avatar
Paul committed
553

Paul Fultz II's avatar
Paul Fultz II committed
554
    migraphx::module m2;
Paul's avatar
Paul committed
555
    {
Paul Fultz II's avatar
Paul Fultz II committed
556
557
558
559
560
        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
561
    }
Paul Fultz II's avatar
Paul Fultz II committed
562
    EXPECT(m1 == m2);
Paul's avatar
Paul committed
563
564
}

565
566
TEST_CASE(simplify_inner_broadcast2)
{
567
    auto b = migraphx::make_op("multibroadcast", {{"out_lens", {2, 1, 4, 5}}});
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
    migraphx::module m1;
    {
        auto x   = m1.add_parameter("x", {migraphx::shape::int32_type, {1, 1, 1, 1}});
        auto y   = m1.add_parameter("y", {migraphx::shape::int32_type, {1, 1, 1, 1}});
        auto xb  = m1.add_instruction(b, x);
        auto yb  = m1.add_instruction(b, y);
        auto sum = m1.add_instruction(migraphx::make_op("add"), xb, yb);
        m1.add_instruction(pass_op{}, sum);
    }
    run_pass(m1);

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

TEST_CASE(simplify_inner_broadcast_scalar)
{
592
    auto b = migraphx::make_op("multibroadcast", {{"out_lens", {32, 384}}});
593
594
595
596
597
598
599
600
601
602
603
604
605
    migraphx::module m1;
    {
        auto x   = m1.add_parameter("x", {migraphx::shape::int32_type, {1, 384}});
        auto y   = m1.add_parameter("y", {migraphx::shape::int32_type, {1, 1}});
        auto xb  = m1.add_instruction(b, x);
        auto yb  = m1.add_instruction(b, y);
        auto sum = m1.add_instruction(migraphx::make_op("add"), xb, yb);
        m1.add_instruction(pass_op{}, sum);
    }
    run_pass(m1);

    migraphx::module m2;
    {
606
607
        auto x = m2.add_parameter("x", {migraphx::shape::int32_type, {1, 384}});
        auto y = m2.add_parameter("y", {migraphx::shape::int32_type, {1, 1}});
608
609
        auto yb =
            m2.add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {1, 384}}}), y);
610
611
612
613
614
        auto sum  = m2.add_instruction(migraphx::make_op("add"), x, yb);
        auto sumb = m2.add_instruction(b, sum);
        m2.add_instruction(pass_op{}, sumb);
    }
    EXPECT(m1 == m2);
615
616
}

617
618
TEST_CASE(simplify_inner_broadcast_different_dims)
{
619
    auto b = migraphx::make_op("multibroadcast", {{"out_lens", {2, 384, 768}}});
620
621
622
623
624
625
626
627
628
629
630
631
632
    migraphx::module m1;
    {
        auto x   = m1.add_parameter("x", {migraphx::shape::int32_type, {384, 768}});
        auto y   = m1.add_parameter("y", {migraphx::shape::int32_type, {768}});
        auto xb  = m1.add_instruction(b, x);
        auto yb  = m1.add_instruction(b, y);
        auto sum = m1.add_instruction(migraphx::make_op("add"), xb, yb);
        m1.add_instruction(pass_op{}, sum);
    }
    run_pass(m1);

    migraphx::module m2;
    {
633
634
        auto x = m2.add_parameter("x", {migraphx::shape::int32_type, {384, 768}});
        auto y = m2.add_parameter("y", {migraphx::shape::int32_type, {768}});
635
636
        auto yb =
            m2.add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {384, 768}}}), y);
637
638
639
640
641
642
643
644
645
        auto sum  = m2.add_instruction(migraphx::make_op("add"), x, yb);
        auto sumb = m2.add_instruction(b, sum);
        m2.add_instruction(pass_op{}, sumb);
    }
    EXPECT(m1 == m2);
}

TEST_CASE(simplify_inner_broadcast_different_broadcasts)
{
646
647
    auto b  = migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 24, 112, 112}}});
    auto mb = migraphx::make_op("multibroadcast", {{"out_lens", {1, 24, 112, 112}}});
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
    migraphx::module m1;
    {
        auto x   = m1.add_parameter("x", {migraphx::shape::int32_type, {24}});
        auto y   = m1.add_parameter("y", {migraphx::shape::int32_type, {24, 1, 1}});
        auto xb  = m1.add_instruction(b, x);
        auto yb  = m1.add_instruction(mb, y);
        auto sum = m1.add_instruction(migraphx::make_op("add"), xb, yb);
        m1.add_instruction(pass_op{}, sum);
    }
    run_pass(m1);

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

672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
TEST_CASE(simplify_inner_broadcast_no_common_axis)
{
    auto b = migraphx::make_op("multibroadcast", {{"out_lens", {1, 5, 10}}});
    migraphx::module m1;
    {
        auto x   = m1.add_parameter("x", {migraphx::shape::int32_type, {5, 10}});
        auto y   = m1.add_parameter("y", {migraphx::shape::int32_type, {1, 5, 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);
    }
    migraphx::module m2 = m1;
    run_pass(m1);
    EXPECT(m1 == m2);
}

689
690
TEST_CASE(simplify_add_conv1)
{
Paul Fultz II's avatar
Paul Fultz II committed
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
    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);
707
708
709
710
}

TEST_CASE(simplify_add_conv_no_fusion_7x7_diff_strides)
{
Paul Fultz II's avatar
Paul Fultz II committed
711
712
713
714
715
716
717
718
719
    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(
720
        migraphx::make_op("convolution", {{"padding", {0, 0}}, {"stride", {3, 3}}}), y, v);
Paul Fultz II's avatar
Paul Fultz II committed
721
722
723
724
725
    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());
726
    // No fusion
Paul Fultz II's avatar
Paul Fultz II committed
727
728
    EXPECT(std::count_if(
               m.begin(), m.end(), [](auto&& ins) { return ins.name() == "convolution"; }) == 2);
729
730
731
732
}

TEST_CASE(simplify_add_conv_1x1_diff_strides1)
{
Paul Fultz II's avatar
Paul Fultz II committed
733
734
735
736
737
738
739
740
741
    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(
742
        migraphx::make_op("convolution", {{"padding", {0, 0}}, {"stride", {2, 2}}}), y, v);
Paul Fultz II's avatar
Paul Fultz II committed
743
744
745
746
747
748
749
    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);
750
751
752
753
}

TEST_CASE(simplify_add_conv_1x1_diff_strides2)
{
Paul Fultz II's avatar
Paul Fultz II committed
754
755
756
757
758
759
760
761
    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(
762
        migraphx::make_op("convolution", {{"padding", {0, 0}}, {"stride", {2, 2}}}), x, w);
Paul Fultz II's avatar
Paul Fultz II committed
763
764
765
766
767
768
769
770
    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);
771
772
773
774
}

TEST_CASE(simplify_add_conv_1x1_diff_strides_odd)
{
Paul Fultz II's avatar
Paul Fultz II committed
775
776
    migraphx::module m;
    auto x = m.add_parameter("x", {migraphx::shape::float_type, {1, 54, 83, 83}});
777
    auto w =
Paul Fultz II's avatar
Paul Fultz II committed
778
779
        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}});
780
    auto v =
Paul Fultz II's avatar
Paul Fultz II committed
781
782
783
        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(
784
        migraphx::make_op("convolution", {{"padding", {0, 0}}, {"stride", {2, 2}}}), y, v);
Paul Fultz II's avatar
Paul Fultz II committed
785
786
787
788
789
790
791
    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);
792
793
794
795
}

TEST_CASE(simplify_add_conv_no_fusion_asymetrical_strides1)
{
Paul Fultz II's avatar
Paul Fultz II committed
796
797
798
799
800
801
802
803
    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(
804
        migraphx::make_op("convolution", {{"padding", {0, 0}}, {"stride", {2, 1}}}), x, w);
Paul Fultz II's avatar
Paul Fultz II committed
805
806
807
808
809
810
    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());
811
    // No fusion
Paul Fultz II's avatar
Paul Fultz II committed
812
813
    EXPECT(std::count_if(
               m.begin(), m.end(), [](auto&& ins) { return ins.name() == "convolution"; }) == 2);
814
815
816
817
}

TEST_CASE(simplify_add_conv_no_fusion_asymetrical_strides2)
{
Paul Fultz II's avatar
Paul Fultz II committed
818
819
820
821
822
823
824
825
826
    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(
827
        migraphx::make_op("convolution", {{"padding", {0, 0}}, {"stride", {2, 1}}}), y, v);
Paul Fultz II's avatar
Paul Fultz II committed
828
829
830
831
832
    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());
833
    // No fusion
Paul Fultz II's avatar
Paul Fultz II committed
834
835
    EXPECT(std::count_if(
               m.begin(), m.end(), [](auto&& ins) { return ins.name() == "convolution"; }) == 2);
836
837
}

838
839
840
TEST_CASE(simplify_concat_add_relu)
{
    auto s = migraphx::shape{migraphx::shape::int32_type, {1}};
Paul Fultz II's avatar
Paul Fultz II committed
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
    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);
869
870
}

871
872
873
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
874
875
876
877
878
879
880
881
882
883
884
    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);
885
        auto concat =
Paul Fultz II's avatar
Paul Fultz II committed
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
            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());
906
907
908
909
910
}

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
911
    migraphx::module m1;
912
    {
913
        auto b    = migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {2, 1, 4, 5}}});
Paul Fultz II's avatar
Paul Fultz II committed
914
915
916
917
918
919
920
        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);
921
        auto concat =
Paul Fultz II's avatar
Paul Fultz II committed
922
923
            m1.add_instruction(migraphx::make_op("concat", {{"axis", 1}}), sum, oneb, twob);
        m1.add_instruction(pass_op{}, concat);
924
    }
Paul Fultz II's avatar
Paul Fultz II committed
925
    run_pass(m1);
926

Paul Fultz II's avatar
Paul Fultz II committed
927
    migraphx::module m2;
928
    {
929
        auto b       = migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {2, 2, 4, 5}}});
Paul Fultz II's avatar
Paul Fultz II committed
930
931
932
933
934
935
936
937
938
939
940
        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());
941
942
}

943
944
945
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
946
947
    migraphx::module m1;
    {
948
        auto b      = migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {2, 1, 4, 5}}});
Paul Fultz II's avatar
Paul Fultz II committed
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
        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;
965
    {
966
        auto b        = migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {2, 2, 4, 5}}});
Paul Fultz II's avatar
Paul Fultz II committed
967
968
969
970
971
972
973
974
975
976
977
978
        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);
979
980
981
982
983
}

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
984
985
    migraphx::module m1;
    {
986
        auto b      = migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {2, 1, 4, 5}}});
Paul Fultz II's avatar
Paul Fultz II committed
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
        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;
1003
    {
1004
        auto b       = migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {2, 1, 4, 5}}});
Paul Fultz II's avatar
Paul Fultz II committed
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
        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);
1018
1019
}

Umang Yadav's avatar
Umang Yadav committed
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
TEST_CASE(concat_convert_fusion)
{
    auto s = migraphx::shape{migraphx::shape::float_type, {64}};
    migraphx::module m1;
    {
        auto x  = m1.add_parameter("x", s);
        auto y  = m1.add_parameter("y", s);
        auto xh = m1.add_instruction(
            migraphx::make_op("convert",
                              {{"target_type", migraphx::to_value(migraphx::shape::half_type)}}),
            x);
        auto yh = m1.add_instruction(
            migraphx::make_op("convert",
                              {{"target_type", migraphx::to_value(migraphx::shape::half_type)}}),
            y);
        auto concat = m1.add_instruction(migraphx::make_op("concat", {{"axis", 0}}), xh, yh);
        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 concat  = m2.add_instruction(migraphx::make_op("concat", {{"axis", 0}}), x, y);
        auto concath = m2.add_instruction(
            migraphx::make_op("convert",
                              {{"target_type", migraphx::to_value(migraphx::shape::half_type)}}),
            concat);
        m2.add_instruction(pass_op{}, concath);
    }
    EXPECT(m1 == m2);
}

1054
1055
TEST_CASE(simplify_div_const)
{
Paul Fultz II's avatar
Paul Fultz II committed
1056
    migraphx::module m1;
1057
    {
Paul's avatar
Paul committed
1058
1059
        auto x   = m1.add_parameter("x", {migraphx::shape::float_type, {1}});
        auto two = m1.add_literal(2.0f);
Paul Fultz II's avatar
Paul Fultz II committed
1060
        m1.add_instruction(migraphx::make_op("div"), x, two);
1061
    }
Paul Fultz II's avatar
Paul Fultz II committed
1062
    run_pass(m1);
1063

Paul Fultz II's avatar
Paul Fultz II committed
1064
    migraphx::module m2;
1065
    {
Paul's avatar
Paul committed
1066
1067
        auto x     = m2.add_parameter("x", {migraphx::shape::float_type, {1}});
        auto two   = m2.add_literal(2.0f);
Paul Fultz II's avatar
Paul Fultz II committed
1068
1069
        auto recip = m2.insert_instruction(std::next(two), migraphx::make_op("recip"), two);
        m2.add_instruction(migraphx::make_op("mul"), x, recip);
1070
    }
Paul Fultz II's avatar
Paul Fultz II committed
1071
    EXPECT(m1 == m2);
1072
1073
}

Paul's avatar
Paul committed
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
TEST_CASE(simplify_div_integral_const)
{
    migraphx::module m1;
    {
        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);
    }
    migraphx::module m2 = m1;
    run_pass(m1);
    EXPECT(m1 == m2);
}

1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
TEST_CASE(simplify_unit_mult_const)
{
    migraphx::module m1;
    {
        auto x    = m1.add_parameter("x", {migraphx::shape::int32_type, {1}});
        auto unit = m1.add_literal(1);
        m1.add_instruction(migraphx::make_op("mul"), x, unit);
    }
    run_pass(m1);

    migraphx::module m2;
    {
        auto x = m2.add_parameter("x", {migraphx::shape::int32_type, {1}});
        m2.add_instruction(migraphx::make_op("identity"), x);
    }

    EXPECT(m1 == m2);
}

TEST_CASE(simplify_unit_mult_const2)
{
    migraphx::module m1;
    {
        auto unit = m1.add_literal(1);
        auto x    = m1.add_parameter("x", {migraphx::shape::int32_type, {1}});
        m1.add_instruction(migraphx::make_op("mul"), unit, x);
    }
    run_pass(m1);

    migraphx::module m2;
    {
        auto x = m2.add_parameter("x", {migraphx::shape::int32_type, {1}});
        m2.add_instruction(migraphx::make_op("identity"), x);
    }

    EXPECT(m1 == m2);
}

TEST_CASE(simplify_unit_mult_const_vec)
{
    migraphx::shape unit_shape{migraphx::shape::int32_type, {2}};
    migraphx::shape x_shape{migraphx::shape::int32_type, {1, 2, 3, 3}};
    migraphx::module m1;
    {
        auto unit  = m1.add_literal({unit_shape, {1, 1}});
        auto x     = m1.add_parameter("x", x_shape);
        auto unitb = m1.add_instruction(
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 2, 3, 3}}}), unit);
        m1.add_instruction(migraphx::make_op("mul"), x, unitb);
    }
    run_pass(m1);

    migraphx::module m2;
    {
        auto x = m2.add_parameter("x", x_shape);
        m2.add_instruction(migraphx::make_op("identity"), x);
    }

    EXPECT(m1 == m2);
}

TEST_CASE(simplify_unit_mult_const_vec2)
{
    migraphx::shape unit_shape{migraphx::shape::int32_type, {2}};
    migraphx::shape x_shape{migraphx::shape::int32_type, {1, 2, 3, 3}};
    migraphx::module m1;
    {
        auto unit  = m1.add_literal({unit_shape, {1, 1}});
        auto x     = m1.add_parameter("x", x_shape);
        auto unitb = m1.add_instruction(
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 2, 3, 3}}}), unit);
        m1.add_instruction(migraphx::make_op("mul"), unitb, x);
    }
    run_pass(m1);

    migraphx::module m2;
    {
        auto x = m2.add_parameter("x", x_shape);
        m2.add_instruction(migraphx::make_op("identity"), x);
    }

    EXPECT(m1 == m2);
}

TEST_CASE(simplify_unit_div_const)
{
    migraphx::module m1;
    {
        auto x    = m1.add_parameter("x", {migraphx::shape::int32_type, {1}});
        auto unit = m1.add_literal(1);
        auto div  = m1.add_instruction(migraphx::make_op("div"), x, unit);
        m1.add_return({div});
    }
    run_pass(m1);

    migraphx::module m2;
    {
        auto x = m2.add_parameter("x", {migraphx::shape::int32_type, {1}});
        m2.add_return({x});
    }

    EXPECT(m1 == m2);
}

TEST_CASE(simplify_unit_div_const_vec)
{
    migraphx::shape unit_shape{migraphx::shape::int32_type, {2}};
    migraphx::shape x_shape{migraphx::shape::int32_type, {1, 2, 3, 3}};
    migraphx::module m1;
    {
        auto unit  = m1.add_literal({unit_shape, {1, 1}});
        auto x     = m1.add_parameter("x", x_shape);
        auto unitb = m1.add_instruction(
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 2, 3, 3}}}), unit);
        m1.add_instruction(migraphx::make_op("div"), x, unitb);
    }
    run_pass(m1);

    migraphx::module m2;
    {
        auto x = m2.add_parameter("x", x_shape);
        m2.add_instruction(migraphx::make_op("identity"), x);
    }

    EXPECT(m1 == m2);
}

TEST_CASE(simplify_neg_unit_mult_const)
{
    migraphx::module m1;
    {
1218
1219
1220
        auto x    = m1.add_parameter("x", {migraphx::shape::int32_type, {1, 6}});
        auto unit = m1.add_literal(
            migraphx::literal{{migraphx::shape::int32_type, {1, 6}}, std::vector<int>(6, -1)});
1221
1222
1223
1224
1225
1226
        m1.add_instruction(migraphx::make_op("mul"), x, unit);
    }
    run_pass(m1);

    migraphx::module m2;
    {
1227
1228
1229
        auto x  = m2.add_parameter("x", {migraphx::shape::int32_type, {1, 6}});
        auto x2 = m2.add_instruction(migraphx::make_op("neg"), x);
        m2.add_instruction(migraphx::make_op("identity"), x2);
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
    }

    EXPECT((m1 == m2));
}

TEST_CASE(simplify_neg_unit_mult_const2)
{
    migraphx::module m1;
    {
        auto unit = m1.add_literal(-1);
        auto x    = m1.add_parameter("x", {migraphx::shape::int32_type, {1}});
        m1.add_instruction(migraphx::make_op("mul"), unit, x);
    }
    run_pass(m1);

    migraphx::module m2;
    {
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
        auto x  = m2.add_parameter("x", {migraphx::shape::int32_type, {1}});
        auto x2 = m2.add_instruction(migraphx::make_op("neg"), x);
        m2.add_instruction(migraphx::make_op("identity"), x2);
    }

    EXPECT((m1 == m2));
}

TEST_CASE(simplify_neg_unit_mult_const_add)
{
    migraphx::module m1;
    {
        auto unit = m1.add_literal(-1);
        auto x    = m1.add_parameter("x", {migraphx::shape::int32_type, {1}});
        auto x2   = m1.add_instruction(migraphx::make_op("mul"), unit, x);
        m1.add_instruction(migraphx::make_op("add"), x2, x2);
    }
    run_pass(m1);

    migraphx::module m2;
    {
        auto x  = m2.add_parameter("x", {migraphx::shape::int32_type, {1}});
        auto x2 = m2.add_instruction(migraphx::make_op("neg"), x);
        m2.add_instruction(migraphx::make_op("add"), x2, x2);
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
    }

    EXPECT((m1 == m2));
}

TEST_CASE(simplify_neg_unit_mul_const_vec)
{
    migraphx::shape unit_shape{migraphx::shape::int32_type, {2}};
    migraphx::shape x_shape{migraphx::shape::int32_type, {1, 2, 3, 3}};
    migraphx::module m1;
    {
        auto unit  = m1.add_literal({unit_shape, {-1, -1}});
        auto x     = m1.add_parameter("x", x_shape);
        auto unitb = m1.add_instruction(
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 2, 3, 3}}}), unit);
        m1.add_instruction(migraphx::make_op("mul"), x, unitb);
    }
    run_pass(m1);

    migraphx::module m2;
    {
1292
1293
1294
        auto x  = m2.add_parameter("x", x_shape);
        auto x2 = m2.add_instruction(migraphx::make_op("neg"), x);
        m2.add_instruction(migraphx::make_op("identity"), x2);
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
    }

    EXPECT(m1 == m2);
}

TEST_CASE(simplify_neg_unit_mul_const_vec2)
{
    migraphx::shape zero_shape{migraphx::shape::int32_type, {2}};
    migraphx::shape x_shape{migraphx::shape::int32_type, {1, 2, 3, 3}};
    migraphx::module m1;
    {
        auto unit  = m1.add_literal({zero_shape, {-1, -1}});
        auto x     = m1.add_parameter("x", x_shape);
        auto unitb = m1.add_instruction(
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 2, 3, 3}}}), unit);
        m1.add_instruction(migraphx::make_op("mul"), unitb, x);
    }
    run_pass(m1);

    migraphx::module m2;
    {
1316
1317
1318
        auto x  = m2.add_parameter("x", x_shape);
        auto x2 = m2.add_instruction(migraphx::make_op("neg"), x);
        m2.add_instruction(migraphx::make_op("identity"), x2);
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
    }

    EXPECT(m1 == m2);
}

TEST_CASE(simplify_neg_unit_div_const)
{
    migraphx::module m1;
    {
        auto x    = m1.add_parameter("x", {migraphx::shape::int32_type, {1}});
        auto unit = m1.add_literal(-1);
        m1.add_instruction(migraphx::make_op("div"), x, unit);
    }
    run_pass(m1);

    migraphx::module m2;
    {
1336
1337
1338
        auto x  = m2.add_parameter("x", {migraphx::shape::int32_type, {1}});
        auto x2 = m2.add_instruction(migraphx::make_op("neg"), x);
        m2.add_instruction(migraphx::make_op("identity"), x2);
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
    }

    EXPECT(m1 == m2);
}

TEST_CASE(simplify_neg_unit_div_const_vec)
{
    migraphx::shape unit_shape{migraphx::shape::int32_type, {2}};
    migraphx::shape x_shape{migraphx::shape::int32_type, {1, 2, 3, 3}};
    migraphx::module m1;
    {
        auto unit  = m1.add_literal({unit_shape, {-1, -1}});
        auto x     = m1.add_parameter("x", x_shape);
        auto unitb = m1.add_instruction(
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 2, 3, 3}}}), unit);
        m1.add_instruction(migraphx::make_op("div"), x, unitb);
    }
    run_pass(m1);

    migraphx::module m2;
    {
1360
1361
1362
        auto x  = m2.add_parameter("x", x_shape);
        auto x2 = m2.add_instruction(migraphx::make_op("neg"), x);
        m2.add_instruction(migraphx::make_op("identity"), x2);
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
    }

    EXPECT(m1 == m2);
}

TEST_CASE(simplify_sub_zero_const)
{
    migraphx::module m1;
    {
        auto x    = m1.add_parameter("x", {migraphx::shape::int32_type, {1}});
        auto zero = m1.add_literal(0);
        m1.add_instruction(migraphx::make_op("sub"), x, zero);
    }
    run_pass(m1);

    migraphx::module m2;
    {
        auto x = m2.add_parameter("x", {migraphx::shape::int32_type, {1}});
        m2.add_instruction(migraphx::make_op("identity"), x);
    }
    EXPECT(m1 == m2);
}

TEST_CASE(simplify_sub_zero_const_vec)
{
    migraphx::shape zero_shape{migraphx::shape::int32_type, {2}};
    migraphx::shape x_shape{migraphx::shape::int32_type, {1, 2, 3, 3}};
    migraphx::module m1;
    {
        auto zero  = m1.add_literal({zero_shape, {0, 0}});
        auto x     = m1.add_parameter("x", x_shape);
        auto zerob = m1.add_instruction(
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 2, 3, 3}}}), zero);
        m1.add_instruction(migraphx::make_op("sub"), x, zerob);
    }
    run_pass(m1);

    migraphx::module m2;
    {
        auto x = m2.add_parameter("x", x_shape);
        m2.add_instruction(migraphx::make_op("identity"), x);
    }

    EXPECT(m1 == m2);
}

TEST_CASE(simplify_sub_neg_zero_const)
{
    migraphx::module m1;
    {
        auto x    = m1.add_parameter("x", {migraphx::shape::int32_type, {1}});
        auto zero = m1.add_literal(0);
        m1.add_instruction(migraphx::make_op("sub"), zero, x);
    }
    run_pass(m1);

    migraphx::module m2;
    {
1421
1422
1423
        auto x  = m2.add_parameter("x", {migraphx::shape::int32_type, {1}});
        auto x2 = m2.add_instruction(migraphx::make_op("neg"), x);
        m2.add_instruction(migraphx::make_op("identity"), x2);
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
    }
    EXPECT(m1 == m2);
}

TEST_CASE(simplify_sub_neg_zero_const_vec)
{
    migraphx::shape zero_shape{migraphx::shape::int32_type, {2}};
    migraphx::shape x_shape{migraphx::shape::int32_type, {1, 2, 3, 3}};
    migraphx::module m1;
    {
        auto zero  = m1.add_literal({zero_shape, {0, 0}});
        auto x     = m1.add_parameter("x", x_shape);
        auto zerob = m1.add_instruction(
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 2, 3, 3}}}), zero);
        m1.add_instruction(migraphx::make_op("sub"), zerob, x);
    }
    run_pass(m1);

    migraphx::module m2;
    {
1444
1445
1446
        auto x  = m2.add_parameter("x", x_shape);
        auto x2 = m2.add_instruction(migraphx::make_op("neg"), x);
        m2.add_instruction(migraphx::make_op("identity"), x2);
1447
1448
1449
1450
1451
    }

    EXPECT(m1 == m2);
}

1452
1453
TEST_CASE(simplify_sub_const)
{
Paul Fultz II's avatar
Paul Fultz II committed
1454
    migraphx::module m1;
1455
    {
Paul Fultz II's avatar
Paul Fultz II committed
1456
1457
1458
        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);
1459
    }
Paul Fultz II's avatar
Paul Fultz II committed
1460
    run_pass(m1);
1461

Paul Fultz II's avatar
Paul Fultz II committed
1462
    migraphx::module m2;
1463
    {
Paul Fultz II's avatar
Paul Fultz II committed
1464
1465
1466
1467
        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);
1468
    }
Paul Fultz II's avatar
Paul Fultz II committed
1469
    EXPECT(m1 == m2);
1470
1471
}

1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
TEST_CASE(simplify_zero_mult_const)
{
    migraphx::module m1;
    {
        auto x       = m1.add_parameter("x", {migraphx::shape::int32_type, {1}});
        auto zero    = m1.add_literal(0);
        auto mul_ins = m1.add_instruction(migraphx::make_op("mul"), x, zero);
        m1.add_return({mul_ins});
    }
    run_pass(m1);

    migraphx::module m2;
    {
        m2.add_parameter("x", {migraphx::shape::int32_type, {1}});
        auto zero = m2.add_literal(0);
        m2.add_return({zero});
    }

    EXPECT(m1 == m2);
}

TEST_CASE(simplify_zero_mult_const2)
{
    migraphx::module m1;
    {
        auto x       = m1.add_parameter("x", {migraphx::shape::int32_type, {1}});
        auto zero    = m1.add_literal(0);
        auto mul_ins = m1.add_instruction(migraphx::make_op("mul"), zero, x);
        m1.add_return({mul_ins});
    }
    run_pass(m1);

    migraphx::module m2;
    {
        m2.add_parameter("x", {migraphx::shape::int32_type, {1}});
        auto zero = m2.add_literal(0);
        m2.add_return({zero});
    }

    EXPECT(m1 == m2);
}

TEST_CASE(simplify_zero_mul_const_vec)
{
    migraphx::shape zero_shape{migraphx::shape::int32_type, {2}};
    migraphx::shape x_shape{migraphx::shape::int32_type, {1, 2, 3, 3}};
    migraphx::module m1;
    {
        auto zero  = m1.add_literal({zero_shape, {0, 0}});
        auto x     = m1.add_parameter("x", x_shape);
        auto zerob = m1.add_instruction(
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 2, 3, 3}}}), zero);
        auto mul_ins = m1.add_instruction(migraphx::make_op("mul"), x, zerob);
        m1.add_return({mul_ins});
    }
    run_pass(m1);

    migraphx::module m2;
    {
        auto zero = m2.add_literal({zero_shape, {0, 0}});
        m2.add_parameter("x", x_shape);
        auto zerob = m2.add_instruction(
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 2, 3, 3}}}), zero);
        m2.add_return({zerob});
    }

    EXPECT(m1 == m2);
}

TEST_CASE(simplify_zero_mul_const_vec2)
{
    migraphx::shape zero_shape{migraphx::shape::int32_type, {2}};
    migraphx::shape x_shape{migraphx::shape::int32_type, {1, 2, 3, 3}};
    migraphx::module m1;
    {
        auto zero  = m1.add_literal({zero_shape, {0, 0}});
        auto x     = m1.add_parameter("x", x_shape);
        auto zerob = m1.add_instruction(
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 2, 3, 3}}}), zero);
        auto mul_ins = m1.add_instruction(migraphx::make_op("mul"), zerob, x);
        m1.add_return({mul_ins});
    }
    run_pass(m1);

    migraphx::module m2;
    {
        auto zero = m2.add_literal({zero_shape, {0, 0}});
        m2.add_parameter("x", x_shape);
        auto zerob = m2.add_instruction(
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 2, 3, 3}}}), zero);
        m2.add_return({zerob});
    }

    EXPECT(m1 == m2);
}

TEST_CASE(simplify_zero_div_const)
{
    migraphx::module m1;
    {
        auto zero    = m1.add_literal(0);
        auto x       = m1.add_parameter("x", {migraphx::shape::int32_type, {1}});
        auto div_ins = m1.add_instruction(migraphx::make_op("div"), zero, x);
        m1.add_return({div_ins});
    }
    run_pass(m1);

    migraphx::module m2;
    {
        auto zero = m2.add_literal(0);
        m2.add_parameter("x", {migraphx::shape::int32_type, {1}});
        m2.add_return({zero});
    }

    EXPECT(m1 == m2);
}

TEST_CASE(simplify_zero_div_const_vec)
{
    migraphx::shape zero_shape{migraphx::shape::int32_type, {2}};
    migraphx::shape x_shape{migraphx::shape::int32_type, {1, 2, 3, 3}};
    migraphx::module m1;
    {
        auto x     = m1.add_parameter("x", x_shape);
        auto zero  = m1.add_literal({zero_shape, {0, 0}});
        auto zerob = m1.add_instruction(
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 2, 3, 3}}}), zero);
        auto div_ins = m1.add_instruction(migraphx::make_op("div"), zerob, x);
        m1.add_return({div_ins});
    }
    run_pass(m1);

    migraphx::module m2;
    {
        m2.add_parameter("x", x_shape);
        auto zero  = m2.add_literal({zero_shape, {0, 0}});
        auto zerob = m2.add_instruction(
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 2, 3, 3}}}), zero);
        m2.add_return({zerob});
    }

    EXPECT(m1 == m2);
}

kahmed10's avatar
kahmed10 committed
1616
1617
TEST_CASE(simplify_rsqrt)
{
Paul Fultz II's avatar
Paul Fultz II committed
1618
    migraphx::module m1;
kahmed10's avatar
kahmed10 committed
1619
    {
Paul Fultz II's avatar
Paul Fultz II committed
1620
1621
1622
        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
1623
    }
Paul Fultz II's avatar
Paul Fultz II committed
1624
    run_pass(m1);
kahmed10's avatar
kahmed10 committed
1625

Paul Fultz II's avatar
Paul Fultz II committed
1626
    migraphx::module m2;
kahmed10's avatar
kahmed10 committed
1627
    {
Paul Fultz II's avatar
Paul Fultz II committed
1628
1629
        auto x = m2.add_parameter("x", {migraphx::shape::int32_type, {1}});
        m2.add_instruction(migraphx::make_op("rsqrt"), x);
kahmed10's avatar
kahmed10 committed
1630
    }
Paul Fultz II's avatar
Paul Fultz II committed
1631
    EXPECT(m1 == m2);
kahmed10's avatar
kahmed10 committed
1632
1633
1634
1635
}

TEST_CASE(simplify_rsqrt_multi_use)
{
Paul Fultz II's avatar
Paul Fultz II committed
1636
    migraphx::module m1;
kahmed10's avatar
kahmed10 committed
1637
    {
Paul Fultz II's avatar
Paul Fultz II committed
1638
1639
1640
1641
1642
        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
1643
    }
Paul Fultz II's avatar
Paul Fultz II committed
1644
    migraphx::module m2{m1};
kahmed10's avatar
kahmed10 committed
1645

Paul Fultz II's avatar
Paul Fultz II committed
1646
1647
    run_pass(m1);
    EXPECT(m1 == m2);
kahmed10's avatar
kahmed10 committed
1648
1649
}

1650
1651
1652
1653
TEST_CASE(simplify_slice_concat)
{
    auto s = migraphx::shape{migraphx::shape::float_type, {256}};

Paul Fultz II's avatar
Paul Fultz II committed
1654
    migraphx::module m1;
1655
    {
Paul Fultz II's avatar
Paul Fultz II committed
1656
1657
1658
        auto x       = m1.add_parameter("x", s);
        auto y       = m1.add_parameter("y", s);
        auto xslice1 = m1.add_instruction(
1659
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {128}}}), x);
Paul Fultz II's avatar
Paul Fultz II committed
1660
        auto xslice2 = m1.add_instruction(
1661
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {128}}, {"ends", {256}}}), x);
Paul Fultz II's avatar
Paul Fultz II committed
1662
        auto yslice1 = m1.add_instruction(
1663
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {128}}}), y);
Paul Fultz II's avatar
Paul Fultz II committed
1664
        auto yslice2 = m1.add_instruction(
1665
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {128}}, {"ends", {256}}}), y);
Paul Fultz II's avatar
Paul Fultz II committed
1666
        auto concat = m1.add_instruction(
1667
            migraphx::make_op("concat", {{"axis", 0}}), xslice1, xslice2, yslice1, yslice2);
Paul Fultz II's avatar
Paul Fultz II committed
1668
        m1.add_instruction(pass_op{}, concat);
1669
    }
Paul Fultz II's avatar
Paul Fultz II committed
1670
    run_pass(m1);
1671

Paul Fultz II's avatar
Paul Fultz II committed
1672
    migraphx::module m2;
1673
    {
Paul Fultz II's avatar
Paul Fultz II committed
1674
1675
1676
1677
        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);
1678
    }
Paul Fultz II's avatar
Paul Fultz II committed
1679
    EXPECT(m1 == m2);
1680
1681
1682
1683
1684
1685
}

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
1686
    migraphx::module m1;
1687
    {
Paul Fultz II's avatar
Paul Fultz II committed
1688
1689
1690
        auto x       = m1.add_parameter("x", s);
        auto y       = m1.add_parameter("y", s);
        auto xslice1 = m1.add_instruction(
1691
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {64}}}), x);
Paul Fultz II's avatar
Paul Fultz II committed
1692
        auto xslice2 = m1.add_instruction(
1693
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {64}}, {"ends", {192}}}), x);
Paul Fultz II's avatar
Paul Fultz II committed
1694
        auto xslice3 = m1.add_instruction(
1695
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {192}}, {"ends", {256}}}), x);
Paul Fultz II's avatar
Paul Fultz II committed
1696
        auto yslice1 = m1.add_instruction(
1697
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {64}}}), y);
Paul Fultz II's avatar
Paul Fultz II committed
1698
        auto yslice2 = m1.add_instruction(
1699
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {64}}, {"ends", {192}}}), y);
Paul Fultz II's avatar
Paul Fultz II committed
1700
        auto yslice3 = m1.add_instruction(
1701
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {192}}, {"ends", {256}}}), y);
Paul Fultz II's avatar
Paul Fultz II committed
1702
1703
1704
1705
1706
1707
1708
1709
        auto concat = m1.add_instruction(migraphx::make_op("concat", {{"axis", 0}}),
                                         xslice1,
                                         xslice2,
                                         xslice3,
                                         yslice1,
                                         yslice2,
                                         yslice3);
        m1.add_instruction(pass_op{}, concat);
1710
    }
Paul Fultz II's avatar
Paul Fultz II committed
1711
    run_pass(m1);
1712

Paul Fultz II's avatar
Paul Fultz II committed
1713
    migraphx::module m2;
1714
    {
Paul Fultz II's avatar
Paul Fultz II committed
1715
1716
1717
1718
        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);
1719
1720
    }

Paul Fultz II's avatar
Paul Fultz II committed
1721
    EXPECT(m1 == m2);
1722
1723
1724
1725
1726
1727
}

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

Paul Fultz II's avatar
Paul Fultz II committed
1728
    migraphx::module m1;
1729
    {
Paul Fultz II's avatar
Paul Fultz II committed
1730
1731
1732
        auto x       = m1.add_parameter("x", s);
        auto y       = m1.add_parameter("y", s);
        auto xslice1 = m1.add_instruction(
1733
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {64}}}), x);
Paul Fultz II's avatar
Paul Fultz II committed
1734
        auto xslice2 = m1.add_instruction(
1735
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {192}}, {"ends", {256}}}), x);
Paul Fultz II's avatar
Paul Fultz II committed
1736
        auto xslice3 = m1.add_instruction(
1737
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {64}}, {"ends", {192}}}), x);
Paul Fultz II's avatar
Paul Fultz II committed
1738
        auto yslice1 = m1.add_instruction(
1739
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {64}}}), y);
Paul Fultz II's avatar
Paul Fultz II committed
1740
        auto yslice2 = m1.add_instruction(
1741
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {192}}, {"ends", {256}}}), y);
Paul Fultz II's avatar
Paul Fultz II committed
1742
        auto yslice3 = m1.add_instruction(
1743
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {64}}, {"ends", {192}}}), y);
Paul Fultz II's avatar
Paul Fultz II committed
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
        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);
1757
1758
}

1759
1760
1761
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
1762
    migraphx::module m1;
1763
    {
1764
        auto b     = migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {3, 1, 4}}});
Paul Fultz II's avatar
Paul Fultz II committed
1765
1766
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
1767
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1768
        auto y = m1.add_instruction(
1769
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
        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;
    {
1785
        auto b       = migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {3, 2, 4}}});
Paul Fultz II's avatar
Paul Fultz II committed
1786
1787
1788
1789
1790
1791
1792
1793
        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(
1794
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), relu);
Paul Fultz II's avatar
Paul Fultz II committed
1795
        auto y = m2.add_instruction(
1796
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), relu);
Paul Fultz II's avatar
Paul Fultz II committed
1797
1798
        auto add = m2.add_instruction(migraphx::make_op("add"), x, y);
        m2.add_instruction(pass_op{}, add);
1799
    }
Paul Fultz II's avatar
Paul Fultz II committed
1800
    EXPECT(m1.sort() == m2.sort());
1801
1802
}

Shucai Xiao's avatar
Shucai Xiao committed
1803
1804
1805
1806
1807
1808
1809
1810
TEST_CASE(simplify_split_reduce0)
{
    auto s = migraphx::shape{migraphx::shape::int32_type, {3, 2, 4}};
    migraphx::module m1;
    {
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
kahmed10's avatar
kahmed10 committed
1811
        auto y = m1.add_instruction(
Shucai Xiao's avatar
Shucai Xiao committed
1812
1813
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);

kahmed10's avatar
kahmed10 committed
1814
1815
        auto one = m1.add_literal(1);
        auto two = m1.add_literal(2);
Shucai Xiao's avatar
Shucai Xiao committed
1816

kahmed10's avatar
kahmed10 committed
1817
1818
        auto arx   = m1.add_instruction(migraphx::make_op("contiguous"), x);
        auto ary   = m1.add_instruction(migraphx::make_op("contiguous"), y);
Shucai Xiao's avatar
Shucai Xiao committed
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
        auto rmax0 = m1.add_instruction(migraphx::make_op("reduce_sum", {{"axes", {0, 1}}}), x);
        auto rmin0 = m1.add_instruction(migraphx::make_op("reduce_mean", {{"axes", {0, 1}}}), x);
        auto rmax1 = m1.add_instruction(migraphx::make_op("gather", {{"axis", 1}}), arx, one);
        auto rmin1 = m1.add_instruction(migraphx::make_op("gather", {{"axis", 1}}), ary, two);
        auto rmax2 = m1.add_instruction(migraphx::make_op("reduce_sum", {{"axes", {0, 1}}}), y);
        auto rmin2 = m1.add_instruction(migraphx::make_op("reduce_mean", {{"axes", {0, 1}}}), y);
        m1.add_return({rmax0, rmin0, rmax1, rmin1, rmax2, rmin2});
    }

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

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

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

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

kahmed10's avatar
kahmed10 committed
1855
1856
        auto rmn  = m2.add_instruction(migraphx::make_op("reduce_mean", {{"axes", {0, 2}}}), input);
        auto slc0 = m2.add_instruction(
Shucai Xiao's avatar
Shucai Xiao committed
1857
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), rmn);
kahmed10's avatar
kahmed10 committed
1858
1859
        auto rmx  = m2.add_instruction(migraphx::make_op("reduce_sum", {{"axes", {0, 2}}}), input);
        auto slc1 = m2.add_instruction(
Shucai Xiao's avatar
Shucai Xiao committed
1860
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), rmx);
kahmed10's avatar
kahmed10 committed
1861
        auto slc2 = m2.add_instruction(
Shucai Xiao's avatar
Shucai Xiao committed
1862
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), rmn);
kahmed10's avatar
kahmed10 committed
1863
        auto slc3 = m2.add_instruction(
Shucai Xiao's avatar
Shucai Xiao committed
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), rmx);
        m2.add_return({slc3, slc2, slc1, slc0});
    }

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

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

    migraphx::module m2;
    {
        auto input = m2.add_parameter("input", s);
        auto x     = m2.add_instruction(
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);
        auto rmn1 = m2.add_instruction(migraphx::make_op("reduce_mean", {{"axes", {0, 1}}}), x);
kahmed10's avatar
kahmed10 committed
1895
        auto y    = m2.add_instruction(
Shucai Xiao's avatar
Shucai Xiao committed
1896
1897
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
        auto rmn2 = m2.add_instruction(migraphx::make_op("reduce_mean", {{"axes", {0, 1}}}), y);
kahmed10's avatar
kahmed10 committed
1898
1899
        auto rms  = m2.add_instruction(migraphx::make_op("reduce_sum", {{"axes", {0, 2}}}), input);
        auto slc0 = m2.add_instruction(
Shucai Xiao's avatar
Shucai Xiao committed
1900
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), rms);
kahmed10's avatar
kahmed10 committed
1901
        auto slc1 = m2.add_instruction(
Shucai Xiao's avatar
Shucai Xiao committed
1902
1903
1904
1905
1906
1907
1908
1909
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), rms);
        m2.add_return({slc1, rmn2, slc0, rmn1});
    }

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

1910
1911
1912
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
1913
    migraphx::module m1;
1914
    {
1915
1916
        auto b     = migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {3, 1, 4}}});
        auto r     = migraphx::make_op("reshape", {{"dims", {3, 4}}});
Paul Fultz II's avatar
Paul Fultz II committed
1917
1918
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
1919
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1920
        auto y = m1.add_instruction(
1921
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
        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;
    {
1939
        auto b       = migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {3, 2, 4}}});
Paul Fultz II's avatar
Paul Fultz II committed
1940
1941
1942
1943
1944
1945
1946
1947
        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 slc1    = m2.add_instruction(
1948
1949
1950
1951
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), relu);

        auto rsp1 = m2.add_instruction(migraphx::make_op("reshape", {{"dims", {3, 4}}}), slc1);

Paul Fultz II's avatar
Paul Fultz II committed
1952
        auto slc2 = m2.add_instruction(
1953
1954
1955
1956
1957
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), relu);

        auto rsp2 = m2.add_instruction(migraphx::make_op("reshape", {{"dims", {3, 4}}}), slc2);

        auto add = m2.add_instruction(migraphx::make_op("add"), rsp1, rsp2);
Paul Fultz II's avatar
Paul Fultz II committed
1958
        m2.add_instruction(pass_op{}, add);
1959
    }
Paul Fultz II's avatar
Paul Fultz II committed
1960
    EXPECT(m1.sort() == m2.sort());
1961
1962
1963
1964
1965
}

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
1966
    migraphx::module m1;
1967
    {
1968
        auto r     = migraphx::make_op("reshape", {{"dims", {3, 2, 4}}});
Paul Fultz II's avatar
Paul Fultz II committed
1969
1970
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
1971
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1972
        auto y = m1.add_instruction(
1973
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
1974
1975
        auto one  = m1.add_literal(1);
        auto oneb = m1.add_instruction(
1976
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {3, 1, 4, 2}}}), one);
Paul Fultz II's avatar
Paul Fultz II committed
1977
1978
        auto two  = m1.add_literal(2);
        auto twob = m1.add_instruction(
1979
            migraphx::make_op("broadcast", {{"axis", 3}, {"out_lens", {3, 2, 4, 1}}}), two);
Paul Fultz II's avatar
Paul Fultz II committed
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
        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());
1993
1994
1995
1996
1997
}

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
1998
    migraphx::module m1;
1999
    {
2000
        auto b     = migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {3, 1, 4}}});
Paul Fultz II's avatar
Paul Fultz II committed
2001
2002
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
2003
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {2}}, {"ends", {3}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2004
        auto y = m1.add_instruction(
2005
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
        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());
2021
2022
2023
2024
2025
}

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
2026
    migraphx::module m1;
2027
    {
2028
        auto b     = migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {3, 1, 4}}});
Paul Fultz II's avatar
Paul Fultz II committed
2029
2030
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
2031
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {2}}, {"ends", {3}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2032
        auto y = m1.add_instruction(
2033
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
        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());
2049
2050
2051
2052
2053
}

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
2054
    migraphx::module m1;
2055
    {
2056
        auto b     = migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {3, 1, 4}}});
Paul Fultz II's avatar
Paul Fultz II committed
2057
2058
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
2059
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2060
        auto y = m1.add_instruction(
2061
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
        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());
2077
2078
2079
2080
2081
}

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
2082
    migraphx::module m1;
2083
    {
2084
        auto b     = migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {3, 1, 4}}});
Paul Fultz II's avatar
Paul Fultz II committed
2085
2086
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
2087
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2088
        auto y = m1.add_instruction(
2089
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
        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);
2100
    }
Paul Fultz II's avatar
Paul Fultz II committed
2101
    run_pass(m1);
2102

Paul Fultz II's avatar
Paul Fultz II committed
2103
    migraphx::module m2;
2104
    {
2105
        auto b       = migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {3, 2, 4}}});
Paul Fultz II's avatar
Paul Fultz II committed
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
        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());
2116
2117
2118
2119
2120
}

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
2121
    migraphx::module m1;
2122
    {
2123
        auto b     = migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {3, 1, 4, 3}}});
Paul Fultz II's avatar
Paul Fultz II committed
2124
2125
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
2126
2127
            migraphx::make_op("slice", {{"axes", {1, 3}}, {"starts", {0, 0}}, {"ends", {1, 3}}}),
            input);
Paul Fultz II's avatar
Paul Fultz II committed
2128
        auto y = m1.add_instruction(
2129
2130
            migraphx::make_op("slice", {{"axes", {1, 3}}, {"starts", {1, 3}}, {"ends", {2, 6}}}),
            input);
Paul Fultz II's avatar
Paul Fultz II committed
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
        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());
2145
2146
2147
2148
2149
}

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
2150
    migraphx::module m1;
2151
    {
2152
        auto b     = migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {3, 1, 4}}});
Paul Fultz II's avatar
Paul Fultz II committed
2153
2154
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
2155
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2156
        auto y = m1.add_instruction(
2157
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
        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;
    {
2174
        auto b     = migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {3, 2, 4}}});
Paul Fultz II's avatar
Paul Fultz II committed
2175
2176
        auto input = m2.add_parameter("input", s);
        auto slice = m2.add_instruction(
2177
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2178
2179
2180
2181
2182
2183
2184
        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(
2185
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), relu);
Paul Fultz II's avatar
Paul Fultz II committed
2186
        auto y = m2.add_instruction(
2187
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), relu);
Paul Fultz II's avatar
Paul Fultz II committed
2188
2189
2190
        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);
2191
    }
Paul Fultz II's avatar
Paul Fultz II committed
2192
    EXPECT(m1.sort() == m2.sort());
2193
2194
2195
2196
2197
}

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
2198
    migraphx::module m1;
2199
    {
2200
        auto b     = migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {3, 1, 4}}});
Paul Fultz II's avatar
Paul Fultz II committed
2201
2202
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
2203
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2204
        auto y = m1.add_instruction(
2205
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
        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;
    {
2223
        auto b     = migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {3, 2, 4}}});
Paul Fultz II's avatar
Paul Fultz II committed
2224
2225
        auto input = m2.add_parameter("input", s);
        auto slice = m2.add_instruction(
2226
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2227
2228
2229
2230
2231
2232
2233
2234
        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(
2235
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), relu);
Paul Fultz II's avatar
Paul Fultz II committed
2236
        auto y = m2.add_instruction(
2237
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), relu);
Paul Fultz II's avatar
Paul Fultz II committed
2238
2239
2240
        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);
2241
    }
Paul Fultz II's avatar
Paul Fultz II committed
2242
    EXPECT(m1.sort() == m2.sort());
2243
2244
2245
2246
2247
}

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
2248
    migraphx::module m1;
2249
    {
Paul Fultz II's avatar
Paul Fultz II committed
2250
2251
        auto input = m1.add_parameter("input", s);
        auto x     = m1.add_instruction(
2252
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {1}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2253
        auto y = m1.add_instruction(
2254
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1}}, {"ends", {2}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2255
2256
        auto sum = m1.add_instruction(migraphx::make_op("add"), x, y);
        m1.add_instruction(pass_op{}, sum);
2257
    }
Paul Fultz II's avatar
Paul Fultz II committed
2258
2259
2260
    migraphx::module m2 = m1;
    run_pass(m1);
    EXPECT(m1.sort() == m2.sort());
2261
2262
}

kahmed10's avatar
kahmed10 committed
2263
void test_dot_horiz(migraphx::shape::type_t type, const std::string& dot_type)
2264
{
kahmed10's avatar
kahmed10 committed
2265
    auto s = migraphx::shape{type, {3, 2, 2}};
Paul Fultz II's avatar
Paul Fultz II committed
2266
2267
2268
2269
2270
    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));
kahmed10's avatar
kahmed10 committed
2271
2272
        auto x     = m1.add_instruction(migraphx::make_op(dot_type), input, a);
        auto y     = m1.add_instruction(migraphx::make_op(dot_type), input, b);
Paul Fultz II's avatar
Paul Fultz II committed
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
        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);
kahmed10's avatar
kahmed10 committed
2284
        auto dot    = m2.add_instruction(migraphx::make_op(dot_type), input, concat);
Paul Fultz II's avatar
Paul Fultz II committed
2285
        auto x      = m2.add_instruction(
2286
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {2}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
2287
        auto y = m2.add_instruction(
2288
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {2}}, {"ends", {4}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
2289
2290
        auto sum = m2.add_instruction(migraphx::make_op("add"), x, y);
        m2.add_instruction(pass_op{}, sum);
2291
    }
Paul Fultz II's avatar
Paul Fultz II committed
2292
    EXPECT(m1.sort() == m2.sort());
2293
2294
}

kahmed10's avatar
kahmed10 committed
2295
2296
2297
2298
TEST_CASE(simplify_dot_horiz) { test_dot_horiz(migraphx::shape::int32_type, "dot"); }

TEST_CASE(simplify_quant_dot_horiz) { test_dot_horiz(migraphx::shape::int8_type, "quant_dot"); }

2299
2300
2301
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
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
    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(
2320
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {2}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
2321
        auto y = m2.add_instruction(
2322
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {2}}, {"ends", {4}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
2323
2324
        auto sum = m2.add_instruction(migraphx::make_op("add"), x, y);
        m2.add_instruction(pass_op{}, sum);
2325
    }
Paul Fultz II's avatar
Paul Fultz II committed
2326
    EXPECT(m1.sort() == m2.sort());
2327
2328
2329
2330
2331
}

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
2332
    migraphx::module m1;
2333
    {
Paul Fultz II's avatar
Paul Fultz II committed
2334
2335
2336
2337
2338
2339
2340
        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);
2341
2342
    }

Paul Fultz II's avatar
Paul Fultz II committed
2343
2344
2345
    migraphx::module m2 = m1;
    run_pass(m1);
    EXPECT(m1.sort() == m2.sort());
2346
2347
}

2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
// test if contiguous is added as necessary for reshapes
TEST_CASE(simplify_dot_horiz_reshape)
{
    auto s = migraphx::shape{migraphx::shape::int32_type, {3, 4, 4}};
    migraphx::module m1;
    {
        auto input = m1.add_parameter("input", s);
        auto a     = m1.add_literal(migraphx::generate_literal(s, 0));
        auto b     = m1.add_literal(migraphx::generate_literal(s, 1));
        auto x     = m1.add_instruction(migraphx::make_op("dot"), input, a);
        auto y     = m1.add_instruction(migraphx::make_op("dot"), input, b);
        auto x_rsp = m1.add_instruction(migraphx::make_op("reshape", {{"dims", {3, 4, 2, 2}}}), x);
        auto y_rsp =
            m1.add_instruction(migraphx::make_op("unsqueeze", {{"axes", {2}}, {"steps", {2}}}), y);
        auto sum = m1.add_instruction(migraphx::make_op("add"), {x_rsp, y_rsp});
        m1.add_instruction(pass_op{}, sum);
    }
    run_pass(m1);

    migraphx::module m2;
    {
        auto input  = m2.add_parameter("input", s);
        auto a      = m2.add_literal(migraphx::generate_literal(s, 0));
        auto b      = m2.add_literal(migraphx::generate_literal(s, 1));
        auto concat = m2.add_instruction(migraphx::make_op("concat", {{"axis", 2}}), a, b);
        auto dot    = m2.add_instruction(migraphx::make_op("dot"), input, concat);
        auto x      = m2.add_instruction(
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {4}}}), dot);
        auto y = m2.add_instruction(
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {4}}, {"ends", {8}}}), dot);
2378
        auto x_rsp = m2.add_instruction(migraphx::make_op("reshape", {{"dims", {3, 4, 2, 2}}}), x);
2379
2380
2381
2382
2383
2384
2385
2386
2387
        auto y_rsp =
            m2.add_instruction(migraphx::make_op("unsqueeze", {{"axes", {2}}, {"steps", {2}}}), y);
        auto sum = m2.add_instruction(migraphx::make_op("add"), {x_rsp, y_rsp});
        m2.add_instruction(pass_op{}, sum);
    }

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

2388
2389
2390
2391
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
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
    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(
2412
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {12}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
2413
        auto y = m2.add_instruction(
2414
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {12}}, {"ends", {24}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
2415
2416
        auto sum = m2.add_instruction(migraphx::make_op("add"), x, y);
        m2.add_instruction(pass_op{}, sum);
2417
    }
Paul Fultz II's avatar
Paul Fultz II committed
2418
    EXPECT(m1.sort() == m2.sort());
2419
2420
}

2421
2422
2423
2424
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
2425
    migraphx::module m1;
2426
    {
Paul Fultz II's avatar
Paul Fultz II committed
2427
2428
2429
2430
        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(
2431
2432
2433
2434
2435
            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
2436
        auto conv2 = m1.add_instruction(
2437
2438
2439
2440
2441
            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
2442
        m1.add_instruction(pass_op{}, conv1, conv2);
2443
    }
Paul Fultz II's avatar
Paul Fultz II committed
2444
2445
    migraphx::module m2 = m1;
    run_pass(m1);
2446

Paul Fultz II's avatar
Paul Fultz II committed
2447
    EXPECT(m1.sort() == m2.sort());
2448
2449
2450
}

TEST_CASE(simplify_conv_horiz_grouped)
2451
2452
2453
2454
{
    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
2455
2456
2457
2458
2459
2460
2461
    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));
2462
        auto convx =
Paul Fultz II's avatar
Paul Fultz II committed
2463
            m1.add_instruction(migraphx::make_op("convolution", {{"padding", {1, 1}}}), input, a);
2464
        auto convy =
Paul Fultz II's avatar
Paul Fultz II committed
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
            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(
2486
            migraphx::make_op("convolution", {{"padding", {1, 1}}}), input, concat1);
Paul Fultz II's avatar
Paul Fultz II committed
2487
        auto convx = m2.add_instruction(
2488
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {6}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
2489
        auto convy = m2.add_instruction(
2490
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {6}}, {"ends", {12}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
2491
2492
2493
        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(
2494
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {0}}, {"ends", {64}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
2495
        auto doty = m2.add_instruction(
2496
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {64}}, {"ends", {128}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
2497
2498
2499
        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);
2500
    }
Paul Fultz II's avatar
Paul Fultz II committed
2501
    EXPECT(m1.sort() == m2.sort());
2502
2503
}

2504
TEST_CASE(simplify_conv_horiz_grouped_extra1)
2505
2506
2507
2508
{
    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
2509
2510
2511
2512
2513
2514
2515
2516
    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));
2517
        auto convx =
Paul Fultz II's avatar
Paul Fultz II committed
2518
            m1.add_instruction(migraphx::make_op("convolution", {{"padding", {1, 1}}}), input, a);
2519
        auto convy =
Paul Fultz II's avatar
Paul Fultz II committed
2520
2521
2522
2523
2524
2525
            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);
2526
        auto sum3    = sqdiffx;
Paul Fultz II's avatar
Paul Fultz II committed
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
        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(
2544
            migraphx::make_op("convolution", {{"padding", {1, 1}}}), input, concat1);
Paul Fultz II's avatar
Paul Fultz II committed
2545
        auto convx = m2.add_instruction(
2546
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {6}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
2547
        auto convy = m2.add_instruction(
2548
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {6}}, {"ends", {12}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
2549
2550
2551
        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(
2552
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {0}}, {"ends", {64}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
2553
        auto doty = m2.add_instruction(
2554
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {64}}, {"ends", {128}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
2555
2556
        auto sum2    = m2.add_instruction(migraphx::make_op("add"), dotx, doty);
        auto sqdiffx = m2.add_instruction(migraphx::make_op("sqdiff"), input, e);
2557
        auto sum3    = sqdiffx;
Paul Fultz II's avatar
Paul Fultz II committed
2558
2559
2560
        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);
2561
    }
Paul Fultz II's avatar
Paul Fultz II committed
2562
    EXPECT(m1.sort() == m2.sort());
2563
2564
}

2565
TEST_CASE(simplify_conv_horiz_grouped_extra2)
2566
2567
2568
2569
{
    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
2570
2571
2572
2573
2574
2575
2576
2577
2578
    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));
2579
        auto convx =
Paul Fultz II's avatar
Paul Fultz II committed
2580
            m1.add_instruction(migraphx::make_op("convolution", {{"padding", {1, 1}}}), input, a);
2581
        auto convy =
Paul Fultz II's avatar
Paul Fultz II committed
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
            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(
2608
            migraphx::make_op("convolution", {{"padding", {1, 1}}}), input, concat1);
Paul Fultz II's avatar
Paul Fultz II committed
2609
        auto convx = m2.add_instruction(
2610
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {6}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
2611
        auto convy = m2.add_instruction(
2612
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {6}}, {"ends", {12}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
2613
2614
2615
        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(
2616
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {0}}, {"ends", {64}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
2617
        auto doty = m2.add_instruction(
2618
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {64}}, {"ends", {128}}}), dot);
Paul Fultz II's avatar
Paul Fultz II committed
2619
2620
2621
2622
2623
2624
2625
2626
2627
        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());
2628
2629
}

2630
2631
TEST_CASE(simplify_mul_slice_conv_horiz_fusion)
{
Paul Fultz II's avatar
Paul Fultz II committed
2632
    migraphx::module m1;
2633
    {
Paul Fultz II's avatar
Paul Fultz II committed
2634
2635
        auto x = m1.add_parameter("x", {migraphx::shape::int32_type, {1, 1024, 17, 17}});
        auto w = m1.add_literal(
2636
            migraphx::generate_literal({migraphx::shape::int32_type, {768, 1024, 1, 1}}));
Paul Fultz II's avatar
Paul Fultz II committed
2637
2638
        auto conv   = m1.add_instruction(migraphx::make_op("convolution"), x, w);
        auto slice1 = m1.add_instruction(
2639
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {384}}}), conv);
2640
        auto a1 =
Paul Fultz II's avatar
Paul Fultz II committed
2641
2642
            m1.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {384}}, 1));
        auto b1 = m1.add_instruction(
2643
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 384, 17, 17}}}), a1);
Paul Fultz II's avatar
Paul Fultz II committed
2644
        auto mul = m1.add_instruction(migraphx::make_op("mul"), slice1, b1);
2645
        auto a2 =
Paul Fultz II's avatar
Paul Fultz II committed
2646
2647
            m1.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {384}}, 2));
        auto b2 = m1.add_instruction(
2648
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 384, 17, 17}}}), a2);
Paul Fultz II's avatar
Paul Fultz II committed
2649
        auto add1 = m1.add_instruction(migraphx::make_op("add"), mul, b2);
2650
        auto a3 =
Paul Fultz II's avatar
Paul Fultz II committed
2651
2652
            m1.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {384}}, 3));
        auto b3 = m1.add_instruction(
2653
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 384, 17, 17}}}), a3);
Paul Fultz II's avatar
Paul Fultz II committed
2654
        auto slice2 = m1.add_instruction(
2655
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {384}}, {"ends", {768}}}), conv);
Paul Fultz II's avatar
Paul Fultz II committed
2656
2657
        auto add2 = m1.add_instruction(migraphx::make_op("add"), slice2, b3);
        m1.add_instruction(pass_op{}, add1, add2);
2658
    }
Paul Fultz II's avatar
Paul Fultz II committed
2659
    run_pass(m1);
2660

Paul Fultz II's avatar
Paul Fultz II committed
2661
    migraphx::module m2;
2662
    {
Paul Fultz II's avatar
Paul Fultz II committed
2663
2664
        auto x = m2.add_parameter("x", {migraphx::shape::int32_type, {1, 1024, 17, 17}});
        auto w = m2.add_literal(
2665
            migraphx::generate_literal({migraphx::shape::int32_type, {768, 1024, 1, 1}}));
Paul Fultz II's avatar
Paul Fultz II committed
2666
        auto wslice1 = m2.add_instruction(
2667
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {384}}}), w);
2668
        auto a1 =
Paul Fultz II's avatar
Paul Fultz II committed
2669
2670
            m2.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {384}}, 1));
        auto b1 = m2.add_instruction(
2671
            migraphx::make_op("broadcast", {{"axis", 0}, {"out_lens", {384, 1024, 1, 1}}}), a1);
Paul Fultz II's avatar
Paul Fultz II committed
2672
2673
        auto mul     = m2.add_instruction(migraphx::make_op("mul"), b1, wslice1);
        auto wslice2 = m2.add_instruction(
2674
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {384}}, {"ends", {768}}}), w);
Paul Fultz II's avatar
Paul Fultz II committed
2675
2676
        auto concat1 = m2.add_instruction(migraphx::make_op("concat", {{"axis", 0}}), mul, wslice2);
        auto conv    = m2.add_instruction(migraphx::make_op("convolution"), x, concat1);
2677
        auto a2 =
Paul Fultz II's avatar
Paul Fultz II committed
2678
            m2.add_literal(migraphx::generate_literal({migraphx::shape::int32_type, {384}}, 2));
2679
        auto a3 =
Paul Fultz II's avatar
Paul Fultz II committed
2680
2681
2682
            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(
2683
            migraphx::make_op("broadcast", {{"axis", 1}, {"out_lens", {1, 768, 17, 17}}}), concat2);
Paul Fultz II's avatar
Paul Fultz II committed
2684
2685
        auto add    = m2.add_instruction(migraphx::make_op("add"), conv, b4);
        auto slice1 = m2.add_instruction(
2686
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {384}}}), add);
Paul Fultz II's avatar
Paul Fultz II committed
2687
        auto slice2 = m2.add_instruction(
2688
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {384}}, {"ends", {768}}}), add);
Paul Fultz II's avatar
Paul Fultz II committed
2689
        m2.add_instruction(pass_op{}, slice1, slice2);
2690
    }
Paul Fultz II's avatar
Paul Fultz II committed
2691
    EXPECT(m1.sort() == m2.sort());
2692
}
2693
2694
2695

template <std::size_t BS, bool TransposeInput>
void reorder_reshape_slice()
2696
2697
2698
{
    std::vector<int64_t> perm0 = {0, 2, 1, 3};
    std::vector<int64_t> perm1 = {0, 2, 3, 1};
2699
2700
2701
2702
2703
2704
2705
    migraphx::module m1;
    {
        auto s = migraphx::shape{migraphx::shape::float_type, {BS, 128, 1920}};
        if(TransposeInput)
        {
            s = migraphx::shape{migraphx::shape::float_type, {BS, 128, 1920}, {165120, 1, 128}};
        }
Paul Fultz II's avatar
Paul Fultz II committed
2706
2707
        auto input = m1.add_parameter("input", s);
        auto slc0  = m1.add_instruction(
2708
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {640}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2709
        auto slc1 = m1.add_instruction(
2710
2711
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {640}}, {"ends", {1280}}}),
            input);
Paul Fultz II's avatar
Paul Fultz II committed
2712
        auto slc2 = m1.add_instruction(
2713
2714
2715
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {1280}}, {"ends", {1920}}}),
            input);

Paul Fultz II's avatar
Paul Fultz II committed
2716
2717
2718
        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);
2719

2720
        std::vector<int64_t> lens = {static_cast<int64_t>(BS), 128, 10, 64};
Paul Fultz II's avatar
Paul Fultz II committed
2721
2722
2723
        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);
2724

2725
2726
2727
        auto t0 = m1.add_instruction(migraphx::make_op("transpose", {{"permutation", perm0}}), r0);
        auto t1 = m1.add_instruction(migraphx::make_op("transpose", {{"permutation", perm0}}), r1);
        auto t2 = m1.add_instruction(migraphx::make_op("transpose", {{"permutation", perm1}}), r2);
2728

Paul Fultz II's avatar
Paul Fultz II committed
2729
2730
2731
        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});
2732
2733
    };

2734
2735
2736
2737
2738
2739
2740
    migraphx::module m2;
    {
        auto s = migraphx::shape{migraphx::shape::float_type, {BS, 128, 1920}};
        if(TransposeInput)
        {
            s = migraphx::shape{migraphx::shape::float_type, {BS, 128, 1920}, {165120, 1, 128}};
        }
2741
2742
        auto input                = m2.add_parameter("input", s);
        auto rsp_input            = input;
2743
2744
        std::vector<int64_t> lens = {static_cast<int64_t>(BS), 128, 30, 64};
        auto r = m2.add_instruction(migraphx::make_op("reshape", {{"dims", lens}}), rsp_input);
2745

Paul Fultz II's avatar
Paul Fultz II committed
2746
        auto slc0 = m2.add_instruction(
2747
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {10}}}), r);
Paul Fultz II's avatar
Paul Fultz II committed
2748
        auto slc1 = m2.add_instruction(
2749
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {10}}, {"ends", {20}}}), r);
Paul Fultz II's avatar
Paul Fultz II committed
2750
        auto slc2 = m2.add_instruction(
2751
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {20}}, {"ends", {30}}}), r);
2752

2753
2754
2755
2756
2757
2758
        auto t0 =
            m2.add_instruction(migraphx::make_op("transpose", {{"permutation", perm0}}), slc0);
        auto t1 =
            m2.add_instruction(migraphx::make_op("transpose", {{"permutation", perm0}}), slc1);
        auto t2 =
            m2.add_instruction(migraphx::make_op("transpose", {{"permutation", perm1}}), slc2);
2759

Paul Fultz II's avatar
Paul Fultz II committed
2760
2761
2762
        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});
2763
    };
2764
2765
    run_pass(m1);
    EXPECT(m1.sort() == m2.sort());
2766
2767
}

2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
TEST_CASE_REGISTER(reorder_reshape_slice<1, true>); // test if contiguous is added as necessary if
                                                    // input is transposed
TEST_CASE_REGISTER(reorder_reshape_slice<4, true>);
TEST_CASE_REGISTER(reorder_reshape_slice<8, true>);
TEST_CASE_REGISTER(reorder_reshape_slice<1, false>);
TEST_CASE_REGISTER(reorder_reshape_slice<4, false>);
TEST_CASE_REGISTER(reorder_reshape_slice<8, false>);

template <std::size_t BS>
void reorder_reshape_slice_move_axis1()
2778
{
2779
2780
2781
    migraphx::module m1;
    {
        auto s                     = migraphx::shape{migraphx::shape::float_type, {BS, 256, 96}};
2782
2783
        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
2784
2785
        auto input                 = m1.add_parameter("input", s);
        auto slc0                  = m1.add_instruction(
2786
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {32}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2787
        auto slc1 = m1.add_instruction(
2788
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {32}}, {"ends", {64}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2789
        auto slc2 = m1.add_instruction(
2790
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {64}}, {"ends", {96}}}), input);
2791

Paul Fultz II's avatar
Paul Fultz II committed
2792
2793
2794
        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);
2795

2796
        std::vector<int64_t> lens = {static_cast<int64_t>(BS), 64, 4, 32};
Paul Fultz II's avatar
Paul Fultz II committed
2797
2798
2799
        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);
2800

2801
2802
2803
        auto t0 = m1.add_instruction(migraphx::make_op("transpose", {{"permutation", perm0}}), r0);
        auto t1 = m1.add_instruction(migraphx::make_op("transpose", {{"permutation", perm0}}), r1);
        auto t2 = m1.add_instruction(migraphx::make_op("transpose", {{"permutation", perm1}}), r2);
2804

Paul Fultz II's avatar
Paul Fultz II committed
2805
2806
2807
        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});
2808
2809
    };

2810
2811
2812
    migraphx::module m2;
    {
        auto s                     = migraphx::shape{migraphx::shape::float_type, {BS, 256, 96}};
2813
2814
        std::vector<int64_t> perm0 = {0, 2, 1, 3};
        std::vector<int64_t> perm1 = {0, 2, 3, 1};
2815
2816
2817
2818
        auto input                 = m2.add_parameter("input", s);
        std::vector<int64_t> lens  = {static_cast<int64_t>(BS), 64, 4, 96};
        auto rsp  = m2.add_instruction(migraphx::make_op("reshape", {{"dims", lens}}), input);
        auto slc0 = m2.add_instruction(
2819
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {0}}, {"ends", {32}}}), rsp);
2820
2821
2822
        auto t0 =
            m2.add_instruction(migraphx::make_op("transpose", {{"permutation", perm0}}), slc0);
        auto slc1 = m2.add_instruction(
2823
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {32}}, {"ends", {64}}}), rsp);
2824
2825
2826
        auto t1 =
            m2.add_instruction(migraphx::make_op("transpose", {{"permutation", perm0}}), slc1);
        auto slc2 = m2.add_instruction(
2827
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {64}}, {"ends", {96}}}), rsp);
2828
2829
        auto t2 =
            m2.add_instruction(migraphx::make_op("transpose", {{"permutation", perm1}}), slc2);
2830

2831
2832
2833
        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});
2834
2835
    };

2836
2837
    run_pass(m1);
    EXPECT(m1.sort() == m2.sort());
2838
2839
}

2840
2841
2842
TEST_CASE_REGISTER(reorder_reshape_slice_move_axis1<4>);
TEST_CASE_REGISTER(reorder_reshape_slice_move_axis1<8>);

2843
2844
TEST_CASE(reorder_reshape_slice_move_axis2)
{
2845
2846
    migraphx::module m1;
    {
2847
        migraphx::shape s{migraphx::shape::float_type, {128, 96}};
Paul Fultz II's avatar
Paul Fultz II committed
2848
2849
        auto input = m1.add_parameter("input", s);
        auto slc0  = m1.add_instruction(
2850
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {32}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2851
        auto slc1 = m1.add_instruction(
2852
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {32}}, {"ends", {64}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2853
        auto slc2 = m1.add_instruction(
2854
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {64}}, {"ends", {96}}}), input);
2855

Paul Fultz II's avatar
Paul Fultz II committed
2856
2857
2858
        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);
2859
2860

        std::vector<int64_t> lens = {1, 16, 8, 32};
Paul Fultz II's avatar
Paul Fultz II committed
2861
2862
2863
        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);
2864

Paul Fultz II's avatar
Paul Fultz II committed
2865
2866
2867
        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});
2868
2869
    };

2870
2871
    migraphx::module m2;
    {
2872
        auto s                    = migraphx::shape{migraphx::shape::float_type, {128, 96}};
2873
        auto input                = m2.add_parameter("input", s);
2874
        std::vector<int64_t> lens = {1, 16, 8, 96};
2875
2876
        auto rsp  = m2.add_instruction(migraphx::make_op("reshape", {{"dims", lens}}), input);
        auto slc0 = m2.add_instruction(
2877
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {0}}, {"ends", {32}}}), rsp);
2878
        auto slc1 = m2.add_instruction(
2879
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {32}}, {"ends", {64}}}), rsp);
2880
        auto slc2 = m2.add_instruction(
2881
2882
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {64}}, {"ends", {96}}}), rsp);

2883
2884
        auto sum = m2.add_instruction(migraphx::make_op("add"), slc0, slc1);
        auto ret = m2.add_instruction(migraphx::make_op("mul"), sum, slc2);
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
        m2.add_return({ret});
    };

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

TEST_CASE(reorder_reshape_slice_len_1)
{
    migraphx::module m1;
    {
        migraphx::shape s{migraphx::shape::float_type, {1, 128, 3}};
        auto input = m1.add_parameter("input", s);
        auto slc0  = m1.add_instruction(
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {1}}}), input);
        auto slc1 = m1.add_instruction(
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {1}}, {"ends", {2}}}), input);
        auto slc2 = m1.add_instruction(
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {2}}, {"ends", {3}}}), input);

        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);

        std::vector<int64_t> lens = {1, 128};
        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);

        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});
    };

    migraphx::module m2;
    {
        auto s                    = migraphx::shape{migraphx::shape::float_type, {1, 128, 3}};
        auto input                = m2.add_parameter("input", s);
        std::vector<int64_t> lens = {1, 384};
        auto rsp  = m2.add_instruction(migraphx::make_op("reshape", {{"dims", lens}}), input);
        auto slc0 = m2.add_instruction(
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {128}}}), rsp);
        auto slc1 = m2.add_instruction(
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {128}}, {"ends", {256}}}), rsp);
        auto slc2 = m2.add_instruction(
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {256}}, {"ends", {384}}}), rsp);

        auto sum = m2.add_instruction(migraphx::make_op("add"), slc0, slc1);
        auto ret = m2.add_instruction(migraphx::make_op("mul"), sum, slc2);
2934
        m2.add_return({ret});
2935
2936
    };

Paul Fultz II's avatar
Paul Fultz II committed
2937
2938
    run_pass(m1);
    EXPECT(m1.sort() == m2.sort());
2939
2940
2941
2942
2943
}

TEST_CASE(reorder_reshape_slice_not_apply)
{
    auto create_p = [] {
Paul Fultz II's avatar
Paul Fultz II committed
2944
        migraphx::module m;
2945
        migraphx::shape s{migraphx::shape::float_type, {128, 96}};
Paul Fultz II's avatar
Paul Fultz II committed
2946
2947
        auto input = m.add_parameter("input", s);
        auto slc0  = m.add_instruction(
2948
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {32}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2949
        auto slc1 = m.add_instruction(
2950
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {32}}, {"ends", {64}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
2951
        auto slc2 = m.add_instruction(
2952
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {64}}, {"ends", {96}}}), input);
2953

Paul Fultz II's avatar
Paul Fultz II committed
2954
2955
2956
        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);
2957
2958

        std::vector<int64_t> lens = {1, 16, 16, 16};
Paul Fultz II's avatar
Paul Fultz II committed
2959
2960
2961
        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);
2962

Paul Fultz II's avatar
Paul Fultz II committed
2963
2964
2965
        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});
2966

Paul Fultz II's avatar
Paul Fultz II committed
2967
        return m;
2968
2969
    };

Paul Fultz II's avatar
Paul Fultz II committed
2970
2971
2972
2973
    auto m1 = create_p();
    auto m2 = m1;
    run_pass(m1);
    EXPECT(m1.sort() == m2.sort());
2974
2975
}

2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
TEST_CASE(reorder_reshape_slice_multi_rsp)
{
    migraphx::module m1;
    {
        migraphx::shape s{migraphx::shape::float_type, {4, 128, 3, 32, 80}};
        auto input = m1.add_parameter("input", s);
        auto t1    = m1.add_instruction(
            migraphx::make_op("transpose", {{"permutation", {2, 0, 3, 1, 4}}}), input);
        auto slc0 = m1.add_instruction(
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {1}}}), t1);
        auto slc1 = m1.add_instruction(
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {1}}, {"ends", {2}}}), t1);
        auto slc2 = m1.add_instruction(
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {2}}, {"ends", {3}}}), t1);

        auto c1_1 = m1.add_instruction(migraphx::make_op("contiguous"), slc1);
        auto c2_1 = m1.add_instruction(migraphx::make_op("contiguous"), slc2);

        auto c1 = m1.add_instruction(migraphx::make_op("contiguous"), slc1);
        auto r1 =
            m1.add_instruction(migraphx::make_op("reshape", {{"dims", {4, 32, 128, 80}}}), c1);

        auto c2 = m1.add_instruction(migraphx::make_op("contiguous"), slc2);
        auto r2 =
            m1.add_instruction(migraphx::make_op("reshape", {{"dims", {4, 32, 128, 80}}}), c2);

        auto r1_1 =
            m1.add_instruction(migraphx::make_op("reshape", {{"dims", {128, 128, 80}}}), c1_1);
        auto r2_1 =
            m1.add_instruction(migraphx::make_op("reshape", {{"dims", {128, 128, 80}}}), c2_1);

        auto c0 = m1.add_instruction(migraphx::make_op("contiguous"), slc0);
        auto r0 = m1.add_instruction(migraphx::make_op("reshape", {{"dims", {128, 128, 80}}}), c0);

        auto t2 =
            m1.add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 2, 1}}}), r1_1);
        auto c_t2 = m1.add_instruction(migraphx::make_op("contiguous"), t2);

        auto dot = m1.add_instruction(migraphx::make_op("dot"), r0, c_t2);

        m1.add_return({r1, r2, dot, r2_1});
    };

    migraphx::module m2;
    {
        migraphx::shape s{migraphx::shape::float_type, {4, 128, 3, 32, 80}};
        auto input = m2.add_parameter("input", s);
        auto t1    = m2.add_instruction(
            migraphx::make_op("transpose", {{"permutation", {2, 0, 3, 1, 4}}}), input);
        auto rsp1 =
3026
            m2.add_instruction(migraphx::make_op("reshape", {{"dims", {384, 128, 80}}}), t1);
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
        auto slc0 = m2.add_instruction(
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {256}}, {"ends", {384}}}), rsp1);
        auto slc1 = m2.add_instruction(
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {128}}, {"ends", {256}}}), rsp1);

        auto t_slc1 =
            m2.add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 2, 1}}}), slc1);
        auto c_t_slc1 = m2.add_instruction(migraphx::make_op("contiguous"), t_slc1);

        auto slc2 = m2.add_instruction(
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {128}}}), rsp1);

        auto dot = m2.add_instruction(migraphx::make_op("dot"), slc2, c_t_slc1);

        auto rsp2 =
3042
            m2.add_instruction(migraphx::make_op("reshape", {{"dims", {12, 32, 128, 80}}}), t1);
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146

        auto slc2_1 = m2.add_instruction(
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {4}}, {"ends", {8}}}), rsp2);

        auto slc2_2 = m2.add_instruction(
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {8}}, {"ends", {12}}}), rsp2);

        m2.add_return({slc2_1, slc2_2, dot, slc0});
    };

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

TEST_CASE(reorder_reshape_slice_partial)
{
    migraphx::module m1;
    {
        migraphx::shape s{migraphx::shape::float_type, {128, 96}};
        auto input = m1.add_parameter("input", s);
        auto slc0  = m1.add_instruction(
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {8}}}), input);
        auto slc1 = m1.add_instruction(
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {8}}, {"ends", {16}}}), input);
        auto slc2 = m1.add_instruction(
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {16}}, {"ends", {24}}}), input);
        auto slc3 = m1.add_instruction(
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {24}}, {"ends", {128}}}), input);

        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);

        std::vector<int64_t> lens = {2, 4, 96};
        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);

        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, slc3});
    };

    migraphx::module m2;
    {
        migraphx::shape s{migraphx::shape::float_type, {128, 96}};
        auto input = m2.add_parameter("input", s);
        auto rsp = m2.add_instruction(migraphx::make_op("reshape", {{"dims", {32, 4, 96}}}), input);
        auto slc3 = m2.add_instruction(
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {24}}, {"ends", {128}}}), input);

        auto slc0 = m2.add_instruction(
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {2}}}), rsp);
        auto slc1 = m2.add_instruction(
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {2}}, {"ends", {4}}}), rsp);
        auto slc2 = m2.add_instruction(
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {4}}, {"ends", {6}}}), rsp);

        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, slc3});
    };

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

TEST_CASE(reorder_reshape_slice_uneven_slice)
{
    auto create_p = [] {
        migraphx::module m;
        migraphx::shape s{migraphx::shape::float_type, {128, 96}};
        auto input = m.add_parameter("input", s);
        auto slc0  = m.add_instruction(
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {31}}}), input);
        auto slc1 = m.add_instruction(
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {31}}, {"ends", {62}}}), input);
        auto slc2 = m.add_instruction(
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {62}}, {"ends", {93}}}), input);
        auto slc3 = m.add_instruction(
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {93}}, {"ends", {128}}}), input);

        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);

        std::vector<int64_t> lens = {1, 31, 96};
        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);

        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, slc3});

        return m;
    };

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

3147
3148
template <std::size_t BS>
void reorder_reshape_slice_diff_dims()
3149
{
3150
3151
3152
3153
3154
    migraphx::module m1;
    {
        auto s     = migraphx::shape{migraphx::shape::float_type, {BS, 96, 96}};
        auto input = m1.add_parameter("input", s);
        auto slc0  = m1.add_instruction(
3155
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {32}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
3156
        auto slc1 = m1.add_instruction(
3157
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {32}}, {"ends", {64}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
3158
        auto slc2 = m1.add_instruction(
3159
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {64}}, {"ends", {96}}}), input);
3160

Paul Fultz II's avatar
Paul Fultz II committed
3161
3162
3163
        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);
3164

3165
3166
        std::vector<int64_t> lens  = {static_cast<int64_t>(BS), 32, 3, 32};
        std::vector<int64_t> lens1 = {static_cast<int64_t>(BS), 48, 2, 32};
Paul Fultz II's avatar
Paul Fultz II committed
3167
        auto r0 = m1.add_instruction(migraphx::make_op("reshape", {{"dims", lens}}), c0);
3168
3169
        auto r1 = m1.add_instruction(migraphx::make_op("reshape", {{"dims", lens1}}), c1);
        auto r2 = m1.add_instruction(migraphx::make_op("reshape", {{"dims", lens}}), c2);
3170

Paul Fultz II's avatar
Paul Fultz II committed
3171
        m1.add_return({r0, r1, r2});
3172
3173
    };

3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
    migraphx::module m2;
    {
        auto s     = migraphx::shape{migraphx::shape::float_type, {BS, 96, 96}};
        auto input = m2.add_parameter("input", s);
        auto slc1  = m2.add_instruction(
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {32}}, {"ends", {64}}}), input);
        auto c1                    = m2.add_instruction(migraphx::make_op("contiguous"), slc1);
        std::vector<int64_t> lens1 = {static_cast<int64_t>(BS), 48, 2, 32};
        auto r1 = m2.add_instruction(migraphx::make_op("reshape", {{"dims", lens1}}), c1);

        std::vector<int64_t> lens = {static_cast<int64_t>(BS), 32, 3, 96};
        auto r_new = m2.add_instruction(migraphx::make_op("reshape", {{"dims", lens}}), input);
        auto slc0  = m2.add_instruction(
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {0}}, {"ends", {32}}}), r_new);
        auto slc2 = m2.add_instruction(
            migraphx::make_op("slice", {{"axes", {3}}, {"starts", {64}}, {"ends", {96}}}), r_new);

        m2.add_return({slc0, r1, slc2});
    };

3194
3195
    run_pass(m1);
    EXPECT(m1.sort() == m2.sort());
3196
3197
}

3198
3199
3200
3201
3202
TEST_CASE_REGISTER(reorder_reshape_slice_diff_dims<4>);
TEST_CASE_REGISTER(reorder_reshape_slice_diff_dims<8>);

template <std::size_t BS>
void reorder_slice_trans()
3203
3204
{
    std::vector<int64_t> perm = {0, 2, 1};
3205
3206
3207
3208

    migraphx::module m1;
    {
        auto s     = migraphx::shape{migraphx::shape::float_type, {BS, 128, 1920}};
Paul Fultz II's avatar
Paul Fultz II committed
3209
3210
        auto input = m1.add_parameter("input", s);
        auto slc0  = m1.add_instruction(
3211
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {640}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
3212
        auto slc1 = m1.add_instruction(
3213
3214
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {640}}, {"ends", {1280}}}),
            input);
Paul Fultz II's avatar
Paul Fultz II committed
3215
        auto slc2 = m1.add_instruction(
3216
3217
3218
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {1280}}, {"ends", {1920}}}),
            input);

3219
3220
3221
        auto t0 = m1.add_instruction(migraphx::make_op("transpose", {{"permutation", perm}}), slc0);
        auto t1 = m1.add_instruction(migraphx::make_op("transpose", {{"permutation", perm}}), slc1);
        auto t2 = m1.add_instruction(migraphx::make_op("transpose", {{"permutation", perm}}), slc2);
3222

Paul Fultz II's avatar
Paul Fultz II committed
3223
3224
3225
        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});
3226
3227
    };

3228
3229
3230
    migraphx::module m2;
    {
        auto s     = migraphx::shape{migraphx::shape::float_type, {BS, 128, 1920}};
Paul Fultz II's avatar
Paul Fultz II committed
3231
        auto input = m2.add_parameter("input", s);
3232
        auto r = m2.add_instruction(migraphx::make_op("transpose", {{"permutation", perm}}), input);
3233

Paul Fultz II's avatar
Paul Fultz II committed
3234
        auto slc0 = m2.add_instruction(
3235
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {0}}, {"ends", {640}}}), r);
Paul Fultz II's avatar
Paul Fultz II committed
3236
        auto slc1 = m2.add_instruction(
3237
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {640}}, {"ends", {1280}}}), r);
Paul Fultz II's avatar
Paul Fultz II committed
3238
        auto slc2 = m2.add_instruction(
3239
            migraphx::make_op("slice", {{"axes", {1}}, {"starts", {1280}}, {"ends", {1920}}}), r);
3240

Paul Fultz II's avatar
Paul Fultz II committed
3241
3242
3243
        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});
3244
3245
    };

3246
3247
    run_pass(m1);
    EXPECT(m1.sort() == m2.sort());
3248
3249
}

3250
3251
3252
3253
3254
TEST_CASE_REGISTER(reorder_slice_trans<1>);
TEST_CASE_REGISTER(reorder_slice_trans<8>);

template <std::size_t BS>
void reorder_slice_trans_diff_perm()
3255
{
3256
3257
3258
    migraphx::module m1;
    {
        auto s                     = migraphx::shape{migraphx::shape::float_type, {BS, 128, 1920}};
3259
3260
        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
3261
3262
        auto input                 = m1.add_parameter("input", s);
        auto slc0                  = m1.add_instruction(
3263
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {640}}}), input);
Paul Fultz II's avatar
Paul Fultz II committed
3264
        auto slc1 = m1.add_instruction(
3265
3266
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {640}}, {"ends", {1280}}}),
            input);
Paul Fultz II's avatar
Paul Fultz II committed
3267
        auto slc2 = m1.add_instruction(
3268
3269
3270
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {1280}}, {"ends", {1920}}}),
            input);

3271
3272
3273
3274
3275
3276
        auto t0 =
            m1.add_instruction(migraphx::make_op("transpose", {{"permutation", perm0}}), slc0);
        auto t1 =
            m1.add_instruction(migraphx::make_op("transpose", {{"permutation", perm0}}), slc1);
        auto t2 =
            m1.add_instruction(migraphx::make_op("transpose", {{"permutation", perm1}}), slc2);
3277

Paul Fultz II's avatar
Paul Fultz II committed
3278
3279
3280
        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});
3281
3282
    };

3283
3284
3285
    run_pass(m1);
    auto m2 = m1;
    EXPECT(m1.sort() == m2.sort());
3286
3287
}

3288
3289
3290
TEST_CASE_REGISTER(reorder_slice_trans_diff_perm<1>);
TEST_CASE_REGISTER(reorder_slice_trans_diff_perm<4>);

3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
TEST_CASE(reorder_slice_trans_multi_outputs)
{
    migraphx::module m1;
    {
        auto s                    = migraphx::shape{migraphx::shape::float_type, {8, 128, 1920}};
        auto input                = m1.add_parameter("input", s);
        std::vector<int64_t> perm = {0, 2, 1};
        auto slc0                 = m1.add_instruction(
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {640}}}), input);
        auto slc1 = m1.add_instruction(
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {640}}, {"ends", {1280}}}),
            input);
        auto slc2 = m1.add_instruction(
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {1280}}, {"ends", {1920}}}),
            input);

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

        auto sum      = m1.add_instruction(migraphx::make_op("add"), t0, t1);
        auto dot      = m1.add_instruction(migraphx::make_op("mul"), sum, t2);
        auto slc_cont = m1.add_instruction(migraphx::make_op("contiguous"), slc1);
        m1.add_return({slc_cont, dot});
    };
    run_pass(m1);
    auto m2 = m1;
    EXPECT(m1.sort() == m2.sort());
}

Shucai Xiao's avatar
Shucai Xiao committed
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
TEST_CASE(reorder_slice_ins_deps)
{
    auto create_module = [] {
        migraphx::module m;
        migraphx::shape sx{migraphx::shape::float_type, {4, 2}};
        migraphx::shape sy{migraphx::shape::float_type, {2, 2}};
        std::vector<float> datax = {0, 1, 2, 3, 4, 5, 6, 7};
        std::vector<float> datay = {0, 1, 2, 3};
        auto inx                 = m.add_literal(migraphx::literal(sx, datax));
        auto iny                 = m.add_literal(migraphx::literal(sy, datay));
        auto slc0                = m.add_instruction(
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {0}}, {"ends", {2}}}), inx);
        auto slc1 = m.add_instruction(
            migraphx::make_op("slice", {{"axes", {0}}, {"starts", {2}}, {"ends", {4}}}), inx);
        auto n0 = m.add_instruction(migraphx::make_op("neg"), slc0);
        auto a0 = m.add_instruction(migraphx::make_op("add"), n0, slc1);
        auto m0 = m.add_instruction(migraphx::make_op("mul"), a0, iny);
        auto r  = m.add_instruction(migraphx::make_op("add"), m0, slc0);
        m.add_return({r});

        return m;
    };

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

3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
TEST_CASE(dot_broadcast_different_rank)
{
    migraphx::module m1;
    {
        auto x  = m1.add_parameter("x", {migraphx::shape::float_type, {768}});
        auto y  = m1.add_parameter("y", {migraphx::shape::float_type, {768, 3072}});
        auto xb = m1.add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", {2, 384, 768}}}), x);
        auto yb = m1.add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", {2, 768, 3072}}}), y);
        auto dot = m1.add_instruction(migraphx::make_op("dot"), xb, yb);
        m1.add_return({dot});
    };

    migraphx::module m2;
    {
        auto x = m2.add_parameter("x", {migraphx::shape::float_type, {768}});
        auto y = m2.add_parameter("y", {migraphx::shape::float_type, {768, 3072}});
        auto xb =
            m2.add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {384, 768}}}), x);
        auto yb =
            m2.add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {768, 3072}}}), y);
        auto dot       = m2.add_instruction(migraphx::make_op("dot"), xb, yb);
        auto broadcast = m2.add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", {2, 384, 3072}}}), dot);
        m2.add_return({broadcast});
    };

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

shivadbhavsar's avatar
shivadbhavsar committed
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
TEST_CASE(dot_fusion_reshape)
{
    migraphx::module m1;
    {
        migraphx::shape s{migraphx::shape::float_type, {2, 4096, 320}};
        auto input = m1.add_parameter("input", s);

        auto p0 = m1.add_literal(
            migraphx::generate_literal({migraphx::shape::float_type, {2, 320, 320}}, 0));
        auto p1 = m1.add_literal(
            migraphx::generate_literal({migraphx::shape::float_type, {2, 320, 320}}, 1));

        auto d0 = m1.add_instruction(migraphx::make_op("dot"), input, p0);
        auto d1 = m1.add_instruction(migraphx::make_op("dot"), input, p1);

        auto r0 =
            m1.add_instruction(migraphx::make_op("reshape", {{"dims", {2, 4096, 8, 40}}}), d0);
        m1.add_return({r0, d1});
    };

    migraphx::module m2;
    {
        migraphx::shape s{migraphx::shape::float_type, {2, 4096, 320}};
        auto input = m2.add_parameter("input", s);

        auto p0 = m2.add_literal(
            migraphx::generate_literal({migraphx::shape::float_type, {2, 320, 320}}, 0));
        auto p1 = m2.add_literal(
            migraphx::generate_literal({migraphx::shape::float_type, {2, 320, 320}}, 1));

        auto c = m2.add_instruction(migraphx::make_op("concat", {{"axis", 2}}), p0, p1);
        auto d = m2.add_instruction(migraphx::make_op("dot"), input, c);

        auto s0 = m2.add_instruction(
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {0}}, {"ends", {320}}}), d);
        auto s1 = m2.add_instruction(
            migraphx::make_op("slice", {{"axes", {2}}, {"starts", {320}}, {"ends", {640}}}), d);

        auto r0 =
3420
            m2.add_instruction(migraphx::make_op("reshape", {{"dims", {2, 4096, 8, 40}}}), s0);
shivadbhavsar's avatar
shivadbhavsar committed
3421
3422
3423
3424
3425
3426
3427
3428

        m2.add_return({r0, s1});
    };

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

3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
TEST_CASE(mul_dot_a)
{
    migraphx::shape as{migraphx::shape::float_type, {2, 256, 32}};
    migraphx::shape bs{migraphx::shape::float_type, {2, 32, 128}};
    migraphx::module m1;
    {
        auto a = m1.add_parameter("input", as);

        auto lit =
            m1.add_literal(migraphx::generate_literal({migraphx::shape::float_type, {1, 1, 32}}));
        auto litb =
            m1.add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", as.lens()}}), lit);
        auto mul = m1.add_instruction(migraphx::make_op("mul"), a, litb);

        auto b   = m1.add_literal(migraphx::generate_literal(bs));
        auto dot = m1.add_instruction(migraphx::make_op("dot"), mul, b);

        m1.add_return({dot});
    };
    run_pass(m1);

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

        auto lit =
            m2.add_literal(migraphx::generate_literal({migraphx::shape::float_type, {1, 1, 32}}));
        auto litb = m2.add_instruction(
            migraphx::make_op("multibroadcast",
                              {{"out_lens", migraphx::reorder_dims(bs.lens(), {0, 2, 1})}}),
            lit);
        auto litt =
            m2.add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 2, 1}}}), litb);

        auto b   = m2.add_literal(migraphx::generate_literal(bs));
        auto mul = m2.add_instruction(migraphx::make_op("mul"), b, litt);
        auto dot = m2.add_instruction(migraphx::make_op("dot"), a, mul);

        m2.add_return({dot});
    };

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

TEST_CASE(mul_dot_b)
{
    migraphx::shape as{migraphx::shape::float_type, {2, 256, 32}};
    migraphx::shape bs{migraphx::shape::float_type, {2, 32, 128}};
    migraphx::module m1;
    {
        auto b = m1.add_parameter("input", bs);

        auto lit =
            m1.add_literal(migraphx::generate_literal({migraphx::shape::float_type, {1, 32, 1}}));
        auto litb =
            m1.add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", bs.lens()}}), lit);
        auto mul = m1.add_instruction(migraphx::make_op("mul"), b, litb);

        auto a   = m1.add_literal(migraphx::generate_literal(as));
        auto dot = m1.add_instruction(migraphx::make_op("dot"), a, mul);

        m1.add_return({dot});
    };
    run_pass(m1);

    migraphx::module m2;
    {

        auto b = m2.add_parameter("input", bs);

        auto lit =
            m2.add_literal(migraphx::generate_literal({migraphx::shape::float_type, {1, 32, 1}}));
        auto litb = m2.add_instruction(
            migraphx::make_op("multibroadcast",
                              {{"out_lens", migraphx::reorder_dims(as.lens(), {0, 2, 1})}}),
            lit);
        auto litt =
            m2.add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 2, 1}}}), litb);

        auto a   = m2.add_literal(migraphx::generate_literal(as));
        auto mul = m2.add_instruction(migraphx::make_op("mul"), a, litt);
        auto dot = m2.add_instruction(migraphx::make_op("dot"), mul, b);

        m2.add_return({dot});
    };

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

TEST_CASE(mul_dot_a_not_k_broadcast)
{
    migraphx::shape as{migraphx::shape::float_type, {2, 256, 32}};
    migraphx::shape bs{migraphx::shape::float_type, {2, 32, 128}};
    migraphx::module m1;
    {
        auto a = m1.add_parameter("input", as);

        auto lit =
            m1.add_literal(migraphx::generate_literal({migraphx::shape::float_type, {1, 256, 1}}));
        auto litb =
            m1.add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", as.lens()}}), lit);
        auto mul = m1.add_instruction(migraphx::make_op("mul"), a, litb);

        auto b   = m1.add_literal(migraphx::generate_literal(bs));
        auto dot = m1.add_instruction(migraphx::make_op("dot"), mul, b);

        m1.add_return({dot});
    };
    migraphx::module m2 = m1;
    run_pass(m1);

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

TEST_CASE(mul_dot_b_not_k_broadcast)
{
    migraphx::shape as{migraphx::shape::float_type, {2, 256, 32}};
    migraphx::shape bs{migraphx::shape::float_type, {2, 32, 128}};
    migraphx::module m1;
    {
        auto b = m1.add_parameter("input", bs);

        auto lit =
            m1.add_literal(migraphx::generate_literal({migraphx::shape::float_type, {1, 1, 128}}));
        auto litb =
            m1.add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", bs.lens()}}), lit);
        auto mul = m1.add_instruction(migraphx::make_op("mul"), b, litb);

        auto a   = m1.add_literal(migraphx::generate_literal(as));
        auto dot = m1.add_instruction(migraphx::make_op("dot"), a, mul);

        m1.add_return({dot});
    };
    migraphx::module m2 = m1;
    run_pass(m1);

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

TEST_CASE(dot_mul_a)
{
    migraphx::shape as{migraphx::shape::float_type, {2, 256, 32}};
    migraphx::shape bs{migraphx::shape::float_type, {2, 32, 128}};
    migraphx::module m1;
    {
        auto a   = m1.add_parameter("input", as);
        auto b   = m1.add_literal(migraphx::generate_literal(bs));
        auto dot = m1.add_instruction(migraphx::make_op("dot"), a, b);
        auto lit =
            m1.add_literal(migraphx::generate_literal({migraphx::shape::float_type, {1, 1, 128}}));
        auto litb = m1.add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", dot->get_shape().lens()}}), lit);
        auto mul = m1.add_instruction(migraphx::make_op("mul"), dot, litb);
        m1.add_return({mul});
    };
    run_pass(m1);

    migraphx::module m2;
    {
        auto a = m2.add_parameter("input", as);
        auto b = m2.add_literal(migraphx::generate_literal(bs));
        auto lit =
            m2.add_literal(migraphx::generate_literal({migraphx::shape::float_type, {1, 1, 128}}));
        auto litb =
            m2.add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", bs.lens()}}), lit);

        auto mul = m2.add_instruction(migraphx::make_op("mul"), b, litb);
        auto dot = m2.add_instruction(migraphx::make_op("dot"), a, mul);
        m2.add_return({dot});
    };

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

TEST_CASE(dot_mul_a_non_const)
{
    migraphx::shape as{migraphx::shape::float_type, {2, 256, 32}};
    migraphx::shape bs{migraphx::shape::float_type, {2, 32, 128}};
    migraphx::module m1;
    {
        auto a   = m1.add_parameter("input", as);
        auto b   = m1.add_literal(migraphx::generate_literal(bs));
        auto dot = m1.add_instruction(migraphx::make_op("dot"), a, b);
        auto lit =
            m1.add_literal(migraphx::generate_literal({migraphx::shape::float_type, {1, 256, 1}}));
        auto litb = m1.add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", dot->get_shape().lens()}}), lit);
        auto mul = m1.add_instruction(migraphx::make_op("mul"), dot, litb);
        m1.add_return({mul});
    };
    migraphx::module m2 = m1;
    run_pass(m1);

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

TEST_CASE(dot_mul_b)
{
    migraphx::shape as{migraphx::shape::float_type, {2, 256, 32}};
    migraphx::shape bs{migraphx::shape::float_type, {2, 32, 128}};
    migraphx::module m1;
    {
        auto a   = m1.add_literal(migraphx::generate_literal(as));
        auto b   = m1.add_parameter("input", bs);
        auto dot = m1.add_instruction(migraphx::make_op("dot"), a, b);
        auto lit =
            m1.add_literal(migraphx::generate_literal({migraphx::shape::float_type, {1, 256, 1}}));
        auto litb = m1.add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", dot->get_shape().lens()}}), lit);
        auto mul = m1.add_instruction(migraphx::make_op("mul"), dot, litb);
        m1.add_return({mul});
    };
    run_pass(m1);

    migraphx::module m2;
    {
        auto a = m2.add_literal(migraphx::generate_literal(as));
        auto b = m2.add_parameter("input", bs);
        auto lit =
            m2.add_literal(migraphx::generate_literal({migraphx::shape::float_type, {1, 256, 1}}));
        auto litb =
            m2.add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", as.lens()}}), lit);

        auto mul = m2.add_instruction(migraphx::make_op("mul"), a, litb);
        auto dot = m2.add_instruction(migraphx::make_op("dot"), mul, b);
        m2.add_return({dot});
    };

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

TEST_CASE(dot_mul_b_non_const)
{
    migraphx::shape as{migraphx::shape::float_type, {2, 256, 32}};
    migraphx::shape bs{migraphx::shape::float_type, {2, 32, 128}};
    migraphx::module m1;
    {
        auto a   = m1.add_literal(migraphx::generate_literal(as));
        auto b   = m1.add_parameter("input", bs);
        auto dot = m1.add_instruction(migraphx::make_op("dot"), a, b);
        auto lit =
            m1.add_literal(migraphx::generate_literal({migraphx::shape::float_type, {1, 1, 128}}));
        auto litb = m1.add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", dot->get_shape().lens()}}), lit);
        auto mul = m1.add_instruction(migraphx::make_op("mul"), dot, litb);
        m1.add_return({mul});
    };
    migraphx::module m2 = m1;
    run_pass(m1);

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

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