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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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.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. TENSOR_CHANGE_TOO_LARGE = "tensor_change_too_large"
  53. TENSOR_CHANGE_TOO_SMALL = "tensor_change_too_small"
  54. TENSOR_NOT_CHANGED = "tensor_not_changed"
  55. ACTIVATION_RANGE = "activation_range"
  56. TENSOR_RANGE = "tensor_range"
  57. class OptimizePhaseEnum(Enum):
  58. """Optimize phases."""
  59. TENSOR_CHECK = 400
  60. OPERATOR_CHECK = 100
  61. LOSS_CHECK = 300
  62. INPUT_DATA_CHECK = 200
  63. class ValueTypeEnum(Enum):
  64. """Value types."""
  65. FLOAT64 = 1
  66. INT64 = 2
  67. BOOL = 3
  68. class PlatformEnum(Enum):
  69. """Platform types."""
  70. GPU = "GPU"
  71. ASCEND = "Ascend"
  72. class TargetTypeEnum(Enum):
  73. """Target types."""
  74. TENSOR = 'tensor'
  75. WEIGHT = 'weight'
  76. ACTIVATION = 'activation'
  77. GRADIENT = 'gradient'
  78. class ParamTypeEnum(Enum):
  79. """Param types."""
  80. CHECK_PARAM = "CHECK_PARAM"
  81. SUPPORT_PARAM = "SUPPORT_PARAM"
  82. class ConditionContext:
  83. """
  84. The class for condition context.
  85. Args:
  86. backend (str): parameter name.
  87. step (int): the type of value.
  88. debugger_capability (tuple): whether the param support no assignment.
  89. """
  90. def __init__(self, backend, step=0, debugger_capability=(1, 0)):
  91. self._backend = backend
  92. self._step = step
  93. self._debugger_capability = debugger_capability
  94. @property
  95. def backend(self):
  96. """Get backend."""
  97. return self._backend
  98. @property
  99. def step(self):
  100. """Get _step."""
  101. return self._step
  102. @property
  103. def debugger_capability(self):
  104. """Get debugger_capability."""
  105. return self._debugger_capability
  106. class ConditionParameter:
  107. """
  108. The class for parameters of conditions.
  109. Args:
  110. name (str): parameter name.
  111. value_type (ValueTypeEnum): the type of value.
  112. valid_test_func (func): the function used to test whether the param is valid.
  113. support_disable (bool): whether the param support no assignment.
  114. default_value (float): default value.
  115. visible_on_ui (bool): whether the param visible on ui.
  116. param_type (ParamTypeEnum): parameters type.
  117. required_params (list): the list of required parameters.
  118. """
  119. def __init__(self, name, value_type: ValueTypeEnum, valid_test_func=None, support_disable=True, default_value=None,
  120. visible_on_ui=True, param_type=ParamTypeEnum.CHECK_PARAM, required_params=None):
  121. self._name = name
  122. self._type = value_type
  123. self._valid_test_func = valid_test_func
  124. self._support_disable = support_disable
  125. self._default_value = default_value
  126. self._visible_on_ui = visible_on_ui
  127. self._param_type = param_type.value
  128. self._required_params = required_params
  129. @property
  130. def name(self):
  131. """Get name of parameter."""
  132. return self._name
  133. @property
  134. def type(self):
  135. """Get type of parameter."""
  136. return self._type
  137. @property
  138. def support_disable(self):
  139. """Get support_disable of parameter."""
  140. return self._support_disable
  141. @property
  142. def default_value(self):
  143. """Get default_value of parameter."""
  144. return self._default_value
  145. @property
  146. def visible_on_ui(self):
  147. """Get visible_on_ui of parameter."""
  148. return self._visible_on_ui
  149. @property
  150. def param_type(self):
  151. """Get param_type of parameter."""
  152. return self._param_type
  153. @property
  154. def required_params(self):
  155. """Get required_param of parameter."""
  156. return self._required_params
  157. def is_valid(self, value):
  158. """Check is the parameter valid."""
  159. if self._valid_test_func is None:
  160. return True
  161. return self._valid_test_func(value)
  162. class Condition:
  163. """
  164. The class for parameters of conditions.
  165. Args:
  166. condition_id (ConditionIdEnum): condition id.
  167. abbr (str): the abbreviation of condition id.
  168. optimize_phase (OptimizePhaseEnum): optimize phase.
  169. parameters (List[ConditionParameter]): parameters.
  170. supported_target_type (TargetTypeEnum): the supported target type.
  171. supported_platforms (tuple[PlatformEnum, PlatformEnum]): the supported platforms.
  172. minimum_debugger_capability (tuple): the minimum debugger capability required.
  173. availability_test_func (func): the function used to test whether the condition is available.
  174. """
  175. def __init__(self, condition_id, abbr, optimize_phase, parameters, supported_target_type, supported_platforms,
  176. minimum_debugger_capability, availability_test_func=None):
  177. self.id = condition_id.value
  178. self._abbr = abbr
  179. self.optimize_phase = optimize_phase
  180. self._parameters = {
  181. parameter.name: parameter for parameter in parameters
  182. }
  183. self._supported_target_type = supported_target_type
  184. self.supported_platforms = supported_platforms
  185. self.minimum_debugger_capability = minimum_debugger_capability
  186. self.availability_test_func = availability_test_func
  187. def get_parameter_definition(self, name):
  188. """Return parameter definition by the name"""
  189. return self._parameters[name]
  190. def is_available(self, condition_context):
  191. """Check is the condition available."""
  192. backend = condition_context.backend
  193. debugger_capability = condition_context.debugger_capability
  194. if debugger_capability < self.minimum_debugger_capability:
  195. logger.debug("The debugger capability is lower than the minimum debugger capability.")
  196. return False
  197. if backend not in [platform.value for platform in self.supported_platforms]:
  198. logger.debug("The condition %s is not supported on the platform.", self.id)
  199. return False
  200. if self.availability_test_func is None:
  201. return True
  202. return self.availability_test_func(condition_context)
  203. @property
  204. def abbr(self):
  205. """The abbreviation of condition"""
  206. return self._abbr
  207. @property
  208. def names(self):
  209. """The name of condition"""
  210. return self._parameters.keys()
  211. @property
  212. def parameters(self):
  213. """The parameters of condition"""
  214. return self._parameters.values()
  215. @property
  216. def supported_target_type(self):
  217. """The supported target type of condition"""
  218. return self._supported_target_type
  219. def check_initialization_available(condition_context):
  220. """Check if initialization is available at this step"""
  221. if condition_context.step == 0:
  222. return True
  223. return False
  224. def check_percentage_param_range(value):
  225. if 0 <= value <= 100:
  226. return True
  227. return False
  228. def check_normal_param_range(value):
  229. if float("-inf") < value < float("inf"):
  230. return True
  231. return False
  232. def check_abs_param_range(value):
  233. if 0 <= value < float("inf"):
  234. return True
  235. return False
  236. def check_not_nan(value):
  237. return not math.isnan(value)