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

#include <migraphx/serialize.hpp>

44
45
#include "test.hpp"

Shucai Xiao's avatar
Shucai Xiao committed
46
47
48
migraphx::program
parse_tf(const std::string& name,
         bool is_nhwc,
kahmed10's avatar
kahmed10 committed
49
50
         const std::unordered_map<std::string, std::vector<std::size_t>>& dim_params = {},
         const std::vector<std::string>& output_node_names                           = {})
51
{
kahmed10's avatar
kahmed10 committed
52
53
    return migraphx::parse_tf(name,
                              migraphx::tf_options{is_nhwc, 1, dim_params, output_node_names});
54
55
}

Paul's avatar
Paul committed
56
57
migraphx::program optimize_tf(const std::string& name, bool is_nhwc)
{
58
    auto prog = migraphx::parse_tf(name, migraphx::tf_options{is_nhwc, 1});
59
    auto* mm  = prog.get_main_module();
Paul's avatar
Paul committed
60
    if(is_nhwc)
61
        migraphx::run_passes(*mm,
Paul's avatar
Paul committed
62
63
64
                             {migraphx::simplify_reshapes{},
                              migraphx::dead_code_elimination{},
                              migraphx::eliminate_identity{}});
kahmed10's avatar
kahmed10 committed
65
66
67
68

    // remove the last return instruction
    auto last_ins = std::prev(mm->end());
    if(last_ins != mm->end())
kahmed10's avatar
kahmed10 committed
69
    {
kahmed10's avatar
kahmed10 committed
70
71
72
73
        if(last_ins->name() == "@return")
        {
            mm->remove_instruction(last_ins);
        }
kahmed10's avatar
kahmed10 committed
74
    }
Paul's avatar
Paul committed
75
76
77
    return prog;
}

78
79
80
TEST_CASE(add_test)
{
    migraphx::program p;
81
82
83
84

    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}});
85
    mm->add_instruction(migraphx::make_op("add"), l0, l1);
Paul's avatar
Paul committed
86
    auto prog = optimize_tf("add_test.pb", false);
87
88
89
90

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
91
92
93
TEST_CASE(addv2_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
94
95
96
97
    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
98
99
100
101
102
    auto prog = optimize_tf("addv2_test.pb", false);

    EXPECT(p == prog);
}

103
104
TEST_CASE(add_bcast_test)
{
Khalique's avatar
Khalique committed
105

106
    migraphx::program p;
107
108

    auto* mm = p.get_main_module();
109
    migraphx::shape s0{migraphx::shape::float_type, {2, 3}};
110
111
    auto l0 = mm->add_parameter("0", s0);
    auto l1 = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {2, 1}});
112
    auto l2 =
113
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", s0.lens()}}), l1);
kahmed10's avatar
kahmed10 committed
114
    mm->add_instruction(migraphx::make_op("add"), l0, l2);
Paul's avatar
Paul committed
115
    auto prog = optimize_tf("add_bcast_test.pb", false);
116
117
118
119

    EXPECT(p == prog);
}

120
121
122
TEST_CASE(argmax_test)
{
    migraphx::program p;
123
124

    auto* mm = p.get_main_module();
Shucai Xiao's avatar
Shucai Xiao committed
125
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {4, 5, 6, 7}});
126
    mm->add_literal(migraphx::literal{migraphx::shape{migraphx::shape::int32_type}, {2}});
127
    auto ins = mm->add_instruction(migraphx::make_op("argmax", {{"axis", 2}}), l0);
kahmed10's avatar
kahmed10 committed
128
129
    auto l1  = mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {2}}}), ins);
    mm->add_return({l1});
Shucai Xiao's avatar
Shucai Xiao committed
130
    auto prog = parse_tf("argmax_test.pb", false, {{"0", {4, 5, 6, 7}}});
131
132
133
134
135
136
137

    EXPECT(p == prog);
}

TEST_CASE(argmin_test)
{
    migraphx::program p;
138
139
140
141

    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}});
142
    auto ins = mm->add_instruction(migraphx::make_op("argmin", {{"axis", 2}}), l0);
kahmed10's avatar
kahmed10 committed
143
144
    auto l1  = mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {2}}}), ins);
    mm->add_return({l1});
145
146
147
148
149
    auto prog = parse_tf("argmin_test.pb", false);

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
150
151
152
TEST_CASE(assert_less_equal_test)
{
    migraphx::program p;
153
154

    auto* mm = p.get_main_module();
Khalique's avatar
Khalique committed
155
    migraphx::shape s0{migraphx::shape::float_type, {2, 3}};
156
157
    auto l0 = mm->add_parameter("0", s0);
    auto l1 = mm->add_parameter("1", s0);
Khalique's avatar
Khalique committed
158
    migraphx::literal l{migraphx::shape{migraphx::shape::int32_type, {2}}, {0, 1}};
159
    auto l2 = mm->add_literal(l);
160
161
162
    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
163
164
165
166
167
    auto prog = optimize_tf("assert_less_equal_test.pb", false);

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
168
169
170
171
TEST_CASE(batchmatmul_test)
{
    migraphx::program p;

172
173
174
175
    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}});

176
    auto trans_l0 =
177
        mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 1, 3, 2}}}), l0);
178
    auto trans_l1 =
179
        mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 1, 3, 2}}}), l1);
Khalique's avatar
Khalique committed
180

181
    mm->add_instruction(migraphx::make_op("dot"), trans_l0, trans_l1);
Khalique's avatar
Khalique committed
182
183
184
185
186
    auto prog = optimize_tf("batchmatmul_test.pb", false);

    EXPECT(p == prog);
}

187
188
TEST_CASE(batchnorm_test)
{
Khalique's avatar
Khalique committed
189
190
    float epsilon  = 1.001e-5f;
    float momentum = 0.9f;
191
192

    migraphx::program p;
193
194

    auto* mm = p.get_main_module();
Khalique's avatar
Khalique committed
195
196
    migraphx::op::batch_norm_inference op{
        epsilon, momentum, migraphx::op::batch_norm_inference::spatial};
197
    migraphx::shape s0{migraphx::shape::float_type, {32}};
198
    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 32, 16, 16}});
199
200
    std::vector<float> const_vals(32);
    std::fill(const_vals.begin(), const_vals.end(), 1.0f);
Khalique's avatar
Khalique committed
201

202
203
204
205
206
    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
207
    auto prog = optimize_tf("batchnorm_test.pb", true);
208
209
210
211

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
212
213
214
215
216
217
TEST_CASE(batchnormv3_test)
{
    float epsilon  = 1.0e-5f;
    float momentum = 0.9f;

    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
218
    auto* mm = p.get_main_module();
kahmed10's avatar
kahmed10 committed
219
220
221
    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
222
    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 32, 16, 16}});
kahmed10's avatar
kahmed10 committed
223
224
225
    std::vector<float> const_vals(32);
    std::fill(const_vals.begin(), const_vals.end(), 1.0f);

Shucai Xiao's avatar
Shucai Xiao committed
226
227
228
229
230
    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
231
232
233
234
235
    auto prog = optimize_tf("batchnormv3_test.pb", true);

    EXPECT(p == prog);
}

236
237
238
TEST_CASE(biasadd_test)
{
    migraphx::program p;
239
240

    auto* mm = p.get_main_module();
241
    migraphx::shape s0{migraphx::shape::float_type, {1, 500, 1, 1}};
242
    uint64_t axis = 1;
243
244
    auto l0       = mm->add_parameter("0", s0);
    auto l1       = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {500}});
245
    auto l2       = mm->add_instruction(
246
        migraphx::make_op("broadcast", {{"axis", axis}, {"out_lens", l0->get_shape().lens()}}), l1);
247
    mm->add_instruction(migraphx::make_op("add"), l0, l2);
Paul's avatar
Paul committed
248
    auto prog = optimize_tf("biasadd_test.pb", true);
249
250
251
252

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
253
254
255
256
257
258
259
260
261
262
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}});
263
    auto l2 = mm->add_instruction(
264
        migraphx::make_op("broadcast", {{"axis", axis}, {"out_lens", l0->get_shape().lens()}}), l1);
265
    mm->add_instruction(migraphx::make_op("add"), l0, l2);
kahmed10's avatar
kahmed10 committed
266
267
268
269
270
    auto prog = optimize_tf("biasadd_scalar_test.pb", true);

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
271
272
273
TEST_CASE(cast_test)
{
    migraphx::program p;
274
275
276

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
277
278
279
280
    mm->add_instruction(
        migraphx::make_op("convert",
                          {{"target_type", migraphx::to_value(migraphx::shape::int32_type)}}),
        l0);
Khalique's avatar
Khalique committed
281
282
283
284
285
    auto prog = optimize_tf("cast_test.pb", false);

    EXPECT(p == prog);
}

286
287
288
TEST_CASE(concat_test)
{
    migraphx::program p;
Khalique's avatar
Khalique committed
289

290
291
292
293
    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}});
294
295
296

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

300
    mm->add_instruction(migraphx::make_op("concat", {{"axis", axis}}), l0, l1);
Paul's avatar
Paul committed
301
    auto prog = optimize_tf("concat_test.pb", false);
302
303
304
305
306
307
308

    EXPECT(p == prog);
}

TEST_CASE(const_test)
{
    migraphx::program p;
309
310
311

    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
312
    auto prog = optimize_tf("constant_test.pb", false);
313
314
315
316

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
317
migraphx::program create_conv()
318
319
{
    migraphx::program p;
Khalique's avatar
Khalique committed
320

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

    migraphx::op::convolution op;
charlie's avatar
charlie committed
330
331
332
    op.padding  = {1, 1, 1, 1};
    op.stride   = {1, 1};
    op.dilation = {1, 1};
333
334
    auto l2 =
        mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {3, 2, 0, 1}}}), l1);
335
    mm->add_instruction(op, l0, l2);
kahmed10's avatar
kahmed10 committed
336
337
338
339
340
341
342
343
344
345
346
    return p;
}

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

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
347
348
349
350
351
352
353
354
355
356
357
TEST_CASE(conv_add_test)
{
    migraphx::program p = create_conv();
    auto* mm            = p.get_main_module();
    auto l0             = std::prev(mm->end());
    mm->add_instruction(migraphx::make_op("add"), l0, l0);
    auto prog = optimize_tf("conv_add_test.pb", true);

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
358
359
360
361
TEST_CASE(conv_nchw_test)
{
    migraphx::program p = create_conv();
    auto prog           = optimize_tf("conv_nchw_test.pb", false);
362
363
364
365

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
TEST_CASE(conv_relu_test)
{
    migraphx::program p = create_conv();
    auto* mm            = p.get_main_module();
    auto l0             = std::prev(mm->end());
    mm->add_instruction(migraphx::make_op("relu"), l0);
    auto prog = optimize_tf("conv_relu_test.pb", true);

    EXPECT(p == prog);
}

TEST_CASE(conv_relu6_test)
{
    migraphx::program p = create_conv();
    auto* mm            = p.get_main_module();
    std::vector<size_t> input_lens{1, 32, 16, 16};
    auto l0      = std::prev(mm->end());
    auto min_val = mm->add_literal(0.0f);
    auto max_val = mm->add_literal(6.0f);
385
386
387
388
    min_val = mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", input_lens}}),
                                  min_val);
    max_val = mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", input_lens}}),
                                  max_val);
kahmed10's avatar
kahmed10 committed
389
390
391
392
393
394
    mm->add_instruction(migraphx::make_op("clip"), l0, min_val, max_val);
    auto prog = optimize_tf("conv_relu6_test.pb", true);

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
395
396
397
398
TEST_CASE(depthwiseconv_test)
{
    migraphx::program p;

399
400
401
    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
402
403
404
    std::vector<float> weight_data(3 * 3 * 3 * 1);
    std::fill(weight_data.begin(), weight_data.end(), 1.0f);
    auto l1 =
405
        mm->add_literal(migraphx::shape{migraphx::shape::float_type, {3, 3, 3, 1}}, weight_data);
Khalique's avatar
Khalique committed
406
407

    migraphx::op::convolution op;
charlie's avatar
charlie committed
408
409
410
411
    op.padding  = {1, 1};
    op.stride   = {1, 1};
    op.dilation = {1, 1};
    op.group    = 3;
412
413
    auto l3 =
        mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {3, 2, 0, 1}}}), l1);
414
415
    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);
416
    mm->add_instruction(op, l0, l5);
Paul's avatar
Paul committed
417
    auto prog = optimize_tf("depthwise_conv_test.pb", true);
Khalique's avatar
Khalique committed
418
419
420
421

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
422
423
424
TEST_CASE(expanddims_test)
{
    migraphx::program p;
Khalique's avatar
Khalique committed
425

426
427
428
429
    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);
430
    mm->add_instruction(migraphx::make_op("reshape", {{"dims", {1, 2, 3, 4}}}), l0);
431
    auto prog = optimize_tf("expanddims_test.pb", false);
Khalique's avatar
Khalique committed
432
433
434
435
436
437
438
439
440

    EXPECT(p == prog);
}

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

441
442
443
444
    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);
445
    mm->add_instruction(migraphx::make_op("reshape", {{"dims", {2, 3, 4, 1}}}), l0);
446
    auto prog = optimize_tf("expanddims_neg_test.pb", false);
Khalique's avatar
Khalique committed
447
448
449
450

    EXPECT(p == prog);
}

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

455
456
457
458
459
460
    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
461
462

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

Khalique's avatar
Khalique committed
466
467
468
    EXPECT(p == prog);
}

469
470
471
TEST_CASE(identity_test)
{
    migraphx::program p;
472
473
474

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
475
    mm->add_instruction(migraphx::make_op("identity"), l0);
Paul's avatar
Paul committed
476
    auto prog = optimize_tf("identity_test.pb", false);
477
478
479
480

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
481
482
483
TEST_CASE(matmul_test)
{
    migraphx::program p;
Khalique's avatar
Khalique committed
484

485
486
487
    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
488

489
490
491
492
    auto trans_l0 =
        mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {1, 0}}}), l0);
    auto trans_l1 =
        mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {1, 0}}}), l1);
493

494
    mm->add_instruction(migraphx::make_op("dot"), trans_l0, trans_l1);
Paul's avatar
Paul committed
495
    auto prog = optimize_tf("matmul_test.pb", false);
Khalique's avatar
Khalique committed
496
497
498
499

    EXPECT(p == prog);
}

500
501
502
TEST_CASE(mean_test)
{
    migraphx::program p;
503
504

    auto* mm = p.get_main_module();
Khalique's avatar
Khalique committed
505
    migraphx::literal l{migraphx::shape{migraphx::shape::int32_type, {2}}, {2, 3}};
506
507
508
    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
509
    migraphx::op::reduce_mean op{{2, 3}};
510
511
    mm->add_instruction(op, l0);
    auto l3 = mm->add_instruction(op, l0);
512
    mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {2, 3}}}), l3);
Paul's avatar
Paul committed
513
    auto prog = optimize_tf("mean_test.pb", false);
514
515
516
517
518
519
520

    EXPECT(p == prog);
}

TEST_CASE(mean_test_nhwc)
{
    migraphx::program p;
521
522

    auto* mm = p.get_main_module();
Khalique's avatar
Khalique committed
523
    migraphx::literal l{migraphx::shape{migraphx::shape::int32_type, {2}}, {1, 2}};
524
    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
525
526
    auto l1 =
        mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 2, 3, 1}}}), l0);
Khalique's avatar
Khalique committed
527
    migraphx::op::reduce_mean op{{1, 2}};
528
    auto l2 = mm->add_instruction(op, l1);
529
    mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {1, 2}}}), l2);
Paul's avatar
Paul committed
530
    auto prog = optimize_tf("mean_test_nhwc.pb", true);
531
532
533
534

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
535
536
537
538
TEST_CASE(mul_test)
{
    migraphx::program p;

539
540
541
542
    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}});

543
    mm->add_instruction(migraphx::make_op("mul"), l0, l1);
Paul's avatar
Paul committed
544
    auto prog = optimize_tf("mul_test.pb", false);
Khalique's avatar
Khalique committed
545
546
547
548

    EXPECT(p == prog);
}

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

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
    auto l1  = mm->add_instruction(migraphx::make_op("relu"), l0);
    auto l2  = mm->add_instruction(migraphx::make_op("tanh"), l0);
    mm->add_return({l1, l2});

    EXPECT(test::throws([&] { parse_tf("multi_output_test.pb", false, {}, {"relu", "relu6"}); }));
    auto prog = parse_tf("multi_output_test.pb", false, {}, {"relu", "tanh"});

    EXPECT(p == prog);
}

565
566
567
TEST_CASE(onehot_test)
{
    migraphx::program p;
568
569
570

    auto* mm = p.get_main_module();
    auto l0  = mm->add_literal(
Khalique's avatar
Khalique committed
571
        migraphx::literal{migraphx::shape{migraphx::shape::int32_type, {5}}, {1, 1, 1, 1, 1}});
572
573
574
575
    mm->add_literal(2);
    mm->add_literal(1.0f);
    mm->add_literal(0.0f);
    auto l1 = mm->add_literal(
Khalique's avatar
Khalique committed
576
        migraphx::literal{migraphx::shape{migraphx::shape::float_type, {2, 2}}, {1, 0, 0, 1}});
577
    int axis = 0;
578
    mm->add_instruction(migraphx::make_op("gather", {{"axis", axis}}), l1, l0);
579
580
581
582
583
    auto prog = optimize_tf("onehot_test.pb", false);

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
584
585
586
587
588
589
590
591
TEST_CASE(noop_test)
{
    migraphx::program p;
    auto prog = optimize_tf("noop_test.pb", false);

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
592
593
594
TEST_CASE(pack_test)
{
    migraphx::program p;
595
596
597
598
599

    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
600
601
602
603
    std::vector<migraphx::instruction_ref> args{l0, l1, l2};
    std::vector<migraphx::instruction_ref> unsqueezed_args;
    int64_t axis = 1;

604
605
606
607
608
609
610
611
612
    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
613
    auto prog = optimize_tf("pack_test.pb", false);
Khalique's avatar
Khalique committed
614
615
616
617

    EXPECT(p == prog);
}

618
619
620
TEST_CASE(pack_test_nhwc)
{
    migraphx::program p;
621
622
623

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 2, 1, 1}});
624
625
626
627
628
629
630
631
    auto lt0 =
        mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {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::make_op("transpose", {{"permutation", {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::make_op("transpose", {{"permutation", {0, 2, 3, 1}}}), l2);
Paul's avatar
Paul committed
632
    std::vector<migraphx::instruction_ref> args{lt0, lt1, lt2};
633
    std::vector<migraphx::instruction_ref> unsqueezed_args;
Paul's avatar
Paul committed
634
    int64_t nchw_axis = 3;
635
636
637
638
639

    std::transform(args.begin(),
                   args.end(),
                   std::back_inserter(unsqueezed_args),
                   [&](migraphx::instruction_ref arg) {
640
641
                       return mm->add_instruction(
                           migraphx::make_op("unsqueeze", {{"axes", {nchw_axis}}}), arg);
642
                   });
643
644
    mm->add_instruction(migraphx::make_op("concat", {{"axis", static_cast<int>(nchw_axis)}}),
                        unsqueezed_args);
Paul's avatar
Paul committed
645
    auto prog = optimize_tf("pack_test_nhwc.pb", true);
646
647
648
649

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
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);
}

667
668
669
TEST_CASE(pooling_test)
{
    migraphx::program p;
670
671
672

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
673
674
    migraphx::op::pooling avg_pool_op{migraphx::op::pooling_mode::average};
    migraphx::op::pooling max_pool_op{migraphx::op::pooling_mode::max};
Shucai Xiao's avatar
Shucai Xiao committed
675
676
677
678
    avg_pool_op.stride  = {2, 2};
    max_pool_op.stride  = {2, 2};
    avg_pool_op.lengths = {2, 2};
    max_pool_op.lengths = {2, 2};
kahmed10's avatar
kahmed10 committed
679
    mm->add_instruction(avg_pool_op, l0);
680
    mm->add_instruction(max_pool_op, l0);
Paul's avatar
Paul committed
681
    auto prog = optimize_tf("pooling_test.pb", true);
682
683
684
685

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
686
687
688
TEST_CASE(pow_test)
{
    migraphx::program p;
689
690
691
692

    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}});
693
    mm->add_instruction(migraphx::make_op("pow"), l0, l1);
Khalique's avatar
Khalique committed
694
695
696
697
698
    auto prog = optimize_tf("pow_test.pb", false);

    EXPECT(p == prog);
}

699
700
701
TEST_CASE(relu_test)
{
    migraphx::program p;
702
703
704

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
705
    mm->add_instruction(migraphx::make_op("relu"), l0);
Paul's avatar
Paul committed
706
    auto prog = optimize_tf("relu_test.pb", false);
707
708
709
710

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
711
712
713
TEST_CASE(relu6_test)
{
    migraphx::program p;
714
715

    auto* mm = p.get_main_module();
kahmed10's avatar
kahmed10 committed
716
    std::vector<size_t> input_lens{1, 3, 16, 16};
717
718
719
    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);
720
721
722
723
    min_val = mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", input_lens}}),
                                  min_val);
    max_val = mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", input_lens}}),
                                  max_val);
724
    mm->add_instruction(migraphx::make_op("clip"), l0, min_val, max_val);
Paul's avatar
Paul committed
725
    auto prog = optimize_tf("relu6_test.pb", false);
Khalique's avatar
Khalique committed
726
727
728
729

    EXPECT(p == prog);
}

730
TEST_CASE(relu6_half_test)
731
732
733
734
{
    migraphx::program p;

    auto* mm = p.get_main_module();
735
736
737
738
739
740
    std::vector<size_t> input_lens{1, 3, 16, 16};
    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::half_type, input_lens});
    auto min_val =
        mm->add_literal(migraphx::literal{migraphx::shape{migraphx::shape::half_type}, {0.0f}});
    auto max_val =
        mm->add_literal(migraphx::literal{migraphx::shape{migraphx::shape::half_type}, {6.0f}});
741
742
743
744
    min_val = mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", input_lens}}),
                                  min_val);
    max_val = mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", input_lens}}),
                                  max_val);
745
746
    mm->add_instruction(migraphx::make_op("clip"), l0, min_val, max_val);
    auto prog = optimize_tf("relu6_half_test.pb", false);
747
748
749
750

    EXPECT(p == prog);
}

751
752
753
TEST_CASE(reshape_test)
{
    migraphx::program p;
754
755
756

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {16}});
757
758
    migraphx::shape s0{migraphx::shape::int32_type, {4}};
    // in tf, the second arg is a literal that contains new dimensions
759
    mm->add_literal(migraphx::literal{s0, {1, 1, 1, 16}});
760
    mm->add_instruction(migraphx::make_op("reshape", {{"dims", {1, 1, 1, 16}}}), l0);
Paul's avatar
Paul committed
761
    auto prog = optimize_tf("reshape_test.pb", false);
762
763
764
765

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
766
767
768
TEST_CASE(rsqrt_test)
{
    migraphx::program p;
769
770
771

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
772
    mm->add_instruction(migraphx::make_op("rsqrt"), l0);
Khalique's avatar
Khalique committed
773
774
775
776
777
    auto prog = optimize_tf("rsqrt_test.pb", false);

    EXPECT(p == prog);
}

778
779
780
TEST_CASE(shape_test)
{
    migraphx::program p;
781
782
783
784

    auto* mm = p.get_main_module();
    mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
    mm->add_literal(
785
786
787
788
789
790
        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
791
792
793
TEST_CASE(slice_test)
{
    migraphx::program p;
794
795

    auto* mm             = p.get_main_module();
Khalique's avatar
Khalique committed
796
    std::size_t num_axes = 2;
797
    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {5, 10}});
Khalique's avatar
Khalique committed
798
    migraphx::shape s0{migraphx::shape::int32_type, {num_axes}};
799
800
    mm->add_literal(migraphx::literal{s0, {1, 0}});
    mm->add_literal(migraphx::literal{s0, {2, -1}});
Khalique's avatar
Khalique committed
801
802
803
804
805
806

    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);
807
    mm->add_instruction(op, l0);
Khalique's avatar
Khalique committed
808
809
810
811
812
    auto prog = optimize_tf("slice_test.pb", false);

    EXPECT(p == prog);
}

813
814
815
TEST_CASE(softmax_test)
{
    migraphx::program p;
816
817
818

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3}});
819
    mm->add_instruction(migraphx::make_op("softmax", {{"axis", 1}}), l0);
Paul's avatar
Paul committed
820
    auto prog = optimize_tf("softmax_test.pb", false);
821
822
823
824

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
825
826
827
TEST_CASE(split_test)
{
    migraphx::program p;
828
829

    auto* mm = p.get_main_module();
kahmed10's avatar
kahmed10 committed
830
    std::vector<int64_t> axes{0, 1};
831
832
833
834
835
    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
836
837
838
839
840
841
    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);
kahmed10's avatar
kahmed10 committed
842
843
844
    auto l4 = mm->add_instruction(migraphx::make_op("concat", {{"axis", 1}}), l1, l2);
    auto l5 = mm->add_instruction(migraphx::make_op("concat", {{"axis", 1}}), l2, l3);
    mm->add_return({l4, l5});
845
    auto prog = parse_tf("split_test.pb", false);
kahmed10's avatar
kahmed10 committed
846
847
848
849
850
851
852

    EXPECT(p == prog);
}

TEST_CASE(split_test_one_output)
{
    migraphx::program p;
853
854
855
856
857

    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
kahmed10's avatar
kahmed10 committed
858
859
    auto l1 = mm->add_instruction(migraphx::make_op("identity"), l0);
    mm->add_return({l1});
860
    auto prog = parse_tf("split_test_one_output.pb", false);
kahmed10's avatar
kahmed10 committed
861
862
863
864
865
866
867

    EXPECT(p == prog);
}

TEST_CASE(split_test_vector_as_input)
{
    migraphx::program p;
868
869

    auto* mm = p.get_main_module();
kahmed10's avatar
kahmed10 committed
870
    std::vector<int64_t> axes{0, 1};
871
    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {5, 30}});
kahmed10's avatar
kahmed10 committed
872
    // split sizes
873
    mm->add_literal(
kahmed10's avatar
kahmed10 committed
874
        migraphx::literal{migraphx::shape{migraphx::shape::int32_type, {3}}, {4, 15, 11}});
875
876
877
    mm->add_literal(1); // split axis
    mm->add_literal(1); // concat axis
    mm->add_literal(1); // concat axis
878
879
880
881
882
883
    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);
kahmed10's avatar
kahmed10 committed
884
885
886
    auto l4 = mm->add_instruction(migraphx::make_op("concat", {{"axis", 1}}), l1, l2);
    auto l5 = mm->add_instruction(migraphx::make_op("concat", {{"axis", 1}}), l2, l3);
    mm->add_return({l4, l5});
887
    auto prog = parse_tf("split_test_vector_as_input.pb", false);
kahmed10's avatar
kahmed10 committed
888
889
890
891

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
892
893
894
TEST_CASE(sqdiff_test)
{
    migraphx::program p;
895
896
897
898

    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}});
899
    mm->add_instruction(migraphx::make_op("sqdiff"), l0, l1);
Khalique's avatar
Khalique committed
900
901
902
903
904
    auto prog = optimize_tf("sqdiff_test.pb", false);

    EXPECT(p == prog);
}

905
906
907
TEST_CASE(squeeze_test)
{
    migraphx::program p;
908
909
910

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

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

Khalique's avatar
Khalique committed
917
918
919
TEST_CASE(stopgradient_test)
{
    migraphx::program p;
920
921
922

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

Khalique's avatar
Khalique committed
926
    EXPECT(p == prog);
Khalique's avatar
Khalique committed
927
928
}

Khalique's avatar
Khalique committed
929
930
931
TEST_CASE(stridedslice_test)
{
    migraphx::program p;
932
933
934

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 10, 1, 1}});
935
936
    auto l1 =
        mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 2, 3, 1}}}), l0);
Khalique's avatar
Khalique committed
937
938
    std::size_t num_axes = 4;
    migraphx::op::slice op;
Khalique's avatar
Khalique committed
939
    op.starts = {0, 0, 0, 0};
Paul's avatar
Paul committed
940
    op.ends   = {1, 1, 1, 5};
Khalique's avatar
Khalique committed
941
942
    op.axes   = std::vector<int64_t>(num_axes);
    std::iota(op.axes.begin(), op.axes.end(), 0);
943
    auto l2          = mm->add_instruction(op, l1);
Paul's avatar
Paul committed
944
    auto shrink_axis = 1;
945
    mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {shrink_axis}}}), l2);
Paul's avatar
Paul committed
946
    auto prog = optimize_tf("stridedslice_test.pb", true);
Khalique's avatar
Khalique committed
947
948
949
950

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
951
952
953
TEST_CASE(stridedslice_masks_test)
{
    migraphx::program p;
954
955
956

    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
957
958
    std::size_t num_axes = 4;
    migraphx::op::slice op;
959
960
    op.starts = {0, 1, 1, 0};
    op.ends   = {1, 3, 3, 10};
Khalique's avatar
Khalique committed
961
962
963
    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)
964
965
966
967
968
969
970
    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});

971
972
    auto l1 =
        mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 2, 3, 1}}}), l0);
973
    auto l2 = mm->add_instruction(op, l1);
974
975
    auto l3 =
        mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 3, 1, 2}}}), l2);
kahmed10's avatar
kahmed10 committed
976
    mm->add_return({l3});
977
    auto prog = parse_tf("stridedslice_masks_test.pb", true);
Khalique's avatar
Khalique committed
978
979
980
981

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
982
983
984
TEST_CASE(sub_test)
{
    migraphx::program p;
985
986
987
988

    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}});
kahmed10's avatar
kahmed10 committed
989
990
    auto l2  = mm->add_instruction(migraphx::make_op("sub"), l0, l1);
    mm->add_return({l2});
991
    auto prog = parse_tf("sub_test.pb", false);
Khalique's avatar
Khalique committed
992
993
994
995

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
996
997
998
TEST_CASE(tanh_test)
{
    migraphx::program p;
999
1000

    auto* mm = p.get_main_module();
kahmed10's avatar
kahmed10 committed
1001
1002
1003
1004
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
    auto l1  = mm->add_instruction(migraphx::make_op("tanh"), l0);
    mm->add_return({l1});
    auto prog = parse_tf("tanh_test.pb", false);
Khalique's avatar
Khalique committed
1005
1006
1007
1008

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
1009
1010
1011
TEST_CASE(transpose_test)
{
    migraphx::program p;
1012
1013
1014

    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
1015
    migraphx::shape s0{migraphx::shape::int32_type, {4}};
1016
    mm->add_literal(migraphx::literal{s0, {0, 2, 3, 1}});
1017
    mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 2, 3, 1}}}), l0);
Khalique's avatar
Khalique committed
1018
1019
1020
1021
1022
    auto prog = optimize_tf("transpose_test.pb", false);

    EXPECT(p == prog);
}

1023
1024
1025
TEST_CASE(variable_batch_test)
{
    migraphx::program p;
1026
1027
1028

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
1029
    mm->add_instruction(migraphx::make_op("identity"), l0);
1030
1031
1032
1033
1034
    auto prog = optimize_tf("variable_batch_test.pb", false);

    EXPECT(p == prog);
}

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