Publishing R3
[platform/upstream/dldt.git] / model-optimizer / extensions / ops / switch.py
1 """
2  Copyright (c) 2018 Intel Corporation
3
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
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
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.
15 """
16
17 import networkx as nx
18 import numpy as np
19 from mo.graph.graph import Node
20 from mo.ops.op import Op
21 from mo.utils.error import Error
22
23
24 class Switch(Op):
25     op = 'Switch'
26
27     def __init__(self, graph: nx.MultiDiGraph, attrs: dict):
28         mandatory_props = {
29             'type': __class__.op,
30             'op': __class__.op,
31             'infer': Switch.switch_infer,
32             'cf_infer': Switch.switch_control_flow_infer
33         }
34         super().__init__(graph, mandatory_props, attrs)
35
36     @staticmethod
37     def switch_infer(node: Node):
38         assert len(node.in_nodes()) == 2
39         tensor = node.in_node(0)
40         port_id = node.in_node(1)
41         if not port_id.has_valid('value'):
42             raise Error("Not supported type of switch (pred is variable)")
43         output_shape = tensor.shape
44         output_value = tensor.value
45         for _, out_node in node.graph.out_edges(node.id):
46             node.graph.node[out_node]['shape'] = np.array(output_shape)
47             node.graph.node[out_node]['value'] = None if output_value is None else np.array(output_value)
48
49     @staticmethod
50     def switch_control_flow_infer(node: Node, is_executable: bool, mark_executability: callable):
51         """
52         Infers control flow through switch operation node. It marks output data nodes executability according to
53         executability of current node and switch data value
54         :param node: Node instance to infer control flow through
55         :param is_executable: if current node is executable
56         :param mark_executability: function to mark executability of node
57         """
58         graph = node.graph
59         n = node.id
60
61         in_edges_with_data = graph.in_edges(n, data=True)
62         out_edges_with_data = graph.out_edges(n, data=True)
63         node_with_switch_value = [u for u, _, attrs in in_edges_with_data if 'in' in attrs and attrs['in'] == 1][0]
64         out_data_with_attrs = [(v, attrs) for u, v, attrs in out_edges_with_data if 'out' in attrs]
65         switch_data_0_port_node_id = [v for v, attrs in out_data_with_attrs if attrs['out'] == 0]
66         switch_data_1_port_node_id = [v for v, attrs in out_data_with_attrs if attrs['out'] == 1]
67         assert 1 <= len(switch_data_0_port_node_id) + len(switch_data_1_port_node_id) <= 2
68
69         switch_value = graph.node[node_with_switch_value]['value']
70         if switch_value:
71             # 1 port activation
72             for n in switch_data_0_port_node_id:
73                 mark_executability(n, False)
74             if is_executable:
75                 for n in switch_data_1_port_node_id:
76                     mark_executability(n, True)
77             else:
78                 for n in switch_data_1_port_node_id:
79                     mark_executability(n, False)
80         else:
81             # 0 port activation
82             for n in switch_data_1_port_node_id:
83                 mark_executability(n, False)
84             if is_executable:
85                 for n in switch_data_0_port_node_id:
86                     mark_executability(n, True)
87             else:
88                 for n in switch_data_0_port_node_id:
89                     mark_executability(n, False)