tf_test.cpp 37.7 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/common.hpp>
28
#include <migraphx/literal.hpp>
Paul's avatar
Paul committed
29
30
31
32
#include <migraphx/pass_manager.hpp>
#include <migraphx/simplify_reshapes.hpp>
#include <migraphx/dead_code_elimination.hpp>
#include <migraphx/eliminate_identity.hpp>
33
34
35
#include <migraphx/program.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/tf.hpp>
36
#include <migraphx/make_op.hpp>
turneram's avatar
turneram committed
37
38
39
40
#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
189
TEST_CASE(batchnorm_test)
{
    migraphx::program p;
190
    auto* mm = p.get_main_module();
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205

    auto x    = mm->add_parameter("x", {migraphx::shape::float_type, {1, 32, 16, 16}});
    auto bias = mm->add_parameter("bias", {migraphx::shape::float_type, {32}});
    auto mean = mm->add_parameter("mean", {migraphx::shape::float_type, {32}});
    auto var  = mm->add_parameter("variance", {migraphx::shape::float_type, {32}});

    std::vector<float> scale_data(32, 1.0);
    auto scale = mm->add_literal(migraphx::shape{migraphx::shape::float_type, {32}}, scale_data);
    auto eps   = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {1e-4f}});

    auto usq_scale = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1, 2}}}), scale);
    auto usq_bias  = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1, 2}}}), bias);
    auto usq_mean  = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1, 2}}}), mean);
    auto usq_var   = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1, 2}}}), var);

206
207
208
209
210
    auto x_sub_mean = add_common_op(*mm, migraphx::make_op("sub"), {x, usq_mean});
    auto var_eps    = add_common_op(*mm, migraphx::make_op("add"), {usq_var, eps});
    auto rsqrt      = mm->add_instruction(migraphx::make_op("rsqrt"), var_eps);
    auto mul0       = add_common_op(*mm, migraphx::make_op("mul"), {usq_scale, rsqrt});
    auto r0         = add_common_op(*mm, migraphx::make_op("mul"), {x_sub_mean, mul0});
211
212
    add_common_op(*mm, migraphx::make_op("add"), {r0, usq_bias});

Paul's avatar
Paul committed
213
    auto prog = optimize_tf("batchnorm_test.pb", true);
214
215
216
217
218
219
220
    EXPECT(p == prog);
}

TEST_CASE(batchnorm_half_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
221

222
223
224
225
226
227
228
229
230
231
232
233
234
235
    auto x    = mm->add_parameter("x", {migraphx::shape::half_type, {1, 32, 16, 16}});
    auto bias = mm->add_parameter("bias", {migraphx::shape::float_type, {32}});
    auto mean = mm->add_parameter("mean", {migraphx::shape::float_type, {32}});
    auto var  = mm->add_parameter("variance", {migraphx::shape::float_type, {32}});

    std::vector<float> scale_data(32, 1.0);
    auto scale = mm->add_literal(migraphx::shape{migraphx::shape::float_type, {32}}, scale_data);
    auto eps   = mm->add_literal(migraphx::literal{migraphx::shape::half_type, {1e-4f}});

    auto usq_scale = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1, 2}}}), scale);
    auto usq_bias  = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1, 2}}}), bias);
    auto usq_mean  = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1, 2}}}), mean);
    auto usq_var   = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1, 2}}}), var);

236
237
238
239
240
    auto x_sub_mean = add_common_op(*mm, migraphx::make_op("sub"), {x, usq_mean});
    auto var_eps    = add_common_op(*mm, migraphx::make_op("add"), {usq_var, eps});
    auto rsqrt      = mm->add_instruction(migraphx::make_op("rsqrt"), var_eps);
    auto mul0       = add_common_op(*mm, migraphx::make_op("mul"), {usq_scale, rsqrt});
    auto r0         = add_common_op(*mm, migraphx::make_op("mul"), {x_sub_mean, mul0});
241
242
243
    add_common_op(*mm, migraphx::make_op("add"), {r0, usq_bias});

    auto prog = optimize_tf("batchnorm_half_test.pb", true);
244
245
246
    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
247
248
249
TEST_CASE(batchnormv3_test)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
250
    auto* mm = p.get_main_module();
kahmed10's avatar
kahmed10 committed
251

252
253
254
255
256
257
258
259
260
261
262
263
264
265
    auto x    = mm->add_parameter("x", {migraphx::shape::float_type, {1, 32, 16, 16}});
    auto bias = mm->add_parameter("bias", {migraphx::shape::float_type, {32}});
    auto mean = mm->add_parameter("mean", {migraphx::shape::float_type, {32}});
    auto var  = mm->add_parameter("variance", {migraphx::shape::float_type, {32}});

    std::vector<float> scale_data(32, 1.0);
    auto scale = mm->add_literal(migraphx::shape{migraphx::shape::float_type, {32}}, scale_data);
    auto eps   = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {1e-6f}});

    auto usq_scale = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1, 2}}}), scale);
    auto usq_bias  = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1, 2}}}), bias);
    auto usq_mean  = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1, 2}}}), mean);
    auto usq_var   = mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1, 2}}}), var);

266
267
268
269
270
    auto x_sub_mean = add_common_op(*mm, migraphx::make_op("sub"), {x, usq_mean});
    auto var_eps    = add_common_op(*mm, migraphx::make_op("add"), {usq_var, eps});
    auto rsqrt      = mm->add_instruction(migraphx::make_op("rsqrt"), var_eps);
    auto mul0       = add_common_op(*mm, migraphx::make_op("mul"), {usq_scale, rsqrt});
    auto r0         = add_common_op(*mm, migraphx::make_op("mul"), {x_sub_mean, mul0});
271
272
273
    add_common_op(*mm, migraphx::make_op("add"), {r0, usq_bias});

    auto prog = optimize_tf("batchnormv3_test.pb", true);
kahmed10's avatar
kahmed10 committed
274
275
276
    EXPECT(p == prog);
}

277
278
279
TEST_CASE(biasadd_test)
{
    migraphx::program p;
280
281

    auto* mm = p.get_main_module();
282
    migraphx::shape s0{migraphx::shape::float_type, {1, 500, 1, 1}};
283
    uint64_t axis = 1;
284
285
    auto l0       = mm->add_parameter("0", s0);
    auto l1       = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {500}});
286
    auto l2       = mm->add_instruction(
287
        migraphx::make_op("broadcast", {{"axis", axis}, {"out_lens", l0->get_shape().lens()}}), l1);
288
    mm->add_instruction(migraphx::make_op("add"), l0, l2);
Paul's avatar
Paul committed
289
    auto prog = optimize_tf("biasadd_test.pb", true);
290
291
292
293

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
294
295
296
297
298
299
300
301
302
303
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}});
304
    auto l2 = mm->add_instruction(
305
        migraphx::make_op("broadcast", {{"axis", axis}, {"out_lens", l0->get_shape().lens()}}), l1);
306
    mm->add_instruction(migraphx::make_op("add"), l0, l2);
kahmed10's avatar
kahmed10 committed
307
308
309
310
311
    auto prog = optimize_tf("biasadd_scalar_test.pb", true);

    EXPECT(p == prog);
}

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

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
318
319
320
321
    mm->add_instruction(
        migraphx::make_op("convert",
                          {{"target_type", migraphx::to_value(migraphx::shape::int32_type)}}),
        l0);
Khalique's avatar
Khalique committed
322
323
324
325
326
    auto prog = optimize_tf("cast_test.pb", false);

    EXPECT(p == prog);
}

327
328
329
TEST_CASE(concat_test)
{
    migraphx::program p;
Khalique's avatar
Khalique committed
330

331
332
333
334
    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}});
335
336
337

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

341
    mm->add_instruction(migraphx::make_op("concat", {{"axis", axis}}), l0, l1);
Paul's avatar
Paul committed
342
    auto prog = optimize_tf("concat_test.pb", false);
343
344
345
346
347
348
349

    EXPECT(p == prog);
}

TEST_CASE(const_test)
{
    migraphx::program p;
350
351
352

    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
353
    auto prog = optimize_tf("constant_test.pb", false);
354
355
356
357

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
358
migraphx::program create_conv()
359
360
{
    migraphx::program p;
Khalique's avatar
Khalique committed
361

362
363
364
    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
365
    std::vector<float> weight_data(3 * 3 * 3 * 32);
366
    std::fill(weight_data.begin(), weight_data.end(), 1.0f);
Khalique's avatar
Khalique committed
367
    auto l1 =
368
        mm->add_literal(migraphx::shape{migraphx::shape::float_type, {3, 3, 3, 32}}, weight_data);
369
370

    migraphx::op::convolution op;
371
372
373
    op.padding  = {1, 1, 1, 1};
    op.stride   = {1, 1};
    op.dilation = {1, 1};
374
375
    auto l2 =
        mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {3, 2, 0, 1}}}), l1);
376
    mm->add_instruction(op, l0, l2);
kahmed10's avatar
kahmed10 committed
377
378
379
380
381
382
383
384
385
386
387
    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
388
389
390
391
392
393
394
395
396
397
398
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
399
400
401
402
TEST_CASE(conv_nchw_test)
{
    migraphx::program p = create_conv();
    auto prog           = optimize_tf("conv_nchw_test.pb", false);
403
404
405
406

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
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);
426
427
428
429
    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
430
431
432
433
434
435
    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
436
437
438
439
TEST_CASE(depthwiseconv_test)
{
    migraphx::program p;

440
441
442
    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
443
444
445
    std::vector<float> weight_data(3 * 3 * 3 * 1);
    std::fill(weight_data.begin(), weight_data.end(), 1.0f);
    auto l1 =
446
        mm->add_literal(migraphx::shape{migraphx::shape::float_type, {3, 3, 3, 1}}, weight_data);
Khalique's avatar
Khalique committed
447
448

    migraphx::op::convolution op;
449
450
451
452
    op.padding  = {1, 1};
    op.stride   = {1, 1};
    op.dilation = {1, 1};
    op.group    = 3;
453
454
    auto l3 =
        mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {3, 2, 0, 1}}}), l1);
455
456
    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);
457
    mm->add_instruction(op, l0, l5);
Paul's avatar
Paul committed
458
    auto prog = optimize_tf("depthwise_conv_test.pb", true);
Khalique's avatar
Khalique committed
459
460
461
462

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
463
464
465
TEST_CASE(expanddims_test)
{
    migraphx::program p;
Khalique's avatar
Khalique committed
466

467
468
469
470
    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);
471
    mm->add_instruction(migraphx::make_op("reshape", {{"dims", {1, 2, 3, 4}}}), l0);
472
    auto prog = optimize_tf("expanddims_test.pb", false);
Khalique's avatar
Khalique committed
473
474
475
476
477
478
479
480
481

    EXPECT(p == prog);
}

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

482
483
484
485
    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);
486
    mm->add_instruction(migraphx::make_op("reshape", {{"dims", {2, 3, 4, 1}}}), l0);
487
    auto prog = optimize_tf("expanddims_neg_test.pb", false);
Khalique's avatar
Khalique committed
488
489
490
491

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
492
493
494
495
TEST_CASE(gather_test)
{
    migraphx::program p;

496
497
498
499
500
501
    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
502
503

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

Khalique's avatar
Khalique committed
507
508
509
    EXPECT(p == prog);
}

510
511
512
TEST_CASE(identity_test)
{
    migraphx::program p;
513
514
515

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
516
    mm->add_instruction(migraphx::make_op("identity"), l0);
Paul's avatar
Paul committed
517
    auto prog = optimize_tf("identity_test.pb", false);
518
519
520
521

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
522
523
524
TEST_CASE(matmul_test)
{
    migraphx::program p;
Khalique's avatar
Khalique committed
525

526
527
528
    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
529

530
531
532
533
    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);
534

535
    mm->add_instruction(migraphx::make_op("dot"), trans_l0, trans_l1);
Paul's avatar
Paul committed
536
    auto prog = optimize_tf("matmul_test.pb", false);
Khalique's avatar
Khalique committed
537
538
539
540

    EXPECT(p == prog);
}

541
542
543
TEST_CASE(mean_test)
{
    migraphx::program p;
544
545

    auto* mm = p.get_main_module();
Khalique's avatar
Khalique committed
546
    migraphx::literal l{migraphx::shape{migraphx::shape::int32_type, {2}}, {2, 3}};
547
548
549
    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
550
    migraphx::op::reduce_mean op{{2, 3}};
551
552
    mm->add_instruction(op, l0);
    auto l3 = mm->add_instruction(op, l0);
553
    mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {2, 3}}}), l3);
Paul's avatar
Paul committed
554
    auto prog = optimize_tf("mean_test.pb", false);
555
556
557
558
559
560
561

    EXPECT(p == prog);
}

TEST_CASE(mean_test_nhwc)
{
    migraphx::program p;
562
563

    auto* mm = p.get_main_module();
Khalique's avatar
Khalique committed
564
    migraphx::literal l{migraphx::shape{migraphx::shape::int32_type, {2}}, {1, 2}};
565
    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
566
567
    auto l1 =
        mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 2, 3, 1}}}), l0);
Khalique's avatar
Khalique committed
568
    migraphx::op::reduce_mean op{{1, 2}};
569
    auto l2 = mm->add_instruction(op, l1);
570
    mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {1, 2}}}), l2);
Paul's avatar
Paul committed
571
    auto prog = optimize_tf("mean_test_nhwc.pb", true);
572
573
574
575

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
576
577
578
579
TEST_CASE(mul_test)
{
    migraphx::program p;

580
581
582
583
    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}});

584
    mm->add_instruction(migraphx::make_op("mul"), l0, l1);
Paul's avatar
Paul committed
585
    auto prog = optimize_tf("mul_test.pb", false);
Khalique's avatar
Khalique committed
586
587
588
589

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
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);
}

606
607
608
TEST_CASE(onehot_test)
{
    migraphx::program p;
609
610
611

    auto* mm = p.get_main_module();
    auto l0  = mm->add_literal(
Khalique's avatar
Khalique committed
612
        migraphx::literal{migraphx::shape{migraphx::shape::int32_type, {5}}, {1, 1, 1, 1, 1}});
613
614
615
616
    mm->add_literal(2);
    mm->add_literal(1.0f);
    mm->add_literal(0.0f);
    auto l1 = mm->add_literal(
Khalique's avatar
Khalique committed
617
        migraphx::literal{migraphx::shape{migraphx::shape::float_type, {2, 2}}, {1, 0, 0, 1}});
618
    int axis = 0;
619
    mm->add_instruction(migraphx::make_op("gather", {{"axis", axis}}), l1, l0);
620
621
622
623
624
    auto prog = optimize_tf("onehot_test.pb", false);

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
625
626
627
628
629
630
631
632
TEST_CASE(noop_test)
{
    migraphx::program p;
    auto prog = optimize_tf("noop_test.pb", false);

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
633
634
635
TEST_CASE(pack_test)
{
    migraphx::program p;
636
637
638
639
640

    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
641
642
643
644
    std::vector<migraphx::instruction_ref> args{l0, l1, l2};
    std::vector<migraphx::instruction_ref> unsqueezed_args;
    int64_t axis = 1;

645
646
647
648
649
650
651
652
653
    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
654
    auto prog = optimize_tf("pack_test.pb", false);
Khalique's avatar
Khalique committed
655
656
657
658

    EXPECT(p == prog);
}

659
660
661
TEST_CASE(pack_test_nhwc)
{
    migraphx::program p;
662
663
664

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 2, 1, 1}});
665
666
667
668
669
670
671
672
    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
673
    std::vector<migraphx::instruction_ref> args{lt0, lt1, lt2};
674
    std::vector<migraphx::instruction_ref> unsqueezed_args;
Paul's avatar
Paul committed
675
    int64_t nchw_axis = 3;
676
677
678
679
680

    std::transform(args.begin(),
                   args.end(),
                   std::back_inserter(unsqueezed_args),
                   [&](migraphx::instruction_ref arg) {
681
682
                       return mm->add_instruction(
                           migraphx::make_op("unsqueeze", {{"axes", {nchw_axis}}}), arg);
683
                   });
684
685
    mm->add_instruction(migraphx::make_op("concat", {{"axis", static_cast<int>(nchw_axis)}}),
                        unsqueezed_args);
Paul's avatar
Paul committed
686
    auto prog = optimize_tf("pack_test_nhwc.pb", true);
687
688
689
690

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
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);
}

708
709
710
TEST_CASE(pooling_test)
{
    migraphx::program p;
711
712
713

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
714
715
    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
716
717
718
719
    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
720
    mm->add_instruction(avg_pool_op, l0);
721
    mm->add_instruction(max_pool_op, l0);
Paul's avatar
Paul committed
722
    auto prog = optimize_tf("pooling_test.pb", true);
723
724
725
726

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
727
728
729
TEST_CASE(pow_test)
{
    migraphx::program p;
730
731
732
733

    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}});
734
    mm->add_instruction(migraphx::make_op("pow"), l0, l1);
Khalique's avatar
Khalique committed
735
736
737
738
739
    auto prog = optimize_tf("pow_test.pb", false);

    EXPECT(p == prog);
}

740
741
742
TEST_CASE(relu_test)
{
    migraphx::program p;
743
744
745

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
746
    mm->add_instruction(migraphx::make_op("relu"), l0);
Paul's avatar
Paul committed
747
    auto prog = optimize_tf("relu_test.pb", false);
748
749
750
751

    EXPECT(p == prog);
}

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

    auto* mm = p.get_main_module();
kahmed10's avatar
kahmed10 committed
757
    std::vector<size_t> input_lens{1, 3, 16, 16};
758
759
760
    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);
761
762
763
764
    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);
765
    mm->add_instruction(migraphx::make_op("clip"), l0, min_val, max_val);
Paul's avatar
Paul committed
766
    auto prog = optimize_tf("relu6_test.pb", false);
Khalique's avatar
Khalique committed
767
768
769
770

    EXPECT(p == prog);
}

771
TEST_CASE(relu6_half_test)
772
773
774
775
{
    migraphx::program p;

    auto* mm = p.get_main_module();
776
777
778
779
780
781
    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}});
782
783
784
785
    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);
786
787
    mm->add_instruction(migraphx::make_op("clip"), l0, min_val, max_val);
    auto prog = optimize_tf("relu6_half_test.pb", false);
788
789
790
791

    EXPECT(p == prog);
}

792
793
794
TEST_CASE(reshape_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, {16}});
798
799
    migraphx::shape s0{migraphx::shape::int32_type, {4}};
    // in tf, the second arg is a literal that contains new dimensions
800
    mm->add_literal(migraphx::literal{s0, {1, 1, 1, 16}});
801
    mm->add_instruction(migraphx::make_op("reshape", {{"dims", {1, 1, 1, 16}}}), l0);
Paul's avatar
Paul committed
802
    auto prog = optimize_tf("reshape_test.pb", false);
803
804
805
806

    EXPECT(p == prog);
}

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

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
813
    mm->add_instruction(migraphx::make_op("rsqrt"), l0);
Khalique's avatar
Khalique committed
814
815
816
817
818
    auto prog = optimize_tf("rsqrt_test.pb", false);

    EXPECT(p == prog);
}

819
820
821
TEST_CASE(shape_test)
{
    migraphx::program p;
822
823
824
825

    auto* mm = p.get_main_module();
    mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
    mm->add_literal(
826
827
828
829
830
831
        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
832
833
834
TEST_CASE(slice_test)
{
    migraphx::program p;
835
836

    auto* mm             = p.get_main_module();
Khalique's avatar
Khalique committed
837
    std::size_t num_axes = 2;
838
    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {5, 10}});
Khalique's avatar
Khalique committed
839
    migraphx::shape s0{migraphx::shape::int32_type, {num_axes}};
840
841
    mm->add_literal(migraphx::literal{s0, {1, 0}});
    mm->add_literal(migraphx::literal{s0, {2, -1}});
Khalique's avatar
Khalique committed
842
843
844
845
846
847

    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);
848
    mm->add_instruction(op, l0);
Khalique's avatar
Khalique committed
849
850
851
852
853
    auto prog = optimize_tf("slice_test.pb", false);

    EXPECT(p == prog);
}

854
855
856
TEST_CASE(softmax_test)
{
    migraphx::program p;
857
858
859

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3}});
860
    mm->add_instruction(migraphx::make_op("softmax", {{"axis", 1}}), l0);
Paul's avatar
Paul committed
861
    auto prog = optimize_tf("softmax_test.pb", false);
862
863
864
865

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
866
867
868
TEST_CASE(split_test)
{
    migraphx::program p;
869
870

    auto* mm = p.get_main_module();
kahmed10's avatar
kahmed10 committed
871
    std::vector<int64_t> axes{0, 1};
872
873
874
875
876
    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
877
878
879
880
881
882
    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
883
884
885
    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});
886
    auto prog = parse_tf("split_test.pb", false);
kahmed10's avatar
kahmed10 committed
887
888
889
890
891
892
893

    EXPECT(p == prog);
}

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

    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
899
900
    auto l1 = mm->add_instruction(migraphx::make_op("identity"), l0);
    mm->add_return({l1});
901
    auto prog = parse_tf("split_test_one_output.pb", false);
kahmed10's avatar
kahmed10 committed
902
903
904
905
906
907
908

    EXPECT(p == prog);
}

TEST_CASE(split_test_vector_as_input)
{
    migraphx::program p;
909
910

    auto* mm = p.get_main_module();
kahmed10's avatar
kahmed10 committed
911
    std::vector<int64_t> axes{0, 1};
912
    auto l0 = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {5, 30}});
kahmed10's avatar
kahmed10 committed
913
    // split sizes
914
    mm->add_literal(
kahmed10's avatar
kahmed10 committed
915
        migraphx::literal{migraphx::shape{migraphx::shape::int32_type, {3}}, {4, 15, 11}});
916
917
918
    mm->add_literal(1); // split axis
    mm->add_literal(1); // concat axis
    mm->add_literal(1); // concat axis
919
920
921
922
923
924
    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
925
926
927
    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});
928
    auto prog = parse_tf("split_test_vector_as_input.pb", false);
kahmed10's avatar
kahmed10 committed
929
930
931
932

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
933
934
935
TEST_CASE(sqdiff_test)
{
    migraphx::program p;
936
937
938
939

    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}});
940
    mm->add_instruction(migraphx::make_op("sqdiff"), l0, l1);
Khalique's avatar
Khalique committed
941
942
943
944
945
    auto prog = optimize_tf("sqdiff_test.pb", false);

    EXPECT(p == prog);
}

946
947
948
TEST_CASE(squeeze_test)
{
    migraphx::program p;
949
950
951

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

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

Khalique's avatar
Khalique committed
958
959
960
TEST_CASE(stopgradient_test)
{
    migraphx::program p;
961
962
963

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

Khalique's avatar
Khalique committed
967
    EXPECT(p == prog);
Khalique's avatar
Khalique committed
968
969
}

Khalique's avatar
Khalique committed
970
971
972
TEST_CASE(stridedslice_test)
{
    migraphx::program p;
973
974
975

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 10, 1, 1}});
976
977
    auto l1 =
        mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 2, 3, 1}}}), l0);
Khalique's avatar
Khalique committed
978
979
    std::size_t num_axes = 4;
    migraphx::op::slice op;
Khalique's avatar
Khalique committed
980
    op.starts = {0, 0, 0, 0};
Paul's avatar
Paul committed
981
    op.ends   = {1, 1, 1, 5};
Khalique's avatar
Khalique committed
982
983
    op.axes   = std::vector<int64_t>(num_axes);
    std::iota(op.axes.begin(), op.axes.end(), 0);
984
    auto l2          = mm->add_instruction(op, l1);
Paul's avatar
Paul committed
985
    auto shrink_axis = 1;
986
    mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {shrink_axis}}}), l2);
Paul's avatar
Paul committed
987
    auto prog = optimize_tf("stridedslice_test.pb", true);
Khalique's avatar
Khalique committed
988
989
990
991

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
992
993
994
TEST_CASE(stridedslice_masks_test)
{
    migraphx::program p;
995
996
997

    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
998
999
    std::size_t num_axes = 4;
    migraphx::op::slice op;
1000
1001
    op.starts = {0, 1, 1, 0};
    op.ends   = {1, 3, 3, 10};
Khalique's avatar
Khalique committed
1002
1003
1004
    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)
1005
1006
1007
1008
1009
1010
1011
    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});

1012
1013
    auto l1 =
        mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 2, 3, 1}}}), l0);
1014
    auto l2 = mm->add_instruction(op, l1);
1015
1016
    auto l3 =
        mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 3, 1, 2}}}), l2);
kahmed10's avatar
kahmed10 committed
1017
    mm->add_return({l3});
1018
    auto prog = parse_tf("stridedslice_masks_test.pb", true);
Khalique's avatar
Khalique committed
1019
1020
1021
1022

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
1023
1024
1025
TEST_CASE(sub_test)
{
    migraphx::program p;
1026
1027
1028
1029

    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
1030
1031
    auto l2  = mm->add_instruction(migraphx::make_op("sub"), l0, l1);
    mm->add_return({l2});
1032
    auto prog = parse_tf("sub_test.pb", false);
Khalique's avatar
Khalique committed
1033
1034
1035
1036

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
1037
1038
1039
TEST_CASE(tanh_test)
{
    migraphx::program p;
1040
1041

    auto* mm = p.get_main_module();
kahmed10's avatar
kahmed10 committed
1042
1043
1044
1045
    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
1046
1047
1048
1049

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
1050
1051
1052
TEST_CASE(transpose_test)
{
    migraphx::program p;
1053
1054
1055

    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
1056
    migraphx::shape s0{migraphx::shape::int32_type, {4}};
1057
    mm->add_literal(migraphx::literal{s0, {0, 2, 3, 1}});
1058
    mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 2, 3, 1}}}), l0);
Khalique's avatar
Khalique committed
1059
1060
1061
1062
1063
    auto prog = optimize_tf("transpose_test.pb", false);

    EXPECT(p == prog);
}

1064
1065
1066
TEST_CASE(variable_batch_test)
{
    migraphx::program p;
1067
1068
1069

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
1070
    mm->add_instruction(migraphx::make_op("identity"), l0);
1071
1072
1073
1074
1075
    auto prog = optimize_tf("variable_batch_test.pb", false);

    EXPECT(p == prog);
}

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