onnx.cpp 116 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
6
7
8
#include <google/protobuf/text_format.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <onnx.pb.h>
#include <iostream>
#include <fstream>
#include <unordered_map>
#include <functional>
#include <array>
Paul's avatar
Paul committed
9
#include <utility>
10
#include <vector>
Paul's avatar
Paul committed
11

Paul's avatar
Paul committed
12
13
#include <migraphx/fallthrough.hpp>
#include <migraphx/program.hpp>
14
#include <migraphx/make_op.hpp>
Paul's avatar
Paul committed
15
16
17
#include <migraphx/ranges.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/config.hpp>
18
#include <migraphx/onnx.hpp>
19
#include <migraphx/pad_calc.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
20
21
#include <migraphx/type_traits.hpp>
#include <migraphx/float_equal.hpp>
22
23
#include <migraphx/file_buffer.hpp>
#include <migraphx/filesystem.hpp>
Paul's avatar
Paul committed
24

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <migraphx/op/as_shape.hpp>
#include <migraphx/op/batch_norm_inference.hpp>
#include <migraphx/op/broadcast.hpp>
#include <migraphx/op/concat.hpp>
#include <migraphx/op/convert.hpp>
#include <migraphx/op/gather.hpp>
#include <migraphx/op/gru.hpp>
#include <migraphx/op/lrn.hpp>
#include <migraphx/op/lstm.hpp>
#include <migraphx/op/multibroadcast.hpp>
#include <migraphx/op/pad.hpp>
#include <migraphx/op/reshape.hpp>
#include <migraphx/op/rnn.hpp>
#include <migraphx/op/rnn_last_cell_output.hpp>
#include <migraphx/op/rnn_last_hs_output.hpp>
#include <migraphx/op/rnn_variable_seq_lens.hpp>
#include <migraphx/op/rnn_var_sl_last_output.hpp>
#include <migraphx/op/scalar.hpp>
#include <migraphx/op/slice.hpp>
#include <migraphx/op/squeeze.hpp>
#include <migraphx/op/transpose.hpp>
#include <migraphx/op/undefined.hpp>
#include <migraphx/op/unknown.hpp>
#include <migraphx/op/unsqueeze.hpp>

Paul's avatar
Paul committed
50
namespace migraphx {
Paul's avatar
Paul committed
51
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
52

53
54
namespace onnx = onnx_for_migraphx;

Paul's avatar
Paul committed
55
56
struct onnx_parser
{
57
58
    std::string filename;
    std::string path    = ".";
Paul's avatar
Paul committed
59
    using attribute_map = std::unordered_map<std::string, onnx::AttributeProto>;
60
61
62
63
64
65
    struct node_info
    {
        attribute_map attributes{};
        std::size_t num_outputs = 1;
    };
    using node_map = std::unordered_map<std::string, onnx::NodeProto>;
Paul's avatar
Paul committed
66
    using op_func =
67
        std::function<std::vector<instruction_ref>(node_info, std::vector<instruction_ref>)>;
Paul's avatar
Paul committed
68
69
    node_map nodes;
    std::unordered_map<std::string, instruction_ref> instructions;
70
71
72
73
    program prog                  = program();
    bool is_pytorch               = false;
    std::size_t default_dim_value = 1;
    std::unordered_map<std::string, std::vector<std::size_t>> map_input_dims;
74
    bool skip_unknown_operators = false;
Paul's avatar
Paul committed
75
76

    std::unordered_map<std::string, op_func> ops;
77
    std::unordered_map<std::string, operation> map_actv_funcs;
Paul's avatar
Paul committed
78
79
80

    onnx_parser()
    {
81
        // sort onnx operator alphabetically through name
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
        add_generic_op("Abs", "abs");
        add_generic_op("Acos", "acos");
        add_generic_op("Acosh", "acosh");
        add_generic_op("Asin", "asin");
        add_generic_op("Asinh", "asinh");
        add_generic_op("Atan", "atan");
        add_generic_op("Atanh", "atanh");
        add_generic_op("Ceil", "ceil");
        add_generic_op("Concat", "concat");
        add_generic_op("Cos", "cos");
        add_generic_op("Cosh", "cosh");
        add_generic_op("Erf", "erf");
        add_generic_op("Exp", "exp");
        add_generic_op("Flatten", "flatten");
        add_generic_op("Floor", "floor");
        add_generic_op("Gather", "gather", true);
        add_generic_op("Identity", "identity");
        add_generic_op("Log", "log");
        add_generic_op("LogSoftmax", "logsoftmax");
        add_generic_op("Neg", "neg");
        add_generic_op("Reciprocal", "recip");
        add_generic_op("Relu", "relu");
        add_generic_op("Round", "round");
        add_generic_op("Sigmoid", "sigmoid");
        add_generic_op("Sign", "sign");
        add_generic_op("Sin", "sin");
        add_generic_op("Sinh", "sinh");
        add_generic_op("Softmax", "softmax");
        add_generic_op("Sqrt", "sqrt");
        add_generic_op("Squeeze", "squeeze", true);
        add_generic_op("Tan", "tan");
        add_generic_op("Tanh", "tanh");
        add_generic_op("Unsqueeze", "unsqueeze", true);

        add_binary_op("Add", "add");
        add_binary_op("Div", "div");
        add_binary_op("Mul", "mul");
        add_binary_op("Pow", "pow");
        add_binary_op("PRelu", "prelu");
        add_binary_op("Sub", "sub");

        add_variadic_op("Sum", "add");
        add_variadic_op("Max", "max");
        add_variadic_op("Min", "min");
Paul's avatar
Paul committed
126

127
        add_mem_op("ATen", &onnx_parser::parse_aten);
128
        add_mem_op("AveragePool", &onnx_parser::parse_pooling);
129
130
        add_mem_op("ArgMax", "argmax", &onnx_parser::parse_arg_op);
        add_mem_op("ArgMin", "argmin", &onnx_parser::parse_arg_op);
131
        add_mem_op("BatchNormalization", &onnx_parser::parse_batchnorm);
132
        add_mem_op("Cast", &onnx_parser::parse_cast);
Khalique's avatar
Khalique committed
133
        add_mem_op("Clip", &onnx_parser::parse_clip);
Paul's avatar
Paul committed
134
        add_mem_op("Constant", &onnx_parser::parse_constant);
135
136
        add_mem_op("ConstantFill", &onnx_parser::parse_constant_fill);
        add_mem_op("ConstantOfShape", &onnx_parser::parse_constant_of_shape);
137
138
        add_mem_op("Conv", "convolution", &onnx_parser::parse_conv);
        add_mem_op("ConvInteger", "quant_convolution", &onnx_parser::parse_conv);
kahmed10's avatar
kahmed10 committed
139
        add_mem_op("ConvTranspose", &onnx_parser::parse_conv_transpose);
140
        add_mem_op("Dropout", &onnx_parser::parse_dropout);
141
        add_mem_op("Elu", &onnx_parser::parse_elu);
142
        add_mem_op("Equal", "equal", &onnx_parser::parse_compare_op);
143
        add_mem_op("Expand", &onnx_parser::parse_expand);
Shucai Xiao's avatar
Shucai Xiao committed
144
        add_mem_op("GatherElements", &onnx_parser::parse_gather_elements);
Paul's avatar
Paul committed
145
        add_mem_op("Gemm", &onnx_parser::parse_gemm);
146
147
        add_mem_op("GlobalAveragePool", &onnx_parser::parse_pooling);
        add_mem_op("GlobalMaxPool", &onnx_parser::parse_pooling);
148
        add_mem_op("Greater", "greater", &onnx_parser::parse_compare_op);
149
150
        add_mem_op("GRU", &onnx_parser::parse_gru);
        add_mem_op("ImageScaler", &onnx_parser::parse_imagescaler);
kahmed10's avatar
kahmed10 committed
151
        add_mem_op("InstanceNormalization", &onnx_parser::parse_instancenorm);
152
        add_mem_op("LeakyRelu", &onnx_parser::parse_leaky_relu);
153
        add_mem_op("Less", "less", &onnx_parser::parse_compare_op);
154
        add_mem_op("LRN", &onnx_parser::parse_lrn);
155
        add_mem_op("LSTM", &onnx_parser::parse_lstm);
156
157
        add_mem_op("MatMul", "dot", &onnx_parser::parse_matmul);
        add_mem_op("MatMulInteger", "quant_dot", &onnx_parser::parse_matmul);
158
        add_mem_op("MaxPool", &onnx_parser::parse_pooling);
Shucai Xiao's avatar
Shucai Xiao committed
159
        add_mem_op("NonZero", &onnx_parser::parse_nonzero);
kahmed10's avatar
kahmed10 committed
160
        add_mem_op("OneHot", &onnx_parser::parse_onehot);
161
        add_mem_op("Pad", &onnx_parser::parse_pad);
kahmed10's avatar
kahmed10 committed
162
        add_mem_op("Range", &onnx_parser::parse_range);
Shucai Xiao's avatar
Shucai Xiao committed
163
164
165
166
        add_mem_op("ReduceL1", &onnx_parser::parse_reduce_l1);
        add_mem_op("ReduceL2", &onnx_parser::parse_reduce_l2);
        add_mem_op("ReduceLogSum", &onnx_parser::parse_reduce_log_sum);
        add_mem_op("ReduceLogSumExp", &onnx_parser::parse_reduce_log_sum_exp);
167
168
169
170
171
        add_mem_op("ReduceMax", "reduce_max", &onnx_parser::parse_reduce_oper);
        add_mem_op("ReduceMean", "reduce_mean", &onnx_parser::parse_reduce_oper);
        add_mem_op("ReduceMin", "reduce_min", &onnx_parser::parse_reduce_oper);
        add_mem_op("ReduceProd", "reduce_prod", &onnx_parser::parse_reduce_oper);
        add_mem_op("ReduceSum", "reduce_sum", &onnx_parser::parse_reduce_oper);
Shucai Xiao's avatar
Shucai Xiao committed
172
        add_mem_op("ReduceSumSquare", &onnx_parser::parse_reduce_sum_square);
173
        add_mem_op("Reshape", &onnx_parser::parse_reshape);
Shucai Xiao's avatar
Shucai Xiao committed
174
        add_mem_op("Resize", &onnx_parser::parse_resize);
175
        add_mem_op("RNN", &onnx_parser::parse_rnn);
Shucai Xiao's avatar
Shucai Xiao committed
176
        add_mem_op("Selu", &onnx_parser::parse_selu);
177
178
        add_mem_op("Shape", &onnx_parser::parse_shape);
        add_mem_op("Slice", &onnx_parser::parse_slice);
179
        add_mem_op("Split", &onnx_parser::parse_split);
kahmed10's avatar
kahmed10 committed
180
        add_mem_op("Tile", &onnx_parser::parse_tile);
181
        add_mem_op("Transpose", &onnx_parser::parse_transpose);
Shucai Xiao's avatar
Shucai Xiao committed
182
        add_mem_op("Upsample", &onnx_parser::parse_upsample);
Shucai Xiao's avatar
Shucai Xiao committed
183
        add_mem_op("Where", &onnx_parser::parse_where);
184
185
186
187
188
189
190

        // init the activation function map
        init_actv_func();
    }

    void init_actv_func()
    {
191
        // Support name format of all lower case or the first letter capital
192
193
194
195
196
197
198
        map_actv_funcs.insert(std::make_pair("tanh", make_op("tanh")));
        map_actv_funcs.insert(std::make_pair("relu", make_op("relu")));
        map_actv_funcs.insert(std::make_pair("sigmoid", make_op("sigmoid")));
        map_actv_funcs.insert(std::make_pair("leakyrelu", make_op("leaky_relu")));
        map_actv_funcs.insert(std::make_pair("elu", make_op("elu")));
    }

199
    operation load(const std::string& name, const node_info& info) const
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
    {
        auto op = make_op(name);
        auto v  = op.to_value();
        for(auto&& x : v)
        {
            if(info.attributes.count(x.get_key()) == 0)
                continue;
            literal s = parse_value(info.attributes.at(x.get_key()));
            if(x.is_array())
            {
                std::vector<value> values;
                s.visit([&](auto y) {
                    std::transform(y.begin(), y.end(), std::back_inserter(values), [](auto z) {
                        return value(z);
                    });
                });
                x = values;
            }
            else
            {
                s.visit([&](auto y) { x = y.front(); });
            }
        }
        op.from_value(v);
        return op;
Paul's avatar
Paul committed
225
226
227
228
    }

    template <class F>
    void add_op(std::string name, F f)
Paul's avatar
Paul committed
229
230
231
232
233
234
235
236
237
    {
        ops.emplace(name, [=](auto&&... xs) {
            return std::vector<instruction_ref>{f(std::forward<decltype(xs)>(xs)...)};
        });
    }

    // Multi output op
    template <class F>
    void add_multi_op(std::string name, F f)
Paul's avatar
Paul committed
238
239
240
241
242
    {
        ops.emplace(name, f);
    }

    template <class F>
243
    void add_mem_op(const std::string& name, F f)
Paul's avatar
Paul committed
244
    {
Paul's avatar
Paul committed
245
        add_op(name, [=](auto&&... xs) {
Paul's avatar
Paul committed
246
247
248
            return std::mem_fn(f)(*this, name, std::forward<decltype(xs)>(xs)...);
        });
    }
Khalique's avatar
Khalique committed
249

250
251
252
253
254
255
256
257
258
    template <class F>
    void add_mem_op(const std::string& onnx_name, const std::string& op_name, F f)
    {
        add_op(onnx_name, [=](auto&&... xs) {
            return std::mem_fn(f)(*this, onnx_name, op_name, std::forward<decltype(xs)>(xs)...);
        });
    }

    void add_binary_op(const std::string& onnx_name, const std::string& op_name)
259
    {
260
        add_op(onnx_name, [this, op_name](node_info info, std::vector<instruction_ref> args) {
Scott Thornton's avatar
Scott Thornton committed
261
            if(args.size() != 2)
Paul's avatar
Paul committed
262
                MIGRAPHX_THROW("binary operators should have 2 operands");
263
            if(contains(info.attributes, "broadcast") and contains(info.attributes, "axis"))
264
            {
265
                uint64_t broadcasted = parse_value(info.attributes.at("broadcast")).at<uint64_t>();
266
267
                if(broadcasted != 0)
                {
268
                    uint64_t axis = parse_value(info.attributes.at("axis")).at<uint64_t>();
Shucai Xiao's avatar
Shucai Xiao committed
269
270
                    auto l = prog.add_instruction(op::broadcast{axis, args[0]->get_shape().lens()},
                                                  args[1]);
271
                    return prog.add_instruction(make_op(op_name), args[0], l);
272
                }
273
                return prog.add_instruction(make_op(op_name), args);
274
            }
Paul's avatar
Paul committed
275
            else
276
            {
277
                return add_broadcastable_binary_op(args[0], args[1], op_name);
278
279
280
281
            }
        });
    }

Shucai Xiao's avatar
Shucai Xiao committed
282
283
    std::vector<std::size_t> compute_broadcasted_lens(std::vector<std::size_t> s0,
                                                      std::vector<std::size_t> s1)
284
285
286
287
288
289
290
291
292
293
294
295
296
    {
        // Example:
        // s0 = (3,2,4,5) and s1 = (2,1,1)
        //
        // In this case we need to broadcast (:,1,1) portion of
        // s1 plus broadcast the 1st dimension of s1
        // giving output_lens = (3,2,4,5)
        //
        // Another example:
        // s0 = (3,2,1,5) and s1 = (2,7,5)
        // In this case we need to broadcast the (:,:,1:,:) axis
        // of s0 plus the 1st dimension of s1 giving
        // output_lens = (3,2,7,5)
Shucai Xiao's avatar
Shucai Xiao committed
297
        if(s0.size() > s1.size())
298
299
300
301
302
303
        {
            s0.swap(s1);
        }

        std::vector<std::size_t> out_lens(s1);
        auto offset = s1.size() - s0.size();
Shucai Xiao's avatar
Shucai Xiao committed
304
305
306
307
        std::transform(s0.begin(),
                       s0.end(),
                       s1.begin() + offset,
                       out_lens.begin() + offset,
308
                       [&](auto a, auto b) {
Shucai Xiao's avatar
Shucai Xiao committed
309
                           if(a != b and a != 1 and b != 1)
310
                           {
Shucai Xiao's avatar
Shucai Xiao committed
311
312
313
314
315
316
                               MIGRAPHX_THROW("COMPUTE_BROADCASTLEN: shape {" +
                                              to_string_range(s0) + "} and {" +
                                              to_string_range(s1) + "} mismatch!");
                           }
                           return std::max(a, b);
                       });
317
318
319
320

        return out_lens;
    }

Shucai Xiao's avatar
Shucai Xiao committed
321
322
    instruction_ref make_contiguous(instruction_ref ins)
    {
Shucai Xiao's avatar
Shucai Xiao committed
323
        if(ins->get_shape().standard())
Shucai Xiao's avatar
Shucai Xiao committed
324
325
326
327
        {
            return ins;
        }

328
        return prog.add_instruction(make_op("contiguous"), ins);
Shucai Xiao's avatar
Shucai Xiao committed
329
330
    }

331
332
    instruction_ref
    add_broadcastable_binary_op(instruction_ref arg0, instruction_ref arg1, const std::string& name)
Khalique's avatar
Khalique committed
333
    {
Khalique's avatar
Khalique committed
334
        if(arg0->get_shape().lens() != arg1->get_shape().lens())
Khalique's avatar
Khalique committed
335
336
        {
            // Get lengths for both arguments
Shucai Xiao's avatar
Shucai Xiao committed
337
338
            auto s0       = arg0->get_shape().lens();
            auto s1       = arg1->get_shape().lens();
339
            auto out_lens = compute_broadcasted_lens(s0, s1);
340
341
342
343
344
345
346
347
348

            auto l0 = arg0;
            if(arg0->get_shape().lens() != out_lens)
                l0 = prog.add_instruction(op::multibroadcast{out_lens}, arg0);

            auto l1 = arg1;
            if(arg1->get_shape().lens() != out_lens)
                l1 = prog.add_instruction(op::multibroadcast{out_lens}, arg1);

349
            return prog.add_instruction(make_op(name), l0, l1);
Khalique's avatar
Khalique committed
350
351
352
        }
        else
        {
353
            return prog.add_instruction(make_op(name), {arg0, arg1});
Khalique's avatar
Khalique committed
354
        }
355
356
    }

357
358
359
    void add_generic_op(const std::string& onnx_name,
                        const std::string& op_name,
                        bool contiguous = false)
Paul's avatar
Paul committed
360
    {
361
362
363
364
365
366
367
368
369
370
371
372
        add_op(
            onnx_name,
            [this, op_name, contiguous](const node_info& info, std::vector<instruction_ref> args) {
                auto op = load(op_name, info);
                if(contiguous)
                {
                    std::transform(args.begin(), args.end(), args.begin(), [&](auto arg) {
                        return this->make_contiguous(arg);
                    });
                }
                return prog.add_instruction(op, args);
            });
Paul's avatar
Paul committed
373
374
    }

375
    void add_variadic_op(const std::string& onnx_name, const std::string& op_name)
Khalique's avatar
Khalique committed
376
    {
377
        add_op(onnx_name, [this, op_name](const node_info&, std::vector<instruction_ref> args) {
Khalique's avatar
Khalique committed
378
            return std::accumulate(std::next(args.begin()),
Khalique's avatar
Khalique committed
379
380
                                   args.end(),
                                   args.front(),
381
382
                                   [this, op_name](instruction_ref a, instruction_ref b) {
                                       return add_broadcastable_binary_op(a, b, op_name);
Khalique's avatar
Khalique committed
383
                                   });
Khalique's avatar
Khalique committed
384
        });
Khalique's avatar
Khalique committed
385
386
    }

kahmed10's avatar
kahmed10 committed
387
388
389
390
391
392
393
394
395
396
397
398
399
400
    template <class T>
    std::vector<int64_t> to_int64_vector(const std::vector<T>& input_vector)
    {
        std::vector<int64_t> output_vector(input_vector.begin(), input_vector.end());
        return output_vector;
    }

    instruction_ref
    add_bias(const std::vector<instruction_ref>& args, instruction_ref curr_ins, uint64_t axis)
    {
        if(args.size() == 3)
        {
            auto bias_bcast =
                prog.add_instruction(op::broadcast{axis, curr_ins->get_shape().lens()}, args[2]);
401
            return prog.add_instruction(make_op("add"), curr_ins, bias_bcast);
kahmed10's avatar
kahmed10 committed
402
403
404
405
        }
        return curr_ins;
    }

406
    static bool is_asym_padding(const std::vector<int64_t>& padding)
407
    {
408
409
410
411
412
413
414
        assert(padding.size() % 2 == 0);
        size_t pad_ndims = padding.size() / 2;

        for(size_t i = 0; i < pad_ndims; i++)
        {
            if(padding[i] != padding[i + pad_ndims])
            {
kahmed10's avatar
kahmed10 committed
415
                return true;
416
417
            }
        }
kahmed10's avatar
kahmed10 committed
418
419
        return false;
    }
420

kahmed10's avatar
kahmed10 committed
421
422
    void check_asym_padding(instruction_ref& ins,
                            const std::vector<int64_t>& padding,
423
                            value& v,
424
425
                            int count_include_pad = 0,
                            float pad_val         = 0)
kahmed10's avatar
kahmed10 committed
426
427
428
429
430
    {
        size_t pad_ndims  = padding.size() / 2;
        auto left_pad_it  = padding.begin();
        auto right_pad_it = left_pad_it + pad_ndims;

431
        if(is_asym_padding(padding) or count_include_pad == 1)
432
        {
433
434
435
436
437
438
            std::vector<int64_t> asym_pads{0, 0, 0, 0}; // don't pad N and C
            // add left pads
            asym_pads.insert(asym_pads.begin() + 2, left_pad_it, right_pad_it);
            // add right pads
            asym_pads.insert(asym_pads.begin() + pad_ndims + 4, right_pad_it, padding.end());
            ins = prog.add_instruction(op::pad{asym_pads, pad_val}, ins);
439
440
441
        }
        else
        {
442
            v["padding"] = std::vector<size_t>(left_pad_it, right_pad_it);
443
444
445
        }
    }

446
447
    instruction_ref
    parse_clip(const std::string&, node_info info, std::vector<instruction_ref> args)
Khalique's avatar
Khalique committed
448
    {
kahmed10's avatar
kahmed10 committed
449
450
451
452
453
454
        auto input_lens = args[0]->get_shape().lens();
        instruction_ref min_arg;
        instruction_ref max_arg;
        bool min_used = false;
        bool max_used = false;

Shucai Xiao's avatar
Shucai Xiao committed
455
        if(args.size() == 3 and args[2]->name() != "undefined")
Khalique's avatar
Khalique committed
456
        {
kahmed10's avatar
kahmed10 committed
457
458
            max_arg  = args[2];
            max_used = true;
Khalique's avatar
Khalique committed
459
        }
Shucai Xiao's avatar
Shucai Xiao committed
460
461

        if(args.size() >= 2 and args[1]->name() != "undefined")
Khalique's avatar
Khalique committed
462
        {
kahmed10's avatar
kahmed10 committed
463
464
465
466
467
468
469
470
471
472
473
474
475
            min_arg  = args[1];
            min_used = true;
        }
        // if using previous opset for attributes
        else if(contains(info.attributes, "min") and contains(info.attributes, "max"))
        {

            float min_val = parse_value(info.attributes.at("min")).at<float>();
            float max_val = parse_value(info.attributes.at("max")).at<float>();
            min_arg       = prog.add_literal(min_val);
            max_arg       = prog.add_literal(max_val);
            min_used      = true;
            max_used      = true;
Khalique's avatar
Khalique committed
476
        }
kahmed10's avatar
kahmed10 committed
477
478

        if(min_used)
Shucai Xiao's avatar
Shucai Xiao committed
479
        {
kahmed10's avatar
kahmed10 committed
480
            min_arg = prog.add_instruction(op::multibroadcast{input_lens}, min_arg);
Shucai Xiao's avatar
Shucai Xiao committed
481
        }
kahmed10's avatar
kahmed10 committed
482
483

        if(max_used)
Shucai Xiao's avatar
Shucai Xiao committed
484
        {
kahmed10's avatar
kahmed10 committed
485
            max_arg = prog.add_instruction(op::multibroadcast{input_lens}, max_arg);
Shucai Xiao's avatar
Shucai Xiao committed
486
        }
kahmed10's avatar
kahmed10 committed
487
488

        if(min_used and max_used)
Shucai Xiao's avatar
Shucai Xiao committed
489
        {
490
            return prog.add_instruction(make_op("clip"), args[0], min_arg, max_arg);
Shucai Xiao's avatar
Shucai Xiao committed
491
492
493
494
495
496
497
        }
        else if(max_used)
        {
            return prog.add_instruction(make_op("min"), args[0], max_arg);
        }
        else if(min_used)
        {
498
            return prog.add_instruction(make_op("max"), args[0], min_arg);
Shucai Xiao's avatar
Shucai Xiao committed
499
500
501
502
503
        }
        else
        {
            return prog.add_instruction(make_op("identity"), args[0]);
        }
Shucai Xiao's avatar
Shucai Xiao committed
504
505
    }

506
507
508
509
    instruction_ref parse_arg_op(const std::string&,
                                 const std::string& op_name,
                                 node_info info,
                                 std::vector<instruction_ref> args)
510
    {
511
        int64_t axis = 0;
512
        if(contains(info.attributes, "axis"))
513
        {
514
            axis = static_cast<int64_t>(parse_value(info.attributes.at("axis")).at<int>());
515
516
        }

Shucai Xiao's avatar
Shucai Xiao committed
517
        int keep_dims = 1;
518
        if(contains(info.attributes, "keepdims"))
Shucai Xiao's avatar
Shucai Xiao committed
519
        {
520
            keep_dims = parse_value(info.attributes.at("keepdims")).at<int>();
Shucai Xiao's avatar
Shucai Xiao committed
521
522
        }

Shucai Xiao's avatar
Shucai Xiao committed
523
        if(keep_dims == 0)
524
        {
525
            auto ins = prog.add_instruction(make_op(op_name, {{"axis", axis}}), std::move(args));
526
            return prog.add_instruction(op::squeeze{{axis}}, ins);
527
528
529
        }
        else
        {
530
            return prog.add_instruction(make_op(op_name, {{"axis", axis}}), std::move(args));
531
        }
532
533
    }

kahmed10's avatar
kahmed10 committed
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
    void calc_reflect_indices(std::vector<int>& indices, const int64_t num_dims)
    {
        int k         = 0;
        bool reversed = false;
        // in reflect padding, if the num_pads > num_dims,
        // compute the extra pad indices periodically, ex. ( 1, 2, 3, 2, 1, 0)
        for(int& idx : indices)
        {
            if(k == num_dims - 1)
                reversed = true;
            if(k == 0)
                reversed = false;
            if(reversed)
                k--;
            else
                k++;
            idx = k;
        }
    }

    instruction_ref reflect_pad(const std::vector<int64_t>& pads, instruction_ref input)
    {
        size_t num_dims = pads.size() / 2;
        std::vector<int> ldims(pads.begin(), pads.begin() + num_dims);
        std::vector<int> rdims(pads.begin() + num_dims, pads.end());
        assert(ldims.size() == rdims.size());

        std::vector<int64_t> axes(num_dims);
        std::iota(axes.begin(), axes.end(), int64_t{0});

        // iterate over dimensions, starting from lowest dimension
        for(int64_t i = num_dims - 1; i >= 0; i--)
        {
            auto axis   = i;
            auto lcount = ldims.at(i);
            auto rcount = rdims.at(i);
            if(lcount == 0 and rcount == 0) // no padding for current dim
                continue;

            // calculate starts and ends for each iteration since shape may change
            std::vector<size_t> dims = input->get_shape().lens();
            std::vector<int64_t> starts(axes.size(), 0);
            std::vector<int64_t> ends(dims.begin(), dims.end());
            std::vector<instruction_ref> slices;

            auto starts_it = starts.begin() + i;
            auto ends_it   = ends.begin() + i;
            auto dims_it   = dims.begin() + i;

            std::vector<int> l_indices(lcount);
            std::vector<int> r_indices(rcount);

            // compute slice indices in a periodic fashion
            calc_reflect_indices(l_indices, *dims_it);
            calc_reflect_indices(r_indices, *dims_it);

            for(int idx : l_indices)
            {
                *starts_it = idx;
                *ends_it   = *starts_it + 1;
                slices.push_back(prog.add_instruction(op::slice{axes, starts, ends}, input));
            }
            // when padding on the left side, the outermost pad should be at the beginning
            std::reverse(slices.begin(), slices.end());
            slices.push_back(input);
            for(int idx : r_indices)
            {
                *starts_it = *dims_it - idx - 1;
                *ends_it   = *starts_it + 1;
                slices.push_back(prog.add_instruction(op::slice{axes, starts, ends}, input));
            }
            input = prog.add_instruction(op::concat{axis}, slices);
        }
        return input;
    }

610
611
612
613
614
615
616
617
618
    void check_attr_sizes(size_t kdims, size_t attr_size, const std::string& error_msg)
    {
        if(kdims != attr_size)
        {
            MIGRAPHX_THROW(error_msg + " k-dims: " + to_string(kdims) +
                           " attribute size: " + to_string(attr_size));
        }
    }

619
    void recalc_conv_attributes(value& v, size_t kdims)
620
    {
621
        if(v["padding"].size() != kdims)
622
        {
623
624
            v["padding"].resize(kdims);
            std::fill_n(v["padding"].begin(), kdims, 0);
625
        }
626
        if(v["stride"].size() != kdims)
627
        {
628
629
            v["stride"].resize(kdims);
            std::fill_n(v["stride"].begin(), kdims, 1);
630
        }
631
        if(v["dilation"].size() != kdims)
632
        {
633
634
            v["dilation"].resize(kdims);
            std::fill_n(v["dilation"].begin(), kdims, 1);
635
636
637
        }
    }

638
    static void cal_auto_padding_size(node_info info,
639
                                      value& v,
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
                                      const std::vector<std::size_t>& k_lens,
                                      const std::vector<std::size_t>& dilation,
                                      const std::vector<std::size_t>& in_lens,
                                      std::vector<int64_t>& paddings)
    {
        size_t kdims = in_lens.size() - 2;
        assert(k_lens.size() == kdims and dilation.size() == kdims);

        if(!contains(info.attributes, "auto_pad"))
        {
            return;
        }

        auto auto_pad = info.attributes["auto_pad"].s();
        if(auto_pad.find("SAME") != std::string::npos)
        {
            bool is_same_upper = (auto_pad.find("SAME_UPPER") != std::string::npos);
            paddings.resize(2 * kdims);

            for(size_t i = 0; i < paddings.size() / 2; i++)
            {
                calculate_padding(i,
                                  paddings,
                                  in_lens[i + 2],
664
                                  v["stride"][i].to<int64_t>(),
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
                                  dilation[i],
                                  k_lens[i],
                                  is_same_upper);
            }
        }
    }

    static void check_padding_mode(node_info info, const std::string& op_name)
    {
        // ensure pads availabe only when auto_pad is "NOT_SET"
        if(contains(info.attributes, "pads") and contains(info.attributes, "auto_pad"))
        {
            auto s = info.attributes["auto_pad"].s();
            if(to_upper(s) != "NOTSET")
            {
                MIGRAPHX_THROW("PARSE_" + op_name +
                               ": auto_pad and padding cannot be specified simultaneously");
            }
        }
    }

686
687
688
689
    instruction_ref parse_conv(const std::string&,
                               const std::string& op_name,
                               node_info info,
                               std::vector<instruction_ref> args)
Paul's avatar
Paul committed
690
    {
691
692
        auto op      = make_op(op_name);
        auto values  = op.to_value();
693
694
        auto l0      = args[0];
        auto weights = args[1];
695
696
697
698
        auto in_lens = l0->get_shape().lens();
        assert(in_lens.size() > 2);
        auto kdims = in_lens.size() - 2;

699
700
701
        // ensure pads availabe only when auto_pad is "NOT_SET"
        check_padding_mode(info, "CONV");

702
        if(contains(info.attributes, "strides"))
Paul's avatar
Paul committed
703
        {
704
705
706
            values["stride"].clear();
            copy(info.attributes["strides"].ints(), std::back_inserter(values["stride"]));
            check_attr_sizes(kdims, values["stride"].size(), "PARSE_CONV: inconsistent strides");
Paul's avatar
Paul committed
707
        }
708
        if(contains(info.attributes, "dilations"))
Paul's avatar
Paul committed
709
        {
710
711
712
713
            values["dilation"].clear();
            copy(info.attributes["dilations"].ints(), std::back_inserter(values["dilation"]));
            check_attr_sizes(
                kdims, values["dilation"].size(), "PARSE_CONV: inconsistent dilations");
Paul's avatar
Paul committed
714
        }
715
716
717
718

        std::vector<int64_t> padding;
        if(contains(info.attributes, "pads"))
        {
719
            values["padding"].clear();
720
721
722
723
            copy(info.attributes["pads"].ints(), std::back_inserter(padding));
            check_attr_sizes(kdims, padding.size() / 2, "PARSE_CONV: inconsistent paddings");
        }

724
        if(contains(info.attributes, "auto_pad"))
725
        {
726
727
            auto weight_lens = weights->get_shape().lens();
            std::vector<std::size_t> k_lens(weight_lens.begin() + 2, weight_lens.end());
728
729
730
731
732
733
            cal_auto_padding_size(info,
                                  values,
                                  k_lens,
                                  values["dilation"].to_vector<std::size_t>(),
                                  in_lens,
                                  padding);
Shucai Xiao's avatar
Shucai Xiao committed
734
735
736
737
738
            auto auto_pad = info.attributes["auto_pad"].s();
            if(auto_pad.find("SAME") != std::string::npos)
            {
                values["padding_mode"] = to_value(op::padding_mode_t::same);
            }
739
        }
740
        check_asym_padding(l0, padding, values);
741

742
        if(contains(info.attributes, "group"))
Khalique's avatar
Khalique committed
743
        {
744
            values["group"] = parse_value(info.attributes.at("group")).at<int>();
Khalique's avatar
Khalique committed
745
        }
kahmed10's avatar
kahmed10 committed
746

747
        recalc_conv_attributes(values, kdims);
748

749
        op.from_value(values);
kahmed10's avatar
kahmed10 committed
750
751
752
753
        auto l1 = prog.add_instruction(op, l0, args[1]);
        return add_bias(args, l1, 1);
    }

754
755
    instruction_ref
    parse_conv_transpose(const std::string&, node_info info, std::vector<instruction_ref> args)
kahmed10's avatar
kahmed10 committed
756
    {
757
758
759
        operation op = make_op("deconvolution");
        value values = op.to_value();
        // op::deconvolution op;
kahmed10's avatar
kahmed10 committed
760
761
        auto l0 = args[0];
        std::vector<std::int64_t> padding;
kahmed10's avatar
kahmed10 committed
762
763
764
765
766
        bool asym_padding = false;
        auto in_lens      = l0->get_shape().lens();
        assert(in_lens.size() > 2);
        auto kdims = in_lens.size() - 2;

767
768
769
        // ensure pads availabe only when auto_pad is "NOT_SET"
        check_padding_mode(info, "CONV_TRANSPOSE");

770
        if(contains(info.attributes, "pads"))
kahmed10's avatar
kahmed10 committed
771
        {
772
            copy(info.attributes["pads"].ints(), std::back_inserter(padding));
kahmed10's avatar
kahmed10 committed
773
774
775
776

            asym_padding = is_asym_padding(padding);

            if(not asym_padding)
kahmed10's avatar
kahmed10 committed
777
            {
kahmed10's avatar
kahmed10 committed
778
779
                size_t pad_ndims = padding.size() / 2;
                check_attr_sizes(kdims, pad_ndims, "PARSE_CONV_TRANSPOSE: inconsistent paddings");
780
                values["padding"].clear();
kahmed10's avatar
kahmed10 committed
781
782
                std::transform(padding.begin(),
                               padding.begin() + pad_ndims,
783
                               std::back_inserter(values["padding"]),
kahmed10's avatar
kahmed10 committed
784
                               [](auto pad_val) { return pad_val; });
kahmed10's avatar
kahmed10 committed
785
786
            }
        }
787
        if(contains(info.attributes, "strides"))
kahmed10's avatar
kahmed10 committed
788
        {
789
790
791
792
            values["stride"].clear();
            copy(info.attributes["strides"].ints(), std::back_inserter(values["stride"]));
            check_attr_sizes(
                kdims, values["stride"].size(), "PARSE_CONV_TRANSPOSE: inconsistent strides");
kahmed10's avatar
kahmed10 committed
793
        }
794
        if(contains(info.attributes, "dilations"))
Paul's avatar
Paul committed
795
        {
796
797
            values["dilation"].clear();
            copy(info.attributes["dilations"].ints(), std::back_inserter(values["dilation"]));
kahmed10's avatar
kahmed10 committed
798
            check_attr_sizes(
799
                kdims, values["dilation"].size(), "PARSE_CONV_TRANSPOSE: inconsistent dilations");
Paul's avatar
Paul committed
800
        }
801
        if(contains(info.attributes, "auto_pad"))
kahmed10's avatar
kahmed10 committed
802
        {
803
804
            auto s = info.attributes["auto_pad"].s();
            if(contains(info.attributes, "pads") and to_upper(s) != "NOTSET")
kahmed10's avatar
kahmed10 committed
805
            {
kahmed10's avatar
kahmed10 committed
806
807
                MIGRAPHX_THROW("PARSE_CONV_TRANSPOSE: auto_pad and padding cannot be specified "
                               "simultaneously");
kahmed10's avatar
kahmed10 committed
808
809
810
811
            }

            if(s.find("SAME") != std::string::npos)
            {
812
                values["padding_mode"] = to_value(op::padding_mode_t::same);
kahmed10's avatar
kahmed10 committed
813
814
815
            }
        }

816
        if(contains(info.attributes, "group"))
kahmed10's avatar
kahmed10 committed
817
        {
818
            values["group"] = parse_value(info.attributes.at("group")).at<int>();
kahmed10's avatar
kahmed10 committed
819
820
        }

821
        recalc_conv_attributes(values, kdims);
kahmed10's avatar
kahmed10 committed
822

823
        op.from_value(values);
kahmed10's avatar
kahmed10 committed
824
825
        auto l1                   = prog.add_instruction(op, l0, args[1]);
        std::vector<int64_t> dims = to_int64_vector(l1->get_shape().lens());
kahmed10's avatar
kahmed10 committed
826
827
        std::vector<int64_t> curr_shape(dims.begin() + 2, dims.end());
        if(asym_padding)
kahmed10's avatar
kahmed10 committed
828
        {
kahmed10's avatar
kahmed10 committed
829
830
831
832
833
834
835
836
837
838
839
840
            std::vector<int64_t> axes(kdims);
            std::iota(axes.begin(), axes.end(), 2); // ignore first 2 dims

            auto pad_kdim_start = padding.begin() + kdims;
            std::vector<int64_t> starts(padding.begin(), pad_kdim_start);

            std::vector<int64_t> ends{};
            std::transform(curr_shape.begin(),
                           curr_shape.end(),
                           pad_kdim_start,
                           std::back_inserter(ends),
                           [](auto curr_dim, auto pad_dim) { return curr_dim - pad_dim; });
kahmed10's avatar
kahmed10 committed
841

kahmed10's avatar
kahmed10 committed
842
            l1 = prog.add_instruction(op::slice{axes, starts, ends}, l1);
kahmed10's avatar
kahmed10 committed
843
844
        }

845
        if(contains(info.attributes, "output_padding"))
kahmed10's avatar
kahmed10 committed
846
        {
kahmed10's avatar
kahmed10 committed
847
848
            size_t non_kdims = dims.size() * 2 - kdims;
            std::vector<int64_t> output_padding(non_kdims, 0);
849
            copy(info.attributes["output_padding"].ints(), std::back_inserter(output_padding));
kahmed10's avatar
kahmed10 committed
850
851
852
853
            check_attr_sizes(kdims,
                             output_padding.size() - non_kdims,
                             "PARSE_CONV_TRANSPOSE: inconsistent output padding");
            l1 = prog.add_instruction(op::pad{output_padding}, l1);
kahmed10's avatar
kahmed10 committed
854
855
        }

856
        if(contains(info.attributes, "output_shape"))
kahmed10's avatar
kahmed10 committed
857
858
        {
            std::vector<int64_t> output_shape;
859
            copy(info.attributes["output_shape"].ints(), std::back_inserter(output_shape));
kahmed10's avatar
kahmed10 committed
860
861
862
863
            check_attr_sizes(
                kdims, output_shape.size(), "PARSE_CONV_TRANSPOSE: inconsistent output shape");
            dims = to_int64_vector(l1->get_shape().lens());
            copy(dims.begin() + 2, dims.end(), curr_shape.begin());
kahmed10's avatar
kahmed10 committed
864
865
            if(curr_shape != output_shape)
            {
kahmed10's avatar
kahmed10 committed
866
867
868
869
870
871
                std::vector<int64_t> target_padding(dims.size() * 2 - kdims, 0);
                std::transform(output_shape.begin(),
                               output_shape.end(),
                               curr_shape.begin(),
                               std::back_inserter(target_padding),
                               [](auto out_dim, auto curr_dim) { return out_dim - curr_dim; });
kahmed10's avatar
kahmed10 committed
872
873
874
875
876
                l1 = prog.add_instruction(op::pad{target_padding}, l1);
            }
        }

        return add_bias(args, l1, 1);
Paul's avatar
Paul committed
877
    }
Paul's avatar
Paul committed
878

879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
    static void
    tune_padding_to_symmetric(int64_t& left, int64_t& right, const int stride, int64_t& s_start)
    {
        s_start = 0;
        if(left > right)
        {
            right = left;
        }
        else if(left < right)
        {
            auto diff = right - left;
            s_start   = (diff + stride - 1) / stride;
            left      = left + s_start * stride;
            right     = left;
        }
    }

896
    static void tune_padding_size(const value& v,
897
898
899
900
901
                                  std::vector<int64_t>& padding,
                                  int count_include_pad,
                                  std::vector<int64_t>& s_start)
    {
        // maxpooling or count_include_pad is 1, no change is required.
902
        if(v.at("mode").to<std::string>() == "max" or count_include_pad == 1)
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
        {
            return;
        }

        // if padding is symmetric, return directly
        if(!is_asym_padding(padding))
        {
            return;
        }

        // asymmetric padding, make it symmetric
        std::size_t n_dims = padding.size() / 2;
        s_start.resize(n_dims);
        for(std::size_t i = 0; i < n_dims; ++i)
        {
918
919
            tune_padding_to_symmetric(
                padding[i], padding[i + n_dims], v.at("stride")[i].to<int64_t>(), s_start[i]);
920
921
922
        }
    }

923
924
    instruction_ref
    parse_pooling(const std::string& name, node_info info, std::vector<instruction_ref> args)
Paul's avatar
Paul committed
925
    {
926
927
928
929
930
        std::string mode = ends_with(name, "MaxPool") ? "max" : "average";
        operation op     = make_op("pooling", {{"mode", mode}});
        value values     = op.to_value();
        auto l0          = args[0];
        auto in_lens     = l0->get_shape().lens();
931
932
933
        assert(in_lens.size() > 2);
        auto kdims = in_lens.size() - 2;

Khalique's avatar
Khalique committed
934
        if(starts_with(name, "Global"))
935
        {
936
            values["lengths"] = std::vector<size_t>(in_lens.begin() + 2, in_lens.end());
937
        }
938

939
940
        // does not support ceil_mode
        if(contains(info.attributes, "ceil_mode"))
Paul's avatar
Paul committed
941
        {
Shucai Xiao's avatar
Shucai Xiao committed
942
            values["ceil_mode"] = static_cast<bool>(info.attributes.at("ceil_mode").i());
943
        }
944

945
946
947
948
949
950
        // count include padding, if count include pad is 1, we always use
        // explicit pad
        int count_include_pad = 0;
        if(contains(info.attributes, "count_include_pad"))
        {
            count_include_pad = info.attributes.at("count_include_pad").i();
Paul's avatar
Paul committed
951
        }
952

953
        if(contains(info.attributes, "strides"))
Paul's avatar
Paul committed
954
        {
955
956
957
            values["stride"].clear();
            copy(info.attributes["strides"].ints(), std::back_inserter(values["stride"]));
            check_attr_sizes(kdims, values["stride"].size(), "PARSE_POOLING: inconsistent strides");
Paul's avatar
Paul committed
958
        }
959
        if(contains(info.attributes, "kernel_shape"))
Paul's avatar
Paul committed
960
        {
961
962
963
964
            values["lengths"].clear();
            copy(info.attributes["kernel_shape"].ints(), std::back_inserter(values["lengths"]));
            check_attr_sizes(
                kdims, values["lengths"].size(), "PARSE_POOLING: inconsistent lengths");
Paul's avatar
Paul committed
965
        }
966

967
968
969
970
        // ensure pads availabe only when auto_pad is "NOT_SET"
        check_padding_mode(info, "POOLING");

        std::vector<int64_t> paddings;
971
        float pad_val = ((mode == "max") ? std::numeric_limits<float>::lowest() : 0.0f);
972
973
        if(contains(info.attributes, "pads"))
        {
974
            values["padding"].clear();
975
976
977
978
979
            copy(info.attributes["pads"].ints(), std::back_inserter(paddings));
            check_attr_sizes(
                kdims, paddings.size() / 2, "PARSE_POOLING: inconsistent explicit paddings");
        }

980
        if(contains(info.attributes, "auto_pad"))
981
        {
982
            values["padding"].clear();
983
            // return paddings could be empty, then setting to 0 for no padding
984
985
986
987
988
989
            cal_auto_padding_size(info,
                                  values,
                                  values["lengths"].to_vector<std::size_t>(),
                                  {1, 1},
                                  in_lens,
                                  paddings);
990
        }
991

992
993
994
995
        if(paddings.size() != 2 * kdims)
        {
            paddings.resize(kdims * 2);
            std::fill_n(paddings.begin(), 2 * kdims, 0);
996
997
        }

998
        if(values["padding"].size() != kdims)
999
        {
1000
1001
            values["padding"].resize(kdims);
            std::fill_n(values["padding"].begin(), kdims, 0);
1002
        }
1003

1004
        if(values["stride"].size() != kdims)
1005
        {
1006
1007
            values["stride"].resize(kdims);
            std::fill_n(values["stride"].begin(), kdims, 1);
1008
        }
1009
1010
1011
1012
1013
        // used to calculate the supposed output shape
        std::vector<int64_t> orig_padding(paddings.begin(), paddings.end());

        std::vector<int64_t> slice_start;
        std::vector<int64_t> slice_end;
1014
        tune_padding_size(values, paddings, count_include_pad, slice_start);
1015
1016
1017
1018
1019
1020
1021
1022

        if(!slice_start.empty())
        {
            // calculate expected output shape
            orig_padding.insert(orig_padding.begin() + kdims, 2, 0);
            orig_padding.insert(orig_padding.begin(), 2, 0);
            op::pad pad{orig_padding, 0.0f};
            shape padded_shape = pad.compute_shape({l0->get_shape()});
1023
            auto out_lens      = make_op("pooling", values).compute_shape({padded_shape}).lens();
1024

1025
1026
1027
1028
1029
1030
1031
1032
1033
            // compute slice_end information
            slice_end.resize(slice_start.size());
            std::transform(out_lens.begin() + 2,
                           out_lens.end(),
                           slice_start.begin(),
                           slice_end.begin(),
                           [](auto i, auto j) { return i + j; });
        }

1034
        check_asym_padding(l0, paddings, values, count_include_pad, pad_val);
1035
        in_lens = l0->get_shape().lens();
1036
1037
        for(size_t i = 0; i < kdims; i++)
        {
1038
1039
            if(values["lengths"][i].to<int64_t>() >
               in_lens[i + 2] + 2 * values["padding"][i].to<int64_t>())
1040
            {
1041
                MIGRAPHX_THROW("PARSE_POOLING: kernel shape is too large");
1042
1043
            }
        }
1044
        op.from_value(values);
1045
1046
1047
1048
1049
1050
        auto l1 = prog.add_instruction(op, l0);
        if(!slice_start.empty())
        {
            std::vector<int64_t> axes(kdims);
            std::iota(axes.begin(), axes.end(), 2);
            l1 = prog.add_instruction(op::slice{axes, slice_start, slice_end}, l1);
1051
1052
        }

1053
        return l1;
Paul's avatar
Paul committed
1054
1055
    }

Paul's avatar
Paul committed
1056
    instruction_ref
1057
    parse_reshape(const std::string&, node_info info, std::vector<instruction_ref> args)
Paul's avatar
Paul committed
1058
    {
1059
        op::reshape op;
Paul's avatar
Paul committed
1060
1061
        if(args.size() == 1)
        {
1062
            literal s = parse_value(info.attributes.at("shape"));
1063
            s.visit([&](auto v) { copy(v, std::back_inserter(op.dims)); });
Paul's avatar
Paul committed
1064
1065
1066
        }
        if(args.size() == 2)
        {
Paul's avatar
Paul committed
1067
            auto s = args[1]->eval();
Shucai Xiao's avatar
Shucai Xiao committed
1068
            check_arg_empty(s, "Reshape: dynamic shape is not supported");
Paul's avatar
Paul committed
1069
            s.visit([&](auto v) { copy(v, std::back_inserter(op.dims)); });
Paul's avatar
Paul committed
1070
        }
1071

Shucai Xiao's avatar
Shucai Xiao committed
1072
        return prog.add_instruction(op, make_contiguous(args[0]));
Paul's avatar
Paul committed
1073
1074
    }

Shucai Xiao's avatar
Shucai Xiao committed
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
    static const auto& get_nearest_op(const std::string& mode)
    {
        using nearest_op = std::function<std::size_t(std::size_t, double)>;
        static std::unordered_map<std::string, nearest_op> const nearest_ops = {
            {"round_prefer_floor",
             [=](std::size_t d_in, double val) {
                 val = std::max(0.0, std::min(d_in - 1.0, val));
                 return static_cast<std::size_t>(std::ceil((val - 0.5)));
             }},
            {"round_prefer_ceil",
             [=](std::size_t d_in, double val) {
                 val = std::max(0.0, std::min(d_in - 1.0, val));
                 return static_cast<std::size_t>(std::round((val)));
             }},
            {"floor",
             [=](std::size_t d_in, double val) {
                 val = std::max(0.0, std::min(d_in - 1.0, val));
                 return static_cast<std::size_t>(std::floor((val)));
             }},
            {"ceil", [=](std::size_t d_in, double val) {
                 val = std::max(0.0, std::min(d_in - 1.0, val));
                 return static_cast<std::size_t>(std::ceil((val)));
             }}};

        if(!contains(nearest_ops, mode))
        {
            MIGRAPHX_THROW("PARSE_RESIZE: nearest_mode " + mode + " not supported!");
        }

        return nearest_ops.at(mode);
    }

    static const auto& get_original_idx_op(const std::string& mode)
    {
        using original_idx_op =
            std::function<double(std::size_t, std::size_t, std::size_t, double)>;
        static std::unordered_map<std::string, original_idx_op> const idx_ops = {
            {"half_pixel",
             [=](std::size_t, std::size_t, std::size_t idx, double scale) {
                 return (idx + 0.5) / scale - 0.5;
             }},
            {"pytorch_half_pixel",
             [=](std::size_t, std::size_t l_out, std::size_t idx, double scale) {
                 return l_out > 1 ? (idx + 0.5) / scale - 0.5 : 0.0;
             }},
            {"align_corners",
             [=](std::size_t l_in, std::size_t l_out, std::size_t idx, double) {
                 return 1.0 * idx * (l_in - 1.0) / (l_out - 1.0);
             }},
            {"asymmetric",
             [=](std::size_t, std::size_t, std::size_t idx, double scale) { return idx / scale; }},
            {"tf_half_pixel_for_nn", [=](std::size_t, std::size_t, std::size_t idx, double scale) {
                 return (idx + 0.5) / scale;
             }}};

        if(!contains(idx_ops, mode))
        {
            MIGRAPHX_THROW("PARSE_RESIZE: coordinate_transformation_mode " + mode +
                           " not supported!");
        }

        return idx_ops.at(mode);
    }

    instruction_ref
    parse_resize(const std::string&, const node_info& info, std::vector<instruction_ref> args)
    {
        std::string coord_trans_mode = "half_pixel";
        if(contains(info.attributes, "coordinate_transformation_mode"))
        {
            coord_trans_mode = info.attributes.at("coordinate_transformation_mode").s();
            // does not support transformation mode "tf_crop_and_resize"
            if(coord_trans_mode == "tf_crop_and_resize")
            {
                MIGRAPHX_THROW("PARSE_RESIZE: \"tf_crop_and_resize\" mode is not supported!");
            }
        }

        // mode: only nearest mode is supported for now
        if(contains(info.attributes, "mode"))
        {
            auto mode = info.attributes.at("mode").s();
            if(mode != "nearest")
            {
                MIGRAPHX_THROW("PARSE_RESIZE: only nearest mode is supported!");
            }
        }

        // nearest mode
        std::string nearest_mode = "round_prefer_floor";
        if(contains(info.attributes, "nearest_mode"))
        {
            nearest_mode = info.attributes.at("nearest_mode").s();
        }

        // check exclude_outside, only support 0
        if(contains(info.attributes, "exclude_outside"))
        {
            int exclude_outside = info.attributes.at("exclude_outside").i();
            if(exclude_outside == 1)
            {
                MIGRAPHX_THROW("PARSE_RESIZE: exclude_outside 1 is not supported!");
            }
        }

        // input data shape info
        auto in_s    = args[0]->get_shape();
        auto in_lens = in_s.lens();

        // output shape is explicitly specified
        std::vector<std::size_t> out_lens(in_lens.size());

        // scale
        std::vector<double> vec_scale;

        // output size is specified in input, so use it as output size
        if(args.size() == 4 and args.back()->name() != "undefined")
        {
            auto arg_out_s = args[3]->eval();
            check_arg_empty(arg_out_s, "PARSE_RESIZE: dynamic output size is not supported!");
            arg_out_s.visit([&](auto ol) { out_lens.assign(ol.begin(), ol.end()); });

            if(out_lens.size() != in_lens.size())
            {
                MIGRAPHX_THROW("PARSE_RESIZE: specified output size does not match input size");
            }

            // compute the scale
            vec_scale.resize(in_lens.size());
            std::transform(in_lens.begin(),
                           in_lens.end(),
                           out_lens.begin(),
                           vec_scale.begin(),
                           [](auto iss, auto oss) { return 1.0 * oss / iss; });
        }
        // need to compute the output lens from input
        else
        {
            auto arg_scale = args[2]->eval();
            check_arg_empty(arg_scale, "PARSE_RESIZE: dynamic input scale is not supported!");

            arg_scale.visit([&](auto v) { vec_scale.assign(v.begin(), v.end()); });
            if(in_lens.size() != vec_scale.size())
            {
                MIGRAPHX_THROW("PARSE_RESIZE: ranks of input and scale are different!");
            }

            std::transform(
                in_lens.begin(),
                in_lens.end(),
                vec_scale.begin(),
                out_lens.begin(),
                [&](auto idx, auto scale) { return static_cast<std::size_t>(idx * scale); });
        }

        shape out_s{in_s.type(), out_lens};
        std::vector<int> ind(out_s.elements());

        // map out_idx to in_idx
        auto nearest_op = get_nearest_op(nearest_mode);
        auto idx_op     = get_original_idx_op(coord_trans_mode);

        shape_for_each(out_s, [&](auto idx) {
            auto in_idx = idx;
            for(auto ii = 0; ii < in_lens.size(); ++ii)
            {
                auto idx_val = idx_op(in_lens[ii], out_lens[ii], in_idx[ii], vec_scale[ii]);
                in_idx[ii]   = nearest_op(in_lens[ii], idx_val);
            }

            ind[out_s.index(idx)] = static_cast<int64_t>(in_s.index(in_idx));
        });

        // reshape input to one-dimension
        std::vector<int64_t> rsp_lens = {static_cast<int64_t>(in_s.elements())};
        shape ind_s{shape::int32_type, out_lens};
        auto rsp     = prog.add_instruction(make_op("reshape", {{"dims", rsp_lens}}), args[0]);
        auto ins_ind = prog.add_literal(literal(ind_s, ind));
        return prog.add_instruction(make_op("gather", {{"axis", 0}}), rsp, ins_ind);
    }

Shucai Xiao's avatar
Shucai Xiao committed
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
    instruction_ref
    parse_gather_elements(const std::string&, node_info info, std::vector<instruction_ref> args)
    {
        int axis = 0;
        if(contains(info.attributes, "axis"))
        {
            axis = parse_value(info.attributes.at("axis")).at<int>();
        }

        // standardize input data and index
        auto arg_data = make_contiguous(args[0]);
        auto arg_ind  = make_contiguous(args[1]);

        auto data_s = arg_data->get_shape();
        auto ind_s  = arg_ind->get_shape();

        if(data_s.lens().size() != ind_s.lens().size())
        {
            MIGRAPHX_THROW("PARSE_GATHER_ELEMENTS: input data and index must have the same rank!");
        }

        int n_rank     = static_cast<int>(data_s.lens().size());
        int tuned_axis = (axis < 0) ? (axis + n_rank) : axis;

        auto axis_stride      = data_s.strides()[tuned_axis];
        int64_t data_elem_num = static_cast<int64_t>(data_s.elements());
        // reshape the input data as one dimension and used as input data
        // to the gather operator
        arg_data = prog.add_instruction(op::reshape{{data_elem_num}}, arg_data);

        std::size_t elem_num = ind_s.elements();
        std::vector<int> ind_index(elem_num);
        std::iota(ind_index.begin(), ind_index.end(), 0);

        // convert index in input indices to that in input data
        std::vector<int> data_indices(elem_num);
        std::transform(ind_index.begin(), ind_index.end(), data_indices.begin(), [&](auto i) {
            return data_s.index(ind_s.multi(i));
        });

        std::vector<int> vec_axis_ind(elem_num);
        std::transform(ind_index.begin(), ind_index.end(), vec_axis_ind.begin(), [&](auto i) {
            return ind_s.multi(i)[tuned_axis];
        });

        auto l_shape_idx =
            prog.add_literal(literal(ind_s, data_indices.begin(), data_indices.end()));
        auto l_dim_idx = prog.add_literal(literal(ind_s, vec_axis_ind.begin(), vec_axis_ind.end()));
        auto l_stride  = prog.add_literal(literal{{ind_s.type(), {1}}, {axis_stride}});
        l_stride       = prog.add_instruction(op::multibroadcast{ind_s.lens()}, l_stride);
1306
1307
1308
        auto dim_diff  = prog.add_instruction(make_op("sub"), arg_ind, l_dim_idx);
        auto delta     = prog.add_instruction(make_op("mul"), dim_diff, l_stride);
        auto ind       = prog.add_instruction(make_op("add"), l_shape_idx, delta);
Shucai Xiao's avatar
Shucai Xiao committed
1309
1310
1311
1312
1313

        op::gather op{0};
        return prog.add_instruction(op, arg_data, ind);
    }

1314
    instruction_ref
1315
    parse_slice(const std::string&, node_info info, std::vector<instruction_ref> args)
1316
1317
    {
        op::slice op;
Shucai Xiao's avatar
Shucai Xiao committed
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339

        // slice can have up to 5 inputs, we first check the 5th one
        // to decide whether MIGRAPHX can handle this slice
        if(args.size() == 5)
        {
            migraphx::argument step_arg = args.back()->eval();
            check_arg_empty(step_arg, "PARSE_SLICE: cannot handle variable steps for slice");
            std::vector<int> steps;
            step_arg.visit([&](auto s) { steps.assign(s.begin(), s.end()); });
            if(!std::all_of(steps.begin(), steps.end(), [](auto s) { return s == 1; }))
            {
                MIGRAPHX_THROW("PARSE_SLICE: cannot handle step other than 1");
            }
        }

        if(args.size() >= 4)
        {
            migraphx::argument axes_arg = args.at(3)->eval();
            check_arg_empty(axes_arg, "PARSE_SLICE: cannot handle variable axes for slice");
            axes_arg.visit([&](auto s) { op.axes.assign(s.begin(), s.end()); });
        }
        else if(contains(info.attributes, "axes"))
1340
        {
1341
            literal s = parse_value(info.attributes.at("axes"));
1342
1343
            s.visit([&](auto v) { copy(v, std::back_inserter(op.axes)); });
        }
Shucai Xiao's avatar
Shucai Xiao committed
1344
1345

        if(args.size() >= 3)
Khalique's avatar
Khalique committed
1346
        {
Shucai Xiao's avatar
Shucai Xiao committed
1347
1348
1349
            migraphx::argument end_arg = args.at(2)->eval();
            check_arg_empty(end_arg, "PARSE_SLICE: cannot handle variable ends for slice");
            end_arg.visit([&](auto s) { op.ends.assign(s.begin(), s.end()); });
Khalique's avatar
Khalique committed
1350
        }
Shucai Xiao's avatar
Shucai Xiao committed
1351
        else if(contains(info.attributes, "ends"))
1352
        {
1353
1354
            literal s = parse_value(info.attributes.at("ends"));
            s.visit([&](auto v) { copy(v, std::back_inserter(op.ends)); });
1355
        }
Shucai Xiao's avatar
Shucai Xiao committed
1356
1357
1358
1359
1360
1361
1362
1363

        if(args.size() >= 2)
        {
            migraphx::argument start_arg = args.at(1)->eval();
            check_arg_empty(start_arg, "PARSE_SLICE: cannot handle variable starts for slice");
            start_arg.visit([&](auto s) { op.starts.assign(s.begin(), s.end()); });
        }
        else if(contains(info.attributes, "starts"))
1364
        {
1365
            literal s = parse_value(info.attributes.at("starts"));
1366
1367
            s.visit([&](auto v) { copy(v, std::back_inserter(op.starts)); });
        }
Shucai Xiao's avatar
Shucai Xiao committed
1368

kahmed10's avatar
kahmed10 committed
1369
1370
1371
1372
1373
1374
1375
        if(op.axes.empty())
        {
            std::vector<int64_t> axes(args[0]->get_shape().lens().size());
            std::iota(axes.begin(), axes.end(), int64_t{0});
            op.axes = axes;
        }

1376
1377
1378
        return prog.add_instruction(op, args[0]);
    }

1379
1380
    instruction_ref
    parse_constant(const std::string&, node_info info, const std::vector<instruction_ref>&)
Paul's avatar
Paul committed
1381
    {
1382
        literal v = parse_value(info.attributes.at("value"));
1383
        // return empty literal
Shucai Xiao's avatar
Shucai Xiao committed
1384
        if(v.get_shape().elements() == 0)
1385
1386
1387
1388
        {
            return prog.add_literal(literal{});
        }

1389
        auto dim_size = info.attributes.at("value").t().dims_size();
1390
1391
        // if dim_size is 0, it is a scalar
        if(dim_size == 0)
1392
        {
1393
            migraphx::shape scalar_shape{v.get_shape().type()};
1394
1395
1396
            return prog.add_literal(migraphx::literal{scalar_shape, v.data()});
        }

Paul's avatar
Paul committed
1397
1398
        return prog.add_literal(v);
    }
Paul's avatar
Paul committed
1399

Paul's avatar
Paul committed
1400
    instruction_ref
1401
    parse_gemm(const std::string&, node_info info, std::vector<instruction_ref> args)
Paul's avatar
Paul committed
1402
1403
    {
        float alpha = 1.0f;
Khalique's avatar
Khalique committed
1404
        float beta  = 1.0f;
Paul's avatar
Paul committed
1405
1406
        bool transa = false;
        bool transb = false;
1407
        if(contains(info.attributes, "alpha"))
Paul's avatar
Paul committed
1408
        {
1409
            alpha = parse_value(info.attributes.at("alpha")).at<float>();
Paul's avatar
Paul committed
1410
        }
1411
        if(contains(info.attributes, "beta"))
Paul's avatar
Paul committed
1412
        {
1413
            beta = parse_value(info.attributes.at("beta")).at<float>();
Paul's avatar
Paul committed
1414
        }
1415
        if(contains(info.attributes, "transA"))
Paul's avatar
Paul committed
1416
        {
1417
            transa = parse_value(info.attributes.at("transA")).at<bool>();
Paul's avatar
Paul committed
1418
        }
1419
        if(contains(info.attributes, "transB"))
Paul's avatar
Paul committed
1420
        {
1421
            transb = parse_value(info.attributes.at("transB")).at<bool>();
Paul's avatar
Paul committed
1422
        }
1423
1424
1425
1426
1427
1428

        std::vector<int64_t> perm(args[0]->get_shape().lens().size());
        std::iota(perm.begin(), perm.end(), int64_t{0});
        // swap the last two elements
        std::swap(*perm.rbegin(), *(perm.rbegin() + 1));

1429
1430
        auto l1 = (transa) ? prog.add_instruction(op::transpose{perm}, args[0]) : args[0];
        auto l2 = (transb) ? prog.add_instruction(op::transpose{perm}, args[1]) : args[1];
Paul's avatar
Paul committed
1431
1432
        if(args.size() == 3)
        {
1433
            if(beta != 0.f && args[2]->get_shape().elements() > 0)
1434
            {
Shucai Xiao's avatar
Shucai Xiao committed
1435
                auto out_lens   = l1->get_shape().lens();
1436
                out_lens.back() = l2->get_shape().lens().back();
Shucai Xiao's avatar
Shucai Xiao committed
1437
                auto l3         = args[2];
Shucai Xiao's avatar
Shucai Xiao committed
1438
1439
                auto l3_lens    = l3->get_shape().lens();
                if(!std::equal(out_lens.begin(), out_lens.end(), l3_lens.begin(), l3_lens.end()))
Khalique's avatar
Khalique committed
1440
                {
1441
                    l3 = prog.add_instruction(op::multibroadcast{out_lens}, args[2]);
Khalique's avatar
Khalique committed
1442
                }
1443
1444
                return prog.add_instruction(
                    make_op("dot", {{"alpha", alpha}, {"beta", beta}}), l1, l2, l3);
1445
            }
Paul's avatar
Paul committed
1446
        }
1447

1448
        return prog.add_instruction(make_op("dot", {{"alpha", alpha}, {"beta", beta}}), l1, l2);
Paul's avatar
Paul committed
1449
1450
    }

1451
1452
1453
1454
    instruction_ref parse_matmul(const std::string&,
                                 const std::string& op_name,
                                 const node_info&,
                                 std::vector<instruction_ref> args)
1455
    {
Shucai Xiao's avatar
Shucai Xiao committed
1456
1457
        auto l0      = args[0];
        auto l1      = args[1];
1458
1459
1460
1461
1462
        auto l0_lens = l0->get_shape().lens();
        auto l1_lens = l1->get_shape().lens();

        // args[0] is a vector, prepend 1 to the shape
        bool is_a_prepended = false;
Shucai Xiao's avatar
Shucai Xiao committed
1463
        if(l0_lens.size() == 1)
1464
1465
1466
1467
1468
1469
1470
        {
            is_a_prepended = true;
            l0_lens.insert(l0_lens.begin(), 1);
            l0 = prog.add_instruction(op::unsqueeze{{0}}, args[0]);
        }

        bool is_b_appended = false;
Shucai Xiao's avatar
Shucai Xiao committed
1471
        if(l1_lens.size() == 1)
1472
1473
1474
1475
1476
1477
1478
1479
        {
            is_b_appended = true;
            l1_lens.push_back(1);
            l1 = prog.add_instruction(op::unsqueeze{{1}}, args[1]);
        }

        instruction_ref bl0 = l0;
        instruction_ref bl1 = l1;
Shucai Xiao's avatar
Shucai Xiao committed
1480
        if(!std::equal(l0_lens.rbegin() + 2, l0_lens.rend(), l1_lens.rbegin() + 2, l1_lens.rend()))
1481
1482
1483
1484
1485
1486
        {
            auto l0_it = l0_lens.begin() + l0_lens.size() - 2;
            std::vector<std::size_t> l0_broadcasted_lens(l0_lens.begin(), l0_it);
            auto l1_it = l1_lens.begin() + l1_lens.size() - 2;
            std::vector<std::size_t> l1_broadcasted_lens(l1_lens.begin(), l1_it);
            auto output_lens = compute_broadcasted_lens(l0_broadcasted_lens, l1_broadcasted_lens);
1487
            l0_broadcasted_lens = output_lens;
1488
            l0_broadcasted_lens.insert(l0_broadcasted_lens.end(), l0_it, l0_lens.end());
1489
            l1_broadcasted_lens = output_lens;
1490
            l1_broadcasted_lens.insert(l1_broadcasted_lens.end(), l1_it, l1_lens.end());
Shucai Xiao's avatar
Shucai Xiao committed
1491
            if(l0_lens != l0_broadcasted_lens)
1492
1493
1494
            {
                bl0 = prog.add_instruction(op::multibroadcast{l0_broadcasted_lens}, l0);
            }
Shucai Xiao's avatar
Shucai Xiao committed
1495
            if(l1_lens != l1_broadcasted_lens)
1496
1497
1498
1499
1500
            {
                bl1 = prog.add_instruction(op::multibroadcast{l1_broadcasted_lens}, l1);
            }
        }

1501
1502
        auto dot_res =
            prog.add_instruction(make_op(op_name, {{"alpha", 1}, {"beta", 0}}), bl0, bl1);
1503
        int64_t num_axis = static_cast<int64_t>(dot_res->get_shape().lens().size());
Shucai Xiao's avatar
Shucai Xiao committed
1504
        if(is_a_prepended)
1505
1506
1507
1508
        {
            dot_res = prog.add_instruction(op::squeeze{{num_axis - 2}}, dot_res);
            --num_axis;
        }
Shucai Xiao's avatar
Shucai Xiao committed
1509
        if(is_b_appended)
1510
1511
1512
        {
            dot_res = prog.add_instruction(op::squeeze{{num_axis - 1}}, dot_res);
        }
Shucai Xiao's avatar
Shucai Xiao committed
1513

1514
1515
1516
        return dot_res;
    }

1517
    instruction_ref
1518
    parse_batchnorm(const std::string&, node_info info, std::vector<instruction_ref> args)
1519
    {
Scott Thornton's avatar
Scott Thornton committed
1520
1521
        float epsilon                                     = 1e-5f;
        float momentum                                    = 0.9f;
1522
        op::batch_norm_inference::bn_infer_mode_t bn_mode = op::batch_norm_inference::spatial;
1523
        if(contains(info.attributes, "epsilon"))
1524
        {
1525
            epsilon = parse_value(info.attributes.at("epsilon")).at<float>();
1526
        }
1527
        if(contains(info.attributes, "momentum"))
1528
        {
1529
            momentum = parse_value(info.attributes.at("momentum")).at<float>();
1530
        }
1531
        if(contains(info.attributes, "spatial"))
1532
        {
1533
            bn_mode = (parse_value(info.attributes.at("spatial")).at<uint64_t>() > 0)
1534
1535
                          ? op::batch_norm_inference::spatial
                          : op::batch_norm_inference::per_activation;
1536
        }
Paul's avatar
Paul committed
1537
        op::batch_norm_inference op{epsilon, momentum, bn_mode};
Paul's avatar
Paul committed
1538
        return prog.add_instruction(op, std::move(args));
1539
1540
    }

1541
1542
    instruction_ref
    parse_instancenorm(const std::string&, node_info info, std::vector<instruction_ref> args)
kahmed10's avatar
kahmed10 committed
1543
1544
    {
        // y = scale * ( x - mean ) / sqrt ( variance + epsilon ) + bias
kahmed10's avatar
kahmed10 committed
1545
1546
        // mean = reduce_mean({D1, D2, ... Dk}, x)
        // variance = reduce_mean({D1, D2, ... Dk}, (x - mean)^2)
kahmed10's avatar
kahmed10 committed
1547
1548

        float epsilon = 1e-5f;
1549
        if(contains(info.attributes, "epsilon"))
kahmed10's avatar
kahmed10 committed
1550
        {
1551
            epsilon = parse_value(info.attributes.at("epsilon")).at<float>();
kahmed10's avatar
kahmed10 committed
1552
1553
1554
1555
1556
        }
        auto x     = args[0];
        auto scale = args[1];
        auto bias  = args[2];
        auto dims  = x->get_shape().lens();
kahmed10's avatar
kahmed10 committed
1557
1558
1559
        auto ndims = dims.size();
        assert(ndims >= 2);
        auto kdims = ndims - 2;
kahmed10's avatar
kahmed10 committed
1560

kahmed10's avatar
kahmed10 committed
1561
1562
1563
1564
        std::vector<int64_t> axes(kdims);
        std::iota(axes.begin(), axes.end(), 2);

        auto mean            = prog.add_instruction(make_op("reduce_mean", {{"axes", axes}}), x);
kahmed10's avatar
kahmed10 committed
1565
        auto mean_bcast      = prog.add_instruction(op::multibroadcast{dims}, mean);
1566
        auto l0              = prog.add_instruction(make_op("sqdiff"), x, mean_bcast);
kahmed10's avatar
kahmed10 committed
1567
        auto variance        = prog.add_instruction(make_op("reduce_mean", {{"axes", axes}}), l0);
1568
        auto l1              = prog.add_instruction(make_op("sub"), x, mean_bcast);
kahmed10's avatar
kahmed10 committed
1569
1570
1571
        auto epsilon_literal = prog.add_literal(epsilon);
        auto epsilon_bcast   = prog.add_instruction(op::multibroadcast{dims}, epsilon_literal);
        auto variance_bcast  = prog.add_instruction(op::multibroadcast{dims}, variance);
1572
1573
1574
        auto l2              = prog.add_instruction(make_op("add"), variance_bcast, epsilon_bcast);
        auto l3              = prog.add_instruction(make_op("rsqrt"), l2);
        auto l4              = prog.add_instruction(make_op("mul"), l1, l3);
kahmed10's avatar
kahmed10 committed
1575
1576
1577
        auto scale_bcast     = prog.add_instruction(op::broadcast{1, dims}, scale);
        ;
        auto bias_bcast = prog.add_instruction(op::broadcast{1, dims}, bias);
1578
1579
        auto l5         = prog.add_instruction(make_op("mul"), l4, scale_bcast);
        return prog.add_instruction(make_op("add"), l5, bias_bcast);
kahmed10's avatar
kahmed10 committed
1580
1581
    }

1582
1583
    instruction_ref
    parse_leaky_relu(const std::string&, node_info info, std::vector<instruction_ref> args)
1584
    {
Khalique's avatar
Khalique committed
1585
        float alpha = 0.01; // default alpha val for leaky relu
1586
        if(contains(info.attributes, "alpha"))
1587
        {
1588
            alpha = parse_value(info.attributes.at("alpha")).at<float>();
1589
        }
1590
        auto op = make_op("leaky_relu", {{"alpha", alpha}});
1591
1592
1593
        return prog.add_instruction(op, args.front());
    }

1594
    instruction_ref parse_elu(const std::string&, node_info info, std::vector<instruction_ref> args)
Khalique's avatar
Khalique committed
1595
1596
    {
        float alpha = 1.0; // default alpha val for elu
1597
        if(contains(info.attributes, "alpha"))
Khalique's avatar
Khalique committed
1598
        {
1599
            alpha = parse_value(info.attributes.at("alpha")).at<float>();
Khalique's avatar
Khalique committed
1600
        }
1601
        auto op = make_op("elu", {{"alpha", alpha}});
Khalique's avatar
Khalique committed
1602
1603
1604
        return prog.add_instruction(op, args.front());
    }

1605
    instruction_ref parse_lrn(const std::string&, node_info info, std::vector<instruction_ref> args)
Khalique's avatar
Khalique committed
1606
1607
    {
        float alpha = 0.0001;
Khalique's avatar
Khalique committed
1608
1609
1610
        float beta  = 0.75;
        float bias  = 1.0;
        int size    = 1;
1611
1612
1613
1614
1615
1616
1617
1618
        if(contains(info.attributes, "alpha"))
            alpha = parse_value(info.attributes.at("alpha")).at<float>();
        if(contains(info.attributes, "beta"))
            beta = parse_value(info.attributes.at("beta")).at<float>();
        if(contains(info.attributes, "bias"))
            bias = parse_value(info.attributes.at("bias")).at<float>();
        if(contains(info.attributes, "size"))
            size = parse_value(info.attributes.at("size")).at<int>();
Khalique's avatar
Khalique committed
1619
1620
1621
1622
        op::lrn op{alpha, beta, bias, size};
        return prog.add_instruction(op, args.front());
    }

1623
1624
    instruction_ref
    parse_imagescaler(const std::string&, node_info info, std::vector<instruction_ref> args)
Khalique's avatar
Khalique committed
1625
1626
1627
    {
        float scale = 1.0;
        std::vector<float> bias{};
1628
        if(contains(info.attributes, "scale"))
Khalique's avatar
Khalique committed
1629
        {
1630
            scale = parse_value(info.attributes.at("scale")).at<float>();
Khalique's avatar
Khalique committed
1631
1632
        }

1633
        if(contains(info.attributes, "bias"))
Khalique's avatar
Khalique committed
1634
        {
1635
            auto&& bias_floats = info.attributes["bias"].floats();
Khalique's avatar
Khalique committed
1636
1637
            bias               = std::vector<float>(bias_floats.begin(), bias_floats.end());
        }
Shucai Xiao's avatar
Shucai Xiao committed
1638
1639
1640
        auto input_shape       = args.front()->get_shape();
        auto const& input_lens = input_shape.lens();
        auto input_type        = input_shape.type();
Khalique's avatar
Khalique committed
1641

Shucai Xiao's avatar
Shucai Xiao committed
1642
1643
        auto scale_val = prog.add_literal(literal{shape{input_type}, {scale}});
        auto bias_vals = prog.add_literal(literal{shape{input_type, {bias.size()}}, bias});
Khalique's avatar
Khalique committed
1644

1645
        auto scale_tensor = prog.add_instruction(migraphx::op::scalar{input_lens}, scale_val);
1646
1647
1648
1649
        auto img_scaled =
            prog.add_instruction(migraphx::make_op("mul"), args.front(), scale_tensor);
        auto bias_bcast = prog.add_instruction(migraphx::op::broadcast{1, input_lens}, bias_vals);
        return prog.add_instruction(migraphx::make_op("add"), img_scaled, bias_bcast);
Khalique's avatar
Khalique committed
1650
    }
Khalique's avatar
Khalique committed
1651

Khalique's avatar
Khalique committed
1652
    instruction_ref
1653
    parse_transpose(const std::string&, node_info info, std::vector<instruction_ref> args)
Khalique's avatar
Khalique committed
1654
1655
    {
        std::vector<int64_t> perm{};
1656
        if(contains(info.attributes, "perm"))
Khalique's avatar
Khalique committed
1657
        {
1658
            auto&& perm_vals = info.attributes["perm"].ints();
Khalique's avatar
Khalique committed
1659
1660
            perm             = std::vector<int64_t>(perm_vals.begin(), perm_vals.end());
        }
Paul's avatar
Paul committed
1661
        return prog.add_instruction(migraphx::op::transpose{perm}, args.front());
Khalique's avatar
Khalique committed
1662
1663
    }

1664
    instruction_ref parse_pad(const std::string&, node_info info, std::vector<instruction_ref> args)
Khalique's avatar
Khalique committed
1665
1666
    {
        std::vector<int64_t> pads{};
1667
1668
1669
1670
1671
1672
1673
        if(args.size() >= 2)
        {
            auto pad_arg = args.at(1)->eval();
            check_arg_empty(pad_arg, "PARSE_PAD: pad input must be constant");
            pad_arg.visit([&](auto v) { pads.assign(v.begin(), v.end()); });
        }
        else if(contains(info.attributes, "pads"))
Khalique's avatar
Khalique committed
1674
        {
1675
            auto&& pad_vals = info.attributes["pads"].ints();
Khalique's avatar
Khalique committed
1676
1677
            pads            = std::vector<int64_t>(pad_vals.begin(), pad_vals.end());
        }
1678
1679
1680
1681
1682
        else
        {
            MIGRAPHX_THROW("PARSE_PAD: pad must be available");
        }

1683
        // check if padding is actually being done (at least one value is nonzero)
Khalique's avatar
Khalique committed
1684
        if(std::all_of(pads.begin(), pads.end(), [](const int& i) { return i == 0; }))
1685
        {
1686
            return prog.add_instruction(make_op("identity"), args.front());
1687
        }
1688

kahmed10's avatar
kahmed10 committed
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
        if(contains(info.attributes, "mode"))
        {
            auto mode = info.attributes.at("mode").s();
            if(mode == "reflect")
                return reflect_pad(pads, args.front());
            if(mode != "constant")
            {
                MIGRAPHX_THROW(
                    "PARSE_PAD: migraphx currently only supports constant and reflect padding");
            }
        }

1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
        float value = 0.0f;
        // third input is the value
        if(args.size() == 3)
        {
            auto val_ins = args.at(2);
            if(!val_ins->can_eval())
            {
                MIGRAPHX_THROW("PARSE_PAD: input value must be constant");
            }
            auto val_arg = val_ins->eval();
            if(val_arg.get_shape().elements() != 1)
            {
                MIGRAPHX_THROW("PARSE_PAD: value should contain only one element");
            }
            value = val_arg.at<float>();
        }
        else if(contains(info.attributes, "value"))
Khalique's avatar
Khalique committed
1718
        {
1719
            value = parse_value(info.attributes.at("value")).at<float>();
Khalique's avatar
Khalique committed
1720
        }
1721

Khalique's avatar
Khalique committed
1722
1723
        return prog.add_instruction(migraphx::op::pad{pads, value}, args.front());
    }
Shucai Xiao's avatar
Shucai Xiao committed
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766

    instruction_ref
    parse_selu(const std::string&, const node_info& info, std::vector<instruction_ref> args)
    {
        auto type   = args[0]->get_shape().type();
        auto lens   = args[0]->get_shape().lens();
        float alpha = 1.67326f;
        if(contains(info.attributes, "alpha"))
        {
            alpha = info.attributes.at("alpha").f();
        }

        float gamma = 1.0507f;
        if(contains(info.attributes, "gamma"))
        {
            gamma = info.attributes.at("gamma").f();
        }

        auto l_alpha = prog.add_literal({{type, {1}}, {alpha}});
        auto l_gamma = prog.add_literal({{type, {1}}, {gamma / 2.0f}});
        if(lens != std::vector<std::size_t>{1})
        {
            l_alpha =
                prog.add_instruction(make_op("multibroadcast", {{"output_lens", lens}}), l_alpha);
            l_gamma =
                prog.add_instruction(make_op("multibroadcast", {{"output_lens", lens}}), l_gamma);
        }

        auto sign_x = prog.add_instruction(make_op("sign"), args[0]);
        auto exp_x  = prog.add_instruction(make_op("exp"), args[0]);

        auto alpha_ex  = prog.add_instruction(make_op("mul"), l_alpha, exp_x);
        auto aex_alpha = prog.add_instruction(make_op("sub"), alpha_ex, l_alpha);

        auto ins1 = prog.add_instruction(make_op("add"), aex_alpha, args[0]);
        auto ins2 = prog.add_instruction(make_op("sub"), aex_alpha, args[0]);

        auto sign2   = prog.add_instruction(make_op("mul"), sign_x, ins2);
        auto ins_sub = prog.add_instruction(make_op("sub"), ins1, sign2);

        return prog.add_instruction(make_op("mul"), ins_sub, l_gamma);
    }

1767
1768
1769
    // Use a literal instruction to replace the shape since, output of
    // shape operator are literals in migraphx
    instruction_ref
1770
    parse_shape(const std::string&, const node_info&, std::vector<instruction_ref> args)
1771
1772
    {
        if(args.size() != 1)
1773
            MIGRAPHX_THROW("Shape: operator should have 1 operand");
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
        std::vector<std::size_t> arg_shape = args[0]->get_shape().lens();
        std::vector<int64_t> vec_shape(arg_shape.size());
        migraphx::shape s(migraphx::shape::int64_type, {arg_shape.size()});
        std::transform(arg_shape.begin(), arg_shape.end(), vec_shape.begin(), [](auto i) {
            return int64_t(i);
        });
        return prog.add_literal(migraphx::literal{s, vec_shape});
    }

    // Use a literal instruction to replace the constantFill operator. In RNN, input shape
    // and value are fixed, so no need to do the actual computation for the constantFill
    // operator
1786
1787
    instruction_ref
    parse_constant_fill(const std::string&, node_info info, std::vector<instruction_ref> args)
1788
1789
1790
1791
1792
    {
        int input_as_shape = 0;
        int dtype          = 1;
        float value        = 0.0f;

1793
        if(contains(info.attributes, "dtype"))
1794
        {
1795
            dtype = parse_value(info.attributes.at("dtype")).at<int>();
1796
        }
Shucai Xiao's avatar
Shucai Xiao committed
1797
        shape::type_t type = get_type(dtype);
1798

1799
        if(contains(info.attributes, "input_as_shape"))
1800
        {
1801
            input_as_shape = parse_value(info.attributes.at("input_as_shape")).at<int>();
1802
1803
        }

1804
        if(contains(info.attributes, "value"))
1805
        {
1806
            value = parse_value(info.attributes.at("value")).at<float>();
1807
1808
        }

1809
        if(contains(info.attributes, "extra_shape"))
Shucai Xiao's avatar
Shucai Xiao committed
1810
        {
1811
            MIGRAPHX_THROW("ConstantFill: cannot handle extra shape attribute");
1812
1813
        }

1814
1815
        if(input_as_shape == 1)
        {
Shucai Xiao's avatar
Shucai Xiao committed
1816
            if(args.size() != 1)
1817
            {
1818
                MIGRAPHX_THROW("ConstantFill: need an input argument as output shape");
1819
1820
            }

1821
            if(contains(info.attributes, "shape"))
Shucai Xiao's avatar
Shucai Xiao committed
1822
            {
1823
                MIGRAPHX_THROW("ConstantFill: cannot set the shape argument and pass in an input "
Shucai Xiao's avatar
Shucai Xiao committed
1824
                               "at the same time");
1825
1826
            }

1827
            migraphx::argument in = args[0]->eval();
Shucai Xiao's avatar
Shucai Xiao committed
1828
            check_arg_empty(in, "ConstantFill: dynamic shape is not supported");
1829

1830
1831
1832
            std::vector<std::size_t> dims;
            in.visit([&](auto input) { dims.assign(input.begin(), input.end()); });
            migraphx::shape s(type, dims);
1833
1834
            std::vector<float> values(s.elements(), value);
            return prog.add_literal(migraphx::literal(s, values));
1835
1836
1837
        }
        else if(input_as_shape == 0)
        {
1838
            if(!contains(info.attributes, "shape"))
Shucai Xiao's avatar
Shucai Xiao committed
1839
            {
1840
                MIGRAPHX_THROW("ConstantFill: attribute output shape is needed");
1841
1842
            }

1843
            literal ls = parse_value(info.attributes.at("shape"));
1844
            std::vector<std::size_t> dims;
Shucai Xiao's avatar
Shucai Xiao committed
1845
            ls.visit([&](auto s) { dims.assign(s.begin(), s.end()); });
1846
            migraphx::shape s{type, dims};
1847
1848
            std::vector<float> values(s.elements(), value);
            return prog.add_literal(migraphx::literal(s, values));
1849
1850
1851
        }
        else
        {
1852
            MIGRAPHX_THROW("ConstantFill: wrong value of attribute input_as_shape");
1853
1854
1855
        }
    }

1856
1857
    instruction_ref
    parse_constant_of_shape(const std::string&, node_info info, std::vector<instruction_ref> args)
1858
1859
    {
        literal l_val{};
1860
        if(contains(info.attributes, "value"))
1861
        {
1862
            l_val = parse_value(info.attributes.at("value"));
Shucai Xiao's avatar
Shucai Xiao committed
1863
            if(l_val.get_shape().elements() != 1)
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
            {
                MIGRAPHX_THROW("ConstantOfShape: attribute value can contain only 1 elements!");
            }
        }
        else
        {
            l_val = literal({shape::float_type, {1}, {0}}, {0.0f});
        }

        // input is empty, output is a scalar
        auto type = l_val.get_shape().type();
1875

Shucai Xiao's avatar
Shucai Xiao committed
1876
        if(args.empty())
1877
        {
Shucai Xiao's avatar
Shucai Xiao committed
1878
            MIGRAPHX_THROW("ConstantOfShape : must have 1 input!");
1879
1880
1881
        }
        else
        {
1882
1883
            migraphx::shape s;
            // empty input tensor, output is a scalar
Shucai Xiao's avatar
Shucai Xiao committed
1884
            if(args[0]->get_shape().elements() == 0)
1885
            {
1886
                s = migraphx::shape{type, {1}, {0}};
1887
            }
1888
1889
1890
            else
            {
                migraphx::argument in = args[0]->eval();
Shucai Xiao's avatar
Shucai Xiao committed
1891
                check_arg_empty(in, "ConstantOfShape: dynamic shape is not supported");
1892

1893
1894
1895
1896
                std::vector<std::size_t> dims;
                in.visit([&](auto input) { dims.assign(input.begin(), input.end()); });
                s = migraphx::shape{type, dims};
            }
1897

Shucai Xiao's avatar
Shucai Xiao committed
1898
            literal l_out{};
1899
            l_val.visit([&](auto val) {
Shucai Xiao's avatar
Shucai Xiao committed
1900
                using val_type = std::remove_cv_t<typename decltype(val)::value_type>;
1901
                // l_val contains only one element
1902
                std::vector<val_type> out_vec(s.elements(), val.front());
1903
1904
1905
1906
1907
1908
1909
                l_out = literal(s, out_vec);
            });

            return prog.add_literal(l_out);
        }
    }

Shucai Xiao's avatar
Shucai Xiao committed
1910
    instruction_ref
1911
    parse_expand(const std::string&, const node_info&, std::vector<instruction_ref> args)
1912
    {
Shucai Xiao's avatar
Shucai Xiao committed
1913
        auto in_lens             = args[0]->get_shape().lens();
1914
        migraphx::argument arg_s = args[1]->eval();
Shucai Xiao's avatar
Shucai Xiao committed
1915
        check_arg_empty(arg_s, "Expand: dynamic shape is not supported");
1916
1917
1918
        std::vector<std::size_t> dims;
        arg_s.visit([&](auto input) { dims.assign(input.begin(), input.end()); });
        auto out_lens = compute_broadcasted_lens(in_lens, dims);
Shucai Xiao's avatar
Shucai Xiao committed
1919
        return prog.add_instruction(op::multibroadcast{out_lens}, args[0]);
1920
1921
    }

Shucai Xiao's avatar
Shucai Xiao committed
1922
    std::vector<instruction_ref>
1923
    parse_rnn(const std::string&, node_info info, std::vector<instruction_ref> args)
Shucai Xiao's avatar
Shucai Xiao committed
1924
1925
    {
        migraphx::shape input_shape = args[0]->get_shape();
1926
        std::size_t hidden_size     = args[1]->get_shape().lens()[1];
Shucai Xiao's avatar
Shucai Xiao committed
1927

1928
        if(contains(info.attributes, "hidden_size"))
Shucai Xiao's avatar
Shucai Xiao committed
1929
        {
1930
            std::size_t hidden_size_att = parse_value(info.attributes.at("hidden_size")).at<int>();
Shucai Xiao's avatar
Shucai Xiao committed
1931
            if(hidden_size != hidden_size_att)
Shucai Xiao's avatar
Shucai Xiao committed
1932
1933
1934
            {
                MIGRAPHX_THROW("RNN: hidden size mismatch in input and attribute");
            }
Shucai Xiao's avatar
Shucai Xiao committed
1935
1936
1937
1938
        }

        // Handling of direction to be added later
        std::string direction{"forward"};
1939
        if(contains(info.attributes, "direction"))
Shucai Xiao's avatar
Shucai Xiao committed
1940
        {
1941
            direction = info.attributes.at("direction").s();
Shucai Xiao's avatar
Shucai Xiao committed
1942
1943
        }

1944
        op::rnn_direction dirct = op::rnn_direction::forward;
Shucai Xiao's avatar
Shucai Xiao committed
1945
1946
        if(direction == "bidirectional")
        {
1947
            dirct = op::rnn_direction::bidirectional;
Shucai Xiao's avatar
Shucai Xiao committed
1948
1949
1950
        }
        else if(direction == "reverse")
        {
1951
            dirct = op::rnn_direction::reverse;
Shucai Xiao's avatar
Shucai Xiao committed
1952
1953
        }

1954
        std::vector<std::string> vec_names{"tanh"};
1955
        if(contains(info.attributes, "activations"))
1956
        {
1957
            auto names = info.attributes.at("activations").strings();
1958
            vec_names.clear();
1959
            vec_names.resize(names.size());
Shucai Xiao's avatar
Shucai Xiao committed
1960
1961
1962
            std::transform(names.begin(), names.end(), vec_names.begin(), [](auto name) {
                return to_lower(name);
            });
1963
1964
        }

1965
1966
1967
        auto name_it = std::find_if(vec_names.begin(), vec_names.end(), [&](auto& name) {
            return (map_actv_funcs.count(name) == 0);
        });
Shucai Xiao's avatar
Shucai Xiao committed
1968
        if(name_it != vec_names.end())
1969
1970
1971
        {
            MIGRAPHX_THROW("RNN: activation function " + std::string(*name_it) + " not supported");
        }
1972

Shucai Xiao's avatar
Shucai Xiao committed
1973
        // bidirectional case should have two activation functions.
Shucai Xiao's avatar
Shucai Xiao committed
1974
        // one is for forward, and the other is for reverse.
Shucai Xiao's avatar
Shucai Xiao committed
1975
        // if only one actv function is provided, we use it in both
1976
        // forward and reverse direction
1977
        if(dirct == op::rnn_direction::bidirectional)
1978
        {
Shucai Xiao's avatar
Shucai Xiao committed
1979
            if(vec_names.size() == 1)
1980
1981
1982
1983
1984
            {
                vec_names.push_back(vec_names.at(0));
            }
        }

Shucai Xiao's avatar
Shucai Xiao committed
1985
        std::vector<operation> vec_actv_funcs(vec_names.size());
Paul's avatar
Paul committed
1986
1987
1988
1989
        std::transform(vec_names.begin(),
                       vec_names.end(),
                       vec_actv_funcs.begin(),
                       [&](const auto& fn) { return map_actv_funcs[fn]; });
Shucai Xiao's avatar
Shucai Xiao committed
1990

Shucai Xiao's avatar
Shucai Xiao committed
1991
1992
        // To be added later
        float clip = 0.0;
1993
        if(contains(info.attributes, "clip"))
Shucai Xiao's avatar
Shucai Xiao committed
1994
        {
1995
            clip = parse_value(info.attributes.at("clip")).at<float>();
Shucai Xiao's avatar
Shucai Xiao committed
1996
1997
        }

1998
1999
        // if the number of arguments is less than 6, append
        // undefined operator to have 6 arguments
Shucai Xiao's avatar
Shucai Xiao committed
2000
        if(args.size() < 6)
2001
2002
2003
2004
2005
        {
            auto ins = prog.add_instruction(op::undefined{});
            args.insert(args.end(), (6 - args.size()), ins);
        }

Shucai Xiao's avatar
Shucai Xiao committed
2006
2007
        // first output for the concatenation of hidden states
        auto hidden_states = prog.add_instruction(op::rnn{hidden_size, vec_actv_funcs, dirct, clip},
Shucai Xiao's avatar
Shucai Xiao committed
2008
                                                  std::move(args));
Shucai Xiao's avatar
Shucai Xiao committed
2009

2010
        // second output for the last hidden state
Shucai Xiao's avatar
Shucai Xiao committed
2011
        auto last_output = prog.add_instruction(op::rnn_last_hs_output{}, hidden_states);
Shucai Xiao's avatar
Shucai Xiao committed
2012

Shucai Xiao's avatar
Shucai Xiao committed
2013
        return {hidden_states, last_output};
Shucai Xiao's avatar
Shucai Xiao committed
2014
2015
    }

2016
    std::vector<instruction_ref>
2017
    parse_gru(const std::string&, node_info info, std::vector<instruction_ref> args)
2018
2019
2020
2021
    {
        migraphx::shape input_shape = args[0]->get_shape();
        std::size_t hidden_size     = args[2]->get_shape().lens()[2];

2022
        if(contains(info.attributes, "hidden_size"))
2023
        {
2024
            std::size_t hidden_size_att = parse_value(info.attributes.at("hidden_size")).at<int>();
Shucai Xiao's avatar
Shucai Xiao committed
2025
            if(hidden_size != hidden_size_att)
Shucai Xiao's avatar
Shucai Xiao committed
2026
2027
2028
            {
                MIGRAPHX_THROW("GRU: hidden size mismatch in input and attribute");
            }
2029
2030
2031
2032
        }

        // Handling of direction to be added later
        std::string direction{"forward"};
2033
        if(contains(info.attributes, "direction"))
2034
        {
2035
            direction = info.attributes.at("direction").s();
2036
2037
        }

2038
        op::rnn_direction dirct = op::rnn_direction::forward;
2039
2040
        if(direction == "bidirectional")
        {
2041
            dirct = op::rnn_direction::bidirectional;
2042
2043
2044
        }
        else if(direction == "reverse")
        {
2045
            dirct = op::rnn_direction::reverse;
2046
2047
        }

2048
        std::vector<std::string> vec_names = {"sigmoid", "tanh"};
2049
        if(contains(info.attributes, "activations"))
2050
        {
2051
            auto names = info.attributes.at("activations").strings();
2052
            vec_names.clear();
Shucai Xiao's avatar
Shucai Xiao committed
2053
            vec_names.resize(names.size());
Shucai Xiao's avatar
Shucai Xiao committed
2054
2055
2056
            std::transform(names.begin(), names.end(), vec_names.begin(), [](auto name) {
                return to_lower(name);
            });
2057
2058
        }

2059
        // need 4 activation functions
2060
        if(dirct == op::rnn_direction::bidirectional)
2061
        {
Shucai Xiao's avatar
Shucai Xiao committed
2062
            // 4 activation functions are used in the bidirectional
2063
            // scenario. No spec is provided in onnx::operator. we
Shucai Xiao's avatar
Shucai Xiao committed
2064
2065
            // use the algorithm that: if 1 actv function is provided,
            // repeat 1 four times. If 2 actv functins are provided,
2066
2067
            // assume forward and reverse use the same pair of actv
            // functions. For the case of 3 actv functions provided,
Shucai Xiao's avatar
Shucai Xiao committed
2068
2069
2070
            // assume the 3rd one is repeated once and used by the
            // reverse direction.
            // This may need change later
2071
            if(vec_names.size() == 1)
2072
            {
2073
                vec_names.insert(vec_names.end(), 3, vec_names.at(0));
2074
            }
2075
            else if(vec_names.size() == 2)
2076
            {
2077
2078
2079
                // repeat the activation functions
                vec_names.push_back(vec_names.at(0));
                vec_names.push_back(vec_names.at(1));
2080
            }
2081
            else if(vec_names.size() == 3)
2082
            {
2083
                vec_names.push_back(vec_names.at(2));
2084
2085
            }
        }
Shucai Xiao's avatar
Shucai Xiao committed
2086
        else
2087
        {
2088
            if(vec_names.size() == 1)
2089
            {
2090
                vec_names.push_back(vec_names.at(0));
2091
2092
2093
            }
        }

2094
2095
2096
        auto name_it = std::find_if(vec_names.begin(), vec_names.end(), [&](auto& name) {
            return (map_actv_funcs.count(name) == 0);
        });
Shucai Xiao's avatar
Shucai Xiao committed
2097
        if(name_it != vec_names.end())
2098
2099
2100
        {
            MIGRAPHX_THROW("GRU: activation function " + std::string(*name_it) + " not supported");
        }
2101

Shucai Xiao's avatar
Shucai Xiao committed
2102
        std::vector<operation> vec_actv_funcs(vec_names.size());
Paul's avatar
Paul committed
2103
2104
2105
2106
        std::transform(vec_names.begin(),
                       vec_names.end(),
                       vec_actv_funcs.begin(),
                       [&](const auto& name) { return map_actv_funcs[name]; });
2107
2108

        float clip = 0.0;
2109
        if(contains(info.attributes, "clip"))
2110
        {
2111
            clip = parse_value(info.attributes.at("clip")).at<float>();
2112
2113
2114
        }

        int linear_before_reset = 0;
2115
        if(contains(info.attributes, "linear_before_reset"))
2116
        {
2117
            linear_before_reset = parse_value(info.attributes.at("linear_before_reset")).at<int>();
2118
2119
        }

Shucai Xiao's avatar
Shucai Xiao committed
2120
        // append undefined opeator to make 6 arguments
Shucai Xiao's avatar
Shucai Xiao committed
2121
        if(args.size() < 6)
Shucai Xiao's avatar
Shucai Xiao committed
2122
2123
2124
2125
2126
        {
            auto ins = prog.add_instruction(op::undefined{});
            args.insert(args.end(), 6 - args.size(), ins);
        }

2127
2128
        // first output for concatenation of hidden states
        auto hidden_states = prog.add_instruction(
Shucai Xiao's avatar
Shucai Xiao committed
2129
            op::gru{hidden_size, vec_actv_funcs, dirct, clip, linear_before_reset},
Shucai Xiao's avatar
Shucai Xiao committed
2130
            std::move(args));
2131
2132

        // second output for last gru output
Shucai Xiao's avatar
Shucai Xiao committed
2133
        auto last_output = prog.add_instruction(op::rnn_last_hs_output{}, hidden_states);
2134

Shucai Xiao's avatar
Shucai Xiao committed
2135
        return {hidden_states, last_output};
2136
2137
    }

Shucai Xiao's avatar
Shucai Xiao committed
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
    void lstm_actv_functions(op::rnn_direction dirct, std::vector<std::string>& actv_func_names)
    {
        // need 6 activation functions for bidirectional directions
        if(dirct == op::rnn_direction::bidirectional)
        {
            // 6 activation functions are used in the bidirectional
            // scenario. No spec is provided in onnx::operator. we
            // use the algorithm that: if 1 actv function is provided,
            // repeat 1st six times. If 2 actv functins are provided,
            // repeat 2nd once, then repeat all three once
            // if 3 actv funcs are provide, repeat all three once.
            // the same algorithm is used for 4, 5, and 6 actv funcions
            // provided. This may need change later
            switch(actv_func_names.size())
            {
            case 1:
                actv_func_names = {actv_func_names.at(0),
                                   actv_func_names.at(0),
                                   actv_func_names.at(0),
                                   actv_func_names.at(0),
                                   actv_func_names.at(0),
                                   actv_func_names.at(0)};
                break;

            case 2:
                // repeat the 2nd actv func once, then repeat all three another time
                actv_func_names = {actv_func_names.at(0),
                                   actv_func_names.at(1),
                                   actv_func_names.at(1),
                                   actv_func_names.at(0),
                                   actv_func_names.at(1),
                                   actv_func_names.at(1)};
                break;

            case 3:
                // repeat all three actv funcs once
                actv_func_names = {actv_func_names.at(0),
                                   actv_func_names.at(1),
                                   actv_func_names.at(2),
                                   actv_func_names.at(0),
                                   actv_func_names.at(1),
                                   actv_func_names.at(2)};
                break;

            case 4:
                actv_func_names = {actv_func_names.at(0),
                                   actv_func_names.at(1),
                                   actv_func_names.at(2),
                                   actv_func_names.at(3),
                                   actv_func_names.at(3),
                                   actv_func_names.at(3)};
                break;

            case 5:
                actv_func_names = {actv_func_names.at(0),
                                   actv_func_names.at(1),
                                   actv_func_names.at(2),
                                   actv_func_names.at(3),
                                   actv_func_names.at(4),
                                   actv_func_names.at(4)};
                break;

            default: break;
            }
        }
        else
        {
            switch(actv_func_names.size())
            {
            case 1:
                actv_func_names = {
                    actv_func_names.at(0), actv_func_names.at(0), actv_func_names.at(0)};
                break;

            case 2:
                // repeat the 2nd actv func once, so we have 3 actv funcs
                actv_func_names = {
                    actv_func_names.at(0), actv_func_names.at(1), actv_func_names.at(1)};
                break;

            default: break;
            }
        }
    }

Shucai Xiao's avatar
Shucai Xiao committed
2223
    std::vector<instruction_ref>
2224
    parse_lstm(const std::string&, node_info info, std::vector<instruction_ref> args)
Shucai Xiao's avatar
Shucai Xiao committed
2225
2226
2227
2228
    {
        migraphx::shape input_shape = args[0]->get_shape();
        std::size_t hidden_size     = args[2]->get_shape().lens()[2];

2229
        if(contains(info.attributes, "hidden_size"))
Shucai Xiao's avatar
Shucai Xiao committed
2230
        {
2231
            std::size_t hidden_size_att = parse_value(info.attributes.at("hidden_size")).at<int>();
Shucai Xiao's avatar
Shucai Xiao committed
2232
2233
2234
2235
2236
2237
2238
2239
            if(hidden_size != hidden_size_att)
            {
                MIGRAPHX_THROW("LSTM: hidden size mismatch in input and attribute");
            }
        }

        // Handling of direction to be added later
        std::string direction{"forward"};
2240
        if(contains(info.attributes, "direction"))
Shucai Xiao's avatar
Shucai Xiao committed
2241
        {
2242
            direction = info.attributes.at("direction").s();
Shucai Xiao's avatar
Shucai Xiao committed
2243
2244
        }

Shucai Xiao's avatar
Shucai Xiao committed
2245
        op::rnn_direction dirct = op::rnn_direction::forward;
Shucai Xiao's avatar
Shucai Xiao committed
2246
2247
        if(direction == "bidirectional")
        {
Shucai Xiao's avatar
Shucai Xiao committed
2248
            dirct = op::rnn_direction::bidirectional;
Shucai Xiao's avatar
Shucai Xiao committed
2249
2250
2251
        }
        else if(direction == "reverse")
        {
Shucai Xiao's avatar
Shucai Xiao committed
2252
            dirct = op::rnn_direction::reverse;
Shucai Xiao's avatar
Shucai Xiao committed
2253
        }
Shucai Xiao's avatar
Shucai Xiao committed
2254
        else if(direction == "forward")
Shucai Xiao's avatar
Shucai Xiao committed
2255
        {
Shucai Xiao's avatar
Shucai Xiao committed
2256
            dirct = op::rnn_direction::forward;
Shucai Xiao's avatar
Shucai Xiao committed
2257
2258
2259
2260
2261
2262
        }
        else
        {
            MIGRAPHX_THROW("LSTM: incorrect direction attribute");
        }

2263
        std::vector<std::string> vec_names = {"sigmoid", "tanh", "tanh"};
2264
        if(contains(info.attributes, "activations"))
Shucai Xiao's avatar
Shucai Xiao committed
2265
        {
2266
            auto names = info.attributes.at("activations").strings();
Shucai Xiao's avatar
Shucai Xiao committed
2267
2268
            vec_names.clear();
            vec_names.resize(names.size());
Shucai Xiao's avatar
Shucai Xiao committed
2269
2270
2271
            std::transform(names.begin(), names.end(), vec_names.begin(), [](auto name) {
                return to_lower(name);
            });
Shucai Xiao's avatar
Shucai Xiao committed
2272
2273
        }

Shucai Xiao's avatar
Shucai Xiao committed
2274
        lstm_actv_functions(dirct, vec_names);
Shucai Xiao's avatar
Shucai Xiao committed
2275

2276
2277
2278
        auto name_it = std::find_if(vec_names.begin(), vec_names.end(), [&](auto& name) {
            return (map_actv_funcs.count(name) == 0);
        });
Shucai Xiao's avatar
Shucai Xiao committed
2279
        if(name_it != vec_names.end())
2280
2281
2282
        {
            MIGRAPHX_THROW("LSTM: activation function " + std::string(*name_it) + " not supported");
        }
Shucai Xiao's avatar
Shucai Xiao committed
2283
2284

        std::vector<operation> vec_actv_funcs(vec_names.size());
Paul's avatar
Paul committed
2285
2286
2287
2288
        std::transform(vec_names.begin(),
                       vec_names.end(),
                       vec_actv_funcs.begin(),
                       [&](const auto& name) { return map_actv_funcs[name]; });
Shucai Xiao's avatar
Shucai Xiao committed
2289
2290

        float clip = 0.0;
2291
        if(contains(info.attributes, "clip"))
Shucai Xiao's avatar
Shucai Xiao committed
2292
        {
2293
            clip = parse_value(info.attributes.at("clip")).at<float>();
Shucai Xiao's avatar
Shucai Xiao committed
2294
2295
2296
        }

        int input_forget = 0;
2297
        if(contains(info.attributes, "input_forget"))
Shucai Xiao's avatar
Shucai Xiao committed
2298
        {
2299
            input_forget = parse_value(info.attributes.at("input_forget")).at<int>();
Shucai Xiao's avatar
Shucai Xiao committed
2300
2301
2302
2303
2304
2305
        }

        // append undefined opeator to make 6 arguments
        if(args.size() < 8)
        {
            auto ins = prog.add_instruction(op::undefined{});
Shucai Xiao's avatar
Shucai Xiao committed
2306
            args.insert(args.end(), 8 - args.size(), ins);
Shucai Xiao's avatar
Shucai Xiao committed
2307
2308
2309
2310
        }

        // first output for concatenation of hidden states
        auto hidden_states = prog.add_instruction(
Shucai Xiao's avatar
Shucai Xiao committed
2311
            op::lstm{hidden_size, vec_actv_funcs, dirct, clip, input_forget}, std::move(args));
Shucai Xiao's avatar
Shucai Xiao committed
2312

Shucai Xiao's avatar
Shucai Xiao committed
2313
        auto last_output = prog.add_instruction(op::rnn_last_hs_output{}, hidden_states);
Shucai Xiao's avatar
Shucai Xiao committed
2314
2315

        // third output for last cell output
Shucai Xiao's avatar
Shucai Xiao committed
2316
        auto last_cell_output = prog.add_instruction(op::rnn_last_cell_output{}, hidden_states);
Shucai Xiao's avatar
Shucai Xiao committed
2317
2318
2319

        return {hidden_states, last_output, last_cell_output};
    }
2320

2321
2322
2323
2324
    instruction_ref parse_reduce_oper(const std::string&,
                                      const std::string& op_name,
                                      node_info info,
                                      std::vector<instruction_ref> args)
Shucai Xiao's avatar
Shucai Xiao committed
2325
2326
2327
2328
    {
        std::size_t n_dim = args.front()->get_shape().lens().size();

        // default to reduce over all dimensions
2329
        std::vector<int64_t> axes(n_dim);
Shucai Xiao's avatar
Shucai Xiao committed
2330
        std::iota(axes.begin(), axes.end(), 0);
2331
        if(contains(info.attributes, "axes"))
Shucai Xiao's avatar
Shucai Xiao committed
2332
2333
        {
            axes.clear();
2334
            auto&& attr_axes = info.attributes["axes"].ints();
2335
            axes             = std::vector<int64_t>(attr_axes.begin(), attr_axes.end());
Shucai Xiao's avatar
Shucai Xiao committed
2336
2337
2338
        }

        int keep_dims = 1;
2339
        if(contains(info.attributes, "keepdims"))
Shucai Xiao's avatar
Shucai Xiao committed
2340
        {
2341
            keep_dims = parse_value(info.attributes.at("keepdims")).at<int>();
Shucai Xiao's avatar
Shucai Xiao committed
2342
2343
2344
2345
        }

        if(keep_dims == 1)
        {
2346
            return prog.add_instruction(make_op(op_name, {{"axes", axes}}), std::move(args));
Shucai Xiao's avatar
Shucai Xiao committed
2347
2348
2349
        }
        else
        {
2350
            auto ins = prog.add_instruction(make_op(op_name, {{"axes", axes}}), std::move(args));
2351
            return prog.add_instruction(op::squeeze{axes}, ins);
2352
2353
        }
    }
2354

Shucai Xiao's avatar
Shucai Xiao committed
2355
    instruction_ref
2356
    parse_reduce_l1(const std::string&, node_info info, std::vector<instruction_ref> args)
Shucai Xiao's avatar
Shucai Xiao committed
2357
    {
2358
2359
        auto abs_ins = prog.add_instruction(make_op("abs"), args[0]);
        return parse_reduce_oper({}, "reduce_sum", std::move(info), {abs_ins});
Shucai Xiao's avatar
Shucai Xiao committed
2360
2361
2362
    }

    instruction_ref
2363
    parse_reduce_l2(const std::string&, node_info info, std::vector<instruction_ref> args)
Shucai Xiao's avatar
Shucai Xiao committed
2364
    {
2365
2366
2367
        auto square_ins = prog.add_instruction(make_op("mul"), args[0], args[0]);
        auto sum_ins    = parse_reduce_oper({}, "reduce_sum", std::move(info), {square_ins});
        return prog.add_instruction(make_op("sqrt"), sum_ins);
Shucai Xiao's avatar
Shucai Xiao committed
2368
2369
    }

2370
2371
    instruction_ref
    parse_reduce_log_sum(const std::string&, node_info info, std::vector<instruction_ref> args)
Shucai Xiao's avatar
Shucai Xiao committed
2372
    {
2373
2374
        auto sum_ins = parse_reduce_oper({}, "reduce_sum", std::move(info), std::move(args));
        return prog.add_instruction(make_op("log"), sum_ins);
Shucai Xiao's avatar
Shucai Xiao committed
2375
2376
    }

2377
2378
    instruction_ref
    parse_reduce_log_sum_exp(const std::string&, node_info info, std::vector<instruction_ref> args)
Shucai Xiao's avatar
Shucai Xiao committed
2379
    {
2380
2381
2382
        auto exp_ins = prog.add_instruction(make_op("exp"), args[0]);
        auto sum_ins = parse_reduce_oper({}, "reduce_sum", std::move(info), {exp_ins});
        return prog.add_instruction(make_op("log"), sum_ins);
Shucai Xiao's avatar
Shucai Xiao committed
2383
2384
    }

2385
2386
    instruction_ref
    parse_reduce_sum_square(const std::string&, node_info info, std::vector<instruction_ref> args)
Shucai Xiao's avatar
Shucai Xiao committed
2387
    {
2388
2389
        auto square_ins = prog.add_instruction(make_op("mul"), args[0], args[0]);
        return parse_reduce_oper({}, "reduce_sum", std::move(info), {square_ins});
Shucai Xiao's avatar
Shucai Xiao committed
2390
2391
    }

Shucai Xiao's avatar
Shucai Xiao committed
2392
    instruction_ref
2393
    parse_cast(const std::string&, node_info info, std::vector<instruction_ref> args)
2394
    {
2395
        if(!contains(info.attributes, "to"))
2396
2397
2398
2399
        {
            MIGRAPHX_THROW("PARSE_CAST: missing to type attribute!");
        }

2400
        int to_type        = parse_value(info.attributes.at("to")).at<int>();
2401
        shape::type_t type = get_type(to_type);
Shucai Xiao's avatar
Shucai Xiao committed
2402
        return prog.add_instruction(make_op("convert", {{"target_type", type}}), std::move(args));
2403
    }
Shucai Xiao's avatar
Shucai Xiao committed
2404

2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
    std::vector<instruction_ref>
    parse_split(const std::string&, node_info info, std::vector<instruction_ref> args)
    {
        int64_t axis = 0;
        if(contains(info.attributes, "axis"))
        {
            axis = parse_value(info.attributes.at("axis")).at<int>();
        }

        auto lens      = args[0]->get_shape().lens();
        int64_t n_rank = static_cast<int64_t>(lens.size());
        if((axis < -n_rank) || (axis >= n_rank))
        {
            MIGRAPHX_THROW("PARSE_SPLIT: axis attribute out of rank!");
        }
        int64_t tuned_axis = (axis < 0) ? axis + n_rank : axis;

        std::vector<int64_t> vec_splits;
        if(contains(info.attributes, "split"))
        {
            literal s = parse_value(info.attributes.at("split"));
            s.visit([&](auto v) { vec_splits.assign(v.begin(), v.end()); });

            if(std::accumulate(vec_splits.begin(), vec_splits.end(), int64_t(0)) !=
               static_cast<int64_t>(lens[tuned_axis]))
            {
                MIGRAPHX_THROW("PARSE_SPLIT: sum of split attribute unequal to dim size of axis!");
            }
        }
        // no split attribute, input is equally divided
        else
        {
            if((lens[tuned_axis] % info.num_outputs) != 0)
            {
                MIGRAPHX_THROW("PARSE_SPLIT: input cannot be equally divided into " +
                               to_string(info.num_outputs) + " splits!");
            }
            auto dl = lens[tuned_axis] / info.num_outputs;
            vec_splits.resize(info.num_outputs, dl);
        }

        std::vector<instruction_ref> ret_ins;
        int64_t start = 0;
        for(auto sl : vec_splits)
        {
            ret_ins.push_back(
                prog.add_instruction(op::slice{{axis}, {start}, {start + sl}}, args[0]));
            start += sl;
        }

        return ret_ins;
    }

kahmed10's avatar
kahmed10 committed
2458
2459
2460
2461
    instruction_ref
    parse_onehot(const std::string&, node_info info, std::vector<instruction_ref> args)
    {
        migraphx::argument depth_arg = args[1]->eval();
Shucai Xiao's avatar
Shucai Xiao committed
2462
        check_arg_empty(depth_arg, "PARSE_ONEHOT: depth - dynamic shape not supported");
kahmed10's avatar
kahmed10 committed
2463
2464
2465
        size_t depth = depth_arg.at<size_t>();

        int64_t axis = -1;
Shucai Xiao's avatar
Shucai Xiao committed
2466
2467
2468
2469
        if(contains(info.attributes, "axis"))
        {
            axis = info.attributes.at("axis").i();
        }
kahmed10's avatar
kahmed10 committed
2470

Shucai Xiao's avatar
Shucai Xiao committed
2471
        std::vector<float> depth_input(depth * depth, 0.0f);
kahmed10's avatar
kahmed10 committed
2472
2473
        for(int i = 0; i < depth; i++)
        {
Shucai Xiao's avatar
Shucai Xiao committed
2474
            depth_input[depth * i + i] = 1.0f;
kahmed10's avatar
kahmed10 committed
2475
2476
        }

Shucai Xiao's avatar
Shucai Xiao committed
2477
2478
2479
2480
2481
2482
2483
2484
        auto type = args[2]->get_shape().type();
        shape s{type, {depth, depth}};
        auto l_val      = prog.add_literal({s, depth_input});
        auto gather_out = prog.add_instruction(op::gather{0}, {l_val, args[0]});

        // Finally, we need a transpose to move the inner most dim to the axis dim
        int n_rank = gather_out->get_shape().lens().size();
        if(axis < -n_rank or axis >= n_rank)
kahmed10's avatar
kahmed10 committed
2485
        {
Shucai Xiao's avatar
Shucai Xiao committed
2486
            MIGRAPHX_THROW("PARSE_ONEHOT: axis out of range");
kahmed10's avatar
kahmed10 committed
2487
        }
Shucai Xiao's avatar
Shucai Xiao committed
2488
2489
2490
2491
2492
2493
2494
2495
2496
        int64_t tuned_axis = (axis < 0) ? axis + n_rank : axis;
        std::vector<int64_t> perm(n_rank - 1);
        std::iota(perm.begin(), perm.end(), 0);
        perm.insert(perm.begin() + tuned_axis, n_rank - 1);
        auto tr_out = prog.add_instruction(op::transpose{perm}, gather_out);
        auto lens   = tr_out->get_shape().lens();

        auto off_val       = prog.add_instruction(op::slice{{0}, {0}, {1}}, args[2]);
        auto on_val        = prog.add_instruction(op::slice{{0}, {1}, {2}}, args[2]);
2497
        auto diff          = prog.add_instruction(make_op("sub"), on_val, off_val);
Shucai Xiao's avatar
Shucai Xiao committed
2498
2499
        auto unsq_off_val  = prog.add_instruction(op::multibroadcast{lens}, off_val);
        auto unsq_diff_val = prog.add_instruction(op::multibroadcast{lens}, diff);
2500
2501
        auto l_mul         = prog.add_instruction(make_op("mul"), tr_out, unsq_diff_val);
        return prog.add_instruction(make_op("add"), l_mul, unsq_off_val);
kahmed10's avatar
kahmed10 committed
2502
2503
    }

kahmed10's avatar
kahmed10 committed
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
    instruction_ref
    parse_tile(const std::string&, const node_info&, std::vector<instruction_ref> args)
    {
        migraphx::argument arg_s = args[1]->eval();
        check_arg_empty(arg_s, "PARSE_TILE: dynamic shape is not supported");
        std::vector<std::int64_t> repeats;
        arg_s.visit([&](auto input) { repeats.assign(input.begin(), input.end()); });

        auto l0 = args[0];
        for(int i = 0; i < repeats.size(); i++)
        {
            auto l1 = l0;
            for(int j = 1; j < repeats[i]; j++)
            {
                l0 = prog.add_instruction(op::concat{i}, l0, l1);
            }
        }
        return l0;
    }

kahmed10's avatar
kahmed10 committed
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
    instruction_ref
    parse_range(const std::string&, const node_info&, std::vector<instruction_ref> args)
    {

        auto start_arg = args[0]->eval();
        check_arg_empty(start_arg, "PARSE_RANGE: start arg dynamic shape is not supported");
        auto limit_arg = args[1]->eval();
        check_arg_empty(limit_arg, "PARSE_RANGE: limit arg dynamic shape is not supported");
        auto delta_arg = args[2]->eval();
        check_arg_empty(delta_arg, "PARSE_RANGE: delta arg dynamic shape is not supported");

        assert(args[0]->get_shape().elements() == 1 and args[1]->get_shape().elements() == 1 and
               args[2]->get_shape().elements() == 1);

        instruction_ref l0;

        visit_all(start_arg, limit_arg, delta_arg)([&](auto start, auto limit, auto delta) {
            auto start_val = start.front();
            auto limit_val = limit.front();
            auto delta_val = delta.front();

            size_t num_elements = static_cast<size_t>(
                ceil(static_cast<double>(limit_val - start_val) / static_cast<double>(delta_val)));

            assert(num_elements > 0);

            using type = decltype(start_val);

            std::vector<type> range_vals(num_elements);

            std::generate(range_vals.begin(), range_vals.end(), [&]() {
                auto result = start_val;
                start_val += delta_val;
                return result;
            });

            l0 = prog.add_literal({shape{args[0]->get_shape().type(), {num_elements}}, range_vals});
        });
        return l0;
    }

2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
    enum class reduce_mode_t
    {
        sum  = 0,
        mean = 1,
        max  = 2
    };

    instruction_ref parse_embedding_bag(const node_info& info, std::vector<instruction_ref> args)
    {
        if(args[2]->get_shape().elements() != 1)
            MIGRAPHX_THROW("PARSE_EMBEDDING_BAG: MIGraphX only supports offsets of size 1");
        reduce_mode_t reduce_mode = reduce_mode_t::sum;
        if(contains(info.attributes, "mode"))
        {
            reduce_mode = static_cast<reduce_mode_t>(info.attributes.at("mode").i());
        }

        auto l0 = prog.add_instruction(op::gather{}, args[0], args[1]);
        switch(reduce_mode)
        {
2585
2586
2587
2588
2589
2590
2591
2592
2593
        case reduce_mode_t::sum:
            l0 = prog.add_instruction(make_op("reduce_sum", {{"axes", {0}}}), l0);
            break;
        case reduce_mode_t::mean:
            l0 = prog.add_instruction(make_op("reduce_mean", {{"axes", {0}}}), l0);
            break;
        case reduce_mode_t::max:
            l0 = prog.add_instruction(make_op("reduce_max", {{"axes", {0}}}), l0);
            break;
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
        }
        return l0;
    }

    instruction_ref
    parse_aten(const std::string&, const node_info& info, std::vector<instruction_ref> args)
    {
        if(contains(info.attributes, "operator"))
        {
            auto op_name = info.attributes.at("operator").s();
            if(op_name.find("embedding_bag") != std::string::npos)
            {
                return parse_embedding_bag(info, std::move(args));
            }
        }
        MIGRAPHX_THROW("PARSE_ATEN: unsupported custom operator");
    }

2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
    std::vector<instruction_ref>
    parse_dropout(const std::string&, const node_info&, std::vector<instruction_ref> args)
    {
        auto out = prog.add_instruction(make_op("identity"), args[0]);
        auto s   = args[0]->get_shape();
        std::vector<int8_t> vec(s.elements(), 1);
        shape mask_s{shape::bool_type, s.lens()};
        auto mask = prog.add_literal(literal(mask_s, vec));

        return {out, mask};
    }

Shucai Xiao's avatar
Shucai Xiao committed
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
    template <class T>
    std::vector<std::size_t> nonzero_indices(const std::vector<T>& data)
    {
        std::vector<std::size_t> indices;
        for(std::size_t i = 0; i < data.size(); ++i)
        {
            if(!float_equal(data[i], 0))
                indices.push_back(i);
        }

        return indices;
    }

    instruction_ref
    parse_nonzero(const std::string&, const node_info&, std::vector<instruction_ref> args)
    {
        migraphx::argument data_arg = args.back()->eval();
        check_arg_empty(data_arg, "PARSE_NONZERO: cannot support non-constant input!");

        std::vector<std::size_t> indices;
        data_arg.visit([&](auto val) {
            using val_type = std::remove_cv_t<typename decltype(val)::value_type>;
            std::vector<val_type> vec_data;
            vec_data.assign(val.begin(), val.end());
            indices = this->nonzero_indices(vec_data);
        });

        shape in_s = args[0]->get_shape();
        shape out_s{shape::int64_type, {in_s.lens().size(), indices.size()}};

        std::vector<int64_t> out_data(out_s.elements());
        for(std::size_t i = 0; i < indices.size(); ++i)
        {
            auto idx = in_s.multi(indices[i]);
            for(std::size_t j = 0; j < in_s.lens().size(); ++j)
            {
                out_data[out_s.index({j, i})] = idx[j];
            }
        }

        return prog.add_literal(literal(out_s, out_data));
    }

2667
2668
2669
2670
    instruction_ref parse_compare_op(const std::string&,
                                     const std::string& op_name,
                                     const node_info&,
                                     std::vector<instruction_ref> args)
2671
    {
2672
        auto l = add_broadcastable_binary_op(args[0], args[1], op_name);
2673
2674
        if(l->get_shape().type() != shape::bool_type)
        {
Shucai Xiao's avatar
Shucai Xiao committed
2675
            l = prog.add_instruction(make_op("convert", {{"target_type", shape::bool_type}}), l);
2676
2677
2678
2679
        }
        return l;
    }

Shucai Xiao's avatar
Shucai Xiao committed
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
    instruction_ref
    parse_upsample(const std::string&, const node_info& info, std::vector<instruction_ref> args)
    {
        if(contains(info.attributes, "mode"))
        {
            auto mode = info.attributes.at("mode").s();
            if(mode != "nearest")
            {
                MIGRAPHX_THROW("PARSE_UPSAMPLE: only nearest mode is supported!");
            }
        }

        auto arg_scale = args[1]->eval();
        check_arg_empty(arg_scale, "PARSE_UPSAMPLE: only constant scale is supported!");
        std::vector<float> vec_scale;
        arg_scale.visit([&](auto v) { vec_scale.assign(v.begin(), v.end()); });

        auto in_s    = args[0]->get_shape();
        auto in_lens = in_s.lens();
        if(in_lens.size() != vec_scale.size())
        {
            MIGRAPHX_THROW("PARSE_UPSAMPLE: ranks of input and scale are different!");
        }

        std::vector<std::size_t> out_lens(in_lens.size());
        std::transform(in_lens.begin(),
                       in_lens.end(),
                       vec_scale.begin(),
                       out_lens.begin(),
                       [&](auto idx, auto scale) { return static_cast<std::size_t>(idx * scale); });

        std::vector<float> idx_scale(in_lens.size());
        std::transform(
            out_lens.begin(),
            out_lens.end(),
            in_lens.begin(),
            idx_scale.begin(),
            [](auto od, auto id) { return (od == id) ? 1.0f : (id - 1.0f) / (od - 1.0f); });

        shape out_s{in_s.type(), out_lens};
        std::vector<int> ind(out_s.elements());

        // map out_idx to in_idx
        shape_for_each(out_s, [&](auto idx) {
            auto in_idx = idx;
            std::transform(idx.begin(),
                           idx.end(),
                           idx_scale.begin(),
                           in_idx.begin(),
                           // nearest mode
                           [](auto index, auto scale) {
                               return static_cast<std::size_t>(std::round(index * scale));
                           });

            ind[out_s.index(idx)] = static_cast<int64_t>(in_s.index(in_idx));
        });

        // reshape input to one-dimension
        std::vector<int64_t> rsp_lens = {static_cast<int64_t>(in_s.elements())};
        shape ind_s{shape::int32_type, out_lens};
        auto rsp     = prog.add_instruction(make_op("reshape", {{"dims", rsp_lens}}), args[0]);
        auto ins_ind = prog.add_literal(literal(ind_s, ind));
        return prog.add_instruction(make_op("gather", {{"axis", 0}}), rsp, ins_ind);
    }

Shucai Xiao's avatar
Shucai Xiao committed
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
    instruction_ref
    parse_where(const std::string&, const node_info&, std::vector<instruction_ref> args)
    {
        auto type = args[1]->get_shape().type();
        // the operation of if cond == 1 select x; else select y,
        // is equivalent to cond * (x - y) + y
        auto cond = prog.add_instruction(make_op("convert", {{"target_type", type}}), args[0]);
        auto diff = add_broadcastable_binary_op(args[1], args[2], "sub");
        auto cd   = add_broadcastable_binary_op(diff, cond, "mul");
        return add_broadcastable_binary_op(cd, args[2], "add");
    }

2757
    void parse_from(std::istream& is, std::string name = "")
Paul's avatar
Paul committed
2758
    {
2759
2760
2761
2762
2763
        this->filename   = std::move(name);
        auto parent_path = fs::path(this->filename).parent_path();
        if(not parent_path.empty())
            this->path = parent_path;

Paul's avatar
Paul committed
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
        onnx::ModelProto model;
        if(model.ParseFromIstream(&is))
        {
            if(model.has_graph())
            {
                this->parse_graph(model.graph());
            }
        }
        else
        {
Paul's avatar
Paul committed
2774
            MIGRAPHX_THROW("Failed reading onnx file.");
Paul's avatar
Paul committed
2775
2776
2777
        }
    }

Paul Fultz II's avatar
Paul Fultz II committed
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
    void parse_from(const void* data, std::size_t size)
    {
        onnx::ModelProto model;
        if(model.ParseFromArray(data, size))
        {
            if(model.has_graph())
            {
                this->parse_graph(model.graph());
            }
        }
        else
        {
            MIGRAPHX_THROW("Failed reading onnx file.");
        }
    }

Paul's avatar
Paul committed
2794
2795
    void parse_graph(const onnx::GraphProto& graph)
    {
2796
        for(auto&& f : graph.initializer())
2797
        {
2798
            instructions[f.name()] = prog.add_literal(parse_tensor(f));
2799
        }
2800

Paul's avatar
Paul committed
2801
2802
2803
        for(auto&& input : graph.input())
        {
            const std::string& name = input.name();
2804
2805
            // input not in initializer_data, so it is a real input
            if(!contains(instructions, name))
2806
            {
2807
2808
2809
2810
2811
2812
2813
                std::vector<std::size_t> dims;
                if(map_input_dims.count(name) > 0)
                {
                    dims = map_input_dims.at(name);
                }

                shape s            = parse_type(input.type(), dims);
2814
2815
                instructions[name] = prog.add_parameter(name, s);
            }
Paul's avatar
Paul committed
2816
        }
2817
2818

        for(auto&& node : graph.node())
Paul's avatar
Paul committed
2819
        {
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
            std::vector<instruction_ref> args;
            for(auto&& input : node.input())
            {
                if(input.empty())
                {
                    this->parse_undefined(input);
                }
                if(instructions.count(input) == 0)
                {
                    MIGRAPHX_THROW("PARSE_GRAPH: invalid onnx file. Input \"" + input +
                                   "\" is unavailable due to unordered nodes!");
                }
                args.push_back(instructions.at(input));
            }

            std::vector<instruction_ref> result;
            std::size_t output_num = static_cast<std::size_t>(node.output().size());
            if(ops.count(node.op_type()) == 0)
            {
2839
2840
2841
2842
                if(skip_unknown_operators)
                    result.push_back(prog.add_instruction(op::unknown{node.op_type()}, args));
                else
                    MIGRAPHX_THROW("Unknown operator: " + node.op_type());
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
            }
            else
            {
                result = ops[node.op_type()]({get_attributes(node), output_num}, args);
            }

            output_num = std::min<std::size_t>(output_num, result.size());
            std::transform(node.output().begin(),
                           node.output().begin() + output_num,
                           result.begin(),
                           std::inserter(instructions, instructions.end()),
                           [](auto&& x, auto&& y) { return std::make_pair(x, y); });
Paul's avatar
Paul committed
2855
        }
Shucai Xiao's avatar
Shucai Xiao committed
2856

2857
        // Find instructions corresponding to the output
Shucai Xiao's avatar
Shucai Xiao committed
2858
        auto prog_output = graph.output();
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
        std::vector<std::string> all_output_names;
        std::vector<std::string> prog_output_names;
        std::transform(prog_output.begin(),
                       prog_output.end(),
                       std::back_inserter(all_output_names),
                       [](auto& node) { return node.name(); });
        std::copy_if(
            all_output_names.begin(),
            all_output_names.end(),
            std::back_inserter(prog_output_names),
            [&](const auto& name) { return !(name.empty() or instructions.count(name) == 0); });

        std::vector<instruction_ref> output_ins;
        std::transform(prog_output_names.begin(),
                       prog_output_names.end(),
                       std::back_inserter(output_ins),
                       [&](const auto& name) { return instructions[name]; });

        // add the return instuction
        prog.add_return(output_ins);
Paul's avatar
Paul committed
2879
2880
    }

Shucai Xiao's avatar
Shucai Xiao committed
2881
    void parse_undefined(const std::string& name)
2882
    {
Shucai Xiao's avatar
Shucai Xiao committed
2883
2884
2885
2886
2887
        if(!contains(instructions, name))
        {
            auto ins           = prog.add_instruction(op::undefined{});
            instructions[name] = ins;
        }
2888
2889
    }

Paul's avatar
Paul committed
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
    static attribute_map get_attributes(const onnx::NodeProto& node)
    {
        std::unordered_map<std::string, onnx::AttributeProto> result;
        for(auto&& attr : node.attribute())
        {
            result[attr.name()] = attr;
        }
        return result;
    }

Shucai Xiao's avatar
Shucai Xiao committed
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
    static shape::type_t get_type(int dtype)
    {
        switch(dtype)
        {
        case 1: return shape::float_type;
        case 2: return shape::uint8_type;
        case 3: return shape::int8_type;
        case 4: return shape::uint16_type;
        case 5: return shape::int16_type;
        case 6: return shape::int32_type;
        case 7: return shape::int64_type;
        case 9: return shape::bool_type;
        case 10: return shape::half_type;
        case 11: return shape::double_type;
        case 12: return shape::uint32_type;
        case 13: return shape::uint64_type;
        default:
        {
            MIGRAPHX_THROW("Prototensor data type " + std::to_string(dtype) + " not supported");
        }
        }
    }

Paul's avatar
Paul committed
2923
2924
2925
2926
2927
2928
2929
    template <class T>
    static literal from_repeated(shape::type_t t, const T& r)
    {
        std::size_t size = r.size();
        return literal{{t, {size}}, r.begin(), r.end()};
    }

2930
    literal parse_value(const onnx::AttributeProto& attr) const
Paul's avatar
Paul committed
2931
2932
2933
2934
2935
2936
    {
        switch(attr.type())
        {
        case onnx::AttributeProto::FLOAT: return literal{attr.f()};
        case onnx::AttributeProto::INT: return literal{attr.i()};
        case onnx::AttributeProto::TENSOR: return parse_tensor(attr.t());
Paul's avatar
Paul committed
2937
        case onnx::AttributeProto::FLOATS: return from_repeated(shape::float_type, attr.floats());
Paul's avatar
Paul committed
2938
        case onnx::AttributeProto::INTS: return from_repeated(shape::int64_type, attr.ints());
Paul's avatar
Paul committed
2939
2940
2941
2942
2943
        case onnx::AttributeProto::UNDEFINED:
        case onnx::AttributeProto::GRAPH:
        case onnx::AttributeProto::STRING:
        case onnx::AttributeProto::STRINGS:
        case onnx::AttributeProto::TENSORS:
2944
2945
        case onnx::AttributeProto::SPARSE_TENSOR:
        case onnx::AttributeProto::SPARSE_TENSORS:
Paul's avatar
Paul committed
2946
2947
        case onnx::AttributeProto::GRAPHS: return {};
        }
Shucai Xiao's avatar
Shucai Xiao committed
2948
        MIGRAPHX_THROW("PARSE_VALUE: Invalid attribute type " + std::to_string(attr.type()));
Paul's avatar
Paul committed
2949
2950
    }

2951
    literal parse_tensor(const onnx::TensorProto& t) const
Paul's avatar
Paul committed
2952
2953
    {
        std::vector<std::size_t> dims(t.dims().begin(), t.dims().end());
2954
2955
2956
2957
2958
2959
2960
2961
        if(not t.external_data().empty())
        {
            const std::string& data_file = t.external_data().at(0).value();
            auto raw_buffer              = read_buffer(path + "/" + data_file);
            std::string s(raw_buffer.begin(), raw_buffer.end());
            auto type = get_type(t.data_type());
            return create_literal(type, dims, s.data());
        }
2962
2963
        if(t.has_raw_data())
        {
wsttiger's avatar
wsttiger committed
2964
            const std::string& s = t.raw_data();
Shucai Xiao's avatar
Shucai Xiao committed
2965
2966
            auto type            = get_type(t.data_type());
            return create_literal(type, dims, s.data());
2967
        }
Shucai Xiao's avatar
Shucai Xiao committed
2968

Paul's avatar
Paul committed
2969
2970
        switch(t.data_type())
        {
Shucai Xiao's avatar
Shucai Xiao committed
2971
2972
2973
2974
        case onnx::TensorProto::BOOL: return create_literal(shape::bool_type, dims, t.int32_data());
        case onnx::TensorProto::INT8: return create_literal(shape::int8_type, dims, t.int32_data());
        case onnx::TensorProto::UINT8:
            return create_literal(shape::uint8_type, dims, t.int32_data());
Paul's avatar
Paul committed
2975
        case onnx::TensorProto::INT16:
Shucai Xiao's avatar
Shucai Xiao committed
2976
2977
2978
            return create_literal(shape::int16_type, dims, t.int32_data());
        case onnx::TensorProto::UINT16:
            return create_literal(shape::uint16_type, dims, t.int32_data());
Paul's avatar
Paul committed
2979
        case onnx::TensorProto::INT32:
Khalique's avatar
Khalique committed
2980
            return create_literal(shape::int32_type, dims, t.int32_data());
Shucai Xiao's avatar
Shucai Xiao committed
2981
2982
        case onnx::TensorProto::UINT32:
            return create_literal(shape::uint32_type, dims, t.uint64_data());
Paul's avatar
Paul committed
2983
        case onnx::TensorProto::INT64:
Khalique's avatar
Khalique committed
2984
            return create_literal(shape::int64_type, dims, t.int64_data());
Shucai Xiao's avatar
Shucai Xiao committed
2985
2986
        case onnx::TensorProto::UINT64:
            return create_literal(shape::uint64_type, dims, t.uint64_data());
Paul's avatar
Paul committed
2987
        case onnx::TensorProto::FLOAT16:
Khalique's avatar
Khalique committed
2988
        {
Khalique's avatar
Khalique committed
2989
            std::vector<uint16_t> data_uint16(t.int32_data().begin(), t.int32_data().end());
2990
            std::vector<half> data_half;
Khalique's avatar
Khalique committed
2991
2992
2993
            std::transform(data_uint16.begin(),
                           data_uint16.end(),
                           std::back_inserter(data_half),
2994
                           [](uint16_t raw_val) { return *reinterpret_cast<half*>(&raw_val); });
2995
            return create_literal(shape::half_type, dims, data_half);
Khalique's avatar
Khalique committed
2996
        }
Shucai Xiao's avatar
Shucai Xiao committed
2997
2998
2999
3000
        case onnx::TensorProto::DOUBLE:
            return create_literal(shape::double_type, dims, t.double_data());
        case onnx::TensorProto::FLOAT:
            return create_literal(shape::float_type, dims, t.float_data());
Paul's avatar
Paul committed
3001
3002
3003
        case onnx::TensorProto::UNDEFINED:
        case onnx::TensorProto::STRING:
        case onnx::TensorProto::COMPLEX64:
Paul's avatar
Paul committed
3004
3005
        case onnx::TensorProto::COMPLEX128: throw std::runtime_error("");
        }
Shucai Xiao's avatar
Shucai Xiao committed
3006
        MIGRAPHX_THROW("PARSE_TENSOR: Invalid tensor type");
Paul's avatar
Paul committed
3007
3008
    }

Khalique's avatar
Khalique committed
3009
    static literal
3010
    create_literal(shape::type_t shape_type, const std::vector<size_t>& dims, const char* data)
3011
    {
Khalique's avatar
Khalique committed
3012
        // in case of scalar constants in onnx file, use dims=1 to fill initializer data
3013
        if(dims.empty())
3014
            return literal{{shape_type}, data};
3015
3016
3017
        return literal{{shape_type, dims}, data};
    }

3018
    template <class T, MIGRAPHX_REQUIRES(not std::is_pointer<T>{})>
Khalique's avatar
Khalique committed
3019
    static literal create_literal(shape::type_t shape_type, const std::vector<size_t>& dims, T data)
3020
3021
    {
        if(dims.empty())
3022
            return literal{{shape_type}, data.begin(), data.end()};
3023
        return literal{{shape_type, dims}, data.begin(), data.end()};
3024
3025
    }

3026
    shape parse_type(const onnx::TypeProto& t, const std::vector<std::size_t>& input_dims) const
Paul's avatar
Paul committed
3027
    {
Shucai Xiao's avatar
Shucai Xiao committed
3028
        shape::type_t shape_type = get_type(t.tensor_type().elem_type());
3029
3030
3031
3032
3033
        if(!input_dims.empty())
        {
            return {shape_type, input_dims};
        }

Paul's avatar
Paul committed
3034
        std::vector<std::size_t> dims;
Paul's avatar
Paul committed
3035
        auto&& tensor_dims = t.tensor_type().shape().dim();
3036
3037
3038
        std::transform(tensor_dims.begin(),
                       tensor_dims.end(),
                       std::back_inserter(dims),
3039
3040
                       [&](auto&& d) -> std::size_t {
                           if(d.has_dim_value())
3041
                           {
3042
                               if(static_cast<int>(d.dim_value()) <= 0)
3043
3044
3045
                               {
                                   return default_dim_value;
                               }
3046
                               return d.dim_value();
3047
                           }
3048
3049
3050
3051
                           else
                           {
                               return default_dim_value;
                           }
3052
                       });
3053

3054
3055
3056
        if(dims.empty())
            return {shape_type};

Paul's avatar
Paul committed
3057
3058
        return {shape_type, dims};
    }
3059

Shucai Xiao's avatar
Shucai Xiao committed
3060
3061
    void check_arg_empty(const argument& arg, const std::string& msg)
    {
Shucai Xiao's avatar
Shucai Xiao committed
3062
        if(arg.empty())
Shucai Xiao's avatar
Shucai Xiao committed
3063
3064
3065
3066
        {
            MIGRAPHX_THROW(msg);
        }
    }
Paul's avatar
Paul committed
3067
3068
};

Paul Fultz II's avatar
Paul Fultz II committed
3069
template <class... Ts>
3070
program parse_onnx_from(const onnx_options& options, Ts&&... xs)
Paul's avatar
Paul committed
3071
3072
{
    onnx_parser parser;
3073
3074
3075
    parser.map_input_dims         = options.map_input_dims;
    parser.default_dim_value      = options.default_dim_value;
    parser.skip_unknown_operators = options.skip_unknown_operators;
3076

3077
    if(options.print_program_on_error)
Paul's avatar
Paul committed
3078
    {
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
        // Log the program when it can't be parsed
        try
        {
            parser.parse_from(std::forward<Ts>(xs)...);
        }
        catch(...)
        {
            std::cerr << parser.prog << std::endl;
            throw;
        }
Paul's avatar
Paul committed
3089
    }
3090
    else
Paul's avatar
Paul committed
3091
    {
3092
        parser.parse_from(std::forward<Ts>(xs)...);
Paul's avatar
Paul committed
3093
3094
3095
3096
    }
    return std::move(parser.prog);
}

3097
program parse_onnx(const std::string& name, const onnx_options& options)
Paul Fultz II's avatar
Paul Fultz II committed
3098
3099
{
    std::fstream input(name.c_str(), std::ios::in | std::ios::binary);
3100
    return parse_onnx_from(options, input, name);
Paul Fultz II's avatar
Paul Fultz II committed
3101
3102
}

3103
program parse_onnx_buffer(const std::string& buffer, const onnx_options& options)
Paul Fultz II's avatar
Paul Fultz II committed
3104
3105
3106
3107
{
    return parse_onnx_from(options, buffer.data(), buffer.size());
}

3108
program parse_onnx_buffer(const void* data, std::size_t size, const onnx_options& options)
Paul Fultz II's avatar
Paul Fultz II committed
3109
3110
3111
3112
{
    return parse_onnx_from(options, data, size);
}

Paul's avatar
Paul committed
3113
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
3114
} // namespace migraphx