Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / mo / middle / passes / debug.py
1 """
2  Copyright (c) 2018-2019 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 from mo.graph.graph import *
18
19
20 def print_attributes_in_col(attrs: dict, indent: int, exclude_attrs: list):
21     for key in sorted(attrs.keys()):
22         if key not in exclude_attrs:
23             print(' ' * indent + key + ': ' + str(attrs[key]))
24
25
26 def debug_ir_emitter(graph, exclude_attrs: list = []):
27     print("--- DEBUG IR BEGIN ---")
28     # print nodes in topological order; print all attributes except 'pb'
29     nodes = nx.topological_sort(graph)
30     np.set_printoptions(threshold=10)
31     for node in nodes:
32         attrs = graph.node[node]
33         print('Node:', node)
34         if attrs['kind'] == 'op':
35             for idx, value in Node(graph, node).in_nodes().items():
36                 print('input', idx, ':', value.node,
37                       ': ' + str(graph.in_edges(value.node)[0][0]) if len(graph.in_edges(value.node)) else '')
38         if 'op' in attrs:
39             print('Op:', attrs['op'])
40         print_attributes_in_col(attrs, 4, exclude_attrs)
41         if attrs['kind'] == 'op':
42             for idx, value in Node(graph, node).out_nodes().items():
43                 print('output', idx, ':', value.node,
44                       ': ' + str(graph.out_edges(value.node)[0][1]) if len(graph.out_edges(value.node)) else '')
45         print('')
46     print("---  DEBUG IR END  ---")
47
48
49 def get_output_node_names(graph: Graph):
50     result = []
51     for node in graph.nodes():
52         node = Node(graph, node)
53         if len(node.out_nodes()) == 0:
54             result.append(node.in_node().name)
55     return result