MCPcopy Create free account
hub / github.com/ROCm/AMDMIGraphX / parse

Method parse

src/onnx/parse_if.cpp:52–131  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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 }))

Callers 1

register_op_parserFunction · 0.45

Calls 15

atMethod · 0.80
frontMethod · 0.80
can_evalMethod · 0.80
as_standardMethod · 0.80
equalFunction · 0.50
make_opFunction · 0.50
elementsMethod · 0.45
get_shapeMethod · 0.45
evalMethod · 0.45
parse_graphMethod · 0.45
create_moduleMethod · 0.45
get_output_shapesMethod · 0.45

Tested by

no test coverage detected