| 37 | std::vector<op_desc> operators() const { return {{"Loop"}}; } |
| 38 | |
| 39 | std::vector<instruction_ref> parse(const op_desc& /*opd*/, |
| 40 | onnx_parser& parser, |
| 41 | const onnx_parser::node_info& info, |
| 42 | std::vector<instruction_ref> args) const |
| 43 | { |
| 44 | // default value of the max_iter_num |
| 45 | int64_t max_iterations = parser.max_loop_iterations; |
| 46 | // iteration input is empty |
| 47 | if(args.at(0)->name() == "undefined") |
| 48 | { |
| 49 | shape iter_s{shape::int64_type}; |
| 50 | args[0] = info.add_literal(literal(iter_s, {max_iterations})); |
| 51 | } |
| 52 | else |
| 53 | { |
| 54 | auto arg_iters = args.at(0)->eval(); |
| 55 | if(not arg_iters.empty()) |
| 56 | { |
| 57 | max_iterations = arg_iters.at<int64_t>(); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // cap max_iter because loop uses static shapes with max_iter size and huge numbers |
| 62 | // here can cause overflow |
| 63 | if(max_iterations > parser.limit_max_iterations) |
| 64 | { |
| 65 | std::cerr << "WARNING: PARSE_LOOP max_iterations exceeds the maximum loop " |
| 66 | "iterations limit, it will be changed from " |
| 67 | << max_iterations << " to " << parser.limit_max_iterations << ".\n"; |
| 68 | max_iterations = parser.limit_max_iterations; |
| 69 | } |
| 70 | |
| 71 | // condition input is empty |
| 72 | if(args.at(1)->name() == "undefined") |
| 73 | { |
| 74 | shape cond_s{shape::bool_type}; |
| 75 | args[1] = info.add_literal(literal(cond_s, {true})); |
| 76 | } |
| 77 | |
| 78 | // retrieve the subgraph |
| 79 | const auto& sub_graph = info.attributes.at("body").g(); |
| 80 | std::string mod_name = info.name + "_loop"; |
| 81 | module_ref sub_mod = parser.prog.create_module(mod_name); |
| 82 | |
| 83 | // parse the sub_graph |
| 84 | (void)parser.parse_graph(sub_mod, sub_graph); |
| 85 | |
| 86 | auto ret = info.add_instruction( |
| 87 | make_op("loop", {{"max_iterations", max_iterations}}), args, {sub_mod}); |
| 88 | auto out_s = ret->get_shape(); |
| 89 | assert(out_s.type() == shape::tuple_type); |
| 90 | |
| 91 | const auto& vec_shapes = out_s.sub_shapes(); |
| 92 | std::vector<instruction_ref> out_inss; |
| 93 | for(std::size_t i = 0; i < vec_shapes.size(); ++i) |
| 94 | { |
| 95 | auto r = info.add_instruction(make_op("get_tuple_elem", {{"index", i}}), ret); |
| 96 | out_inss.push_back(r); |
nothing calls this directly
no test coverage detected