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.

utils.py 2.8 kB

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # Copyright 2019 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. """Lineage utils."""
  16. from functools import wraps
  17. import re
  18. from mindinsight.datavisual.data_transform.summary_watcher import SummaryWatcher
  19. from mindinsight.lineagemgr.common.log import logger as log
  20. from mindinsight.lineagemgr.common.exceptions.exceptions import LineageParamRunContextError, \
  21. LineageGetModelFileError, LineageLogError, LineageParamValueError, LineageDirNotExistError, \
  22. LineageParamSummaryPathError
  23. from mindinsight.lineagemgr.common.validator.validate import validate_path
  24. from mindinsight.utils.exceptions import MindInsightException
  25. def enum_to_list(enum):
  26. return [enum_ele.value for enum_ele in enum]
  27. def try_except(logger):
  28. """
  29. Catch or raise exceptions while collecting lineage.
  30. Args:
  31. logger (logger): The logger instance which logs the warning info.
  32. Returns:
  33. function, the decorator which we use to retry the decorated function.
  34. """
  35. def try_except_decorate(func):
  36. @wraps(func)
  37. def wrapper(self, *args, **kwargs):
  38. try:
  39. func(self, *args, **kwargs)
  40. except (AttributeError, MindInsightException,
  41. LineageParamRunContextError, LineageLogError,
  42. LineageGetModelFileError, IOError) as err:
  43. logger.error(err)
  44. try:
  45. raise_except = self.raise_exception
  46. except AttributeError:
  47. raise_except = False
  48. if raise_except is True:
  49. raise
  50. return wrapper
  51. return try_except_decorate
  52. def normalize_summary_dir(summary_dir):
  53. """Normalize summary dir."""
  54. try:
  55. summary_dir = validate_path(summary_dir)
  56. except (LineageParamValueError, LineageDirNotExistError) as error:
  57. log.error(str(error))
  58. log.exception(error)
  59. raise LineageParamSummaryPathError(str(error.message))
  60. return summary_dir
  61. def get_timestamp(filename):
  62. """Get timestamp from filename."""
  63. timestamp = int(re.search(SummaryWatcher().SUMMARY_FILENAME_REGEX, filename)[1])
  64. return timestamp

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