| 50 | } |
| 51 | |
| 52 | std::vector<instruction_ref> parse(const op_desc& /*opd*/, |
| 53 | onnx_parser& parser, |
| 54 | const onnx_parser::node_info& info, |
| 55 | std::vector<instruction_ref> args) const |
| 56 | { |
| 57 | const auto& then_graph = info.attributes.at("then_branch").g(); |
| 58 | const auto& else_graph = info.attributes.at("else_branch").g(); |
| 59 | |
| 60 | if(args.front()->get_shape().elements() != 1) |
| 61 | { |
| 62 | MIGRAPHX_THROW("PARSE_IF: " + info.name + |
| 63 | " condition input can have only one element!"); |
| 64 | } |
| 65 | |
| 66 | // Fold instruction if condition is constant thus can be evaled |
| 67 | // prior to inference |
| 68 | if(args.front()->can_eval()) |
| 69 | { |
| 70 | auto cond_arg = args.front()->eval(); |
| 71 | auto* mod = info.mod; |
| 72 | // then branch |
| 73 | if(cond_arg.at<bool>()) |
| 74 | { |
| 75 | return parser.parse_graph(mod, then_graph, true); |
| 76 | } |
| 77 | // else branch |
| 78 | else |
| 79 | { |
| 80 | return parser.parse_graph(mod, else_graph, true); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | std::string then_name = info.name + "_if"; |
| 85 | module_ref then_mdl = parser.prog.create_module(then_name); |
| 86 | |
| 87 | std::string else_name = info.name + "_else"; |
| 88 | module_ref else_mdl = parser.prog.create_module(else_name); |
| 89 | |
| 90 | // parse the then sub_graph |
| 91 | (void)parser.parse_graph(then_mdl, then_graph); |
| 92 | |
| 93 | // parse_the else sub_graph |
| 94 | (void)parser.parse_graph(else_mdl, else_graph); |
| 95 | |
| 96 | auto then_out_shapes = then_mdl->get_output_shapes(); |
| 97 | auto else_out_shapes = else_mdl->get_output_shapes(); |
| 98 | if(not std::equal(then_out_shapes.begin(), |
| 99 | then_out_shapes.end(), |
| 100 | else_out_shapes.begin(), |
| 101 | else_out_shapes.end())) |
| 102 | { |
| 103 | if(not std::equal(then_out_shapes.begin(), |
| 104 | then_out_shapes.end(), |
| 105 | else_out_shapes.begin(), |
| 106 | else_out_shapes.end(), |
| 107 | [](const shape& then_s, const shape& else_s) { |
| 108 | return then_s.as_standard() == else_s.as_standard(); |
| 109 | })) |
no test coverage detected