"start.sh" did not exist on "b916e23035c58994db6c112a78bd13a907ec476a"
tf_test.cpp 37.4 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
#include <migraphx/op/convolution.hpp>
#include <migraphx/op/reduce_mean.hpp>
#include <migraphx/op/pooling.hpp>
40
41
42

#include <migraphx/serialize.hpp>

43
44
#include "test.hpp"

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

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

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

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

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

    EXPECT(p == prog);
}

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

    EXPECT(p == prog);
}

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

105
    migraphx::program p;
106
107

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

    EXPECT(p == prog);
}

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

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

    EXPECT(p == prog);
}

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

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

    EXPECT(p == prog);
}

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

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

    EXPECT(p == prog);
}

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

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

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

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

    EXPECT(p == prog);
}

186
187
188
TEST_CASE(batchnorm_test)
{
    migraphx::program p;
189
    auto* mm = p.get_main_module();
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204

    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);

205
206
207
208
209
    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});
210
211
    add_common_op(*mm, migraphx::make_op("add"), {r0, usq_bias});

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

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

221
222
223
224
225
226
227
228
229
230
231
232
233
234
    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);

235
236
237
238
239
    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});
240
241
242
    add_common_op(*mm, migraphx::make_op("add"), {r0, usq_bias});

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

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

251
252
253
254
255
256
257
258
259
260
261
262
263
264
    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);

265
266
267
268
269
    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});
270
271
272
    add_common_op(*mm, migraphx::make_op("add"), {r0, usq_bias});

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

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

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

    EXPECT(p == prog);
}

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

    EXPECT(p == prog);
}

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

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

    EXPECT(p == prog);
}

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

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

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

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

    EXPECT(p == prog);
}

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

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

    EXPECT(p == prog);
}

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

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

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

    EXPECT(p == prog);
}

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

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

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

    EXPECT(p == prog);
}

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

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

    EXPECT(p == prog);
}

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

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

    EXPECT(p == prog);
}

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

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

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

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

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

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

    EXPECT(p == prog);
}

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

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

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

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

    EXPECT(p == prog);
}

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

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

    EXPECT(p == prog);
}

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

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

    EXPECT(p == prog);
}

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

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

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

    EXPECT(p == prog);
}

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

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

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

    EXPECT(p == prog);
}

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

    EXPECT(p == prog);
}

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

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

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

    EXPECT(p == prog);
}

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

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

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

    EXPECT(p == prog);
}

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

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

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

    EXPECT(p == prog);
}

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

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

    EXPECT(p == prog);
}

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

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

    EXPECT(p == prog);
}

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

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

    EXPECT(p == prog);
}

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

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

    EXPECT(p == prog);
}

791
792
793
TEST_CASE(reshape_test)
{
    migraphx::program p;
794
795
796

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

    EXPECT(p == prog);
}

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

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

    EXPECT(p == prog);
}

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

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

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

Charlie Lin's avatar
Charlie Lin committed
842
843
    mm->add_instruction(
        migraphx::make_op("slice", {{"starts", {1, 0}}, {"ends", {3, 10}}, {"axes", {0, 1}}}), l0);
Khalique's avatar
Khalique committed
844
845
846
847
848
    auto prog = optimize_tf("slice_test.pb", false);

    EXPECT(p == prog);
}

849
850
851
TEST_CASE(softmax_test)
{
    migraphx::program p;
852
853
854

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

    EXPECT(p == prog);
}

kahmed10's avatar
kahmed10 committed
861
862
863
TEST_CASE(split_test)
{
    migraphx::program p;
864
865

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

    EXPECT(p == prog);
}

TEST_CASE(split_test_one_output)
{
    migraphx::program p;
889
890
891
892
893

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

    EXPECT(p == prog);
}

TEST_CASE(split_test_vector_as_input)
{
    migraphx::program p;
904
905

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

    EXPECT(p == prog);
}

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

    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}});
935
    mm->add_instruction(migraphx::make_op("sqdiff"), l0, l1);
Khalique's avatar
Khalique committed
936
937
938
939
940
    auto prog = optimize_tf("sqdiff_test.pb", false);

    EXPECT(p == prog);
}

941
942
943
TEST_CASE(squeeze_test)
{
    migraphx::program p;
944
945
946

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

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

Khalique's avatar
Khalique committed
953
954
955
TEST_CASE(stopgradient_test)
{
    migraphx::program p;
956
957
958

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

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

Khalique's avatar
Khalique committed
965
966
967
TEST_CASE(stridedslice_test)
{
    migraphx::program p;
968
969
970

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 10, 1, 1}});
971
972
    auto l1 =
        mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 2, 3, 1}}}), l0);
Charlie Lin's avatar
Charlie Lin committed
973
974
975
976
    auto l2 = mm->add_instruction(
        migraphx::make_op(
            "slice", {{"starts", {0, 0, 0, 0}}, {"ends", {1, 1, 1, 5}}, {"axes", {0, 1, 2, 3}}}),
        l1);
Paul's avatar
Paul committed
977
    auto shrink_axis = 1;
978
    mm->add_instruction(migraphx::make_op("squeeze", {{"axes", {shrink_axis}}}), l2);
Paul's avatar
Paul committed
979
    auto prog = optimize_tf("stridedslice_test.pb", true);
Khalique's avatar
Khalique committed
980
981
982
983

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
984
985
986
TEST_CASE(stridedslice_masks_test)
{
    migraphx::program p;
987
988
989

    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
990
    // add literals for starts, ends, and strides in tf (NHWC format)
991
992
993
994
995
996
997
    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});

998
999
    auto l1 =
        mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 2, 3, 1}}}), l0);
Charlie Lin's avatar
Charlie Lin committed
1000
1001
1002
1003
    auto l2 = mm->add_instruction(
        migraphx::make_op(
            "slice", {{"starts", {0, 1, 1, 0}}, {"ends", {1, 3, 3, 10}}, {"axes", {0, 1, 2, 3}}}),
        l1);
1004
1005
    auto l3 =
        mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 3, 1, 2}}}), l2);
kahmed10's avatar
kahmed10 committed
1006
    mm->add_return({l3});
1007
    auto prog = parse_tf("stridedslice_masks_test.pb", true);
Khalique's avatar
Khalique committed
1008
1009
1010
1011

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
1012
1013
1014
TEST_CASE(sub_test)
{
    migraphx::program p;
1015
1016
1017
1018

    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
1019
1020
    auto l2  = mm->add_instruction(migraphx::make_op("sub"), l0, l1);
    mm->add_return({l2});
1021
    auto prog = parse_tf("sub_test.pb", false);
Khalique's avatar
Khalique committed
1022
1023
1024
1025

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
1026
1027
1028
TEST_CASE(tanh_test)
{
    migraphx::program p;
1029
1030

    auto* mm = p.get_main_module();
kahmed10's avatar
kahmed10 committed
1031
1032
1033
1034
    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
1035
1036
1037
1038

    EXPECT(p == prog);
}

Khalique's avatar
Khalique committed
1039
1040
1041
TEST_CASE(transpose_test)
{
    migraphx::program p;
1042
1043
1044

    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
1045
    migraphx::shape s0{migraphx::shape::int32_type, {4}};
1046
    mm->add_literal(migraphx::literal{s0, {0, 2, 3, 1}});
1047
    mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {0, 2, 3, 1}}}), l0);
Khalique's avatar
Khalique committed
1048
1049
1050
1051
1052
    auto prog = optimize_tf("transpose_test.pb", false);

    EXPECT(p == prog);
}

1053
1054
1055
TEST_CASE(variable_batch_test)
{
    migraphx::program p;
1056
1057
1058

    auto* mm = p.get_main_module();
    auto l0  = mm->add_parameter("0", migraphx::shape{migraphx::shape::float_type, {1, 3, 16, 16}});
1059
    mm->add_instruction(migraphx::make_op("identity"), l0);
1060
1061
1062
1063
1064
    auto prog = optimize_tf("variable_batch_test.pb", false);

    EXPECT(p == prog);
}

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