tf_test.cpp 29.4 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
75
76
77
    auto l2 =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"output_lens", s0.lens()}}), l0);
    auto l3 =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"output_lens", s0.lens()}}), l1);
    mm->add_instruction(migraphx::make_op("add"), l2, l3);
Paul's avatar
Paul committed
78
    auto prog = optimize_tf("add_bcast_test.pb", false);
79
80
81
82

    EXPECT(p == prog);
}

83
84
85
TEST_CASE(argmax_test)
{
    migraphx::program p;
86
87

    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
88
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {4, 5, 6, 7}});
89
    mm->add_literal(migraphx::literal{migraphx::shape{migraphx::shape::int32_type}, {2}});
90
91
    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
92
    auto prog = parse_tf("argmax_test.pb", false, {{"0", {4, 5, 6, 7}}});
93
94
95
96
97
98
99

    EXPECT(p == prog);
}

TEST_CASE(argmin_test)
{
    migraphx::program p;
100
101
102
103

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

    EXPECT(p == prog);
}

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

    auto* mm = p.get_main_module();
Khalique's avatar
Khalique committed
116
    migraphx::shape s0{migraphx::shape::float_type, {2, 3}};
117
118
    auto l0 = mm->add_parameter("0", s0);
    auto l1 = mm->add_parameter("1", s0);
Khalique's avatar
Khalique committed
119
    migraphx::literal l{migraphx::shape{migraphx::shape::int32_type, {2}}, {0, 1}};
120
    auto l2 = mm->add_literal(l);
121
122
123
    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
124
125
126
127
128
    auto prog = optimize_tf("assert_less_equal_test.pb", false);

    EXPECT(p == prog);
}

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

133
134
135
136
    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}});

137
138
139
140
    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
141

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

    EXPECT(p == prog);
}

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

    migraphx::program p;
154
155

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

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

    EXPECT(p == prog);
}

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

    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
179
    auto* mm = p.get_main_module();
kahmed10's avatar
kahmed10 committed
180
181
182
    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
183
    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 32, 16, 16}});
kahmed10's avatar
kahmed10 committed
184
185
186
    std::vector<float> const_vals(32);
    std::fill(const_vals.begin(), const_vals.end(), 1.0f);

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

    EXPECT(p == prog);
}

197
198
199
TEST_CASE(biasadd_test)
{
    migraphx::program p;
200
201

    auto* mm = p.get_main_module();
202
    migraphx::shape s0{migraphx::shape::float_type, {1, 500, 1, 1}};
203
    uint64_t axis = 1;
204
205
    auto l0       = mm->add_parameter("0", s0);
    auto l1       = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {500}});
206
207
208
    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
209
    auto prog = optimize_tf("biasadd_test.pb", true);
210
211
212
213

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
214
215
216
217
218
219
220
221
222
223
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}});
224
225
226
    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
227
228
229
230
231
    auto prog = optimize_tf("biasadd_scalar_test.pb", true);

    EXPECT(p == prog);
}

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

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

    EXPECT(p == prog);
}

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

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

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

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

    EXPECT(p == prog);
}

TEST_CASE(const_test)
{
    migraphx::program p;
270
271
272

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

    EXPECT(p == prog);
}

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

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

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

    EXPECT(p == prog);
}

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

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

    migraphx::op::convolution op;
    op.padding_mode = migraphx::op::padding_mode_t::same;
Khalique's avatar
Khalique committed
330
    op.padding      = {1, 1};
Khalique's avatar
Khalique committed
331
332
333
    op.stride       = {1, 1};
    op.dilation     = {1, 1};
    op.group        = 3;
334
335
336
    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);
337
    mm->add_instruction(op, l0, l5);
Paul's avatar
Paul committed
338
    auto prog = optimize_tf("depthwise_conv_test.pb", true);
Khalique's avatar
Khalique committed
339
340
341
342

    EXPECT(p == prog);
}

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

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

    EXPECT(p == prog);
}

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

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

    EXPECT(p == prog);
}

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

376
377
378
379
380
381
    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
382
383

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

Khalique's avatar
Khalique committed
387
388
389
    EXPECT(p == prog);
}

390
391
392
TEST_CASE(identity_test)
{
    migraphx::program p;
393
394
395

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

    EXPECT(p == prog);
}

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

406
407
408
    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
409

410
411
    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);
412

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

    EXPECT(p == prog);
}

419
420
421
TEST_CASE(mean_test)
{
    migraphx::program p;
422
423

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

    EXPECT(p == prog);
}

TEST_CASE(mean_test_nhwc)
{
    migraphx::program p;
440
441

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

    EXPECT(p == prog);
}

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

457
458
459
460
    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}});

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

    EXPECT(p == prog);
}

467
468
469
TEST_CASE(onehot_test)
{
    migraphx::program p;
470
471
472

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

    EXPECT(p == prog);
}

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

    EXPECT(p == prog);
}

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

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

506
507
508
509
510
511
512
513
514
    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
515
    auto prog = optimize_tf("pack_test.pb", false);
Khalique's avatar
Khalique committed
516
517
518
519

    EXPECT(p == prog);
}

520
521
522
TEST_CASE(pack_test_nhwc)
{
    migraphx::program p;
523
524
525

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

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

    EXPECT(p == prog);
}

549
550
551
TEST_CASE(pooling_test)
{
    migraphx::program p;
552
553
554

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
555
556
    migraphx::op::pooling avg_pool_op{"average"};
    migraphx::op::pooling max_pool_op{"max"};
Shucai Xiao's avatar
Shucai Xiao committed
557
558
559
560
    avg_pool_op.stride  = {2, 2};
    max_pool_op.stride  = {2, 2};
    avg_pool_op.lengths = {2, 2};
    max_pool_op.lengths = {2, 2};
561
    mm->add_instruction(max_pool_op, l0);
Paul's avatar
Paul committed
562
    auto prog = optimize_tf("pooling_test.pb", true);
563
564
565
566

    EXPECT(p == prog);
}

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

    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}});
574
    mm->add_instruction(migraphx::make_op("pow"), l0, l1);
Khalique's avatar
Khalique committed
575
576
577
578
579
    auto prog = optimize_tf("pow_test.pb", false);

    EXPECT(p == prog);
}

580
581
582
TEST_CASE(relu_test)
{
    migraphx::program p;
583
584
585

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
586
    mm->add_instruction(migraphx::make_op("relu"), l0);
Paul's avatar
Paul committed
587
    auto prog = optimize_tf("relu_test.pb", false);
588
589
590
591

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
592
593
594
TEST_CASE(relu6_test)
{
    migraphx::program p;
595
596

    auto* mm = p.get_main_module();
kahmed10's avatar
kahmed10 committed
597
    std::vector<size_t> input_lens{1, 3, 16, 16};
598
599
600
    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);
601
602
603
604
605
    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
606
    auto prog = optimize_tf("relu6_test.pb", false);
Khalique's avatar
Khalique committed
607
608
609
610

    EXPECT(p == prog);
}

611
612
613
TEST_CASE(reshape_test)
{
    migraphx::program p;
614
615
616

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {16}});
617
618
    migraphx::shape s0{migraphx::shape::int32_type, {4}};
    // in tf, the second arg is a literal that contains new dimensions
619
    mm->add_literal(migraphx::literal{s0, {1, 1, 1, 16}});
620
    mm->add_instruction(migraphx::make_op("reshape", {{"dims", {1, 1, 1, 16}}}), l0);
Paul's avatar
Paul committed
621
    auto prog = optimize_tf("reshape_test.pb", false);
622
623
624
625

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
626
627
628
TEST_CASE(rsqrt_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, {1, 3, 16, 16}});
632
    mm->add_instruction(migraphx::make_op("rsqrt"), l0);
Khalique's avatar
Khalique committed
633
634
635
636
637
    auto prog = optimize_tf("rsqrt_test.pb", false);

    EXPECT(p == prog);
}

638
639
640
TEST_CASE(shape_test)
{
    migraphx::program p;
641
642
643
644

    auto* mm = p.get_main_module();
    mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
    mm->add_literal(
645
646
647
648
649
650
        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
651
652
653
TEST_CASE(slice_test)
{
    migraphx::program p;
654
655

    auto* mm             = p.get_main_module();
Khalique's avatar
Khalique committed
656
    std::size_t num_axes = 2;
657
    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {5, 10}});
Khalique's avatar
Khalique committed
658
    migraphx::shape s0{migraphx::shape::int32_type, {num_axes}};
659
660
    mm->add_literal(migraphx::literal{s0, {1, 0}});
    mm->add_literal(migraphx::literal{s0, {2, -1}});
Khalique's avatar
Khalique committed
661
662
663
664
665
666

    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);
667
    mm->add_instruction(op, l0);
Khalique's avatar
Khalique committed
668
669
670
671
672
    auto prog = optimize_tf("slice_test.pb", false);

    EXPECT(p == prog);
}

673
674
675
TEST_CASE(softmax_test)
{
    migraphx::program p;
676
677
678

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3}});
679
    mm->add_instruction(migraphx::make_op("softmax", {{"axis", 1}}), l0);
Paul's avatar
Paul committed
680
    auto prog = optimize_tf("softmax_test.pb", false);
681
682
683
684

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
685
686
687
TEST_CASE(split_test)
{
    migraphx::program p;
688
689

    auto* mm = p.get_main_module();
kahmed10's avatar
kahmed10 committed
690
    std::vector<int64_t> axes{0, 1};
691
692
693
694
695
    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
696
697
698
699
700
701
702
703
    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
704

705
    auto prog = parse_tf("split_test.pb", false);
kahmed10's avatar
kahmed10 committed
706
707
708
709
710
711
712

    EXPECT(p == prog);
}

TEST_CASE(split_test_one_output)
{
    migraphx::program p;
713
714
715
716
717

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

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

    EXPECT(p == prog);
}

TEST_CASE(split_test_vector_as_input)
{
    migraphx::program p;
728
729

    auto* mm = p.get_main_module();
kahmed10's avatar
kahmed10 committed
730
    std::vector<int64_t> axes{0, 1};
731
    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {5, 30}});
kahmed10's avatar
kahmed10 committed
732
    // split sizes
733
    mm->add_literal(
kahmed10's avatar
kahmed10 committed
734
        migraphx::literal{migraphx::shape{migraphx::shape::int32_type, {3}}, {4, 15, 11}});
735
736
737
    mm->add_literal(1); // split axis
    mm->add_literal(1); // concat axis
    mm->add_literal(1); // concat axis
738
739
740
741
742
743
744
745
    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
746

747
    auto prog = parse_tf("split_test_vector_as_input.pb", false);
kahmed10's avatar
kahmed10 committed
748
749
750
751

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
752
753
754
TEST_CASE(sqdiff_test)
{
    migraphx::program p;
755
756
757
758

    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}});
759
    mm->add_instruction(migraphx::make_op("sqdiff"), l0, l1);
Khalique's avatar
Khalique committed
760
761
762
763
764
    auto prog = optimize_tf("sqdiff_test.pb", false);

    EXPECT(p == prog);
}

765
766
767
TEST_CASE(squeeze_test)
{
    migraphx::program p;
768
769
770

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

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

Khalique's avatar
Khalique committed
777
778
779
TEST_CASE(stopgradient_test)
{
    migraphx::program p;
780
781
782

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

Khalique's avatar
Khalique committed
786
    EXPECT(p == prog);
Khalique's avatar
Khalique committed
787
788
}

Khalique's avatar
Khalique committed
789
790
791
TEST_CASE(stridedslice_test)
{
    migraphx::program p;
792
793
794

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 10, 1, 1}});
795
    auto l1  = mm->add_instruction(migraphx::make_op("transpose", {{"dims", {0, 2, 3, 1}}}), l0);
Khalique's avatar
Khalique committed
796
797
    std::size_t num_axes = 4;
    migraphx::op::slice op;
Khalique's avatar
Khalique committed
798
    op.starts = {0, 0, 0, 0};
Paul's avatar
Paul committed
799
    op.ends   = {1, 1, 1, 5};
Khalique's avatar
Khalique committed
800
801
    op.axes   = std::vector<int64_t>(num_axes);
    std::iota(op.axes.begin(), op.axes.end(), 0);
802
    auto l2          = mm->add_instruction(op, l1);
Paul's avatar
Paul committed
803
    auto shrink_axis = 1;
804
    mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {shrink_axis}}}), l2);
Paul's avatar
Paul committed
805
    auto prog = optimize_tf("stridedslice_test.pb", true);
Khalique's avatar
Khalique committed
806
807
808
809

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
810
811
812
TEST_CASE(stridedslice_masks_test)
{
    migraphx::program p;
813
814
815

    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
816
817
    std::size_t num_axes = 4;
    migraphx::op::slice op;
818
819
    op.starts = {0, 1, 1, 0};
    op.ends   = {1, 3, 3, 10};
Khalique's avatar
Khalique committed
820
821
822
    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)
823
824
825
826
827
828
829
    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});

830
    auto l1 = mm->add_instruction(migraphx::make_op("transpose", {{"dims", {0, 2, 3, 1}}}), l0);
831
    auto l2 = mm->add_instruction(op, l1);
832
    mm->add_instruction(migraphx::make_op("transpose", {{"dims", {0, 3, 1, 2}}}), l2);
833
    auto prog = parse_tf("stridedslice_masks_test.pb", true);
Khalique's avatar
Khalique committed
834
835
836
837

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
838
839
840
TEST_CASE(sub_test)
{
    migraphx::program p;
841
842
843
844

    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}});
845
    mm->add_instruction(migraphx::make_op("sub"), l0, l1);
846
    auto prog = parse_tf("sub_test.pb", false);
Khalique's avatar
Khalique committed
847
848
849
850

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
851
852
853
TEST_CASE(tanh_test)
{
    migraphx::program p;
854
855
856
857

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

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
864
865
866
TEST_CASE(transpose_test)
{
    migraphx::program p;
867
868
869

    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
870
    migraphx::shape s0{migraphx::shape::int32_type, {4}};
871
    mm->add_literal(migraphx::literal{s0, {0, 2, 3, 1}});
872
    mm->add_instruction(migraphx::make_op("transpose", {{"dims", {0, 2, 3, 1}}}), l0);
Khalique's avatar
Khalique committed
873
874
875
876
877
    auto prog = optimize_tf("transpose_test.pb", false);

    EXPECT(p == prog);
}

878
879
880
TEST_CASE(variable_batch_test)
{
    migraphx::program p;
881
882
883

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
884
    mm->add_instruction(migraphx::make_op("identity"), l0);
885
886
887
888
889
    auto prog = optimize_tf("variable_batch_test.pb", false);

    EXPECT(p == prog);
}

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