tf_test.cpp 29.7 KB
Newer Older
1
2
#include <iostream>
#include <vector>
Shucai Xiao's avatar
Shucai Xiao committed
3
#include <unordered_map>
4
#include <migraphx/literal.hpp>
Paul's avatar
Paul committed
5
6
7
8
#include <migraphx/pass_manager.hpp>
#include <migraphx/simplify_reshapes.hpp>
#include <migraphx/dead_code_elimination.hpp>
#include <migraphx/eliminate_identity.hpp>
9
10
11
12
#include <migraphx/operators.hpp>
#include <migraphx/program.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/tf.hpp>
13
14
15
16
#include <migraphx/make_op.hpp>

#include <migraphx/serialize.hpp>

17
18
#include "test.hpp"

Shucai Xiao's avatar
Shucai Xiao committed
19
20
21
22
migraphx::program
parse_tf(const std::string& name,
         bool is_nhwc,
         const std::unordered_map<std::string, std::vector<std::size_t>>& dim_params = {})
23
{
Shucai Xiao's avatar
Shucai Xiao committed
24
    return migraphx::parse_tf(name, migraphx::tf_options{is_nhwc, 1, dim_params});
25
26
}

Paul's avatar
Paul committed
27
28
migraphx::program optimize_tf(const std::string& name, bool is_nhwc)
{
29
    auto prog = migraphx::parse_tf(name, migraphx::tf_options{is_nhwc, 1});
30
    auto* mm  = prog.get_main_module();
Paul's avatar
Paul committed
31
    if(is_nhwc)
32
        migraphx::run_passes(*mm,
Paul's avatar
Paul committed
33
34
35
                             {migraphx::simplify_reshapes{},
                              migraphx::dead_code_elimination{},
                              migraphx::eliminate_identity{}});
Paul's avatar
Paul committed
36
37
38
    return prog;
}

39
40
41
TEST_CASE(add_test)
{
    migraphx::program p;
42
43
44
45

    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}});
46
    mm->add_instruction(migraphx::make_op("add"), l0, l1);
Paul's avatar
Paul committed
47
    auto prog = optimize_tf("add_test.pb", false);
48
49
50
51

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
52
53
54
TEST_CASE(addv2_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
55
56
57
58
    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::make_op("add"), l0, l1);
kahmed10's avatar
kahmed10 committed
59
60
61
62
63
    auto prog = optimize_tf("addv2_test.pb", false);

    EXPECT(p == prog);
}

64
65
TEST_CASE(add_bcast_test)
{
Khalique's avatar
Khalique committed
66

67
    migraphx::program p;
68
69

    auto* mm = p.get_main_module();
70
    migraphx::shape s0{migraphx::shape::float_type, {2, 3}};
71
72
    auto l0 = mm->add_parameter("0", s0);
    auto l1 = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {2, 1}});
73
74
    auto l2 =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"output_lens", s0.lens()}}), l1);
kahmed10's avatar
kahmed10 committed
75
    mm->add_instruction(migraphx::make_op("add"), l0, l2);
Paul's avatar
Paul committed
76
    auto prog = optimize_tf("add_bcast_test.pb", false);
77
78
79
80

    EXPECT(p == prog);
}

81
82
83
TEST_CASE(argmax_test)
{
    migraphx::program p;
84
85

    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
86
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {4, 5, 6, 7}});
87
    mm->add_literal(migraphx::literal{migraphx::shape{migraphx::shape::int32_type}, {2}});
88
89
    auto ins = mm->add_instruction(migraphx::make_op("argmax", {{"axis", 2}}), l0);
    mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {2}}}), ins);
Shucai Xiao's avatar
Shucai Xiao committed
90
    auto prog = parse_tf("argmax_test.pb", false, {{"0", {4, 5, 6, 7}}});
91
92
93
94
95
96
97

    EXPECT(p == prog);
}

TEST_CASE(argmin_test)
{
    migraphx::program p;
98
99
100
101

    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}});
102
103
    auto ins = mm->add_instruction(migraphx::make_op("argmin", {{"axis", 2}}), l0);
    mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {2}}}), ins);
104
105
106
107
108
    auto prog = parse_tf("argmin_test.pb", false);

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
109
110
111
TEST_CASE(assert_less_equal_test)
{
    migraphx::program p;
112
113

    auto* mm = p.get_main_module();
Khalique's avatar
Khalique committed
114
    migraphx::shape s0{migraphx::shape::float_type, {2, 3}};
115
116
    auto l0 = mm->add_parameter("0", s0);
    auto l1 = mm->add_parameter("1", s0);
Khalique's avatar
Khalique committed
117
    migraphx::literal l{migraphx::shape{migraphx::shape::int32_type, {2}}, {0, 1}};
118
    auto l2 = mm->add_literal(l);
119
120
121
    mm->add_instruction(migraphx::make_op("add"), l0, l1);
    auto l3 = mm->add_instruction(migraphx::make_op("identity"), l0, l1);
    mm->add_instruction(migraphx::make_op("identity"), l3, l2);
Khalique's avatar
Khalique committed
122
123
124
125
126
    auto prog = optimize_tf("assert_less_equal_test.pb", false);

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
127
128
129
130
TEST_CASE(batchmatmul_test)
{
    migraphx::program p;

131
132
133
134
    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}});

135
136
137
138
    auto trans_l0 =
        mm->add_instruction(migraphx::make_op("transpose", {{"dims", {0, 1, 3, 2}}}), l0);
    auto trans_l1 =
        mm->add_instruction(migraphx::make_op("transpose", {{"dims", {0, 1, 3, 2}}}), l1);
Khalique's avatar
Khalique committed
139

140
    mm->add_instruction(migraphx::make_op("dot"), trans_l0, trans_l1);
Khalique's avatar
Khalique committed
141
142
143
144
145
    auto prog = optimize_tf("batchmatmul_test.pb", false);

    EXPECT(p == prog);
}

146
147
TEST_CASE(batchnorm_test)
{
Khalique's avatar
Khalique committed
148
149
    float epsilon  = 1.001e-5f;
    float momentum = 0.9f;
150
151

    migraphx::program p;
152
153

    auto* mm = p.get_main_module();
Khalique's avatar
Khalique committed
154
155
    migraphx::op::batch_norm_inference op{
        epsilon, momentum, migraphx::op::batch_norm_inference::spatial};
156
    migraphx::shape s0{migraphx::shape::float_type, {32}};
157
    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 32, 16, 16}});
158
159
    std::vector<float> const_vals(32);
    std::fill(const_vals.begin(), const_vals.end(), 1.0f);
Khalique's avatar
Khalique committed
160

161
162
163
164
165
    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
166
    auto prog = optimize_tf("batchnorm_test.pb", true);
167
168
169
170

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
171
172
173
174
175
176
TEST_CASE(batchnormv3_test)
{
    float epsilon  = 1.0e-5f;
    float momentum = 0.9f;

    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
177
    auto* mm = p.get_main_module();
kahmed10's avatar
kahmed10 committed
178
179
180
    migraphx::op::batch_norm_inference op{
        epsilon, momentum, migraphx::op::batch_norm_inference::spatial};
    migraphx::shape s0{migraphx::shape::float_type, {32}};
Shucai Xiao's avatar
Shucai Xiao committed
181
    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 32, 16, 16}});
kahmed10's avatar
kahmed10 committed
182
183
184
    std::vector<float> const_vals(32);
    std::fill(const_vals.begin(), const_vals.end(), 1.0f);

Shucai Xiao's avatar
Shucai Xiao committed
185
186
187
188
189
    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);
kahmed10's avatar
kahmed10 committed
190
191
192
193
194
    auto prog = optimize_tf("batchnormv3_test.pb", true);

    EXPECT(p == prog);
}

195
196
197
TEST_CASE(biasadd_test)
{
    migraphx::program p;
198
199

    auto* mm = p.get_main_module();
200
    migraphx::shape s0{migraphx::shape::float_type, {1, 500, 1, 1}};
201
    uint64_t axis = 1;
202
203
    auto l0       = mm->add_parameter("0", s0);
    auto l1       = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {500}});
204
205
206
    auto l2       = mm->add_instruction(
        migraphx::make_op("broadcast", {{"axis", axis}, {"dims", l0->get_shape().lens()}}), l1);
    mm->add_instruction(migraphx::make_op("add"), l0, l2);
Paul's avatar
Paul committed
207
    auto prog = optimize_tf("biasadd_test.pb", true);
208
209
210
211

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
212
213
214
215
216
217
218
219
220
221
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}});
222
223
224
    auto l2 = mm->add_instruction(
        migraphx::make_op("broadcast", {{"axis", axis}, {"dims", l0->get_shape().lens()}}), l1);
    mm->add_instruction(migraphx::make_op("add"), l0, l2);
kahmed10's avatar
kahmed10 committed
225
226
227
228
229
    auto prog = optimize_tf("biasadd_scalar_test.pb", true);

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
230
231
232
TEST_CASE(cast_test)
{
    migraphx::program p;
233
234
235

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
236
237
238
239
    mm->add_instruction(
        migraphx::make_op("convert",
                          {{"target_type", migraphx::to_value(migraphx::shape::int32_type)}}),
        l0);
Khalique's avatar
Khalique committed
240
241
242
243
244
    auto prog = optimize_tf("cast_test.pb", false);

    EXPECT(p == prog);
}

245
246
247
TEST_CASE(concat_test)
{
    migraphx::program p;
Khalique's avatar
Khalique committed
248

249
250
251
252
    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}});
253
254
255

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

259
    mm->add_instruction(migraphx::make_op("concat", {{"axis", axis}}), l0, l1);
Paul's avatar
Paul committed
260
    auto prog = optimize_tf("concat_test.pb", false);
261
262
263
264
265
266
267

    EXPECT(p == prog);
}

TEST_CASE(const_test)
{
    migraphx::program p;
268
269
270

    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
271
    auto prog = optimize_tf("constant_test.pb", false);
272
273
274
275

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
276
migraphx::program create_conv()
277
278
{
    migraphx::program p;
Khalique's avatar
Khalique committed
279

280
281
282
    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
283
    std::vector<float> weight_data(3 * 3 * 3 * 32);
284
    std::fill(weight_data.begin(), weight_data.end(), 1.0f);
Khalique's avatar
Khalique committed
285
    auto l1 =
286
        mm->add_literal(migraphx::shape{migraphx::shape::float_type, {3, 3, 3, 32}}, weight_data);
287
288
289

    migraphx::op::convolution op;
    op.padding_mode = migraphx::op::padding_mode_t::same;
Khalique's avatar
Khalique committed
290
    op.padding      = {1, 1};
Khalique's avatar
Khalique committed
291
292
    op.stride       = {1, 1};
    op.dilation     = {1, 1};
293
    auto l2 = mm->add_instruction(migraphx::make_op("transpose", {{"dims", {3, 2, 0, 1}}}), l1);
294
    mm->add_instruction(op, l0, l2);
kahmed10's avatar
kahmed10 committed
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
    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);
310
311
312
313

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
314
315
316
317
TEST_CASE(depthwiseconv_test)
{
    migraphx::program p;

318
319
320
    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
321
322
323
    std::vector<float> weight_data(3 * 3 * 3 * 1);
    std::fill(weight_data.begin(), weight_data.end(), 1.0f);
    auto l1 =
324
        mm->add_literal(migraphx::shape{migraphx::shape::float_type, {3, 3, 3, 1}}, weight_data);
Khalique's avatar
Khalique committed
325
326
327

    migraphx::op::convolution op;
    op.padding_mode = migraphx::op::padding_mode_t::same;
Khalique's avatar
Khalique committed
328
    op.padding      = {1, 1};
Khalique's avatar
Khalique committed
329
330
331
    op.stride       = {1, 1};
    op.dilation     = {1, 1};
    op.group        = 3;
332
333
334
    auto l3 = mm->add_instruction(migraphx::make_op("transpose", {{"dims", {3, 2, 0, 1}}}), l1);
    auto l4 = mm->add_instruction(migraphx::make_op("contiguous"), l3);
    auto l5 = mm->add_instruction(migraphx::make_op("reshape", {{"dims", {3, 1, 3, 3}}}), l4);
335
    mm->add_instruction(op, l0, l5);
Paul's avatar
Paul committed
336
    auto prog = optimize_tf("depthwise_conv_test.pb", true);
Khalique's avatar
Khalique committed
337
338
339
340

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
341
342
343
TEST_CASE(expanddims_test)
{
    migraphx::program p;
Khalique's avatar
Khalique committed
344

345
346
347
348
    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);
349
    mm->add_instruction(migraphx::make_op("reshape", {{"dims", {1, 2, 3, 4}}}), l0);
350
    auto prog = optimize_tf("expanddims_test.pb", false);
Khalique's avatar
Khalique committed
351
352
353
354
355
356
357
358
359

    EXPECT(p == prog);
}

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

360
361
362
363
    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);
364
    mm->add_instruction(migraphx::make_op("reshape", {{"dims", {2, 3, 4, 1}}}), l0);
365
    auto prog = optimize_tf("expanddims_neg_test.pb", false);
Khalique's avatar
Khalique committed
366
367
368
369

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
370
371
372
373
TEST_CASE(gather_test)
{
    migraphx::program p;

374
375
376
377
378
379
    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
380
381

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

Khalique's avatar
Khalique committed
385
386
387
    EXPECT(p == prog);
}

388
389
390
TEST_CASE(identity_test)
{
    migraphx::program p;
391
392
393

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
394
    mm->add_instruction(migraphx::make_op("identity"), l0);
Paul's avatar
Paul committed
395
    auto prog = optimize_tf("identity_test.pb", false);
396
397
398
399

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
400
401
402
TEST_CASE(matmul_test)
{
    migraphx::program p;
Khalique's avatar
Khalique committed
403

404
405
406
    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
407

408
409
    auto trans_l0 = mm->add_instruction(migraphx::make_op("transpose", {{"dims", {1, 0}}}), l0);
    auto trans_l1 = mm->add_instruction(migraphx::make_op("transpose", {{"dims", {1, 0}}}), l1);
410

411
    mm->add_instruction(migraphx::make_op("dot"), trans_l0, trans_l1);
Paul's avatar
Paul committed
412
    auto prog = optimize_tf("matmul_test.pb", false);
Khalique's avatar
Khalique committed
413
414
415
416

    EXPECT(p == prog);
}

417
418
419
TEST_CASE(mean_test)
{
    migraphx::program p;
420
421

    auto* mm = p.get_main_module();
Khalique's avatar
Khalique committed
422
    migraphx::literal l{migraphx::shape{migraphx::shape::int32_type, {2}}, {2, 3}};
423
424
425
    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
426
    migraphx::op::reduce_mean op{{2, 3}};
427
428
    mm->add_instruction(op, l0);
    auto l3 = mm->add_instruction(op, l0);
429
    mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {2, 3}}}), l3);
Paul's avatar
Paul committed
430
    auto prog = optimize_tf("mean_test.pb", false);
431
432
433
434
435
436
437

    EXPECT(p == prog);
}

TEST_CASE(mean_test_nhwc)
{
    migraphx::program p;
438
439

    auto* mm = p.get_main_module();
Khalique's avatar
Khalique committed
440
    migraphx::literal l{migraphx::shape{migraphx::shape::int32_type, {2}}, {1, 2}};
441
    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
442
    auto l1 = mm->add_instruction(migraphx::make_op("transpose", {{"dims", {0, 2, 3, 1}}}), l0);
Khalique's avatar
Khalique committed
443
    migraphx::op::reduce_mean op{{1, 2}};
444
    auto l2 = mm->add_instruction(op, l1);
445
    mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {1, 2}}}), l2);
Paul's avatar
Paul committed
446
    auto prog = optimize_tf("mean_test_nhwc.pb", true);
447
448
449
450

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
451
452
453
454
TEST_CASE(mul_test)
{
    migraphx::program p;

455
456
457
458
    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}});

459
    mm->add_instruction(migraphx::make_op("mul"), l0, l1);
Paul's avatar
Paul committed
460
    auto prog = optimize_tf("mul_test.pb", false);
Khalique's avatar
Khalique committed
461
462
463
464

    EXPECT(p == prog);
}

465
466
467
TEST_CASE(onehot_test)
{
    migraphx::program p;
468
469
470

    auto* mm = p.get_main_module();
    auto l0  = mm->add_literal(
Khalique's avatar
Khalique committed
471
        migraphx::literal{migraphx::shape{migraphx::shape::int32_type, {5}}, {1, 1, 1, 1, 1}});
472
473
474
475
    mm->add_literal(2);
    mm->add_literal(1.0f);
    mm->add_literal(0.0f);
    auto l1 = mm->add_literal(
Khalique's avatar
Khalique committed
476
        migraphx::literal{migraphx::shape{migraphx::shape::float_type, {2, 2}}, {1, 0, 0, 1}});
477
    int axis = 0;
478
    mm->add_instruction(migraphx::make_op("gather", {{"axis", axis}}), l1, l0);
479
480
481
482
483
    auto prog = optimize_tf("onehot_test.pb", false);

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
484
485
486
487
488
489
490
491
TEST_CASE(noop_test)
{
    migraphx::program p;
    auto prog = optimize_tf("noop_test.pb", false);

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
492
493
494
TEST_CASE(pack_test)
{
    migraphx::program p;
495
496
497
498
499

    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
500
501
502
503
    std::vector<migraphx::instruction_ref> args{l0, l1, l2};
    std::vector<migraphx::instruction_ref> unsqueezed_args;
    int64_t axis = 1;

504
505
506
507
508
509
510
511
512
    std::transform(
        args.begin(),
        args.end(),
        std::back_inserter(unsqueezed_args),
        [&](migraphx::instruction_ref arg) {
            return mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {axis}}}), arg);
        });
    mm->add_instruction(migraphx::make_op("concat", {{"axis", static_cast<int>(axis)}}),
                        unsqueezed_args);
Paul's avatar
Paul committed
513
    auto prog = optimize_tf("pack_test.pb", false);
Khalique's avatar
Khalique committed
514
515
516
517

    EXPECT(p == prog);
}

518
519
520
TEST_CASE(pack_test_nhwc)
{
    migraphx::program p;
521
522
523

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 2, 1, 1}});
524
    auto lt0 = mm->add_instruction(migraphx::make_op("transpose", {{"dims", {0, 2, 3, 1}}}), l0);
525
    auto l1  = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {1, 2, 1, 1}});
526
    auto lt1 = mm->add_instruction(migraphx::make_op("transpose", {{"dims", {0, 2, 3, 1}}}), l1);
527
    auto l2  = mm->add_parameter("2", migraphx::shape{migraphx::shape::float_type, {1, 2, 1, 1}});
528
    auto lt2 = mm->add_instruction(migraphx::make_op("transpose", {{"dims", {0, 2, 3, 1}}}), l2);
Paul's avatar
Paul committed
529
    std::vector<migraphx::instruction_ref> args{lt0, lt1, lt2};
530
    std::vector<migraphx::instruction_ref> unsqueezed_args;
Paul's avatar
Paul committed
531
    int64_t nchw_axis = 3;
532
533
534
535
536

    std::transform(args.begin(),
                   args.end(),
                   std::back_inserter(unsqueezed_args),
                   [&](migraphx::instruction_ref arg) {
537
538
                       return mm->add_instruction(
                           migraphx::make_op("unsqueeze", {{"axes", {nchw_axis}}}), arg);
539
                   });
540
541
    mm->add_instruction(migraphx::make_op("concat", {{"axis", static_cast<int>(nchw_axis)}}),
                        unsqueezed_args);
Paul's avatar
Paul committed
542
    auto prog = optimize_tf("pack_test_nhwc.pb", true);
543
544
545
546

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
TEST_CASE(pad_test)
{
    migraphx::program p;

    auto* mm = p.get_main_module();

    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {2, 4}});
    std::vector<int> pad_literals{1, 1, 2, 2};
    std::vector<int> pads{1, 2, 1, 2};
    mm->add_literal(migraphx::shape{migraphx::shape::int32_type, {2, 2}}, pad_literals);

    mm->add_instruction(migraphx::make_op("pad", {{"pads", pads}}), l0);
    auto prog = optimize_tf("pad_test.pb", false);

    EXPECT(p == prog);
}

564
565
566
TEST_CASE(pooling_test)
{
    migraphx::program p;
567
568
569

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
570
571
    migraphx::op::pooling avg_pool_op{"average"};
    migraphx::op::pooling max_pool_op{"max"};
Shucai Xiao's avatar
Shucai Xiao committed
572
573
574
575
    avg_pool_op.stride  = {2, 2};
    max_pool_op.stride  = {2, 2};
    avg_pool_op.lengths = {2, 2};
    max_pool_op.lengths = {2, 2};
576
    mm->add_instruction(max_pool_op, l0);
Paul's avatar
Paul committed
577
    auto prog = optimize_tf("pooling_test.pb", true);
578
579
580
581

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
582
583
584
TEST_CASE(pow_test)
{
    migraphx::program p;
585
586
587
588

    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}});
589
    mm->add_instruction(migraphx::make_op("pow"), l0, l1);
Khalique's avatar
Khalique committed
590
591
592
593
594
    auto prog = optimize_tf("pow_test.pb", false);

    EXPECT(p == prog);
}

595
596
597
TEST_CASE(relu_test)
{
    migraphx::program p;
598
599
600

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
601
    mm->add_instruction(migraphx::make_op("relu"), l0);
Paul's avatar
Paul committed
602
    auto prog = optimize_tf("relu_test.pb", false);
603
604
605
606

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
607
608
609
TEST_CASE(relu6_test)
{
    migraphx::program p;
610
611

    auto* mm = p.get_main_module();
kahmed10's avatar
kahmed10 committed
612
    std::vector<size_t> input_lens{1, 3, 16, 16};
613
614
615
    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);
616
617
618
619
620
    min_val      = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"output_lens", input_lens}}), min_val);
    max_val = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"output_lens", input_lens}}), max_val);
    mm->add_instruction(migraphx::make_op("clip"), l0, min_val, max_val);
Paul's avatar
Paul committed
621
    auto prog = optimize_tf("relu6_test.pb", false);
Khalique's avatar
Khalique committed
622
623
624
625

    EXPECT(p == prog);
}

626
627
628
TEST_CASE(reshape_test)
{
    migraphx::program p;
629
630
631

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {16}});
632
633
    migraphx::shape s0{migraphx::shape::int32_type, {4}};
    // in tf, the second arg is a literal that contains new dimensions
634
    mm->add_literal(migraphx::literal{s0, {1, 1, 1, 16}});
635
    mm->add_instruction(migraphx::make_op("reshape", {{"dims", {1, 1, 1, 16}}}), l0);
Paul's avatar
Paul committed
636
    auto prog = optimize_tf("reshape_test.pb", false);
637
638
639
640

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
641
642
643
TEST_CASE(rsqrt_test)
{
    migraphx::program p;
644
645
646

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
647
    mm->add_instruction(migraphx::make_op("rsqrt"), l0);
Khalique's avatar
Khalique committed
648
649
650
651
652
    auto prog = optimize_tf("rsqrt_test.pb", false);

    EXPECT(p == prog);
}

653
654
655
TEST_CASE(shape_test)
{
    migraphx::program p;
656
657
658
659

    auto* mm = p.get_main_module();
    mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
    mm->add_literal(
660
661
662
663
664
665
        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
666
667
668
TEST_CASE(slice_test)
{
    migraphx::program p;
669
670

    auto* mm             = p.get_main_module();
Khalique's avatar
Khalique committed
671
    std::size_t num_axes = 2;
672
    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {5, 10}});
Khalique's avatar
Khalique committed
673
    migraphx::shape s0{migraphx::shape::int32_type, {num_axes}};
674
675
    mm->add_literal(migraphx::literal{s0, {1, 0}});
    mm->add_literal(migraphx::literal{s0, {2, -1}});
Khalique's avatar
Khalique committed
676
677
678
679
680
681

    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);
682
    mm->add_instruction(op, l0);
Khalique's avatar
Khalique committed
683
684
685
686
687
    auto prog = optimize_tf("slice_test.pb", false);

    EXPECT(p == prog);
}

688
689
690
TEST_CASE(softmax_test)
{
    migraphx::program p;
691
692
693

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3}});
694
    mm->add_instruction(migraphx::make_op("softmax", {{"axis", 1}}), l0);
Paul's avatar
Paul committed
695
    auto prog = optimize_tf("softmax_test.pb", false);
696
697
698
699

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
700
701
702
TEST_CASE(split_test)
{
    migraphx::program p;
703
704

    auto* mm = p.get_main_module();
kahmed10's avatar
kahmed10 committed
705
    std::vector<int64_t> axes{0, 1};
706
707
708
709
710
    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
711
712
713
714
715
716
717
718
    auto l1 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", axes}, {"starts", {0, 0}}, {"ends", {5, 10}}}), l0);
    auto l2 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", axes}, {"starts", {0, 10}}, {"ends", {5, 20}}}), l0);
    auto l3 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", axes}, {"starts", {0, 20}}, {"ends", {5, 30}}}), l0);
    mm->add_instruction(migraphx::make_op("concat", {{"axis", 1}}), l1, l2);
    mm->add_instruction(migraphx::make_op("concat", {{"axis", 1}}), l2, l3);
kahmed10's avatar
kahmed10 committed
719

720
    auto prog = parse_tf("split_test.pb", false);
kahmed10's avatar
kahmed10 committed
721
722
723
724
725
726
727

    EXPECT(p == prog);
}

TEST_CASE(split_test_one_output)
{
    migraphx::program p;
728
729
730
731
732

    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
733
    mm->add_instruction(migraphx::make_op("identity"), l0);
kahmed10's avatar
kahmed10 committed
734

735
    auto prog = parse_tf("split_test_one_output.pb", false);
kahmed10's avatar
kahmed10 committed
736
737
738
739
740
741
742

    EXPECT(p == prog);
}

TEST_CASE(split_test_vector_as_input)
{
    migraphx::program p;
743
744

    auto* mm = p.get_main_module();
kahmed10's avatar
kahmed10 committed
745
    std::vector<int64_t> axes{0, 1};
746
    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {5, 30}});
kahmed10's avatar
kahmed10 committed
747
    // split sizes
748
    mm->add_literal(
kahmed10's avatar
kahmed10 committed
749
        migraphx::literal{migraphx::shape{migraphx::shape::int32_type, {3}}, {4, 15, 11}});
750
751
752
    mm->add_literal(1); // split axis
    mm->add_literal(1); // concat axis
    mm->add_literal(1); // concat axis
753
754
755
756
757
758
759
760
    auto l1 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", axes}, {"starts", {0, 0}}, {"ends", {5, 4}}}), l0);
    auto l2 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", axes}, {"starts", {0, 4}}, {"ends", {5, 19}}}), l0);
    auto l3 = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", axes}, {"starts", {0, 19}}, {"ends", {5, 30}}}), l0);
    mm->add_instruction(migraphx::make_op("concat", {{"axis", 1}}), l1, l2);
    mm->add_instruction(migraphx::make_op("concat", {{"axis", 1}}), l2, l3);
kahmed10's avatar
kahmed10 committed
761

762
    auto prog = parse_tf("split_test_vector_as_input.pb", false);
kahmed10's avatar
kahmed10 committed
763
764
765
766

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
767
768
769
TEST_CASE(sqdiff_test)
{
    migraphx::program p;
770
771
772
773

    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}});
774
    mm->add_instruction(migraphx::make_op("sqdiff"), l0, l1);
Khalique's avatar
Khalique committed
775
776
777
778
779
    auto prog = optimize_tf("sqdiff_test.pb", false);

    EXPECT(p == prog);
}

780
781
782
TEST_CASE(squeeze_test)
{
    migraphx::program p;
783
784
785

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 2, 3, 1}});
786
    mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {0, 3}}}), l0);
Paul's avatar
Paul committed
787
    auto prog = optimize_tf("squeeze_test.pb", false);
788
789
790

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

Khalique's avatar
Khalique committed
792
793
794
TEST_CASE(stopgradient_test)
{
    migraphx::program p;
795
796
797

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

Khalique's avatar
Khalique committed
801
    EXPECT(p == prog);
Khalique's avatar
Khalique committed
802
803
}

Khalique's avatar
Khalique committed
804
805
806
TEST_CASE(stridedslice_test)
{
    migraphx::program p;
807
808
809

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 10, 1, 1}});
810
    auto l1  = mm->add_instruction(migraphx::make_op("transpose", {{"dims", {0, 2, 3, 1}}}), l0);
Khalique's avatar
Khalique committed
811
812
    std::size_t num_axes = 4;
    migraphx::op::slice op;
Khalique's avatar
Khalique committed
813
    op.starts = {0, 0, 0, 0};
Paul's avatar
Paul committed
814
    op.ends   = {1, 1, 1, 5};
Khalique's avatar
Khalique committed
815
816
    op.axes   = std::vector<int64_t>(num_axes);
    std::iota(op.axes.begin(), op.axes.end(), 0);
817
    auto l2          = mm->add_instruction(op, l1);
Paul's avatar
Paul committed
818
    auto shrink_axis = 1;
819
    mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {shrink_axis}}}), l2);
Paul's avatar
Paul committed
820
    auto prog = optimize_tf("stridedslice_test.pb", true);
Khalique's avatar
Khalique committed
821
822
823
824

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
825
826
827
TEST_CASE(stridedslice_masks_test)
{
    migraphx::program p;
828
829
830

    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
831
832
    std::size_t num_axes = 4;
    migraphx::op::slice op;
833
834
    op.starts = {0, 1, 1, 0};
    op.ends   = {1, 3, 3, 10};
Khalique's avatar
Khalique committed
835
836
837
    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)
838
839
840
841
842
843
844
    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});

845
    auto l1 = mm->add_instruction(migraphx::make_op("transpose", {{"dims", {0, 2, 3, 1}}}), l0);
846
    auto l2 = mm->add_instruction(op, l1);
847
    mm->add_instruction(migraphx::make_op("transpose", {{"dims", {0, 3, 1, 2}}}), l2);
848
    auto prog = parse_tf("stridedslice_masks_test.pb", true);
Khalique's avatar
Khalique committed
849
850
851
852

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
853
854
855
TEST_CASE(sub_test)
{
    migraphx::program p;
856
857
858
859

    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}});
860
    mm->add_instruction(migraphx::make_op("sub"), l0, l1);
861
    auto prog = parse_tf("sub_test.pb", false);
Khalique's avatar
Khalique committed
862
863
864
865

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
866
867
868
TEST_CASE(tanh_test)
{
    migraphx::program p;
869
870
871
872

    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}});
873
    mm->add_instruction(migraphx::make_op("sub"), l0, l1);
874
    auto prog = parse_tf("sub_test.pb", false);
Khalique's avatar
Khalique committed
875
876
877
878

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
879
880
881
TEST_CASE(transpose_test)
{
    migraphx::program p;
882
883
884

    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
885
    migraphx::shape s0{migraphx::shape::int32_type, {4}};
886
    mm->add_literal(migraphx::literal{s0, {0, 2, 3, 1}});
887
    mm->add_instruction(migraphx::make_op("transpose", {{"dims", {0, 2, 3, 1}}}), l0);
Khalique's avatar
Khalique committed
888
889
890
891
892
    auto prog = optimize_tf("transpose_test.pb", false);

    EXPECT(p == prog);
}

893
894
895
TEST_CASE(variable_batch_test)
{
    migraphx::program p;
896
897
898

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
899
    mm->add_instruction(migraphx::make_op("identity"), l0);
900
901
902
903
904
    auto prog = optimize_tf("variable_batch_test.pb", false);

    EXPECT(p == prog);
}

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