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