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.

path_parser.py 5.3 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 file provides path resolution."""
  16. import os
  17. from mindinsight.datavisual.data_transform.summary_watcher import SummaryWatcher
  18. class SummaryPathParser:
  19. """
  20. Summary path parser.
  21. This class is a utility class, users can use it to parse summary dir,
  22. parse summary log path, get the latest lineage summary log, etc.
  23. """
  24. LINEAGE_SUMMARY_SUFFIX = '_lineage'
  25. _LINEAGE_SUMMARY_SUFFIX_LEN = len(LINEAGE_SUMMARY_SUFFIX)
  26. @staticmethod
  27. def get_summary_dirs(summary_base_dir):
  28. """
  29. Get summary dirs according to summary base dir.
  30. Args:
  31. summary_base_dir (str): Summary base dir.
  32. Returns:
  33. list[str], all summary dirs in summary base dir. The summary dir is
  34. absolute path.
  35. """
  36. summary_watcher = SummaryWatcher()
  37. relative_dirs = summary_watcher.list_summary_directories(
  38. summary_base_dir=summary_base_dir
  39. )
  40. summary_dirs = list(
  41. map(
  42. lambda item: os.path.realpath(
  43. os.path.join(summary_base_dir, item.get('relative_path'))
  44. ),
  45. relative_dirs
  46. )
  47. )
  48. return summary_dirs
  49. @staticmethod
  50. def get_latest_lineage_summary(summary_dir):
  51. """
  52. Get latest lineage summary log path according to summary dir.
  53. Args:
  54. summary_dir (str): Summary dir.
  55. Returns:
  56. Union[str, None], if the lineage summary log exist, return the path,
  57. else return None. The lineage summary log path is absolute path.
  58. """
  59. summary_watcher = SummaryWatcher()
  60. summaries = summary_watcher.list_summaries(summary_base_dir=summary_dir)
  61. latest_file_name = SummaryPathParser._get_latest_lineage_file(summaries)
  62. return os.path.join(summary_dir, latest_file_name) \
  63. if latest_file_name is not None else None
  64. @staticmethod
  65. def get_latest_lineage_summaries(summary_base_dir):
  66. """
  67. Get all latest lineage summary logs in summary base dir.
  68. Args:
  69. summary_base_dir (str): Summary base dir.
  70. Returns:
  71. list[str], all latest lineage summary logs in summary base dir. The
  72. lineage summary log is absolute path.
  73. """
  74. summary_watcher = SummaryWatcher()
  75. relative_dirs = summary_watcher.list_summary_directories(
  76. summary_base_dir=summary_base_dir
  77. )
  78. latest_summaries = []
  79. for item in relative_dirs:
  80. relative_dir = item.get('relative_path')
  81. summaries = summary_watcher.list_summaries(
  82. summary_base_dir=summary_base_dir,
  83. relative_path=relative_dir
  84. )
  85. latest_file_name = SummaryPathParser._get_latest_lineage_file(
  86. summaries
  87. )
  88. if latest_file_name is None:
  89. continue
  90. latest_file = os.path.realpath(
  91. os.path.join(
  92. summary_base_dir,
  93. relative_dir,
  94. latest_file_name
  95. )
  96. )
  97. latest_summaries.append(latest_file)
  98. return latest_summaries
  99. @staticmethod
  100. def _get_latest_lineage_file(summaries):
  101. """
  102. Get latest lineage summary file.
  103. If there is a file with the suffix `LINEAGE_SUMMARY_SUFFIX`, check
  104. whether there is a file with the same name that does not include the
  105. suffix `LINEAGE_SUMMARY_SUFFIX`. When both exist, the file is considered
  106. to be a lineage summary log.
  107. Args:
  108. summaries (list[dict]): All summary logs info in summary dir.
  109. Returns:
  110. str, the latest lineage summary file name.
  111. """
  112. try:
  113. latest_summary = max(
  114. summaries,
  115. key=lambda summary: summary.get('create_time')
  116. )
  117. except ValueError:
  118. return None
  119. max_create_time = latest_summary.get('create_time')
  120. summary_file_names = []
  121. for summary in summaries:
  122. if summary.get('create_time') == max_create_time:
  123. summary_file_names.append(summary.get('file_name'))
  124. latest_lineage_name = None
  125. for name in summary_file_names:
  126. if not name.endswith(SummaryPathParser.LINEAGE_SUMMARY_SUFFIX):
  127. continue
  128. ms_name = name[:-SummaryPathParser._LINEAGE_SUMMARY_SUFFIX_LEN]
  129. if ms_name in summary_file_names:
  130. latest_lineage_name = name
  131. return latest_lineage_name

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

Contributors (1)