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.

watchpoint.py 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. """Define the watchpoint stream."""
  16. from mindinsight.datavisual.data_transform.graph.node import NodeTypeEnum
  17. from mindinsight.debugger.common.exceptions.exceptions import DebuggerParamValueError
  18. from mindinsight.debugger.common.log import logger as log
  19. from mindinsight.debugger.proto.debug_grpc_pb2 import SetCMD, WatchCondition
  20. WATCHPOINT_CONDITION_MAPPING = {
  21. 'INF': WatchCondition.Condition.inf,
  22. 'NAN': WatchCondition.Condition.nan,
  23. 'OVERFLOW': WatchCondition.Condition.overflow,
  24. 'MAX_GT': WatchCondition.Condition.max_gt,
  25. 'MAX_LT': WatchCondition.Condition.max_lt,
  26. 'MIN_GT': WatchCondition.Condition.min_gt,
  27. 'MIN_LT': WatchCondition.Condition.min_lt,
  28. 'MAX_MIN_GT': WatchCondition.Condition.max_min_gt,
  29. 'MAX_MIN_LT': WatchCondition.Condition.max_min_lt,
  30. 'MEAN_GT': WatchCondition.Condition.mean_gt,
  31. 'MEAN_LT': WatchCondition.Condition.mean_lt
  32. }
  33. class WatchNodeTree:
  34. """The WatchNode Node Structure."""
  35. NOT_WATCH = 0 # the scope node and the nodes below are not watched
  36. PARTIAL_WATCH = 1 # at least one node under the scope node is not watched
  37. TOTAL_WATCH = 2 # the scope node and the nodes below are all watched
  38. def __init__(self, node_name='', node_type=None, full_name='', watch_status=1):
  39. self._node_name = node_name
  40. self._full_name = full_name
  41. self._node_type = self._translate_node_type(node_type)
  42. self._watch_status = watch_status
  43. self._children = {}
  44. @property
  45. def node_name(self):
  46. """The property of node name."""
  47. return self._node_name
  48. @property
  49. def full_name(self):
  50. """The property of node name."""
  51. return self._full_name
  52. @property
  53. def node_type(self):
  54. """The property of node type."""
  55. return self._node_type
  56. @node_type.setter
  57. def node_type(self, value):
  58. """Set the node type."""
  59. self._node_type = self._translate_node_type(value)
  60. @property
  61. def watch_status(self):
  62. """The property of watch status about current node."""
  63. return self._watch_status
  64. def enable_watch_status(self):
  65. """The property of watch status about current node."""
  66. self._watch_status = WatchNodeTree.TOTAL_WATCH
  67. @staticmethod
  68. def _translate_node_type(node_type):
  69. """Translate node type to watch node type."""
  70. if not node_type or node_type == NodeTypeEnum.NAME_SCOPE.value or \
  71. node_type == NodeTypeEnum.AGGREGATION_SCOPE.value:
  72. return 'scope'
  73. return 'leaf'
  74. def get(self, sub_name):
  75. """Get sub node."""
  76. return self._children.get(sub_name)
  77. def get_children(self):
  78. """Get all childrens."""
  79. for name_scope, sub_watch_node in self._children.items():
  80. yield name_scope, sub_watch_node
  81. def add_node(self, node_name, node_type, full_name=''):
  82. """
  83. Add watch node to watch node tree.
  84. Args:
  85. node_name (str): The node name.
  86. node_type (str): The node type.
  87. full_name (str): The full name of node.
  88. """
  89. log.debug("Add node %s with type: %s, full_name: %s", node_name, node_type, full_name)
  90. scope_names = node_name.split('/', 1)
  91. if len(scope_names) == 1:
  92. if not self.get(node_name):
  93. self.add(node_name, node_type, full_name, watch_status=WatchNodeTree.TOTAL_WATCH)
  94. else:
  95. self.get(node_name).enable_watch_status()
  96. return
  97. scope_name, sub_names = scope_names
  98. sub_tree = self.get(scope_name)
  99. if not sub_tree:
  100. sub_tree = self.add(scope_name, watch_status=1)
  101. sub_tree.add_node(sub_names, node_type, full_name)
  102. def add(self, name, node_type=None, full_name='', watch_status=1):
  103. """Add sub WatchPointTree."""
  104. sub_name = '/'.join([self._node_name, name]) if self._node_name else name
  105. sub_tree = WatchNodeTree(sub_name, node_type, full_name, watch_status)
  106. self._children[name] = sub_tree
  107. return sub_tree
  108. def remove_node(self, node_name):
  109. """Remove sub node from current tree."""
  110. log.debug("Remove %s", node_name)
  111. scope_names = node_name.split('/', 1)
  112. sub_tree_name = scope_names[0]
  113. sub_tree = self._children.get(sub_tree_name)
  114. if not sub_tree:
  115. log.error("Failed to find node %s in WatchNodeTree.", sub_tree_name)
  116. raise DebuggerParamValueError("Failed to find node {}".format(sub_tree_name))
  117. if len(scope_names) > 1:
  118. sub_tree.remove_node(scope_names[1])
  119. if sub_tree.watch_status == WatchNodeTree.NOT_WATCH or len(scope_names) == 1:
  120. self._children.pop(sub_tree_name)
  121. self._watch_status = WatchNodeTree.PARTIAL_WATCH if self._children else \
  122. WatchNodeTree.NOT_WATCH
  123. class Watchpoint:
  124. """
  125. The class of watchpoint stream.
  126. Args:
  127. watchpoint_id (int): The id of Watchpoint.
  128. watch_condition (dict): The condition of Watchpoint.
  129. - condition (str): Accept `INF` or `NAN`.
  130. - param (list[float]): Not defined yet.
  131. """
  132. def __init__(self, watchpoint_id, watch_condition):
  133. self._id = watchpoint_id
  134. self._condition = watch_condition
  135. self._watch_node = WatchNodeTree()
  136. @property
  137. def watchpoint_id(self):
  138. """The property of watchpoint id."""
  139. return self._id
  140. @property
  141. def nodes(self):
  142. """The property of watch nodes."""
  143. return self._watch_node
  144. @property
  145. def condition(self):
  146. """The property of watch condition."""
  147. return self._condition
  148. def copy_nodes_from(self, other_watchpoint):
  149. """
  150. Copy nodes from other watchpoint.
  151. Args:
  152. other_watchpoint (Watchpoint): Other watchpoint.
  153. """
  154. self._watch_node = other_watchpoint.nodes
  155. def add_nodes(self, nodes):
  156. """Add node into watchcpoint."""
  157. if not nodes:
  158. log.warning("Add empty nodes.")
  159. return
  160. if not isinstance(nodes, list):
  161. nodes = [nodes]
  162. for node in nodes:
  163. self._watch_node.add_node(node.name, node.type, node.full_name)
  164. def remove_nodes(self, nodes):
  165. """Remove nodes from watchpoint."""
  166. if not nodes:
  167. return
  168. if not isinstance(nodes, list):
  169. nodes = [nodes]
  170. for node in nodes:
  171. node_name = node.split(':')[0]
  172. self._watch_node.remove_node(node_name)
  173. def get_node_status(self, node_name, node_type, full_name):
  174. """Judge if the node is in watch nodes."""
  175. scope_names = node_name.split('/')
  176. cur_node = self._watch_node
  177. status = 1
  178. for scope_name in scope_names:
  179. cur_node = cur_node.get(scope_name)
  180. if cur_node is None:
  181. status = WatchNodeTree.NOT_WATCH
  182. break
  183. if cur_node.watch_status == WatchNodeTree.TOTAL_WATCH:
  184. status = WatchNodeTree.TOTAL_WATCH
  185. break
  186. if status == WatchNodeTree.TOTAL_WATCH and cur_node.node_name != node_name:
  187. self._watch_node.add_node(node_name, node_type, full_name)
  188. return status
  189. def get_watch_node(self, cur_watch_node, watch_node_list):
  190. """
  191. Traverse the watch nodes and add total watched node list to `watch_node_list`.
  192. Args:
  193. cur_watch_node (WatchNodeTree): The current watch node.
  194. watch_node_list (list[WatchNodeTree]): The list of total watched node.
  195. """
  196. if cur_watch_node.watch_status == WatchNodeTree.TOTAL_WATCH:
  197. watch_node_list.append(cur_watch_node)
  198. return
  199. for _, watch_node in cur_watch_node.get_children():
  200. self.get_watch_node(watch_node, watch_node_list)
  201. def get_set_cmd(self):
  202. """Return the watchpoint in proto format."""
  203. # get watch nodes.
  204. watch_nodes = []
  205. self.get_watch_node(self._watch_node, watch_nodes)
  206. # construct SetCMD
  207. set_cmd = SetCMD()
  208. set_cmd.id = self._id
  209. set_cmd.delete = False
  210. set_cmd.watch_condition.condition = WATCHPOINT_CONDITION_MAPPING.get(
  211. self._condition.get('condition'))
  212. if self._condition.get('param'):
  213. # at most one param is provided
  214. set_cmd.watch_condition.value = self._condition.get('param')
  215. for watch_node in watch_nodes:
  216. event_node = set_cmd.watch_nodes.add()
  217. event_node.node_name = watch_node.full_name
  218. event_node.node_type = watch_node.node_type
  219. return set_cmd
  220. def get_watch_condition_info(self):
  221. """Get watch condition info."""
  222. watchpoint_info = {
  223. 'id': self._id,
  224. 'watch_condition': self._condition
  225. }
  226. return watchpoint_info
  227. class WatchpointHit:
  228. """The watchpoint hit structure."""
  229. def __init__(self, tensor_proto, watchpoint, node_name):
  230. self._node_name = node_name
  231. self._full_name = tensor_proto.node_name
  232. self._slot = tensor_proto.slot
  233. self._watchpoint = watchpoint
  234. @property
  235. def tensor_full_name(self):
  236. """The property of tensor_name."""
  237. tensor_name = ':'.join([self._full_name, self._slot])
  238. return tensor_name
  239. @property
  240. def tensor_name(self):
  241. """The property of tensor_name."""
  242. return ':'.join([self._node_name, self._slot])
  243. @property
  244. def watchpoint(self):
  245. """The property of watchpoint."""
  246. watchpoint = self._watchpoint.get_watch_condition_info()
  247. return watchpoint
  248. def __eq__(self, other):
  249. """Define the equal condition."""
  250. flag = self.tensor_full_name == other.tensor_full_name and self.watchpoint == other.watchpoint
  251. return flag