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.

__init__.py 4.9 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. """Conf module."""
  16. import os
  17. import json
  18. import types
  19. from importlib import import_module
  20. class Settings:
  21. """
  22. Definition of Settings class.
  23. Examples:
  24. >>> from mindinsight.conf import settings
  25. >>> print(settings.PORT)
  26. """
  27. _prefix = 'MINDINSIGHT_'
  28. _explicit_settings = set()
  29. _default_settings = set()
  30. def __init__(self):
  31. """Initialization of Settings."""
  32. self.load_from_defaults()
  33. self.load_from_constants()
  34. self.refresh()
  35. def refresh(self):
  36. """Refresh settings from config file and environment variables."""
  37. self.update_from_file()
  38. self.update_from_env()
  39. def load_from_defaults(self):
  40. """Update settings from defaults module."""
  41. default_settings = import_module('mindinsight.conf.defaults')
  42. for setting in dir(default_settings):
  43. if setting.isupper():
  44. setattr(self, setting, getattr(default_settings, setting))
  45. self._default_settings.add(setting)
  46. def load_from_constants(self):
  47. """Update settings from constants module"""
  48. constant_settings = import_module('mindinsight.conf.constants')
  49. for setting in dir(constant_settings):
  50. if setting.isupper():
  51. setattr(self, setting, getattr(constant_settings, setting))
  52. def update_from_file(self):
  53. """Update settings from config file."""
  54. config_path = os.environ.get('MINDINSIGHT_CONFIG', '')
  55. if not config_path:
  56. return
  57. config_module = None
  58. # python:full.path.for.config.module
  59. if config_path.startswith('python:'):
  60. config_module = import_module(config_path[len('python:'):])
  61. # file:full/path/for/config.py
  62. elif config_path.startswith('file:'):
  63. config_path = config_path[len('file:'):]
  64. module_name = '__mindinsightconfig__'
  65. config_module = types.ModuleType(module_name)
  66. machinery = import_module('importlib.machinery')
  67. loader = machinery.SourceFileLoader(module_name, config_path)
  68. loader.exec_module(config_module)
  69. if config_module is None:
  70. return
  71. for setting in dir(config_module):
  72. if setting.isupper() and setting in self._default_settings:
  73. setting_value = getattr(config_module, setting)
  74. setattr(self, setting, setting_value)
  75. self._explicit_settings.add(setting)
  76. def update_from_env(self):
  77. """Update settings from environment variables."""
  78. for key, value in os.environ.items():
  79. if not key.startswith(self._prefix):
  80. continue
  81. setting = key[len(self._prefix):]
  82. if setting not in self._default_settings:
  83. continue
  84. setting_value = getattr(self, setting)
  85. if isinstance(setting_value, bool):
  86. value = (value == 'True')
  87. elif isinstance(setting_value, (int, float)):
  88. value = type(setting_value)(value)
  89. elif isinstance(setting_value, (list, dict)):
  90. value = json.loads(value)
  91. setattr(self, setting, value)
  92. self._explicit_settings.add(setting)
  93. def config_workspace(self, workspace):
  94. """
  95. Config workspace value.
  96. Args:
  97. workspace (str): Path of workspace.
  98. """
  99. setattr(self, 'WORKSPACE', workspace)
  100. self._explicit_settings.add('WORKSPACE')
  101. def is_overridden(self, setting_name):
  102. """
  103. Check if specified setting is overridden.
  104. Args:
  105. setting_name (str): Setting name to be checked.
  106. Returns:
  107. bool, indicate whether given setting name is overridden.
  108. """
  109. return setting_name in self._explicit_settings
  110. def dump(self):
  111. """
  112. Dump settings data.
  113. Returns:
  114. dict, json formatted data of settings.
  115. """
  116. config = {}
  117. for setting in dir(self):
  118. if setting.isupper():
  119. config[setting] = getattr(self, setting)
  120. return config
  121. settings = Settings()

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

Contributors (1)