tf_test.cpp 38 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
206
207
208
209
210
211
212
213

    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 rt    = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {0.5}});
    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);

    auto numer   = 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 denom   = add_common_op(*mm, migraphx::make_op("pow"), {var_eps, rt});
    auto div0    = add_common_op(*mm, migraphx::make_op("div"), {numer, denom});
    auto r0      = add_common_op(*mm, migraphx::make_op("mul"), {div0, usq_scale});
    add_common_op(*mm, migraphx::make_op("add"), {r0, usq_bias});

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

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

223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
    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 rt    = mm->add_literal(migraphx::literal{migraphx::shape::half_type, {0.5}});
    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);

    auto numer   = 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 denom   = add_common_op(*mm, migraphx::make_op("pow"), {var_eps, rt});
    auto div0    = add_common_op(*mm, migraphx::make_op("div"), {numer, denom});
    auto r0      = add_common_op(*mm, migraphx::make_op("mul"), {div0, usq_scale});
    add_common_op(*mm, migraphx::make_op("add"), {r0, usq_bias});

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

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

254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
    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 rt    = mm->add_literal(migraphx::literal{migraphx::shape::float_type, {0.5}});
    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);

    auto numer   = 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 denom   = add_common_op(*mm, migraphx::make_op("pow"), {var_eps, rt});
    auto div0    = add_common_op(*mm, migraphx::make_op("div"), {numer, denom});
    auto r0      = add_common_op(*mm, migraphx::make_op("mul"), {div0, usq_scale});
    add_common_op(*mm, migraphx::make_op("add"), {r0, usq_bias});

    auto prog = optimize_tf("batchnormv3_test.pb", true);
kahmed10's avatar
kahmed10 committed
277
278
279
    EXPECT(p == prog);
}

280
281
282
TEST_CASE(biasadd_test)
{
    migraphx::program p;
283
284

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

    EXPECT(p == prog);
}

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

    EXPECT(p == prog);
}

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

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

    EXPECT(p == prog);
}

330
331
332
TEST_CASE(concat_test)
{
    migraphx::program p;
Khalique's avatar
Khalique committed
333

334
335
336
337
    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}});
338
339
340

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

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

    EXPECT(p == prog);
}

TEST_CASE(const_test)
{
    migraphx::program p;
353
354
355

    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
356
    auto prog = optimize_tf("constant_test.pb", false);
357
358
359
360

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
361
migraphx::program create_conv()
362
363
{
    migraphx::program p;
Khalique's avatar
Khalique committed
364

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

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

    EXPECT(p == prog);
}

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

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

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

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
468
469
470
TEST_CASE(expanddims_test)
{
    migraphx::program p;
Khalique's avatar
Khalique committed
471

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

    EXPECT(p == prog);
}

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

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

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
497
498
499
500
TEST_CASE(gather_test)
{
    migraphx::program p;

501
502
503
504
505
506
    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
507
508

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

Khalique's avatar
Khalique committed
512
513
514
    EXPECT(p == prog);
}

515
516
517
TEST_CASE(identity_test)
{
    migraphx::program p;
518
519
520

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

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
527
528
529
TEST_CASE(matmul_test)
{
    migraphx::program p;
Khalique's avatar
Khalique committed
530

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

535
536
537
538
    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);
539

540
    mm->add_instruction(migraphx::make_op("dot"), trans_l0, trans_l1);
Paul's avatar
Paul committed
541
    auto prog = optimize_tf("matmul_test.pb", false);
Khalique's avatar
Khalique committed
542
543
544
545

    EXPECT(p == prog);
}

546
547
548
TEST_CASE(mean_test)
{
    migraphx::program p;
549
550

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

    EXPECT(p == prog);
}

TEST_CASE(mean_test_nhwc)
{
    migraphx::program p;
567
568

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

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
581
582
583
584
TEST_CASE(mul_test)
{
    migraphx::program p;

585
586
587
588
    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 1, 1, 16}});
    auto l1  = mm->add_parameter("1", migraphx::shape{migraphx::shape::float_type, {1, 1, 1, 16}});

589
    mm->add_instruction(migraphx::make_op("mul"), l0, l1);
Paul's avatar
Paul committed
590
    auto prog = optimize_tf("mul_test.pb", false);
Khalique's avatar
Khalique committed
591
592
593
594

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
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);
}

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

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

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
630
631
632
633
634
635
636
637
TEST_CASE(noop_test)
{
    migraphx::program p;
    auto prog = optimize_tf("noop_test.pb", false);

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
638
639
640
TEST_CASE(pack_test)
{
    migraphx::program p;
641
642
643
644
645

    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
646
647
648
649
    std::vector<migraphx::instruction_ref> args{l0, l1, l2};
    std::vector<migraphx::instruction_ref> unsqueezed_args;
    int64_t axis = 1;

650
651
652
653
654
655
656
657
658
    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
659
    auto prog = optimize_tf("pack_test.pb", false);
Khalique's avatar
Khalique committed
660
661
662
663

    EXPECT(p == prog);
}

664
665
666
TEST_CASE(pack_test_nhwc)
{
    migraphx::program p;
667
668
669

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

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

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
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);
}

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

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

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
732
733
734
TEST_CASE(pow_test)
{
    migraphx::program p;
735
736
737
738

    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}});
739
    mm->add_instruction(migraphx::make_op("pow"), l0, l1);
Khalique's avatar
Khalique committed
740
741
742
743
744
    auto prog = optimize_tf("pow_test.pb", false);

    EXPECT(p == prog);
}

745
746
747
TEST_CASE(relu_test)
{
    migraphx::program p;
748
749
750

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

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
757
758
759
TEST_CASE(relu6_test)
{
    migraphx::program p;
760
761

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

    EXPECT(p == prog);
}

776
TEST_CASE(relu6_half_test)
777
778
779
780
{
    migraphx::program p;

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

    EXPECT(p == prog);
}

797
798
799
TEST_CASE(reshape_test)
{
    migraphx::program p;
800
801
802

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {16}});
803
804
    migraphx::shape s0{migraphx::shape::int32_type, {4}};
    // in tf, the second arg is a literal that contains new dimensions
805
    mm->add_literal(migraphx::literal{s0, {1, 1, 1, 16}});
806
    mm->add_instruction(migraphx::make_op("reshape", {{"dims", {1, 1, 1, 16}}}), l0);
Paul's avatar
Paul committed
807
    auto prog = optimize_tf("reshape_test.pb", false);
808
809
810
811

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
812
813
814
TEST_CASE(rsqrt_test)
{
    migraphx::program p;
815
816
817

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

    EXPECT(p == prog);
}

824
825
826
TEST_CASE(shape_test)
{
    migraphx::program p;
827
828
829
830

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

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

    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);
853
    mm->add_instruction(op, l0);
Khalique's avatar
Khalique committed
854
855
856
857
858
    auto prog = optimize_tf("slice_test.pb", false);

    EXPECT(p == prog);
}

859
860
861
TEST_CASE(softmax_test)
{
    migraphx::program p;
862
863
864

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

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
871
872
873
TEST_CASE(split_test)
{
    migraphx::program p;
874
875

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

    EXPECT(p == prog);
}

TEST_CASE(split_test_one_output)
{
    migraphx::program p;
899
900
901
902
903

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

    EXPECT(p == prog);
}

TEST_CASE(split_test_vector_as_input)
{
    migraphx::program p;
914
915

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

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
938
939
940
TEST_CASE(sqdiff_test)
{
    migraphx::program p;
941
942
943
944

    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}});
945
    mm->add_instruction(migraphx::make_op("sqdiff"), l0, l1);
Khalique's avatar
Khalique committed
946
947
948
949
950
    auto prog = optimize_tf("sqdiff_test.pb", false);

    EXPECT(p == prog);
}

951
952
953
TEST_CASE(squeeze_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, 2, 3, 1}});
957
    mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {0, 3}}}), l0);
Paul's avatar
Paul committed
958
    auto prog = optimize_tf("squeeze_test.pb", false);
959
960
961

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

Khalique's avatar
Khalique committed
963
964
965
TEST_CASE(stopgradient_test)
{
    migraphx::program p;
966
967
968

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

Khalique's avatar
Khalique committed
972
    EXPECT(p == prog);
Khalique's avatar
Khalique committed
973
974
}

Khalique's avatar
Khalique committed
975
976
977
TEST_CASE(stridedslice_test)
{
    migraphx::program p;
978
979
980

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

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
997
998
999
TEST_CASE(stridedslice_masks_test)
{
    migraphx::program p;
1000
1001
1002

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

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

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
1028
1029
1030
TEST_CASE(sub_test)
{
    migraphx::program p;
1031
1032
1033
1034

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

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
1042
1043
1044
TEST_CASE(tanh_test)
{
    migraphx::program p;
1045
1046

    auto* mm = p.get_main_module();
kahmed10's avatar
kahmed10 committed
1047
1048
1049
1050
    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
1051
1052
1053
1054

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
1055
1056
1057
TEST_CASE(transpose_test)
{
    migraphx::program p;
1058
1059
1060

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

    EXPECT(p == prog);
}

1069
1070
1071
TEST_CASE(variable_batch_test)
{
    migraphx::program p;
1072
1073
1074

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
1075
    mm->add_instruction(migraphx::make_op("identity"), l0);
1076
1077
1078
1079
1080
    auto prog = optimize_tf("variable_batch_test.pb", false);

    EXPECT(p == prog);
}

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