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.

conditionmgr.py 4.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. Condition manager..
  17. This module provide condition manager function.
  18. """
  19. from mindinsight.debugger.conditionmgr.condition import Condition
  20. from mindinsight.debugger.conditionmgr.condition import TargetTypeEnum
  21. from mindinsight.debugger.conditionmgr.condition_list import CONDITION_LIST
  22. from mindinsight.debugger.conditionmgr.log import logger
  23. class ConditionMgr:
  24. """Condition manager."""
  25. def __init__(self):
  26. self.conditions = {}
  27. self.no_parameter_conditions = []
  28. self._register_default_conditions()
  29. def _register_default_conditions(self):
  30. """Register default condition definitions"""
  31. self.register_conditions(CONDITION_LIST)
  32. def register_condition(self, condition):
  33. """Register conditions into dict"""
  34. if not condition.parameters:
  35. self.no_parameter_conditions.append(condition.id)
  36. self.conditions[condition.id] = condition
  37. def register_conditions(self, conditions):
  38. """Register conditions"""
  39. for condition in conditions:
  40. self.register_condition(condition)
  41. def get_condition(self, condition_id) -> Condition:
  42. """Get condition by condition id"""
  43. return self.conditions[condition_id]
  44. def has_condition(self, condition_id, condition_context) -> bool:
  45. """Return if the condition exist and avilible"""
  46. if condition_id in self.conditions:
  47. condition = self.get_condition(condition_id)
  48. return condition.is_available(condition_context)
  49. logger.warning("Condition id %s not found.", condition_id)
  50. return False
  51. def get_no_param_condition(self) -> list:
  52. """Return the list of condition without parameters"""
  53. return self.no_parameter_conditions
  54. @staticmethod
  55. def check_and_sort(collections, target_type, reply):
  56. """Check the collection and sort conditions"""
  57. collection = collections.get(target_type)
  58. if collection:
  59. collection = sorted(collection, key=lambda x: x.get('id'))
  60. reply.append({"id": target_type + "_condition_collection", "conditions": collection})
  61. else:
  62. logger.warning("Condition collection for %s is None.", target_type)
  63. def get_all_collections(self, condition_context):
  64. """Get all register conditions."""
  65. collections = {
  66. TargetTypeEnum.WEIGHT.value: [], TargetTypeEnum.TENSOR.value: [], TargetTypeEnum.GRADIENT.value: [],
  67. TargetTypeEnum.ACTIVATION.value: []
  68. }
  69. for condition in self.conditions.values():
  70. parameters = []
  71. if not condition.is_available(condition_context):
  72. continue
  73. for param in condition.parameters:
  74. if not param.visible_on_ui:
  75. continue
  76. parameters.append({
  77. "name": param.name,
  78. "type": param.type.name,
  79. "support_disable": param.support_disable,
  80. "default_value": param.default_value,
  81. "param_type": param.param_type,
  82. "required_params": param.required_params
  83. })
  84. collections[condition.supported_target_type.value].append({
  85. "id": condition.id,
  86. "parameters": parameters,
  87. "supported_target_type": condition.supported_target_type.name,
  88. "abbr": condition.abbr
  89. })
  90. reply = []
  91. self.check_and_sort(collections, TargetTypeEnum.TENSOR.value, reply)
  92. self.check_and_sort(collections, TargetTypeEnum.WEIGHT.value, reply)
  93. self.check_and_sort(collections, TargetTypeEnum.ACTIVATION.value, reply)
  94. self.check_and_sort(collections, TargetTypeEnum.GRADIENT.value, reply)
  95. return reply