| 523 | } |
| 524 | |
| 525 | std::vector<instruction_ref> |
| 526 | onnx_parser::parse_graph(module* mod, const onnx::GraphProto& graph, bool inlining) |
| 527 | { |
| 528 | std::vector<size_t> node_indices(graph.node_size()); |
| 529 | |
| 530 | if(check_sorted(graph, parent_input_nodes)) |
| 531 | { |
| 532 | std::iota(node_indices.begin(), node_indices.end(), 0); |
| 533 | } |
| 534 | else |
| 535 | { |
| 536 | std::cerr << "Warning: onnx model is not topologically sorted. Attempting to sort..." |
| 537 | << std::endl; |
| 538 | node_indices = toposort(graph); |
| 539 | } |
| 540 | |
| 541 | std::unordered_map<std::string, instruction_ref> mod_insts = |
| 542 | parse_intializer(*this, mod, graph); |
| 543 | |
| 544 | mod_insts = parse_inputs(*this, mod, graph, mod_insts); |
| 545 | |
| 546 | std::copy(mod_insts.begin(), mod_insts.end(), std::inserter(instructions, instructions.end())); |
| 547 | |
| 548 | for(auto& node_index : node_indices) |
| 549 | { |
| 550 | const onnx::NodeProto& node = graph.node(node_index); |
| 551 | if(enabled(MIGRAPHX_TRACE_ONNX_PARSER{})) |
| 552 | { |
| 553 | std::cout << "operator: " << node.op_type() << '\t' << node.name() << std::endl; |
| 554 | for(auto&& attr : node.attribute()) |
| 555 | { |
| 556 | std::cout << " " << attr.name() << " = " << to_string(attr) << std::endl; |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | std::vector<instruction_ref> args; |
| 561 | for(auto&& input : node.input()) |
| 562 | { |
| 563 | if(input.empty()) |
| 564 | { |
| 565 | this->parse_undefined(mod, input); |
| 566 | } |
| 567 | if(instructions.count(input) == 0) |
| 568 | { |
| 569 | MIGRAPHX_THROW("PARSE_GRAPH: invalid onnx file. Input \"" + input + |
| 570 | "\" is unavailable due to unordered nodes!"); |
| 571 | } |
| 572 | args.push_back(instructions.at(input)); |
| 573 | } |
| 574 | |
| 575 | std::vector<instruction_ref> result; |
| 576 | std::size_t output_num = node.output().size(); |
| 577 | if(ops.count(node.op_type()) == 0) |
| 578 | { |
| 579 | if(skip_unknown_operators) |
| 580 | result.push_back(mod->add_instruction(op::unknown{node.op_type()}, args)); |
| 581 | else |
| 582 | MIGRAPHX_THROW("Unknown operator: " + node.op_type()); |
no test coverage detected