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.

metadata_handler.py 6.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. # Copyright 2020-2021 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 metadata stream handler."""
  16. from mindinsight.debugger.common.log import LOGGER as log
  17. from mindinsight.debugger.common.utils import ServerStatus, DebuggerServerMode
  18. from mindinsight.debugger.stream_handler.base_handler import StreamHandlerBase
  19. class MetadataHandler(StreamHandlerBase):
  20. """Metadata Handler."""
  21. def __init__(self):
  22. self._state = ServerStatus.PENDING
  23. self._device_name = ""
  24. self.step = 0
  25. self._client_ip = ""
  26. self._cur_node_name = ""
  27. self._cur_full_name = ""
  28. self.backend = ""
  29. self._enable_recheck = False
  30. self._cur_graph_name = ""
  31. # If recommendation_confirmed is true, it only means the user has answered yes or no to the question,
  32. # it does not necessarily mean that the user will use the recommended watch points.
  33. self._recommendation_confirmed = False
  34. self._debugger_version = {}
  35. # maximum step number among all devices
  36. self._max_step_num = 0
  37. self._debugger_type = DebuggerServerMode.ONLINE.value
  38. @property
  39. def debugger_type(self):
  40. """The property of debugger_type."""
  41. return self._debugger_type
  42. @debugger_type.setter
  43. def debugger_type(self, debugger_type):
  44. """The property of debugger_type."""
  45. self._debugger_type = debugger_type
  46. @property
  47. def device_name(self):
  48. """The property of device name."""
  49. return self._device_name
  50. @property
  51. def node_name(self):
  52. """The property of current node name."""
  53. return self._cur_node_name
  54. @node_name.setter
  55. def node_name(self, node_name):
  56. """The property of current node name."""
  57. self._cur_node_name = node_name
  58. @property
  59. def graph_name(self):
  60. """The property of current node name."""
  61. return self._cur_graph_name
  62. @graph_name.setter
  63. def graph_name(self, graph_name):
  64. """The property of current node name."""
  65. self._cur_graph_name = graph_name if graph_name else ''
  66. @property
  67. def full_name(self):
  68. """The property of current node name."""
  69. return self._cur_full_name
  70. @property
  71. def state(self):
  72. """The property of state."""
  73. return self._state.value
  74. @state.setter
  75. def state(self, value):
  76. """
  77. Set the property of state.
  78. Args:
  79. value (str): The new state.
  80. """
  81. self._state = ServerStatus(value)
  82. @property
  83. def client_ip(self):
  84. """The property of client ip."""
  85. return self._client_ip
  86. @client_ip.setter
  87. def client_ip(self, value):
  88. """
  89. Set the property of client ip.
  90. Args:
  91. value (str): The new ip.
  92. """
  93. self._client_ip = str(value)
  94. @property
  95. def enable_recheck(self):
  96. """The property of enable_recheck."""
  97. return self._enable_recheck and self._state == ServerStatus.WAITING
  98. @enable_recheck.setter
  99. def enable_recheck(self, value):
  100. """
  101. Set the property of enable_recheck.
  102. Args:
  103. value (bool): The new ip.
  104. """
  105. self._enable_recheck = bool(value)
  106. @property
  107. def recommendation_confirmed(self):
  108. """The property of recommendation_confirmed."""
  109. return self._recommendation_confirmed
  110. @recommendation_confirmed.setter
  111. def recommendation_confirmed(self, value):
  112. """
  113. Set the property of recommendation_confirmed.
  114. Args:
  115. value (str): The new ip.
  116. """
  117. self._recommendation_confirmed = value
  118. @property
  119. def debugger_version(self):
  120. """The property of debugger_version."""
  121. return self._debugger_version
  122. @debugger_version.setter
  123. def debugger_version(self, value):
  124. """
  125. Set the property of debugger_version.
  126. Args:
  127. value (dict): The semantic versioning of mindinsight and mindspore,
  128. format is {'ms': 'x.x.x', 'mi': 'x.x.x'}.
  129. """
  130. self._debugger_version = value
  131. @property
  132. def max_step_num(self):
  133. """The property of max_step_num."""
  134. return self._max_step_num
  135. @max_step_num.setter
  136. def max_step_num(self, max_step_num):
  137. """Set the property of max_step_num."""
  138. self._max_step_num = max_step_num
  139. def put(self, value):
  140. """
  141. Put value into metadata cache. Called by grpc server.
  142. Args:
  143. value (MetadataProto): The Metadata proto message.
  144. """
  145. self._device_name = value.device_name.split(':')[0]
  146. self.step = value.cur_step
  147. self._cur_full_name = value.cur_node
  148. self.backend = value.backend if value.backend else "Ascend"
  149. log.debug("Put metadata into cache at the %d-th step.", self.step)
  150. def get(self, filter_condition=None):
  151. """
  152. Get updated value. Called by main server.
  153. Args:
  154. filter_condition (Union[str, list[str]]): The filter property.
  155. Returns:
  156. dict, the metadata.
  157. """
  158. metadata = {}
  159. if filter_condition is None:
  160. metadata = {
  161. 'state': self.state,
  162. 'step': self.step,
  163. 'device_name': self.device_name,
  164. 'pos': '0',
  165. 'ip': self.client_ip,
  166. 'node_name': self.node_name,
  167. 'backend': self.backend,
  168. 'enable_recheck': self.enable_recheck,
  169. 'graph_name': self.graph_name,
  170. 'recommendation_confirmed': self._recommendation_confirmed,
  171. 'debugger_version': self.debugger_version
  172. }
  173. if self.debugger_type == 'offline':
  174. metadata['total_step_num'] = self.max_step_num
  175. else:
  176. if not isinstance(filter_condition, list):
  177. filter_condition = [filter_condition]
  178. for field in filter_condition:
  179. metadata[field] = getattr(self, field) if \
  180. hasattr(self, field) else None
  181. return {'metadata': metadata}