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.2 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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.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. NAN = "nan"
  34. OVERFLOW_ASCEND_CHIP = "overflow"
  35. INF = "inf"
  36. MAX_GT = "max_gt"
  37. MAX_LT = "max_lt"
  38. MIN_GT = "min_gt"
  39. MIN_LT = "min_lt"
  40. MAX_MIN_GT = "max_min_gt"
  41. MAX_MIN_LT = "max_min_lt"
  42. MEAN_GT = "mean_gt"
  43. MEAN_LT = "mean_lt"
  44. TENSOR_INITIALIZATION = "tensor_initialization"
  45. TENSOR_TOO_LARGE = "tensor_too_large"
  46. TENSOR_TOO_SMALL = "tensor_too_small"
  47. TENSOR_ALL_ZERO = "tensor_all_zero"
  48. WEIGHT_NOT_CHANGED = "weight_not_changed"
  49. WEIGHT_CHANGE_TOO_LARGE = "weight_change_too_large"
  50. WEIGHT_CHANGE_TOO_SMALL = "weight_change_too_small"
  51. TENSOR_CHANGE_TOO_LARGE = "tensor_change_too_large"
  52. TENSOR_CHANGE_TOO_SMALL = "tensor_change_too_small"
  53. TENSOR_NOT_CHANGED = "tensor_not_changed"
  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 ConditionContext:
  76. """
  77. The class for condition context.
  78. Args:
  79. backend (str): parameter name.
  80. step (int): the type of value.
  81. debugger_capability (tuple): whether the param support no assignment.
  82. """
  83. def __init__(self, backend, step=0, debugger_capability=(1, 0)):
  84. self._backend = backend
  85. self._step = step
  86. self._debugger_capability = debugger_capability
  87. @property
  88. def backend(self):
  89. """Get backend."""
  90. return self._backend
  91. @property
  92. def step(self):
  93. """Get _step."""
  94. return self._step
  95. @property
  96. def debugger_capability(self):
  97. """Get debugger_capability."""
  98. return self._debugger_capability
  99. class ConditionParameter:
  100. """
  101. The class for parameters of conditions.
  102. Args:
  103. name (str): parameter name.
  104. value_type (ValueTypeEnum): the type of value.
  105. valid_test_func (func): the function used to test whether the param is valid.
  106. support_disable (bool): whether the param support no assignment.
  107. default_value (float): default value.
  108. visible_on_ui (bool): whether the param visible on ui.
  109. """
  110. def __init__(self, name, value_type: ValueTypeEnum, valid_test_func=None, support_disable=True, default_value=None,
  111. visible_on_ui=True):
  112. self._name = name
  113. self._type = value_type
  114. self._valid_test_func = valid_test_func
  115. self._support_disable = support_disable
  116. self._default_value = default_value
  117. self._visible_on_ui = visible_on_ui
  118. @property
  119. def name(self):
  120. """Get name of parameter."""
  121. return self._name
  122. @property
  123. def type(self):
  124. """Get type of parameter."""
  125. return self._type
  126. @property
  127. def support_disable(self):
  128. """Get support_disable of parameter."""
  129. return self._support_disable
  130. @property
  131. def default_value(self):
  132. """Get default_value of parameter."""
  133. return self._default_value
  134. @property
  135. def visible_on_ui(self):
  136. """Get visible_on_ui of parameter."""
  137. return self._visible_on_ui
  138. def is_valid(self, value):
  139. """Check is the parameter valid."""
  140. if self._valid_test_func is None:
  141. return True
  142. return self._valid_test_func(value)
  143. class Condition:
  144. """
  145. The class for parameters of conditions.
  146. Args:
  147. condition_id (str): condition id.
  148. abbr (str): the abbreviation of condition id.
  149. optimize_phase (OptimizePhaseEnum): optimize phase.
  150. parameters (List[ConditionParameter]): parameters.
  151. supported_target_type (TargetTypeEnum): the supported target type.
  152. supported_platforms (tuple[PlatformEnum, PlatformEnum]): the supported platforms.
  153. minimum_debugger_capability (tuple): the minimum debugger capability required.
  154. availability_test_func (func): the function used to test whether the condition is available.
  155. """
  156. def __init__(self, condition_id, abbr, optimize_phase, parameters, supported_target_type, supported_platforms,
  157. minimum_debugger_capability, availability_test_func=None):
  158. self.id = condition_id
  159. self._abbr = abbr
  160. self.optimize_phase = optimize_phase
  161. self._parameters = {
  162. parameter.name: parameter for parameter in parameters
  163. }
  164. self._supported_target_type = supported_target_type
  165. self.supported_platforms = supported_platforms
  166. self.minimum_debugger_capability = minimum_debugger_capability
  167. self.availability_test_func = availability_test_func
  168. def get_parameter_definition(self, name):
  169. """Return parameter definition by the name"""
  170. return self._parameters[name]
  171. def is_available(self, condition_context):
  172. """Check is the condition available."""
  173. backend = condition_context.backend
  174. debugger_capability = condition_context.debugger_capability
  175. if debugger_capability < self.minimum_debugger_capability:
  176. logger.debug("The debugger capability is lower than the minimum debugger capability.")
  177. return False
  178. if backend not in [platform.value for platform in self.supported_platforms]:
  179. logger.debug("The condition %s is not supported on the platform.", self.id)
  180. return False
  181. if self.availability_test_func is None:
  182. return True
  183. return self.availability_test_func(condition_context)
  184. @property
  185. def abbr(self):
  186. """The abbreviation of condition"""
  187. return self._abbr
  188. @property
  189. def names(self):
  190. """The name of condition"""
  191. return self._parameters.keys()
  192. @property
  193. def parameters(self):
  194. """The parameters of condition"""
  195. return self._parameters.values()
  196. @property
  197. def supported_target_type(self):
  198. """The supported target type of condition"""
  199. return self._supported_target_type
  200. def check_initialization_available(condition_context):
  201. """Check if initialization is available at this step"""
  202. if condition_context.step == 0:
  203. return True
  204. return False
  205. def check_percentage_param_range(value):
  206. if 0 <= value <= 100:
  207. return True
  208. return False
  209. def check_normal_param_range(value):
  210. if float("-inf") < value < float("inf"):
  211. return True
  212. return False
  213. def check_abs_param_range(value):
  214. if 0 <= value < float("inf"):
  215. return True
  216. return False