2 Copyright (c) 2018-2019 Intel Corporation
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
19 from mo.graph.graph import Node, Graph
20 from mo.ops.op import Op
26 def __init__(self, graph: Graph, attrs: dict):
29 'infer': __class__.infer,
30 'cf_infer': __class__.control_flow_infer
32 super().__init__(graph, mandatory_props, attrs)
35 def infer(node: Node):
36 assert len(node.in_nodes()) == 2
37 tensor = node.in_node(0)
38 port_id = node.in_node(1)
40 output_shape = tensor.shape
41 # Case with variable predicate
42 if not port_id.has_valid('value'):
44 for _, out_node in node.graph.out_edges(node.id):
45 node.graph.node[out_node]['shape'] = np.array(output_shape)
47 output_value = tensor.value
48 for _, out_node in node.graph.out_edges(node.id):
49 node.graph.node[out_node]['shape'] = np.array(output_shape)
50 node.graph.node[out_node]['value'] = None if output_value is None else np.array(output_value)
53 def control_flow_infer(node: Node, is_executable: bool, mark_executability: callable):
55 Infers control flow through switch operation node. It marks output data nodes executability according to
56 executability of current node and switch data value
57 :param node: Node instance to infer control flow through
58 :param is_executable: if current node is executable
59 :param mark_executability: function to mark executability of node
61 out_data_nodes = node.out_nodes(control_flow=True)
62 node_with_switch_value = node.in_node(1)
64 switch_data_0_port_node_id = [out_data_nodes[0].id] if 0 in out_data_nodes else []
65 switch_data_1_port_node_id = [out_data_nodes[1].id] if 1 in out_data_nodes else []
66 assert 1 <= len(switch_data_0_port_node_id) + len(switch_data_1_port_node_id) <= 2
68 if not node_with_switch_value.has_valid('value'):
69 # Mark both ports as executable
70 resulting_switch_data_node_ids = switch_data_0_port_node_id + switch_data_1_port_node_id
71 for n in resulting_switch_data_node_ids:
72 mark_executability(n, True)
74 switch_value = node_with_switch_value.value.item(0)
75 resulting_switch_data_node_ids = [switch_data_0_port_node_id, switch_data_1_port_node_id]
77 for n in resulting_switch_data_node_ids[not switch_value]:
78 mark_executability(n, False)
79 for n in resulting_switch_data_node_ids[switch_value]:
80 mark_executability(n, is_executable)