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

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