#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace migraphx { inline namespace MIGRAPHX_INLINE_NS { struct onnx_parser { using attribute_map = std::unordered_map; using node_map = std::unordered_map; using op_func = std::function(attribute_map, std::vector)>; node_map nodes; std::unordered_map instructions; program prog = program(); bool is_pytorch = false; std::unordered_map ops; std::unordered_map map_actv_funcs; onnx_parser() { add_generic_op("MatMul", op::dot{}); add_generic_op("Relu", op::relu{}); add_generic_op("Sigmoid", op::sigmoid{}); add_generic_op("Abs", op::abs{}); add_generic_op("Exp", op::exp{}); add_generic_op("Log", op::log{}); // disable dropout for inference add_generic_op("Dropout", op::identity{}); add_generic_op("Identity", op::identity{}); add_generic_op("Sin", op::sin{}); add_generic_op("Cos", op::cos{}); add_generic_op("Tan", op::tan{}); add_generic_op("Sinh", op::sinh{}); add_generic_op("Cosh", op::cosh{}); add_generic_op("Tanh", op::tanh{}); add_generic_op("Asin", op::asin{}); add_generic_op("Acos", op::acos{}); add_generic_op("Atan", op::atan{}); add_binary_op("Add", op::add{}); add_binary_op("Div", op::div{}); add_binary_op("Mul", op::mul{}); add_binary_op("Sub", op::sub{}); add_variadic_op("Sum", op::add{}); add_variadic_op("Max", op::max{}); add_variadic_op("Min", op::min{}); add_mem_op("ImageScaler", &onnx_parser::parse_imagescaler); add_mem_op("LeakyRelu", &onnx_parser::parse_leaky_relu); add_mem_op("Elu", &onnx_parser::parse_elu); add_mem_op("Constant", &onnx_parser::parse_constant); add_mem_op("Conv", &onnx_parser::parse_conv); add_mem_op("MaxPool", &onnx_parser::parse_pooling); add_mem_op("AveragePool", &onnx_parser::parse_pooling); add_mem_op("GlobalMaxPool", &onnx_parser::parse_pooling); add_mem_op("GlobalAveragePool", &onnx_parser::parse_pooling); add_mem_op("Reshape", &onnx_parser::parse_reshape); add_mem_op("Flatten", &onnx_parser::parse_flatten); add_mem_op("Gemm", &onnx_parser::parse_gemm); add_mem_op("BatchNormalization", &onnx_parser::parse_batchnorm); add_mem_op("Softmax", &onnx_parser::parse_softmax); add_mem_op("Squeeze", &onnx_parser::parse_squeeze); add_mem_op("Unsqueeze", &onnx_parser::parse_unsqueeze); add_mem_op("Slice", &onnx_parser::parse_slice); add_mem_op("Concat", &onnx_parser::parse_concat); add_mem_op("Gather", &onnx_parser::parse_gather); add_mem_op("Shape", &onnx_parser::parse_shape); add_mem_op("ConstantFill", &onnx_parser::parse_constant_fill); add_mem_op("Transpose", &onnx_parser::parse_transpose); add_mem_op("RNN", &onnx_parser::parse_rnn); add_mem_op("Pad", &onnx_parser::parse_pad); // init the activation function map init_actv_func(); } void init_actv_func() { map_actv_funcs.insert(std::make_pair("tanh", op::tanh{})); map_actv_funcs.insert(std::make_pair("relu", op::relu{})); map_actv_funcs.insert(std::make_pair("sigmoid", op::sigmoid{})); map_actv_funcs.insert(std::make_pair("leakyrelu", op::leaky_relu{})); map_actv_funcs.insert(std::make_pair("elu", op::elu{})); } template void add_op(std::string name, F f) { ops.emplace(name, [=](auto&&... xs) { return std::vector{f(std::forward(xs)...)}; }); } // Multi output op template void add_multi_op(std::string name, F f) { ops.emplace(name, f); } template void add_mem_op(std::string name, F f) { add_op(name, [=](auto&&... xs) { return std::mem_fn(f)(*this, name, std::forward(xs)...); }); } template void add_binary_op(std::string name, T x) { add_op(name, [this, x](attribute_map attributes, std::vector args) { if(args.size() != 2) MIGRAPHX_THROW("binary operators should have 2 operands"); if(contains(attributes, "broadcast") and contains(attributes, "axis")) { uint64_t broadcasted = parse_value(attributes.at("broadcast")).at(); if(broadcasted != 0) { uint64_t axis = parse_value(attributes.at("axis")).at(); auto l = prog.add_instruction(op::broadcast{axis, args[0]->get_shape()}, args[1]); return prog.add_instruction(x, args[0], l); } return prog.add_instruction(x, args); } else { return add_broadcastable_binary_op(args[0], args[1], x); } }); } template instruction_ref add_broadcastable_binary_op(instruction_ref arg0, instruction_ref arg1, T x) { if(arg0->get_shape() != arg1->get_shape()) { // 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) // // Get lengths for both arguments const std::vector* s0 = &arg0->get_shape().lens(); const std::vector* s1 = &arg1->get_shape().lens(); // Make sure s0 is the smaller size if(s0->size() > s1->size()) std::swap(s0, s1); std::vector output_lens(*s1); auto offset = s1->size() - s0->size(); std::transform(s0->begin(), s0->end(), s1->begin() + offset, output_lens.begin() + offset, [](auto a, auto b) { return std::max(a, b); }); auto l0 = prog.add_instruction(op::multibroadcast{output_lens}, arg0); auto l1 = prog.add_instruction(op::multibroadcast{output_lens}, arg1); return prog.add_instruction(x, l0, l1); } else { return prog.add_instruction(x, {arg0, arg1}); } } template void add_generic_op(std::string name, T x) { add_op(name, [this, x](attribute_map, std::vector args) { return prog.add_instruction(x, args); }); } template void add_variadic_op(std::string name, T x) { add_op(name, [this, x](attribute_map, std::vector args) { return std::accumulate(std::next(args.begin()), args.end(), args.front(), [this, x](instruction_ref a, instruction_ref b) { return add_broadcastable_binary_op(a, b, x); }); }); } instruction_ref parse_softmax(const std::string&, const attribute_map&, std::vector args) { auto dims = args.front()->get_shape().lens(); auto r = prog.add_instruction(op::reshape{{long(dims[0]), long(dims[1]), 1, 1}}, args.front()); auto s = prog.add_instruction(op::softmax{}, r); return prog.add_instruction(op::reshape{{long(dims[0]), long(dims[1])}}, s); } instruction_ref parse_conv(const std::string&, attribute_map attributes, std::vector args) { op::convolution op; auto l0 = args[0]; if(contains(attributes, "pads")) { if(contains(attributes, "auto_pad")) { MIGRAPHX_THROW("auto_pad and padding cannot be specified simultaneously"); } std::vector padding; copy(attributes["pads"].ints(), std::back_inserter(padding)); if(padding.size() != 4) { MIGRAPHX_THROW("padding should have 4 values"); } if(padding[0] != padding[2] || padding[1] != padding[3]) { // insert zeros for pad op (args[0] has 4 dims) padding = {0, 0, padding[0], padding[1], 0, 0, padding[2], padding[3]}; l0 = prog.add_instruction(op::pad{padding}, l0); } else { op.padding[0] = padding[0]; op.padding[1] = padding[1]; } } if(contains(attributes, "strides")) { copy(attributes["strides"].ints(), op.stride.begin()); } if(contains(attributes, "dilations")) { copy(attributes["dilations"].ints(), op.dilation.begin()); } if(contains(attributes, "auto_pad")) { auto s = attributes["auto_pad"].s(); if(contains(attributes, "pads") and to_upper(s) != "NOTSET") { MIGRAPHX_THROW("auto_pad and padding cannot be specified simultaneously"); } if(s.find("SAME") != std::string::npos) { op.padding_mode = op::padding_mode_t::same; } } if(contains(attributes, "group")) { op.group = parse_value(attributes.at("group")).at(); } if(args.size() == 3) { uint64_t axis = 1; auto l1 = prog.add_instruction(op, args[0], args[1]); auto l2 = prog.add_instruction(op::broadcast{axis, l1->get_shape()}, args[2]); return prog.add_instruction(op::add{}, l1, l2); } return prog.add_instruction(op, l0, args[1]); } instruction_ref parse_pooling(const std::string& name, attribute_map attributes, std::vector args) { op::pooling op{ends_with(name, "MaxPool") ? "max" : "average"}; auto l0 = args[0]; if(starts_with(name, "Global")) { auto lens = args.front()->get_shape().lens(); op.lengths = {lens[2], lens[3]}; } if(contains(attributes, "pads")) { std::vector padding; copy(attributes["pads"].ints(), std::back_inserter(padding)); if(padding.size() != 4) { MIGRAPHX_THROW("padding should have 4 values"); } if(padding[0] != padding[2] || padding[1] != padding[3]) { // insert zeros for pad op (args[0] has 4 dims) padding = {0, 0, padding[0], padding[1], 0, 0, padding[2], padding[3]}; l0 = prog.add_instruction(op::pad{padding}, l0); } else { op.padding[0] = padding[0]; op.padding[1] = padding[1]; } } if(contains(attributes, "strides")) { copy(attributes["strides"].ints(), op.stride.begin()); } if(contains(attributes, "kernel_shape")) { copy(attributes["kernel_shape"].ints(), op.lengths.begin()); } if(contains(attributes, "auto_pad")) { auto s = attributes["auto_pad"].s(); if(s.find("SAME_UPPER") == std::string::npos) { MIGRAPHX_THROW("auto_pad only supports SAME_UPPER for pooling"); } op.padding_mode = op::padding_mode_t::same; } return prog.add_instruction(op, l0); } instruction_ref parse_reshape(const std::string&, attribute_map attributes, std::vector args) { op::reshape op; if(args.size() == 1) { literal s = parse_value(attributes.at("shape")); s.visit([&](auto v) { copy(v, std::back_inserter(op.dims)); }); } if(args.size() == 2) { literal s = args[1]->get_literal(); s.visit([&](auto v) { copy(v, std::back_inserter(op.dims)); }); } return prog.add_instruction(op, args[0]); } instruction_ref parse_flatten(const std::string&, attribute_map attributes, std::vector args) { uint64_t axis = 1; if(contains(attributes, "axis")) { axis = parse_value(attributes.at("axis")).at(); } return prog.add_instruction(op::flatten{axis}, args[0]); } instruction_ref parse_squeeze(const std::string&, attribute_map attributes, std::vector args) { op::squeeze op; literal s = parse_value(attributes.at("axes")); s.visit([&](auto v) { copy(v, std::back_inserter(op.axes)); }); return prog.add_instruction(op, args[0]); } instruction_ref parse_unsqueeze(const std::string&, attribute_map attributes, std::vector args) { op::unsqueeze op; literal s = parse_value(attributes.at("axes")); s.visit([&](auto v) { copy(v, std::back_inserter(op.axes)); }); return prog.add_instruction(op, args[0]); } instruction_ref parse_concat(const std::string&, attribute_map attributes, std::vector args) { std::size_t axis = parse_value(attributes.at("axis")).at(); op::concat op{axis}; return prog.add_instruction(op, std::move(args)); } instruction_ref parse_gather(const std::string&, attribute_map attributes, std::vector args) { int axis = 0; if(contains(attributes, "axis")) { axis = parse_value(attributes.at("axis")).at(); } op::gather op{axis}; return prog.add_instruction(op, std::move(args)); } instruction_ref parse_slice(const std::string&, attribute_map attributes, std::vector args) { op::slice op; if(contains(attributes, "axes")) { literal s = parse_value(attributes.at("axes")); s.visit([&](auto v) { copy(v, std::back_inserter(op.axes)); }); } { literal s = parse_value(attributes.at("ends")); s.visit([&](auto v) { copy(v, std::back_inserter(op.ends)); }); } { literal s = parse_value(attributes.at("starts")); s.visit([&](auto v) { copy(v, std::back_inserter(op.starts)); }); } return prog.add_instruction(op, args[0]); } instruction_ref parse_constant(const std::string&, attribute_map attributes, const std::vector&) { literal v = parse_value(attributes.at("value")); return prog.add_literal(v); } instruction_ref parse_gemm(const std::string&, attribute_map attributes, std::vector args) { float alpha = 1.0f; float beta = 1.0f; bool transa = false; bool transb = false; if(contains(attributes, "alpha")) { alpha = parse_value(attributes.at("alpha")).at(); } if(contains(attributes, "beta")) { beta = parse_value(attributes.at("beta")).at(); } if(contains(attributes, "transA")) { transa = parse_value(attributes.at("transA")).at(); } if(contains(attributes, "transB")) { transb = parse_value(attributes.at("transB")).at(); } std::vector perm = {1, 0}; 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]; if(args.size() == 3) { if(beta != 0.f) { auto l3 = prog.add_instruction(op::dot{alpha}, l1, l2); auto l4 = args[2]; if(l4->get_shape().scalar()) // ignore args[2] (no C value added to alpha*A*B) return l3; if(beta != 1.f) { auto beta_val = prog.add_literal(beta); auto l5 = prog.add_instruction(op::scalar{args[2]->get_shape()}, beta_val); l4 = prog.add_instruction(op::mul{}, args[2], l5); } return add_broadcastable_binary_op(l3, l4, op::add{}); } } return prog.add_instruction(op::dot{alpha, beta}, l1, l2); } instruction_ref parse_batchnorm(const std::string&, attribute_map attributes, std::vector args) { float epsilon = 1e-5f; float momentum = 0.9f; op::batch_norm_inference::bn_infer_mode_t bn_mode = op::batch_norm_inference::spatial; bool is_test = false; if(contains(attributes, "epsilon")) { epsilon = parse_value(attributes.at("epsilon")).at(); } if(contains(attributes, "momentum")) { momentum = parse_value(attributes.at("momentum")).at(); } if(contains(attributes, "is_test")) { is_test = parse_value(attributes.at("is_test")).at() > 0; } if(contains(attributes, "spatial")) { bn_mode = (parse_value(attributes.at("spatial")).at() > 0) ? op::batch_norm_inference::spatial : op::batch_norm_inference::per_activation; } (void)is_test; op::batch_norm_inference op{epsilon, momentum, bn_mode}; return prog.add_instruction(op, std::move(args)); } instruction_ref parse_leaky_relu(const std::string&, attribute_map attributes, std::vector args) { float alpha = 0.01; // default alpha val for leaky relu if(contains(attributes, "alpha")) { alpha = parse_value(attributes.at("alpha")).at(); } op::leaky_relu op{alpha}; return prog.add_instruction(op, args.front()); } instruction_ref parse_elu(const std::string&, attribute_map attributes, std::vector args) { float alpha = 1.0; // default alpha val for elu if(contains(attributes, "alpha")) { alpha = parse_value(attributes.at("alpha")).at(); } op::elu op{alpha}; return prog.add_instruction(op, args.front()); } instruction_ref parse_imagescaler(const std::string&, attribute_map attributes, std::vector args) { float scale = 1.0; std::vector bias{}; if(contains(attributes, "scale")) { scale = parse_value(attributes.at("scale")).at(); } if(contains(attributes, "bias")) { auto&& bias_floats = attributes["bias"].floats(); bias = std::vector(bias_floats.begin(), bias_floats.end()); } auto input_shape = args.front()->get_shape(); auto scale_val = prog.add_literal(scale); auto bias_vals = prog.add_literal( migraphx::literal{migraphx::shape{migraphx::shape::float_type, {bias.size()}}, bias}); auto scale_tensor = prog.add_instruction(migraphx::op::scalar{input_shape}, scale_val); auto img_scaled = prog.add_instruction(migraphx::op::mul{}, args.front(), scale_tensor); auto bias_bcast = prog.add_instruction(migraphx::op::broadcast{1, input_shape}, bias_vals); return prog.add_instruction(migraphx::op::add{}, img_scaled, bias_bcast); } instruction_ref parse_transpose(const std::string&, attribute_map attributes, std::vector args) { std::vector perm{}; if(contains(attributes, "perm")) { auto&& perm_vals = attributes["perm"].ints(); perm = std::vector(perm_vals.begin(), perm_vals.end()); } return prog.add_instruction(migraphx::op::transpose{perm}, args.front()); } instruction_ref parse_pad(const std::string&, attribute_map attributes, std::vector args) { std::vector pads{}; float value = 0.0f; if(contains(attributes, "pads")) { auto&& pad_vals = attributes["pads"].ints(); pads = std::vector(pad_vals.begin(), pad_vals.end()); } if(contains(attributes, "value")) { value = parse_value(attributes.at("value")).at(); } if(contains(attributes, "mode")) { auto mode = attributes.at("mode").s(); if(mode != "constant") MIGRAPHX_THROW("migraphx currently only supports constant padding"); } return prog.add_instruction(migraphx::op::pad{pads, value}, args.front()); } // Use a literal instruction to replace the shape since, output of // shape operator are literals in migraphx instruction_ref parse_shape(const std::string&, const attribute_map&, std::vector args) { if(args.size() != 1) MIGRAPHX_THROW("Shape: operator should have 1 operand"); std::vector arg_shape = args[0]->get_shape().lens(); std::vector 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 instruction_ref parse_constant_fill(const std::string&, attribute_map attributes, std::vector args) { int input_as_shape = 0; int dtype = 1; float value = 0.0f; if(contains(attributes, "dtype")) { dtype = parse_value(attributes.at("dtype")).at(); } migraphx::shape::type_t type = get_type(dtype); if(contains(attributes, "input_as_shape")) { input_as_shape = parse_value(attributes.at("input_as_shape")).at(); } if(contains(attributes, "value")) { value = parse_value(attributes.at("value")).at(); } if(contains(attributes, "extra_shape")) { MIGRAPHX_THROW("ConstantFill: cannot handle extra shape attribute"); } if(input_as_shape == 1) { if(args.size() != 1) { MIGRAPHX_THROW("ConstantFill: need an input argument as output shape"); } if(contains(attributes, "shape")) { MIGRAPHX_THROW("ConstantFill: cannot set the shape argument and pass in an input " "at the same time"); } migraphx::argument in = args[0]->eval(); if(in.empty()) { MIGRAPHX_THROW("ConstantFill: cannot handle dynamic shape as input"); } std::vector dims; in.visit([&](auto input) { dims.assign(input.begin(), input.end()); }); migraphx::shape s(type, dims); std::vector values(s.elements(), value); return prog.add_literal(migraphx::literal(s, values)); } else if(input_as_shape == 0) { if(!contains(attributes, "shape")) { MIGRAPHX_THROW("ConstantFill: attribute output shape is needed"); } literal ls = parse_value(attributes.at("shape")); std::vector dims; ls.visit([&](auto s) { dims.assign(s.begin(), s.end()); }); migraphx::shape s{type, dims}; std::vector values(s.elements(), value); return prog.add_literal(migraphx::literal(s, values)); } else { MIGRAPHX_THROW("ConstantFill: wrong value of attribute input_as_shape"); } } std::vector parse_rnn(const std::string&, attribute_map attributes, std::vector args) { migraphx::shape input_shape = args[0]->get_shape(); migraphx::shape w_shape = args[1]->get_shape(); std::size_t hidden_size = w_shape.lens()[1]; if(contains(attributes, "hidden_size")) { std::size_t hidden_size_att = parse_value(attributes.at("hidden_size")).at(); if(hidden_size != hidden_size_att) { MIGRAPHX_THROW("RNN: hidden size mismatch in input and attribute"); } } // Handling of direction to be added later std::string direction{"forward"}; if(contains(attributes, "direction")) { direction = attributes.at("direction").s(); } op::rnn::rnn_direction_t dirct = op::rnn::forward; if(direction == "bidirectional") { dirct = op::rnn::bidirectional; } else if(direction == "reverse") { dirct = op::rnn::reverse; } std::vector vec_names{"tanh"}; if(contains(attributes, "activations")) { auto names = attributes.at("activations").strings(); vec_names.clear(); for_each(names.begin(), names.end(), [&](auto& fn) { vec_names.push_back(fn); }); } for_each(vec_names.begin(), vec_names.end(), [&](auto& fn) { if(map_actv_funcs.count(fn) == 0) { MIGRAPHX_THROW("RNN: activation function " + std::string(fn) + " not supported"); } }); // bidirectional case should have two activation functions. // one is for forward, and the other is for reverse. // if only one actv function is provided, we use it in both // forward and reverse direction if(dirct == op::rnn::bidirectional) { if(vec_names.size() == 1) { vec_names.push_back(vec_names.at(0)); } } std::vector vec_actv_funcs(vec_names.size()); std::transform(vec_names.begin(), vec_names.end(), vec_actv_funcs.begin(), [&](auto& fn) { return map_actv_funcs[fn]; }); // To be added later float clip = 0.0; if(contains(attributes, "clip")) { clip = parse_value(attributes.at("clip")).at(); } // if the number of arguments is less than 6, append // undefined operator to have 6 arguments if(args.size() < 6) { auto ins = prog.add_instruction(op::undefined{}); args.insert(args.end(), (6 - args.size()), ins); } std::vector result; // first output for the concatenation of hidden states auto hidden_states = prog.add_instruction(op::rnn{hidden_size, vec_actv_funcs, dirct, clip}, std::move(args)); result.push_back(hidden_states); // second output for the last hidden state auto last_output = prog.add_instruction(op::rnn_last_output{}, hidden_states); result.push_back(last_output); return result; } void parse_from(std::istream& is) { onnx::ModelProto model; if(model.ParseFromIstream(&is)) { if(model.has_graph()) { this->parse_graph(model.graph()); } } else { MIGRAPHX_THROW("Failed reading onnx file."); } } void parse_graph(const onnx::GraphProto& graph) { nodes = get_nodes(graph); std::unordered_map initializer_data; for(auto&& f : graph.initializer()) { initializer_data[f.name()] = f; } for(auto&& input : graph.input()) { const std::string& name = input.name(); // Does the input have an initializer? if(contains(initializer_data, name)) { auto t = initializer_data[name]; instructions[name] = prog.add_literal(parse_tensor(t)); } else { // TODO: Get shape of input parameter shape s = parse_type(input.type()); instructions[name] = prog.add_parameter(name, s); } } for(auto&& p : nodes) { this->parse_node(p.first); } } void parse_undefined(const std::string& name) { auto ins = prog.add_instruction(op::undefined{}); instructions[name] = ins; } void parse_node(const std::string& name) { if(name.empty()) MIGRAPHX_THROW("Onnx node must have a name"); if(instructions.count(name) == 0) { auto&& node = nodes.at(name); std::vector args; for(auto&& input : node.input()) { if(nodes.count(input) > 0) { assert(name != input); this->parse_node(input); } else if(input.empty()) { this->parse_undefined(input); } args.push_back(instructions.at(input)); } std::vector result; if(ops.count(node.op_type()) == 0) { result.push_back(prog.add_instruction(unknown{node.op_type()}, args)); } else { result = ops[node.op_type()](get_attributes(node), args); } // Even no output nodes produce output in migraphx if(node.output().empty() and result.size() == 1) { instructions[name] = result.front(); } else { assert(node.output().size() >= result.size()); std::transform(result.begin(), result.end(), node.output().begin(), std::inserter(instructions, instructions.end()), [](auto&& x, auto&& y) { return std::make_pair(y, x); }); } } } static attribute_map get_attributes(const onnx::NodeProto& node) { std::unordered_map result; for(auto&& attr : node.attribute()) { result[attr.name()] = attr; } return result; } static node_map get_nodes(const onnx::GraphProto& graph) { std::unordered_map result; std::size_t n = 0; for(auto&& node : graph.node()) { if(node.output().empty()) { if(node.name().empty()) { result["migraphx_unamed_node_" + std::to_string(n)] = node; n++; } else { result[node.name()] = node; } } for(auto&& output : node.output()) { result[output] = node; } } return result; } template 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()}; } static literal parse_value(const onnx::AttributeProto& attr) { switch(attr.type()) { case onnx::AttributeProto::UNDEFINED: return {}; case onnx::AttributeProto::FLOAT: return literal{attr.f()}; case onnx::AttributeProto::INT: return literal{attr.i()}; case onnx::AttributeProto::STRING: return {}; case onnx::AttributeProto::TENSOR: return parse_tensor(attr.t()); case onnx::AttributeProto::GRAPH: return {}; case onnx::AttributeProto::FLOATS: return from_repeated(shape::float_type, attr.floats()); case onnx::AttributeProto::INTS: return from_repeated(shape::int64_type, attr.ints()); case onnx::AttributeProto::STRINGS: return {}; case onnx::AttributeProto::TENSORS: return {}; case onnx::AttributeProto::GRAPHS: return {}; } MIGRAPHX_THROW("Invalid attribute type"); } static literal parse_tensor(const onnx::TensorProto& t) { std::vector dims(t.dims().begin(), t.dims().end()); // in case of scalar constants in onnx file, use dims=1 to fill initializer data if(dims.empty()) { dims = {1}; } if(t.has_raw_data()) { const std::string& s = t.raw_data(); switch(t.data_type()) { case onnx::TensorProto::UNDEFINED: throw std::runtime_error(""); case onnx::TensorProto::FLOAT: return literal{{shape::float_type, dims}, s.data()}; case onnx::TensorProto::UINT8: throw std::runtime_error(""); case onnx::TensorProto::INT8: return literal{{shape::int32_type, dims}, s.data()}; case onnx::TensorProto::UINT16: return literal{{shape::int32_type, dims}, s.data()}; case onnx::TensorProto::INT16: return literal{{shape::int32_type, dims}, s.data()}; case onnx::TensorProto::INT32: return literal{{shape::int32_type, dims}, s.data()}; case onnx::TensorProto::INT64: return literal{{shape::int64_type, dims}, s.data()}; case onnx::TensorProto::STRING: throw std::runtime_error(""); case onnx::TensorProto::BOOL: return literal{{shape::int32_type, dims}, s.data()}; case onnx::TensorProto::FLOAT16: return literal{{shape::half_type, dims}, s.data()}; case onnx::TensorProto::DOUBLE: return literal{{shape::double_type, dims}, s.data()}; case onnx::TensorProto::UINT32: throw std::runtime_error(""); case onnx::TensorProto::UINT64: throw std::runtime_error(""); case onnx::TensorProto::COMPLEX64: throw std::runtime_error(""); case onnx::TensorProto::COMPLEX128: throw std::runtime_error(""); } MIGRAPHX_THROW("Invalid tensor type"); } switch(t.data_type()) { case onnx::TensorProto::UNDEFINED: throw std::runtime_error(""); case onnx::TensorProto::FLOAT: return literal{{shape::float_type, dims}, t.float_data().begin(), t.float_data().end()}; case onnx::TensorProto::UINT8: throw std::runtime_error(""); case onnx::TensorProto::INT8: return literal{{shape::int32_type, dims}, t.int32_data().begin(), t.int32_data().end()}; case onnx::TensorProto::UINT16: return literal{{shape::int32_type, dims}, t.int32_data().begin(), t.int32_data().end()}; case onnx::TensorProto::INT16: return literal{{shape::int32_type, dims}, t.int32_data().begin(), t.int32_data().end()}; case onnx::TensorProto::INT32: return literal{{shape::int32_type, dims}, t.int32_data().begin(), t.int32_data().end()}; case onnx::TensorProto::INT64: return literal{{shape::int64_type, dims}, t.int64_data().begin(), t.int64_data().end()}; case onnx::TensorProto::STRING: throw std::runtime_error(""); case onnx::TensorProto::BOOL: return literal{{shape::int32_type, dims}, t.int32_data().begin(), t.int32_data().end()}; case onnx::TensorProto::FLOAT16: return literal{{shape::half_type, dims}, t.float_data().begin(), t.float_data().end()}; case onnx::TensorProto::DOUBLE: return literal{ {shape::double_type, dims}, t.double_data().begin(), t.double_data().end()}; case onnx::TensorProto::UINT32: throw std::runtime_error(""); case onnx::TensorProto::UINT64: throw std::runtime_error(""); case onnx::TensorProto::COMPLEX64: throw std::runtime_error(""); case onnx::TensorProto::COMPLEX128: throw std::runtime_error(""); } MIGRAPHX_THROW("Invalid tensor type"); } static shape parse_type(const onnx::TypeProto& t) { shape::type_t shape_type{}; switch(t.tensor_type().elem_type()) { case onnx::TensorProto::UNDEFINED: break; // throw std::runtime_error("Unsupported type UNDEFINED"); case onnx::TensorProto::FLOAT: shape_type = shape::float_type; break; case onnx::TensorProto::UINT8: break; // throw std::runtime_error("Unsupported type UINT8"); case onnx::TensorProto::INT8: shape_type = shape::int8_type; break; case onnx::TensorProto::UINT16: shape_type = shape::uint16_type; break; case onnx::TensorProto::INT16: shape_type = shape::int16_type; break; case onnx::TensorProto::INT32: shape_type = shape::int32_type; break; case onnx::TensorProto::INT64: shape_type = shape::int64_type; break; case onnx::TensorProto::STRING: break; // throw std::runtime_error("Unsupported type STRING"); case onnx::TensorProto::BOOL: break; // throw std::runtime_error("Unsupported type BOOL"); case onnx::TensorProto::FLOAT16: shape_type = shape::half_type; break; case onnx::TensorProto::DOUBLE: shape_type = shape::double_type; break; case onnx::TensorProto::UINT32: shape_type = shape::uint32_type; break; case onnx::TensorProto::UINT64: shape_type = shape::uint64_type; break; case onnx::TensorProto::COMPLEX64: break; // throw std::runtime_error("Unsupported type COMPLEX64"); case onnx::TensorProto::COMPLEX128: break; // throw std::runtime_error("Unsupported type COMPLEX128"); } std::vector dims; auto&& tensor_dims = t.tensor_type().shape().dim(); std::transform(tensor_dims.begin(), tensor_dims.end(), std::back_inserter(dims), [](auto&& d) -> std::size_t { if(not d.has_dim_value()) { long default_batch_size = 1; // FIXME return default_batch_size; } return d.dim_value(); }); return {shape_type, dims}; } 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 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"); } } } }; program parse_onnx(const std::string& name) { std::fstream input(name.c_str(), std::ios::in | std::ios::binary); onnx_parser parser; #ifndef NDEBUG // Log the program when it can't be parsed try { parser.parse_from(input); } catch(...) { std::cerr << parser.prog << std::endl; throw; } #else parser.parse_from(input); #endif return std::move(parser.prog); } } // namespace MIGRAPHX_INLINE_NS } // namespace migraphx