"vscode:/vscode.git/clone" did not exist on "b1d3c95425737dcb51598c044b9f2119955882fa"
tf_test.cpp 27.6 KB
Newer Older
1
2
3
#include <iostream>
#include <vector>
#include <migraphx/literal.hpp>
Paul's avatar
Paul committed
4
5
6
7
#include <migraphx/pass_manager.hpp>
#include <migraphx/simplify_reshapes.hpp>
#include <migraphx/dead_code_elimination.hpp>
#include <migraphx/eliminate_identity.hpp>
8
9
10
11
12
13
#include <migraphx/operators.hpp>
#include <migraphx/program.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/tf.hpp>
#include "test.hpp"

14
15
16
17
18
migraphx::program parse_tf(const std::string& name, bool is_nhwc)
{
    return migraphx::parse_tf(name, migraphx::tf_options{is_nhwc, 1});
}

Paul's avatar
Paul committed
19
20
migraphx::program optimize_tf(const std::string& name, bool is_nhwc)
{
21
    auto prog = migraphx::parse_tf(name, migraphx::tf_options{is_nhwc, 1});
22
    auto* mm  = prog.get_main_module();
Paul's avatar
Paul committed
23
    if(is_nhwc)
24
        migraphx::run_passes(*mm,
Paul's avatar
Paul committed
25
26
27
                             {migraphx::simplify_reshapes{},
                              migraphx::dead_code_elimination{},
                              migraphx::eliminate_identity{}});
Paul's avatar
Paul committed
28
29
30
    return prog;
}

31
32
33
TEST_CASE(add_test)
{
    migraphx::program p;
34
35
36
37
38

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 2, 2, 3}});
    auto l1  = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {1, 2, 2, 3}});
    mm->add_instruction(migraphx::op::add{}, l0, l1);
Paul's avatar
Paul committed
39
    auto prog = optimize_tf("add_test.pb", false);
40
41
42
43

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
44
45
46
47
48
49
50
51
52
53
54
TEST_CASE(addv2_test)
{
    migraphx::program p;
    auto l0 = p.add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 2, 2, 3}});
    auto l1 = p.add_parameter("1", migraphx::shape{migraphx::shape::float_type, {1, 2, 2, 3}});
    p.add_instruction(migraphx::op::add{}, l0, l1);
    auto prog = optimize_tf("addv2_test.pb", false);

    EXPECT(p == prog);
}

55
56
TEST_CASE(add_bcast_test)
{
Khalique's avatar
Khalique committed
57

58
    migraphx::program p;
59
60

    auto* mm = p.get_main_module();
61
    migraphx::shape s0{migraphx::shape::float_type, {2, 3}};
62
63
64
65
66
    auto l0 = mm->add_parameter("0", s0);
    auto l1 = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {2, 1}});
    auto l2 = mm->add_instruction(migraphx::op::multibroadcast{s0.lens()}, l0);
    auto l3 = mm->add_instruction(migraphx::op::multibroadcast{s0.lens()}, l1);
    mm->add_instruction(migraphx::op::add{}, l2, l3);
Paul's avatar
Paul committed
67
    auto prog = optimize_tf("add_bcast_test.pb", false);
68
69
70
71

    EXPECT(p == prog);
}

72
73
74
TEST_CASE(argmax_test)
{
    migraphx::program p;
75
76
77
78
79
80

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
    mm->add_literal(migraphx::literal{migraphx::shape{migraphx::shape::int32_type}, {2}});
    auto ins = mm->add_instruction(migraphx::op::argmax{2}, l0);
    mm->add_instruction(migraphx::op::squeeze{{2}}, ins);
81
82
83
84
85
86
87
88
    auto prog = parse_tf("argmax_test.pb", false);

    EXPECT(p == prog);
}

TEST_CASE(argmin_test)
{
    migraphx::program p;
89
90
91
92
93
94

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {3, 4, 5, 6}});
    mm->add_literal(migraphx::literal{migraphx::shape{migraphx::shape::int32_type}, {2}});
    auto ins = mm->add_instruction(migraphx::op::argmin{2}, l0);
    mm->add_instruction(migraphx::op::squeeze{{2}}, ins);
95
96
97
98
99
    auto prog = parse_tf("argmin_test.pb", false);

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
100
101
102
TEST_CASE(assert_less_equal_test)
{
    migraphx::program p;
103
104

    auto* mm = p.get_main_module();
Khalique's avatar
Khalique committed
105
    migraphx::shape s0{migraphx::shape::float_type, {2, 3}};
106
107
    auto l0 = mm->add_parameter("0", s0);
    auto l1 = mm->add_parameter("1", s0);
Khalique's avatar
Khalique committed
108
    migraphx::literal l{migraphx::shape{migraphx::shape::int32_type, {2}}, {0, 1}};
109
110
111
112
    auto l2 = mm->add_literal(l);
    mm->add_instruction(migraphx::op::add{}, l0, l1);
    auto l3 = mm->add_instruction(migraphx::op::identity{}, l0, l1);
    mm->add_instruction(migraphx::op::identity{}, l3, l2);
Khalique's avatar
Khalique committed
113
114
115
116
117
    auto prog = optimize_tf("assert_less_equal_test.pb", false);

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
118
119
120
121
TEST_CASE(batchmatmul_test)
{
    migraphx::program p;

122
123
124
125
126
127
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 2, 8, 4}});
    auto l1  = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {1, 2, 4, 8}});

    auto trans_l0 = mm->add_instruction(migraphx::op::transpose{{0, 1, 3, 2}}, l0);
    auto trans_l1 = mm->add_instruction(migraphx::op::transpose{{0, 1, 3, 2}}, l1);
Khalique's avatar
Khalique committed
128

129
    mm->add_instruction(migraphx::op::dot{}, trans_l0, trans_l1);
Khalique's avatar
Khalique committed
130
131
132
133
134
    auto prog = optimize_tf("batchmatmul_test.pb", false);

    EXPECT(p == prog);
}

135
136
TEST_CASE(batchnorm_test)
{
Khalique's avatar
Khalique committed
137
138
    float epsilon  = 1.001e-5f;
    float momentum = 0.9f;
139
140

    migraphx::program p;
141
142

    auto* mm = p.get_main_module();
Khalique's avatar
Khalique committed
143
144
    migraphx::op::batch_norm_inference op{
        epsilon, momentum, migraphx::op::batch_norm_inference::spatial};
145
    migraphx::shape s0{migraphx::shape::float_type, {32}};
146
    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 32, 16, 16}});
147
148
    std::vector<float> const_vals(32);
    std::fill(const_vals.begin(), const_vals.end(), 1.0f);
Khalique's avatar
Khalique committed
149

150
151
152
153
154
    auto l2 = mm->add_parameter("2", s0);
    auto l3 = mm->add_parameter("3", s0);
    auto l4 = mm->add_parameter("4", s0);
    auto l1 = mm->add_literal(migraphx::literal{s0, const_vals});
    mm->add_instruction(op, l0, l1, l2, l3, l4);
Paul's avatar
Paul committed
155
    auto prog = optimize_tf("batchnorm_test.pb", true);
156
157
158
159

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
TEST_CASE(batchnormv3_test)
{
    float epsilon  = 1.0e-5f;
    float momentum = 0.9f;

    migraphx::program p;
    migraphx::op::batch_norm_inference op{
        epsilon, momentum, migraphx::op::batch_norm_inference::spatial};
    migraphx::shape s0{migraphx::shape::float_type, {32}};
    auto l0 = p.add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 32, 16, 16}});
    std::vector<float> const_vals(32);
    std::fill(const_vals.begin(), const_vals.end(), 1.0f);

    auto l2 = p.add_parameter("2", s0);
    auto l3 = p.add_parameter("3", s0);
    auto l4 = p.add_parameter("4", s0);
    auto l1 = p.add_literal(migraphx::literal{s0, const_vals});
    p.add_instruction(op, l0, l1, l2, l3, l4);
    auto prog = optimize_tf("batchnormv3_test.pb", true);

    EXPECT(p == prog);
}

183
184
185
TEST_CASE(biasadd_test)
{
    migraphx::program p;
186
187

    auto* mm = p.get_main_module();
188
    migraphx::shape s0{migraphx::shape::float_type, {1, 500, 1, 1}};
189
    uint64_t axis = 1;
190
191
192
193
    auto l0       = mm->add_parameter("0", s0);
    auto l1       = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {500}});
    auto l2       = mm->add_instruction(migraphx::op::broadcast{axis, l0->get_shape().lens()}, l1);
    mm->add_instruction(migraphx::op::add{}, l0, l2);
Paul's avatar
Paul committed
194
    auto prog = optimize_tf("biasadd_test.pb", true);
195
196
197
198

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
TEST_CASE(biasadd_scalar_test)
{
    migraphx::program p;

    auto* mm = p.get_main_module();
    migraphx::shape s0{migraphx::shape::float_type, {1, 1}};
    uint64_t axis = 1;
    auto l0       = mm->add_parameter("0", s0);
    auto l1       = mm->add_literal(
        migraphx::literal{migraphx::shape{migraphx::shape::float_type, {1}, {0}}, {1.0}});
    auto l2 = mm->add_instruction(migraphx::op::broadcast{axis, l0->get_shape().lens()}, l1);
    mm->add_instruction(migraphx::op::add{}, l0, l2);
    auto prog = optimize_tf("biasadd_scalar_test.pb", true);

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
216
217
218
TEST_CASE(cast_test)
{
    migraphx::program p;
219
220
221
222

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
    mm->add_instruction(migraphx::op::convert{migraphx::shape::int32_type}, l0);
Khalique's avatar
Khalique committed
223
224
225
226
227
    auto prog = optimize_tf("cast_test.pb", false);

    EXPECT(p == prog);
}

228
229
230
TEST_CASE(concat_test)
{
    migraphx::program p;
Khalique's avatar
Khalique committed
231

232
233
234
235
    auto* mm = p.get_main_module();

    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {4, 7, 3}});
    auto l1 = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {4, 2, 3}});
236
237
238

    int axis = 1;
    // tf uses axis as the third input, and it is in int32 format
Khalique's avatar
Khalique committed
239
    // add the literal using a vector in order to set stride to 1 (like in tf parser)
240
    mm->add_literal(migraphx::shape{migraphx::shape::int32_type}, std::vector<int>{axis});
241

242
    mm->add_instruction(migraphx::op::concat{axis}, l0, l1);
Paul's avatar
Paul committed
243
    auto prog = optimize_tf("concat_test.pb", false);
244
245
246
247
248
249
250

    EXPECT(p == prog);
}

TEST_CASE(const_test)
{
    migraphx::program p;
251
252
253

    auto* mm = p.get_main_module();
    mm->add_literal(migraphx::shape{migraphx::shape::float_type}, std::vector<float>{1.0f});
Paul's avatar
Paul committed
254
    auto prog = optimize_tf("constant_test.pb", false);
255
256
257
258

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
259
migraphx::program create_conv()
260
261
{
    migraphx::program p;
Khalique's avatar
Khalique committed
262

263
264
265
    auto* mm = p.get_main_module();

    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
Khalique's avatar
Khalique committed
266
    std::vector<float> weight_data(3 * 3 * 3 * 32);
267
    std::fill(weight_data.begin(), weight_data.end(), 1.0f);
Khalique's avatar
Khalique committed
268
    auto l1 =
269
        mm->add_literal(migraphx::shape{migraphx::shape::float_type, {3, 3, 3, 32}}, weight_data);
270
271
272

    migraphx::op::convolution op;
    op.padding_mode = migraphx::op::padding_mode_t::same;
Khalique's avatar
Khalique committed
273
    op.padding      = {1, 1};
Khalique's avatar
Khalique committed
274
275
    op.stride       = {1, 1};
    op.dilation     = {1, 1};
276
277
    auto l2         = mm->add_instruction(migraphx::op::transpose{{3, 2, 0, 1}}, l1);
    mm->add_instruction(op, l0, l2);
kahmed10's avatar
kahmed10 committed
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
    return p;
}

TEST_CASE(conv_test)
{
    migraphx::program p = create_conv();
    auto prog           = optimize_tf("conv_test.pb", true);

    EXPECT(p == prog);
}

TEST_CASE(conv_nchw_test)
{
    migraphx::program p = create_conv();
    auto prog           = optimize_tf("conv_nchw_test.pb", false);
293
294
295
296

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
297
298
299
300
TEST_CASE(depthwiseconv_test)
{
    migraphx::program p;

301
302
303
    auto* mm = p.get_main_module();

    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
Khalique's avatar
Khalique committed
304
305
306
    std::vector<float> weight_data(3 * 3 * 3 * 1);
    std::fill(weight_data.begin(), weight_data.end(), 1.0f);
    auto l1 =
307
        mm->add_literal(migraphx::shape{migraphx::shape::float_type, {3, 3, 3, 1}}, weight_data);
Khalique's avatar
Khalique committed
308
309
310

    migraphx::op::convolution op;
    op.padding_mode = migraphx::op::padding_mode_t::same;
Khalique's avatar
Khalique committed
311
    op.padding      = {1, 1};
Khalique's avatar
Khalique committed
312
313
314
    op.stride       = {1, 1};
    op.dilation     = {1, 1};
    op.group        = 3;
315
316
317
318
    auto l3         = mm->add_instruction(migraphx::op::transpose{{3, 2, 0, 1}}, l1);
    auto l4         = mm->add_instruction(migraphx::op::contiguous{}, l3);
    auto l5         = mm->add_instruction(migraphx::op::reshape{{3, 1, 3, 3}}, l4);
    mm->add_instruction(op, l0, l5);
Paul's avatar
Paul committed
319
    auto prog = optimize_tf("depthwise_conv_test.pb", true);
Khalique's avatar
Khalique committed
320
321
322
323

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
324
325
326
TEST_CASE(expanddims_test)
{
    migraphx::program p;
Khalique's avatar
Khalique committed
327

328
329
330
331
332
    auto* mm = p.get_main_module();

    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {2, 3, 4}});
    mm->add_literal(0);
    mm->add_instruction(migraphx::op::reshape{{1, 2, 3, 4}}, l0);
333
    auto prog = optimize_tf("expanddims_test.pb", false);
Khalique's avatar
Khalique committed
334
335
336
337
338
339
340
341
342

    EXPECT(p == prog);
}

TEST_CASE(expanddims_test_neg_dims)
{
    // this check makes sure the pb parses negative dim value correctly
    migraphx::program p;

343
344
345
346
347
    auto* mm = p.get_main_module();

    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {2, 3, 4}});
    mm->add_literal(-1);
    mm->add_instruction(migraphx::op::reshape{{2, 3, 4, 1}}, l0);
348
    auto prog = optimize_tf("expanddims_neg_test.pb", false);
Khalique's avatar
Khalique committed
349
350
351
352

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
353
354
355
356
TEST_CASE(gather_test)
{
    migraphx::program p;

357
358
359
360
361
362
    auto* mm = p.get_main_module();

    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {2, 4}});
    auto l1 = mm->add_literal(
        migraphx::literal{migraphx::shape{migraphx::shape::int32_type, {2}}, {1, 1}});
    mm->add_literal(1);
Khalique's avatar
Khalique committed
363
364

    int axis = 1;
365
    mm->add_instruction(migraphx::op::gather{axis}, l0, l1);
Khalique's avatar
Khalique committed
366
367
    auto prog = optimize_tf("gather_test.pb", false);

Khalique's avatar
Khalique committed
368
369
370
    EXPECT(p == prog);
}

371
372
373
TEST_CASE(identity_test)
{
    migraphx::program p;
374
375
376
377

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
    mm->add_instruction(migraphx::op::identity{}, l0);
Paul's avatar
Paul committed
378
    auto prog = optimize_tf("identity_test.pb", false);
379
380
381
382

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
383
384
385
TEST_CASE(matmul_test)
{
    migraphx::program p;
Khalique's avatar
Khalique committed
386

387
388
389
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {8, 4}});
    auto l1  = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {4, 8}});
Khalique's avatar
Khalique committed
390

391
392
393
394
    auto trans_l0 = mm->add_instruction(migraphx::op::transpose{{1, 0}}, l0);
    auto trans_l1 = mm->add_instruction(migraphx::op::transpose{{1, 0}}, l1);

    mm->add_instruction(migraphx::op::dot{}, trans_l0, trans_l1);
Paul's avatar
Paul committed
395
    auto prog = optimize_tf("matmul_test.pb", false);
Khalique's avatar
Khalique committed
396
397
398
399

    EXPECT(p == prog);
}

400
401
402
TEST_CASE(mean_test)
{
    migraphx::program p;
403
404

    auto* mm = p.get_main_module();
Khalique's avatar
Khalique committed
405
    migraphx::literal l{migraphx::shape{migraphx::shape::int32_type, {2}}, {2, 3}};
406
407
408
    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
    mm->add_literal(l);
    mm->add_literal(l);
Paul's avatar
Paul committed
409
    migraphx::op::reduce_mean op{{2, 3}};
410
411
412
    mm->add_instruction(op, l0);
    auto l3 = mm->add_instruction(op, l0);
    mm->add_instruction(migraphx::op::squeeze{{2, 3}}, l3);
Paul's avatar
Paul committed
413
    auto prog = optimize_tf("mean_test.pb", false);
414
415
416
417
418
419
420

    EXPECT(p == prog);
}

TEST_CASE(mean_test_nhwc)
{
    migraphx::program p;
421
422

    auto* mm = p.get_main_module();
Khalique's avatar
Khalique committed
423
    migraphx::literal l{migraphx::shape{migraphx::shape::int32_type, {2}}, {1, 2}};
424
425
    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
    auto l1 = mm->add_instruction(migraphx::op::transpose{{0, 2, 3, 1}}, l0);
Khalique's avatar
Khalique committed
426
    migraphx::op::reduce_mean op{{1, 2}};
427
428
    auto l2 = mm->add_instruction(op, l1);
    mm->add_instruction(migraphx::op::squeeze{{1, 2}}, l2);
Paul's avatar
Paul committed
429
    auto prog = optimize_tf("mean_test_nhwc.pb", true);
430
431
432
433

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
434
435
436
437
TEST_CASE(mul_test)
{
    migraphx::program p;

438
439
440
441
442
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 1, 1, 16}});
    auto l1  = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {1, 1, 1, 16}});

    mm->add_instruction(migraphx::op::mul{}, l0, l1);
Paul's avatar
Paul committed
443
    auto prog = optimize_tf("mul_test.pb", false);
Khalique's avatar
Khalique committed
444
445
446
447

    EXPECT(p == prog);
}

448
449
450
TEST_CASE(onehot_test)
{
    migraphx::program p;
451
452
453

    auto* mm = p.get_main_module();
    auto l0  = mm->add_literal(
Khalique's avatar
Khalique committed
454
        migraphx::literal{migraphx::shape{migraphx::shape::int32_type, {5}}, {1, 1, 1, 1, 1}});
455
456
457
458
    mm->add_literal(2);
    mm->add_literal(1.0f);
    mm->add_literal(0.0f);
    auto l1 = mm->add_literal(
Khalique's avatar
Khalique committed
459
        migraphx::literal{migraphx::shape{migraphx::shape::float_type, {2, 2}}, {1, 0, 0, 1}});
460
    int axis = 0;
461
    mm->add_instruction(migraphx::op::gather{axis}, l1, l0);
462
463
464
465
466
    auto prog = optimize_tf("onehot_test.pb", false);

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
467
468
469
470
471
472
473
474
TEST_CASE(noop_test)
{
    migraphx::program p;
    auto prog = optimize_tf("noop_test.pb", false);

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
475
476
477
TEST_CASE(pack_test)
{
    migraphx::program p;
478
479
480
481
482

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {2}});
    auto l1  = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {2}});
    auto l2  = mm->add_parameter("2", migraphx::shape{migraphx::shape::float_type, {2}});
Khalique's avatar
Khalique committed
483
484
485
486
    std::vector<migraphx::instruction_ref> args{l0, l1, l2};
    std::vector<migraphx::instruction_ref> unsqueezed_args;
    int64_t axis = 1;

Khalique's avatar
Khalique committed
487
488
489
490
    std::transform(args.begin(),
                   args.end(),
                   std::back_inserter(unsqueezed_args),
                   [&](migraphx::instruction_ref arg) {
491
                       return mm->add_instruction(migraphx::op::unsqueeze{{axis}}, arg);
Khalique's avatar
Khalique committed
492
                   });
493
    mm->add_instruction(migraphx::op::concat{static_cast<int>(axis)}, unsqueezed_args);
Paul's avatar
Paul committed
494
    auto prog = optimize_tf("pack_test.pb", false);
Khalique's avatar
Khalique committed
495
496
497
498

    EXPECT(p == prog);
}

499
500
501
TEST_CASE(pack_test_nhwc)
{
    migraphx::program p;
502
503
504
505
506
507
508
509

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 2, 1, 1}});
    auto lt0 = mm->add_instruction(migraphx::op::transpose{{0, 2, 3, 1}}, l0);
    auto l1  = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {1, 2, 1, 1}});
    auto lt1 = mm->add_instruction(migraphx::op::transpose{{0, 2, 3, 1}}, l1);
    auto l2  = mm->add_parameter("2", migraphx::shape{migraphx::shape::float_type, {1, 2, 1, 1}});
    auto lt2 = mm->add_instruction(migraphx::op::transpose{{0, 2, 3, 1}}, l2);
Paul's avatar
Paul committed
510
    std::vector<migraphx::instruction_ref> args{lt0, lt1, lt2};
511
    std::vector<migraphx::instruction_ref> unsqueezed_args;
Paul's avatar
Paul committed
512
    int64_t nchw_axis = 3;
513
514
515
516
517

    std::transform(args.begin(),
                   args.end(),
                   std::back_inserter(unsqueezed_args),
                   [&](migraphx::instruction_ref arg) {
518
                       return mm->add_instruction(migraphx::op::unsqueeze{{nchw_axis}}, arg);
519
                   });
520
    mm->add_instruction(migraphx::op::concat{static_cast<int>(nchw_axis)}, unsqueezed_args);
Paul's avatar
Paul committed
521
    auto prog = optimize_tf("pack_test_nhwc.pb", true);
522
523
524
525

    EXPECT(p == prog);
}

526
527
528
TEST_CASE(pooling_test)
{
    migraphx::program p;
529
530
531

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
532
533
    migraphx::op::pooling avg_pool_op{"average"};
    migraphx::op::pooling max_pool_op{"max"};
Shucai Xiao's avatar
Shucai Xiao committed
534
535
536
537
    avg_pool_op.stride  = {2, 2};
    max_pool_op.stride  = {2, 2};
    avg_pool_op.lengths = {2, 2};
    max_pool_op.lengths = {2, 2};
538
    mm->add_instruction(max_pool_op, l0);
Paul's avatar
Paul committed
539
    auto prog = optimize_tf("pooling_test.pb", true);
540
541
542
543

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
544
545
546
TEST_CASE(pow_test)
{
    migraphx::program p;
547
548
549
550
551

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 2, 2, 3}});
    auto l1  = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {1, 2, 2, 3}});
    mm->add_instruction(migraphx::op::pow{}, l0, l1);
Khalique's avatar
Khalique committed
552
553
554
555
556
    auto prog = optimize_tf("pow_test.pb", false);

    EXPECT(p == prog);
}

557
558
559
TEST_CASE(relu_test)
{
    migraphx::program p;
560
561
562
563

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
    mm->add_instruction(migraphx::op::relu{}, l0);
Paul's avatar
Paul committed
564
    auto prog = optimize_tf("relu_test.pb", false);
565
566
567
568

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
569
570
571
TEST_CASE(relu6_test)
{
    migraphx::program p;
572
573

    auto* mm = p.get_main_module();
kahmed10's avatar
kahmed10 committed
574
    std::vector<size_t> input_lens{1, 3, 16, 16};
575
576
577
578
579
580
    auto l0      = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, input_lens});
    auto min_val = mm->add_literal(0.0f);
    auto max_val = mm->add_literal(6.0f);
    min_val      = mm->add_instruction(migraphx::op::multibroadcast{input_lens}, min_val);
    max_val      = mm->add_instruction(migraphx::op::multibroadcast{input_lens}, max_val);
    mm->add_instruction(migraphx::op::clip{}, l0, min_val, max_val);
Paul's avatar
Paul committed
581
    auto prog = optimize_tf("relu6_test.pb", false);
Khalique's avatar
Khalique committed
582
583
584
585

    EXPECT(p == prog);
}

586
587
588
TEST_CASE(reshape_test)
{
    migraphx::program p;
589
590
591

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {16}});
592
593
    migraphx::shape s0{migraphx::shape::int32_type, {4}};
    // in tf, the second arg is a literal that contains new dimensions
594
595
    mm->add_literal(migraphx::literal{s0, {1, 1, 1, 16}});
    mm->add_instruction(migraphx::op::reshape{{1, 1, 1, 16}}, l0);
Paul's avatar
Paul committed
596
    auto prog = optimize_tf("reshape_test.pb", false);
597
598
599
600

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
601
602
603
TEST_CASE(rsqrt_test)
{
    migraphx::program p;
604
605
606
607

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
    mm->add_instruction(migraphx::op::rsqrt{}, l0);
Khalique's avatar
Khalique committed
608
609
610
611
612
    auto prog = optimize_tf("rsqrt_test.pb", false);

    EXPECT(p == prog);
}

613
614
615
TEST_CASE(shape_test)
{
    migraphx::program p;
616
617
618
619

    auto* mm = p.get_main_module();
    mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
    mm->add_literal(
620
621
622
623
624
625
        migraphx::literal{migraphx::shape{migraphx::shape::int32_type, {4}}, {1, 3, 16, 16}});
    auto prog = optimize_tf("shape_test.pb", false);

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
626
627
628
TEST_CASE(slice_test)
{
    migraphx::program p;
629
630

    auto* mm             = p.get_main_module();
Khalique's avatar
Khalique committed
631
    std::size_t num_axes = 2;
632
    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {5, 10}});
Khalique's avatar
Khalique committed
633
    migraphx::shape s0{migraphx::shape::int32_type, {num_axes}};
634
635
    mm->add_literal(migraphx::literal{s0, {1, 0}});
    mm->add_literal(migraphx::literal{s0, {2, -1}});
Khalique's avatar
Khalique committed
636
637
638
639
640
641

    migraphx::op::slice op;
    op.starts = {1, 0};
    op.ends   = {3, 10};
    op.axes   = std::vector<int64_t>(num_axes);
    std::iota(op.axes.begin(), op.axes.end(), 0);
642
    mm->add_instruction(op, l0);
Khalique's avatar
Khalique committed
643
644
645
646
647
    auto prog = optimize_tf("slice_test.pb", false);

    EXPECT(p == prog);
}

648
649
650
TEST_CASE(softmax_test)
{
    migraphx::program p;
651
652
653
654

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3}});
    mm->add_instruction(migraphx::op::softmax{1}, l0);
Paul's avatar
Paul committed
655
    auto prog = optimize_tf("softmax_test.pb", false);
656
657
658
659

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
660
661
662
TEST_CASE(split_test)
{
    migraphx::program p;
663
664

    auto* mm = p.get_main_module();
kahmed10's avatar
kahmed10 committed
665
    std::vector<int64_t> axes{0, 1};
666
667
668
669
670
671
672
673
674
675
    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {5, 30}});
    mm->add_literal(3); // num_splits
    mm->add_literal(1); // split axis
    mm->add_literal(1); // concat axis
    mm->add_literal(1); // concat axis
    auto l1 = mm->add_instruction(migraphx::op::slice{axes, {0, 0}, {5, 10}}, l0);
    auto l2 = mm->add_instruction(migraphx::op::slice{axes, {0, 10}, {5, 20}}, l0);
    auto l3 = mm->add_instruction(migraphx::op::slice{axes, {0, 20}, {5, 30}}, l0);
    mm->add_instruction(migraphx::op::concat{1}, l1, l2);
    mm->add_instruction(migraphx::op::concat{1}, l2, l3);
kahmed10's avatar
kahmed10 committed
676

677
    auto prog = parse_tf("split_test.pb", false);
kahmed10's avatar
kahmed10 committed
678
679
680
681
682
683
684

    EXPECT(p == prog);
}

TEST_CASE(split_test_one_output)
{
    migraphx::program p;
685
686
687
688
689
690

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {5, 30}});
    mm->add_literal(1); // num_splits
    mm->add_literal(1); // split axis
    mm->add_instruction(migraphx::op::identity{}, l0);
kahmed10's avatar
kahmed10 committed
691

692
    auto prog = parse_tf("split_test_one_output.pb", false);
kahmed10's avatar
kahmed10 committed
693
694
695
696
697
698
699

    EXPECT(p == prog);
}

TEST_CASE(split_test_vector_as_input)
{
    migraphx::program p;
700
701

    auto* mm = p.get_main_module();
kahmed10's avatar
kahmed10 committed
702
    std::vector<int64_t> axes{0, 1};
703
    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {5, 30}});
kahmed10's avatar
kahmed10 committed
704
    // split sizes
705
    mm->add_literal(
kahmed10's avatar
kahmed10 committed
706
        migraphx::literal{migraphx::shape{migraphx::shape::int32_type, {3}}, {4, 15, 11}});
707
708
709
710
711
712
713
714
    mm->add_literal(1); // split axis
    mm->add_literal(1); // concat axis
    mm->add_literal(1); // concat axis
    auto l1 = mm->add_instruction(migraphx::op::slice{axes, {0, 0}, {5, 4}}, l0);
    auto l2 = mm->add_instruction(migraphx::op::slice{axes, {0, 4}, {5, 19}}, l0);
    auto l3 = mm->add_instruction(migraphx::op::slice{axes, {0, 19}, {5, 30}}, l0);
    mm->add_instruction(migraphx::op::concat{1}, l1, l2);
    mm->add_instruction(migraphx::op::concat{1}, l2, l3);
kahmed10's avatar
kahmed10 committed
715

716
    auto prog = parse_tf("split_test_vector_as_input.pb", false);
kahmed10's avatar
kahmed10 committed
717
718
719
720

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
721
722
723
TEST_CASE(sqdiff_test)
{
    migraphx::program p;
724
725
726
727
728

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 2, 2, 3}});
    auto l1  = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {1, 2, 2, 3}});
    mm->add_instruction(migraphx::op::sqdiff{}, l0, l1);
Khalique's avatar
Khalique committed
729
730
731
732
733
    auto prog = optimize_tf("sqdiff_test.pb", false);

    EXPECT(p == prog);
}

734
735
736
TEST_CASE(squeeze_test)
{
    migraphx::program p;
737
738
739
740

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 2, 3, 1}});
    mm->add_instruction(migraphx::op::squeeze{{0, 3}}, l0);
Paul's avatar
Paul committed
741
    auto prog = optimize_tf("squeeze_test.pb", false);
742
743
744

    EXPECT(p == prog);
}
Khalique's avatar
Khalique committed
745

Khalique's avatar
Khalique committed
746
747
748
TEST_CASE(stopgradient_test)
{
    migraphx::program p;
749
750
751
752

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
    mm->add_instruction(migraphx::op::identity{}, l0);
Khalique's avatar
Khalique committed
753
754
    auto prog = optimize_tf("stopgradient_test.pb", false);

Khalique's avatar
Khalique committed
755
    EXPECT(p == prog);
Khalique's avatar
Khalique committed
756
757
}

Khalique's avatar
Khalique committed
758
759
760
TEST_CASE(stridedslice_test)
{
    migraphx::program p;
761
762
763
764

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 10, 1, 1}});
    auto l1  = mm->add_instruction(migraphx::op::transpose{{0, 2, 3, 1}}, l0);
Khalique's avatar
Khalique committed
765
766
    std::size_t num_axes = 4;
    migraphx::op::slice op;
Khalique's avatar
Khalique committed
767
    op.starts = {0, 0, 0, 0};
Paul's avatar
Paul committed
768
    op.ends   = {1, 1, 1, 5};
Khalique's avatar
Khalique committed
769
770
    op.axes   = std::vector<int64_t>(num_axes);
    std::iota(op.axes.begin(), op.axes.end(), 0);
771
    auto l2          = mm->add_instruction(op, l1);
Paul's avatar
Paul committed
772
    auto shrink_axis = 1;
773
    mm->add_instruction(migraphx::op::squeeze{{shrink_axis}}, l2);
Paul's avatar
Paul committed
774
    auto prog = optimize_tf("stridedslice_test.pb", true);
Khalique's avatar
Khalique committed
775
776
777
778

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
779
780
781
TEST_CASE(stridedslice_masks_test)
{
    migraphx::program p;
782
783
784

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 10, 3, 3}});
Khalique's avatar
Khalique committed
785
786
    std::size_t num_axes = 4;
    migraphx::op::slice op;
787
788
    op.starts = {0, 1, 1, 0};
    op.ends   = {1, 3, 3, 10};
Khalique's avatar
Khalique committed
789
790
791
    op.axes   = std::vector<int64_t>(num_axes);
    std::iota(op.axes.begin(), op.axes.end(), 0);
    // add literals for starts, ends, and strides in tf (NHWC format)
792
793
794
795
796
797
798
799
800
801
    mm->add_literal(migraphx::shape{migraphx::shape::int32_type, {4}},
                    std::vector<int>{0, 1, 1, 0});
    mm->add_literal(migraphx::shape{migraphx::shape::int32_type, {4}},
                    std::vector<int>{0, 0, 0, 0});
    mm->add_literal(migraphx::shape{migraphx::shape::int32_type, {4}},
                    std::vector<int>{1, 1, 1, 1});

    auto l1 = mm->add_instruction(migraphx::op::transpose{{0, 2, 3, 1}}, l0);
    auto l2 = mm->add_instruction(op, l1);
    mm->add_instruction(migraphx::op::transpose{{0, 3, 1, 2}}, l2);
802
    auto prog = parse_tf("stridedslice_masks_test.pb", true);
Khalique's avatar
Khalique committed
803
804
805
806

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
807
808
809
TEST_CASE(sub_test)
{
    migraphx::program p;
810
811
812
813
814

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 2, 2, 3}});
    auto l1  = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {1, 2, 2, 3}});
    mm->add_instruction(migraphx::op::sub{}, l0, l1);
815
    auto prog = parse_tf("sub_test.pb", false);
Khalique's avatar
Khalique committed
816
817
818
819

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
820
821
822
TEST_CASE(tanh_test)
{
    migraphx::program p;
823
824
825
826
827

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 2, 2, 3}});
    auto l1  = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {1, 2, 2, 3}});
    mm->add_instruction(migraphx::op::sub{}, l0, l1);
828
    auto prog = parse_tf("sub_test.pb", false);
Khalique's avatar
Khalique committed
829
830
831
832

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
833
834
835
TEST_CASE(transpose_test)
{
    migraphx::program p;
836
837
838

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
Khalique's avatar
Khalique committed
839
    migraphx::shape s0{migraphx::shape::int32_type, {4}};
840
841
    mm->add_literal(migraphx::literal{s0, {0, 2, 3, 1}});
    mm->add_instruction(migraphx::op::transpose{{0, 2, 3, 1}}, l0);
Khalique's avatar
Khalique committed
842
843
844
845
846
    auto prog = optimize_tf("transpose_test.pb", false);

    EXPECT(p == prog);
}

847
848
849
TEST_CASE(variable_batch_test)
{
    migraphx::program p;
850
851
852
853

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
    mm->add_instruction(migraphx::op::identity{}, l0);
854
855
856
857
858
    auto prog = optimize_tf("variable_batch_test.pb", false);

    EXPECT(p == prog);
}

859
int main(int argc, const char* argv[]) { test::run(argc, argv); }