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