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.

condition.py 9.5 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. """
  16. Management of all conditions.
  17. This module is used to register all conditions, as well as their parameters.
  18. This module also provide the available conditions to condition_collections api.
  19. """
  20. from enum import Enum
  21. from mindinsight.debugger.conditionmgr.log import logger
  22. class ParamNameEnum(Enum):
  23. """Param names."""
  24. ABS_MEAN_GT = "abs_mean_gt"
  25. ABS_MEAN_LT = "abs_mean_lt"
  26. ABS_MEAN_UPDATE_RATIO_GT = "abs_mean_update_ratio_gt"
  27. ABS_MEAN_UPDATE_RATIO_LT = "abs_mean_update_ratio_lt"
  28. ATOL = "atol"
  29. EQUAL_NAN = "equal_nan"
  30. EPSILON = "epsilon"
  31. MAX_GT = "max_gt"
  32. MAX_LT = "max_lt"
  33. MIN_GT = "min_gt"
  34. MIN_LT = "min_lt"
  35. MEAN_GT = "mean_gt"
  36. MEAN_LT = "mean_lt"
  37. MAX_MIN_GT = "max_min_gt"
  38. MAX_MIN_LT = "max_min_lt"
  39. PARAM = "param"
  40. RANGE_START_INCLUSIVE = "range_start_inclusive"
  41. RANGE_END_INCLUSIVE = "range_end_inclusive"
  42. RANGE_PERCENTAGE_GT = "range_percentage_gt"
  43. RANGE_PERCENTAGE_LT = "range_percentage_lt"
  44. RTOL = "rtol"
  45. ZERO_PERCENTAGE_GE = "zero_percentage_ge"
  46. class ConditionIdEnum(Enum):
  47. """Condition ids."""
  48. WEIGHT_INITIALIZATION = "weight_initialization"
  49. WEIGHT_OVERFLOW = "weight_overflow"
  50. WEIGHT_TOO_LARGE = "weight_too_large"
  51. WEIGHT_TOO_SMALL = "weight_too_small"
  52. GRADIENT_VANISHING = "gradient_vanishing"
  53. GRADIENT_TOO_LARGE = "gradient_too_large"
  54. GRADIENT_EXPLODING = "gradient_exploding"
  55. TENSOR_OVERFLOW = "tensor_overflow"
  56. OPERATOR_OVERFLOW = "operator_overflow"
  57. TENSOR_TOO_LARGE = "tensor_too_large"
  58. TENSOR_TOO_SMALL = "tensor_too_small"
  59. TENSOR_ALL_ZERO = "tensor_all_zero"
  60. WEIGHT_NOT_CHANGED = "weight_not_changed"
  61. WEIGHT_CHANGE_TOO_LARGE = "weight_change_too_large"
  62. WEIGHT_CHANGE_TOO_SMALL = "weight_change_too_small"
  63. ACTIVATION_RANGE = "activation_range"
  64. TENSOR_RANGE = "tensor_range"
  65. class OptimizePhaseEnum(Enum):
  66. """Optimize phases."""
  67. TENSOR_CHECK = 400
  68. OPERATOR_CHECK = 100
  69. LOSS_CHECK = 300
  70. INPUT_DATA_CHECK = 200
  71. class ValueTypeEnum(Enum):
  72. """Value types."""
  73. FLOAT64 = 1
  74. INT64 = 2
  75. BOOL = 3
  76. class PlatformEnum(Enum):
  77. """Platform types."""
  78. GPU = "GPU"
  79. ASCEND = "Ascend"
  80. class TargetTypeEnum(Enum):
  81. """Target types."""
  82. TENSOR = 'tensor'
  83. ACTIVATION = 'activation'
  84. GRADIENT = 'gradient'
  85. PARAMETER = 'parameter'
  86. WEIGHT = 'weight'
  87. class ParamTypeEnum(Enum):
  88. """Param types."""
  89. CHECK_PARAM = "CHECK_PARAM"
  90. SUPPORT_PARAM = "SUPPORT_PARAM"
  91. class ActivationFuncEnum(Enum):
  92. """Activation functions."""
  93. TANH = 'Tanh'
  94. SIGMOID = 'Sigmoid'
  95. RELU = 'ReLU'
  96. class ConditionContext:
  97. """
  98. The class for condition context.
  99. Args:
  100. backend (str): parameter name.
  101. step (int): the type of value.
  102. debugger_capability (tuple): whether the param support no assignment.
  103. """
  104. def __init__(self, backend, step=0, debugger_capability=(1, 1)):
  105. self._backend = backend
  106. self._step = step
  107. self._debugger_capability = debugger_capability
  108. @property
  109. def backend(self):
  110. """Get backend."""
  111. return self._backend
  112. @property
  113. def step(self):
  114. """Get _step."""
  115. return self._step
  116. @property
  117. def debugger_capability(self):
  118. """Get debugger_capability."""
  119. return self._debugger_capability
  120. class ConditionParameter:
  121. """
  122. The class for parameters of conditions.
  123. Args:
  124. name (ParamNameEnum): parameter name.
  125. value_type (ValueTypeEnum): the type of value.
  126. valid_test_func (func): the function used to test whether the param is valid.
  127. support_disable (bool): whether the param support no assignment.
  128. default_value (float): default value.
  129. visible_on_ui (bool): whether the param visible on ui.
  130. param_type (ParamTypeEnum): parameters type.
  131. required_params (list): the list of required parameters.
  132. """
  133. def __init__(self, name, value_type: ValueTypeEnum, valid_test_func=None, support_disable=True, default_value=None,
  134. visible_on_ui=True, param_type=ParamTypeEnum.CHECK_PARAM, required_params=None):
  135. self._name = name.value
  136. self._type = value_type
  137. self._valid_test_func = valid_test_func
  138. self._support_disable = support_disable
  139. self._default_value = default_value
  140. self._visible_on_ui = visible_on_ui
  141. self._param_type = param_type.value
  142. self._required_params = required_params
  143. @property
  144. def name(self):
  145. """Get name of parameter."""
  146. return self._name
  147. @property
  148. def type(self):
  149. """Get type of parameter."""
  150. return self._type
  151. @property
  152. def support_disable(self):
  153. """Get support_disable of parameter."""
  154. return self._support_disable
  155. @property
  156. def default_value(self):
  157. """Get default_value of parameter."""
  158. return self._default_value
  159. @property
  160. def visible_on_ui(self):
  161. """Get visible_on_ui of parameter."""
  162. return self._visible_on_ui
  163. @property
  164. def param_type(self):
  165. """Get param_type of parameter."""
  166. return self._param_type
  167. @property
  168. def required_params(self):
  169. """Get required_param of parameter."""
  170. return self._required_params
  171. def is_valid(self, value):
  172. """Check is the parameter valid."""
  173. if self._valid_test_func is None:
  174. return True
  175. return self._valid_test_func(value)
  176. class Condition:
  177. """
  178. The class for parameters of conditions.
  179. Args:
  180. condition_id (ConditionIdEnum): condition id.
  181. abbr (str): the abbreviation of condition id.
  182. optimize_phase (OptimizePhaseEnum): optimize phase.
  183. parameters (List[ConditionParameter]): parameters.
  184. supported_target_type (TargetTypeEnum): the supported target type.
  185. supported_platforms (tuple[PlatformEnum, PlatformEnum]): the supported platforms.
  186. minimum_debugger_capability (tuple): the minimum debugger capability required.
  187. availability_test_func (func): the function used to test whether the condition is available.
  188. """
  189. def __init__(self, condition_id, abbr, optimize_phase, parameters, supported_target_type, supported_platforms,
  190. minimum_debugger_capability, availability_test_func=None):
  191. self.id = condition_id.value
  192. self._abbr = abbr
  193. self.optimize_phase = optimize_phase
  194. self._parameters = {
  195. parameter.name: parameter for parameter in parameters
  196. }
  197. self.ordered_parameter_names = [parameter.name for parameter in parameters]
  198. self._supported_target_type = supported_target_type
  199. self.supported_platforms = supported_platforms
  200. self.minimum_debugger_capability = minimum_debugger_capability
  201. self.availability_test_func = availability_test_func
  202. def get_parameter_definition(self, name):
  203. """Return parameter definition by the name"""
  204. return self._parameters[name]
  205. def is_available(self, condition_context):
  206. """Check is the condition available."""
  207. backend = condition_context.backend
  208. debugger_capability = condition_context.debugger_capability
  209. if debugger_capability < self.minimum_debugger_capability:
  210. logger.debug("The debugger capability is lower than the minimum debugger capability.")
  211. return False
  212. if backend not in [platform.value for platform in self.supported_platforms]:
  213. logger.debug("The condition %s is not supported on the platform.", self.id)
  214. return False
  215. if self.availability_test_func is None:
  216. return True
  217. return self.availability_test_func(condition_context)
  218. @property
  219. def abbr(self):
  220. """The abbreviation of condition"""
  221. return self._abbr
  222. @property
  223. def names(self):
  224. """The name of condition"""
  225. return self._parameters.keys()
  226. @property
  227. def parameters(self):
  228. """The parameters of condition"""
  229. return self._parameters.values()
  230. @property
  231. def supported_target_type(self):
  232. """The supported target type of condition"""
  233. return self._supported_target_type
  234. def check_initialization_available(condition_context):
  235. """Check if initialization is available at this step"""
  236. if condition_context.step == 0:
  237. return True
  238. return False
  239. def check_percentage_param_range(value):
  240. if 0 <= value <= 100:
  241. return True
  242. return False
  243. def check_normal_param_range(value):
  244. if float("-inf") < value < float("inf"):
  245. return True
  246. return False
  247. def check_abs_param_range(value):
  248. if 0 <= value < float("inf"):
  249. return True
  250. return False