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.

lineage_summary_analyzer.py 6.4 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 module provides python APIs to get lineage summary from summary log."""
  16. import struct
  17. from collections import namedtuple
  18. from enum import Enum
  19. from mindinsight.datavisual.proto_files.mindinsight_summary_pb2 import Event
  20. from mindinsight.datavisual.utils import crc32
  21. from mindinsight.lineagemgr.common.exceptions.exceptions import MindInsightException, \
  22. LineageVerificationException, LineageSummaryAnalyzeException
  23. from mindinsight.lineagemgr.common.log import logger as log
  24. from mindinsight.lineagemgr.common.validator.validate_path import safe_normalize_path
  25. from mindinsight.lineagemgr.summary.file_handler import FileHandler
  26. LineageInfo = namedtuple('LineageInfo', ['train_lineage', 'eval_lineage', 'dataset_graph'])
  27. class SummaryTag(Enum):
  28. """The tag value of lineage fields."""
  29. # the value is `field_number << 3 | wire_type`
  30. WALL_TIME = 'wall_time'
  31. STEP = 'step'
  32. VERSION = 'version'
  33. GRAPH = 'graph'
  34. SUMMARY = 'summary'
  35. TRAIN_LINEAGE = 'train_lineage'
  36. EVAL_LINEAGE = 'evaluation_lineage'
  37. DATASET_GRAPH = 'dataset_graph'
  38. class SummaryAnalyzer:
  39. """
  40. Summary log Analyzer.
  41. Args:
  42. file_path (str): The path of summary log.
  43. Raises:
  44. LineageVerificationException: Raise when verification failed.
  45. """
  46. HEADER_SIZE = 8
  47. HEADER_CRC_SIZE = 4
  48. BODY_CRC_SIZE = 4
  49. def __init__(self, file_path):
  50. self.file_handler = FileHandler(file_path)
  51. def load_events(self):
  52. """
  53. Load events in summary log.
  54. Returns:
  55. generator, the event generator.
  56. """
  57. while self._has_next():
  58. yield self._read_event()
  59. def _has_next(self):
  60. """
  61. Check if the file has reached the end.
  62. Returns:
  63. bool, whether the file has reached the end.
  64. """
  65. current_offset = self.file_handler.tell()
  66. if current_offset < self.file_handler.size:
  67. return True
  68. return False
  69. def _read_event(self):
  70. """
  71. Read event.
  72. Returns:
  73. Event, the event body.
  74. """
  75. body_size = self._read_header()
  76. body_str = self._read_body(body_size)
  77. event = Event().FromString(body_str)
  78. return event
  79. def _read_header(self):
  80. """
  81. Read header information.
  82. Returns:
  83. int, the length of event body.
  84. """
  85. header_str = self.file_handler.read(self.HEADER_SIZE)
  86. header_crc_str = self.file_handler.read(self.HEADER_CRC_SIZE)
  87. SummaryAnalyzer._check_crc(header_str, header_crc_str)
  88. body_len = struct.unpack("<Q", header_str)[0]
  89. return body_len
  90. def _read_body(self, body_size):
  91. """
  92. Read event body information.
  93. Args:
  94. body_size (int): The size of event body.
  95. Returns:
  96. bytes, the event body in bytes.
  97. """
  98. body_str = self.file_handler.read(body_size)
  99. body_crc_str = self.file_handler.read(self.BODY_CRC_SIZE)
  100. SummaryAnalyzer._check_crc(body_str, body_crc_str)
  101. return body_str
  102. @staticmethod
  103. def _check_crc(source_str, crc_str):
  104. """
  105. Check the integrity of source string.
  106. Args:
  107. source_str (bytes): Source string in bytes.
  108. crc_str (bytes): CRC string of source string in bytes.
  109. Raises:
  110. LineageVerificationException: Raise when verification failed.
  111. """
  112. if crc32.GetValueFromStr(crc_str) != \
  113. crc32.GetMaskCrc32cValue(source_str, len(source_str)):
  114. log.error("The CRC verification failed.")
  115. raise LineageVerificationException("The CRC verification failed.")
  116. class LineageSummaryAnalyzer(SummaryAnalyzer):
  117. """
  118. Summary log analyzer for lineage information.
  119. Args:
  120. file_path (str): The path of summary log.
  121. Raises:
  122. LineageSummaryAnalyzeException: If failed to get lineage information.
  123. """
  124. def __init__(self, file_path):
  125. file_path = safe_normalize_path(file_path, 'lineage_summary_path', None)
  126. super(LineageSummaryAnalyzer, self).__init__(file_path)
  127. def get_latest_info(self):
  128. """
  129. Get latest lineage info in summary log file.
  130. Returns:
  131. LineageInfo, the lineage summary information.
  132. """
  133. lineage_events = {
  134. SummaryTag.TRAIN_LINEAGE: None,
  135. SummaryTag.EVAL_LINEAGE: None,
  136. SummaryTag.DATASET_GRAPH: None
  137. }
  138. for event in self.load_events():
  139. for tag, _ in lineage_events.items():
  140. if event.HasField(tag.value):
  141. lineage_events[tag] = event
  142. break
  143. lineage_info = LineageInfo(
  144. train_lineage=lineage_events.get(SummaryTag.TRAIN_LINEAGE),
  145. eval_lineage=lineage_events.get(SummaryTag.EVAL_LINEAGE),
  146. dataset_graph=lineage_events.get(SummaryTag.DATASET_GRAPH)
  147. )
  148. return lineage_info
  149. @classmethod
  150. def get_summary_infos(cls, file_path):
  151. """
  152. Get lineage summary information from summary log file.
  153. Args:
  154. file_path (str): The file path of summary log.
  155. Returns:
  156. LineageInfo, the lineage summary information.
  157. Raises:
  158. LineageSummaryAnalyzeException: If failed to get lineage information.
  159. """
  160. analyzer = cls(file_path)
  161. try:
  162. lineage_info = analyzer.get_latest_info()
  163. except (MindInsightException, IOError) as err:
  164. log.error("Failed to get lineage information.")
  165. log.exception(err)
  166. raise LineageSummaryAnalyzeException()
  167. return lineage_info

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

Contributors (1)