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.

_summary_adapter.py 11 kB

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. """The converter between proto format event of lineage and dict."""
  16. import time
  17. from mindinsight.datavisual.proto_files.mindinsight_lineage_pb2 import LineageEvent
  18. from mindinsight.lineagemgr.common.exceptions.exceptions import LineageParamTypeError, \
  19. LineageParamValueError
  20. from mindinsight.lineagemgr.common.log import logger as log
  21. def package_dataset_graph(graph):
  22. """
  23. Package dataset graph.
  24. Args:
  25. graph (dict): Dataset graph.
  26. Returns:
  27. LineageEvent, the proto message event contains dataset graph.
  28. """
  29. dataset_graph_event = LineageEvent()
  30. dataset_graph_event.wall_time = time.time()
  31. dataset_graph = dataset_graph_event.dataset_graph
  32. if "children" in graph:
  33. children = graph.pop("children")
  34. if children:
  35. _package_children(children=children, message=dataset_graph)
  36. _package_current_dataset(operation=graph, message=dataset_graph)
  37. return dataset_graph_event
  38. def _package_children(children, message):
  39. """
  40. Package children in dataset operation.
  41. Args:
  42. children (list[dict]): Child operations.
  43. message (DatasetGraph): Children proto message.
  44. """
  45. for child in children:
  46. if child:
  47. child_graph_message = getattr(message, "children").add()
  48. grandson = child.pop("children")
  49. if grandson:
  50. _package_children(children=grandson, message=child_graph_message)
  51. # package other parameters
  52. _package_current_dataset(operation=child, message=child_graph_message)
  53. def _package_current_dataset(operation, message):
  54. """
  55. Package operation parameters in event message.
  56. Args:
  57. operation (dict): Operation dict.
  58. message (Operation): Operation proto message.
  59. """
  60. for key, value in operation.items():
  61. if key == "operations":
  62. for operator in value:
  63. _package_enhancement_operation(
  64. operator,
  65. message.operations.add()
  66. )
  67. elif key == "sampler":
  68. _package_enhancement_operation(
  69. value,
  70. message.sampler
  71. )
  72. else:
  73. _package_parameter(key, value, message.parameter)
  74. def _package_enhancement_operation(operation, message):
  75. """
  76. Package enhancement operation in MapDataset.
  77. Args:
  78. operation (dict): Enhancement operation.
  79. message (Operation): Enhancement operation proto message.
  80. """
  81. for key, value in operation.items():
  82. if isinstance(value, list):
  83. if all(isinstance(ele, int) for ele in value):
  84. message.size.extend(value)
  85. else:
  86. message.weights.extend(value)
  87. else:
  88. _package_parameter(key, value, message.operationParam)
  89. def _package_parameter(key, value, message):
  90. """
  91. Package parameters in operation.
  92. Args:
  93. key (str): Operation name.
  94. value (Union[str, bool, int, float, list, None]): Operation args.
  95. message (OperationParameter): Operation proto message.
  96. """
  97. if isinstance(value, str):
  98. message.mapStr[key] = value
  99. elif isinstance(value, bool):
  100. message.mapBool[key] = value
  101. elif isinstance(value, int):
  102. message.mapInt[key] = value
  103. elif isinstance(value, float):
  104. message.mapDouble[key] = value
  105. elif isinstance(value, list) and key != "operations":
  106. if value:
  107. replace_value_list = list(map(lambda x: "" if x is None else x, value))
  108. message.mapStrList[key].strValue.extend(replace_value_list)
  109. elif value is None:
  110. message.mapStr[key] = "None"
  111. else:
  112. error_msg = "Parameter {} is not supported " \
  113. "in event package.".format(key)
  114. log.error(error_msg)
  115. raise LineageParamTypeError(error_msg)
  116. def organize_graph(graph_message):
  117. """
  118. Convert a dataset graph to its dict format.
  119. Args:
  120. graph_message (DatasetGraph): Graph event message.
  121. Returns:
  122. dict, dataset graph.
  123. """
  124. result = {}
  125. # update current dataset graph dict
  126. result.update(_organize_current_dataset(
  127. parameter=getattr(graph_message, 'parameter'),
  128. operations=getattr(graph_message, 'operations'),
  129. sampler=getattr(graph_message, 'sampler')
  130. ))
  131. # update children dataset graph dict
  132. result.update(
  133. _organize_children(getattr(graph_message, 'children'))
  134. )
  135. return result
  136. def _organize_children(children_message):
  137. """
  138. Convert children message to its dict format.
  139. Args:
  140. children_message (list[DatasetGraph]): Children message.
  141. Returns:
  142. dict, children dict of dataset graph.
  143. """
  144. children_list = []
  145. children_dict = {'children': children_list}
  146. if children_message:
  147. for child_event in children_message:
  148. child_dict = {}
  149. # update current dataset to child
  150. child_dict.update(
  151. _organize_current_dataset(
  152. parameter=getattr(child_event, 'parameter'),
  153. operations=getattr(child_event, 'operations'),
  154. sampler=getattr(child_event, 'sampler')
  155. )
  156. )
  157. # update child's children
  158. child_dict.update(
  159. _organize_children(getattr(child_event, 'children'))
  160. )
  161. children_list.append(child_dict)
  162. children_dict['children'] = children_list
  163. return children_dict
  164. def _organize_current_dataset(parameter, operations, sampler):
  165. """
  166. Convert current dataset message to its dict format.
  167. Note:
  168. Current dataset message include parameter, operations,
  169. sampler message of dataset graph event.
  170. Args:
  171. parameter (OperationParameter): Parameter message.
  172. operations (Operation): Operations message.
  173. sampler (Operation): Sampler message.
  174. Returns:
  175. dict, current dataset.
  176. """
  177. current_dataset = {}
  178. if parameter:
  179. current_dataset.update(
  180. _organize_parameter(parameter)
  181. )
  182. if operations:
  183. operation_list = []
  184. for operation in operations:
  185. operation_list.append(
  186. _organize_operation(operation)
  187. )
  188. current_dataset.update(
  189. {'operations': operation_list}
  190. )
  191. if sampler:
  192. if _organize_operation(sampler):
  193. current_dataset.update({
  194. 'sampler':
  195. _organize_operation(sampler)
  196. })
  197. return current_dataset
  198. def _organize_operation(operation):
  199. """
  200. Convert operation message to its dict format.
  201. Args:
  202. operation (Operation): Operation message.
  203. Returns:
  204. dict, operation.
  205. """
  206. operation_dict = {}
  207. operation_dict.update(_organize_parameter(getattr(operation, 'operationParam')))
  208. tmp_list = []
  209. repeated_keys = ['size', 'weights']
  210. for key in repeated_keys:
  211. for str_ele in getattr(operation, key):
  212. tmp_list.append(str_ele)
  213. dict()
  214. if tmp_list:
  215. operation_dict.update({key: tmp_list})
  216. return operation_dict
  217. def _organize_parameter(parameter):
  218. """
  219. Convert operation parameter message to its dict format.
  220. Args:
  221. parameter (OperationParameter): Operation parameter message.
  222. Returns:
  223. dict, operation parameter.
  224. """
  225. parameter_result = dict()
  226. parameter_keys = [
  227. 'mapStr',
  228. 'mapBool',
  229. 'mapInt',
  230. 'mapDouble',
  231. ]
  232. for parameter_key in parameter_keys:
  233. base_attr = getattr(parameter, parameter_key)
  234. parameter_value = dict(base_attr)
  235. # convert str 'None' to None
  236. for key, value in parameter_value.items():
  237. if value == 'None':
  238. parameter_value[key] = None
  239. parameter_result.update(parameter_value)
  240. # drop `mapStrList` and `strValue` keys in result parameter
  241. str_list_para = dict(getattr(parameter, 'mapStrList'))
  242. result_str_list_para = dict()
  243. for key, value in str_list_para.items():
  244. str_list_para_list = list()
  245. for str_ele in getattr(value, 'strValue'):
  246. str_list_para_list.append(str_ele)
  247. str_list_para_list = list(map(lambda x: None if x == '' else x, str_list_para_list))
  248. result_str_list_para[key] = str_list_para_list
  249. parameter_result.update(result_str_list_para)
  250. return parameter_result
  251. def package_user_defined_info(user_dict):
  252. """
  253. Package user defined info.
  254. Args:
  255. user_dict(dict): User defined info dict.
  256. Returns:
  257. LineageEvent, the proto message event contains user defined info.
  258. """
  259. user_event = LineageEvent()
  260. user_event.wall_time = time.time()
  261. user_defined_info = user_event.user_defined_info
  262. _package_user_defined_info(user_dict, user_defined_info)
  263. return user_event
  264. def _package_user_defined_info(user_defined_dict, user_defined_message):
  265. """
  266. Setting attribute in user defined proto message.
  267. Args:
  268. user_defined_dict (dict): User define info dict.
  269. user_defined_message (LineageEvent): Proto message of user defined info.
  270. Raises:
  271. LineageParamValueError: When the value is out of range.
  272. LineageParamTypeError: When given a type not support yet.
  273. """
  274. for key, value in user_defined_dict.items():
  275. if not isinstance(key, str):
  276. raise LineageParamTypeError("The key must be str.")
  277. if isinstance(value, int):
  278. attr_name = "map_int32"
  279. elif isinstance(value, float):
  280. attr_name = "map_double"
  281. elif isinstance(value, str):
  282. attr_name = "map_str"
  283. else:
  284. error_msg = "Value type {} is not supported in user defined event package." \
  285. "Only str, int and float are permitted now.".format(type(value))
  286. log.error(error_msg)
  287. raise LineageParamTypeError(error_msg)
  288. add_user_defined_info = user_defined_message.user_info.add()
  289. try:
  290. getattr(add_user_defined_info, attr_name)[key] = value
  291. except ValueError:
  292. raise LineageParamValueError("Value is out of range or not be supported yet.")

MindInsight为MindSpore提供了简单易用的调优调试能力。在训练过程中,可以将标量、张量、图像、计算图、模型超参、训练耗时等数据记录到文件中,通过MindInsight可视化页面进行查看及分析。