You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

debugger_graph.py 9.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """This file is used to define the basic graph."""
  16. from collections import deque
  17. from mindinsight.datavisual.data_transform.graph.msgraph import MSGraph
  18. from mindinsight.debugger.common.exceptions.exceptions import \
  19. DebuggerNodeNotInGraphError, DebuggerParamValueError
  20. from mindinsight.debugger.common.log import logger as log
  21. from .node import NodeTree
  22. class DebuggerGraph(MSGraph):
  23. """The `DebuggerGraph` object provides interfaces to describe a debugger graph."""
  24. def __init__(self):
  25. super(DebuggerGraph, self).__init__()
  26. self._node_tree = None
  27. def get_node_name_by_full_name(self, full_name):
  28. """Get node name by full names."""
  29. inner_name = self._full_name_map_name.get(full_name, '')
  30. if not inner_name:
  31. log.warning("Node %s does not find the relative inner node name.", full_name)
  32. return inner_name
  33. def get_full_name_by_node_name(self, node_name):
  34. """Get full name by node name for leaf nodes."""
  35. node = self._normal_node_map.get(node_name)
  36. if not node:
  37. log.warning("Node %s is not leaf node.", node_name)
  38. return node.full_name if node else ''
  39. def get_nodes(self, searched_node_list):
  40. """
  41. Search node names by a given pattern.
  42. Args:
  43. searched_node_list (list[Node]): A list of leaf nodes that
  44. matches the given search pattern.
  45. Returns:
  46. A list of dict including the searched nodes.
  47. [{
  48. "name": "Default",
  49. "type": "name_scope",
  50. "nodes": [{
  51. "name": "Default/Conv2D1",
  52. "type": "name_scope",
  53. "nodes": [{
  54. ...
  55. }]
  56. }]
  57. },
  58. {
  59. "name": "Gradients",
  60. "type": "name_scope",
  61. "nodes": [{
  62. "name": "Gradients/Default",
  63. "type": "name_scope",
  64. "nodes": [{
  65. ...
  66. }]
  67. }]
  68. """
  69. # save the node in the NodeTree
  70. self._node_tree = NodeTree()
  71. for node in searched_node_list:
  72. self._build_node_tree(node.name, node.type)
  73. # get the searched nodes in the NodeTree and reorganize them
  74. searched_list = []
  75. self._traverse_node_tree(self._node_tree, searched_list)
  76. return searched_list
  77. def search_nodes_by_pattern(self, pattern):
  78. """
  79. Search node by a given pattern.
  80. Args:
  81. pattern (Union[str, None]): The pattern of the node to search,
  82. if None, return all node names.
  83. Returns:
  84. list[Node], a list of node.
  85. """
  86. if pattern is not None:
  87. pattern = pattern.lower()
  88. searched_nodes = [
  89. node for name, node in self._leaf_nodes.items()
  90. if pattern in name.lower()
  91. ]
  92. else:
  93. searched_nodes = [node for _, node in self._leaf_nodes.items()]
  94. return searched_nodes
  95. def _build_node_tree(self, node_name, node_type):
  96. """Build node tree."""
  97. scope_names = node_name.split('/')
  98. cur_node = self._node_tree
  99. for scope_name in scope_names[:-1]:
  100. sub_node = cur_node.get(scope_name)
  101. if not sub_node:
  102. sub_node = cur_node.add(scope_name)
  103. cur_node = sub_node
  104. cur_node.add(scope_names[-1], node_type)
  105. def _traverse_node_tree(self, cur_node, search_node_list):
  106. """Traverse the watch nodes and update the total watched node list."""
  107. if not cur_node.get_children():
  108. return
  109. for _, sub_node in cur_node.get_children():
  110. sub_nodes = []
  111. self._traverse_node_tree(sub_node, sub_nodes)
  112. sub_node_dict = {
  113. 'name': sub_node.node_name,
  114. 'type': sub_node.node_type,
  115. 'nodes': sub_nodes
  116. }
  117. search_node_list.append(sub_node_dict)
  118. def get_node_type(self, node_name):
  119. """
  120. Get the type of the node.
  121. Args:
  122. node_name (str): The full name of the node with its scope.
  123. Returns:
  124. A string, leaf or name_scope.
  125. """
  126. if node_name and not self.exist_node(name=node_name):
  127. raise DebuggerNodeNotInGraphError(node_name=node_name)
  128. node = self._normal_node_map.get(node_name)
  129. return node.type
  130. def get_tensor_history(self, node_name, depth=0):
  131. """
  132. Get the tensor history of a specified node.
  133. Args:
  134. node_name (str): The debug name of the node.
  135. depth (int): The number of layers the user wants to trace. Default is 0.
  136. Returns:
  137. list, a list of the traced tensors' name and node type,
  138. arranged in order from leaf node to root node.
  139. int, the number of output tensors.
  140. """
  141. node = self._leaf_nodes.get(node_name)
  142. tensor_history = self._get_tensor_infos_of_node(node)
  143. cur_outputs_nums = len(tensor_history)
  144. cur_depth = 0
  145. trace_list = deque([(node, cur_depth)])
  146. while trace_list:
  147. cur_node, cur_depth = trace_list.popleft()
  148. tensors_info = self._get_input_tensors_of_node(cur_node)
  149. if tensors_info:
  150. tensor_history.extend(tensors_info)
  151. if cur_depth < depth:
  152. for name in cur_node.inputs.keys():
  153. trace_list.append((self._leaf_nodes[name], cur_depth + 1))
  154. return tensor_history, cur_outputs_nums
  155. @staticmethod
  156. def _get_tensor_infos_of_node(cur_node, slot=None):
  157. """Get tensors info of specified node."""
  158. tensors_info = []
  159. if slot is None:
  160. slots = range(cur_node.output_nums)
  161. elif slot >= 0:
  162. slots = [slot]
  163. else:
  164. log.info("Skip get tensor info for %s:%s.", cur_node.name, slot)
  165. return tensors_info
  166. for num in slots:
  167. tensor_info = {
  168. 'name': cur_node.name + ':' + str(num),
  169. 'full_name': cur_node.full_name + ':' + str(num),
  170. 'node_type': cur_node.type
  171. }
  172. tensors_info.append(tensor_info)
  173. return tensors_info
  174. def _get_input_tensors_of_node(self, cur_node):
  175. """Get input tensors of node."""
  176. tensors_info = []
  177. for name in cur_node.inputs.keys():
  178. node = self._leaf_nodes.get(name)
  179. tensor_info = self._get_tensor_infos_of_node(node)
  180. tensors_info.extend(tensor_info)
  181. return tensors_info
  182. def get_bfs_order(self):
  183. """
  184. Traverse the graph in order of breath-first search.
  185. Returns:
  186. list, including the leaf nodes arranged in BFS order.
  187. """
  188. root = self.get_default_root()
  189. log.info('Randomly choose node %s as root to do BFS.', root.name)
  190. bfs_order = []
  191. self.get_bfs_graph(root.name, bfs_order)
  192. length = len(self._leaf_nodes.keys())
  193. # Find rest un-traversed nodes
  194. for node_name, _ in self._leaf_nodes.items():
  195. if node_name not in bfs_order:
  196. self.get_bfs_graph(node_name, bfs_order)
  197. if len(bfs_order) != length:
  198. log.error("The length of bfs and leaf nodes are not equal.")
  199. msg = "Not all nodes are traversed!"
  200. raise DebuggerParamValueError(msg)
  201. return bfs_order
  202. def get_bfs_graph(self, node_name, bfs_order):
  203. """
  204. Traverse the graph in order of breath-first search.
  205. Returns:
  206. list, including the leaf nodes arranged in BFS order.
  207. """
  208. temp_list = deque()
  209. temp_list.append(node_name)
  210. while temp_list:
  211. node_name = temp_list.popleft()
  212. node = self._leaf_nodes.get(node_name)
  213. if not node:
  214. log.warning('Cannot find node %s in graph. Ignored.', node_name)
  215. continue
  216. bfs_order.append(node_name)
  217. if node.inputs:
  218. for name in node.inputs.keys():
  219. if name not in temp_list and name not in bfs_order:
  220. temp_list.append(name)
  221. if node.outputs:
  222. for name in node.outputs.keys():
  223. if name not in temp_list and name not in bfs_order:
  224. temp_list.append(name)
  225. def get_default_root(self):
  226. """
  227. Get a node as default root for BFS in graph. Using the
  228. leaf node with the smallest node id as the default root.
  229. Returns:
  230. str, the name of the default root.
  231. """
  232. default_root = None
  233. for _, item in self._leaf_nodes.items():
  234. if item.node_id == '1':
  235. default_root = item
  236. break
  237. if default_root is None:
  238. log.error("Abnormal graph. Invalid node for BFS.")
  239. msg = 'Abnormal graph. Invalid node for BFS.'
  240. raise DebuggerParamValueError(msg)
  241. return default_root