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.

validate.py 8.7 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. """Validate the profiler parameters."""
  16. from mindinsight.profiler.common.exceptions.exceptions import ProfilerParamTypeErrorException, \
  17. ProfilerDeviceIdException, ProfilerOpTypeException, \
  18. ProfilerSortConditionException, ProfilerFilterConditionException, ProfilerGroupConditionException
  19. from mindinsight.profiler.common.log import logger as log
  20. AICORE_TYPE_COL = ["op_type", "execution_time", "execution_frequency", "precent"]
  21. AICORE_DETAIL_COL = ["op_name", "op_type", "execution_time", "subgraph", "full_op_name"]
  22. AICPU_COL = ["serial_number", "op_name", "total_time", "dispatch_time", "RunV2_start",
  23. "compute_start", "memcpy_start", "memcpy_end", "RunV2_end"]
  24. def validate_condition(search_condition):
  25. """
  26. Verify the param in search_condition is valid or not.
  27. Args:
  28. search_condition (dict): The search condition.
  29. Raises:
  30. ProfilerParamTypeErrorException: If the type of the param in search_condition is invalid.
  31. ProfilerDeviceIdException: If the device_id param in search_condition is invalid.
  32. ProfilerOpTypeException: If the op_type param in search_condition is invalid.
  33. ProfilerGroupConditionException: If the group_condition param in search_condition is invalid.
  34. ProfilerSortConditionException: If the sort_condition param in search_condition is invalid.
  35. ProfilerFilterConditionException: If the filter_condition param in search_condition is invalid.
  36. """
  37. if not isinstance(search_condition, dict):
  38. log.error("Invalid search_condition type, it should be dict.")
  39. raise ProfilerParamTypeErrorException(
  40. "Invalid search_condition type, it should be dict.")
  41. if "device_id" in search_condition:
  42. device_id = search_condition.get("device_id")
  43. if not isinstance(device_id, str):
  44. raise ProfilerDeviceIdException("Invalid device_id type, it should be str.")
  45. if "op_type" in search_condition:
  46. op_type = search_condition.get("op_type")
  47. if op_type == "aicpu":
  48. search_scope = AICPU_COL
  49. elif op_type == "aicore_type":
  50. search_scope = AICORE_TYPE_COL
  51. elif op_type == "aicore_detail":
  52. search_scope = AICORE_DETAIL_COL
  53. else:
  54. raise ProfilerOpTypeException("The op_type must in ['aicpu', 'aicore_type', 'aicore_detail']")
  55. else:
  56. raise ProfilerOpTypeException("The op_type must in ['aicpu', 'aicore_type', 'aicore_detail']")
  57. if "group_condition" in search_condition:
  58. validata_group_condition(search_condition)
  59. if "sort_condition" in search_condition:
  60. validate_sort_condition(search_condition, search_scope)
  61. if "filter_condition" in search_condition:
  62. validate_filter_condition(search_condition)
  63. def validata_group_condition(search_condition):
  64. """
  65. Verify the group_condition in search_condition is valid or not.
  66. Args:
  67. search_condition (dict): The search condition.
  68. Raises:
  69. ProfilerGroupConditionException: If the group_condition param in search_condition is invalid.
  70. """
  71. group_condition = search_condition.get("group_condition")
  72. if not isinstance(group_condition, dict):
  73. raise ProfilerGroupConditionException("The group condition must be dict.")
  74. if "limit" in group_condition:
  75. limit = group_condition.get("limit", 0)
  76. if isinstance(limit, bool) \
  77. or not isinstance(group_condition.get("limit"), int):
  78. log.error("The limit must be int.")
  79. raise ProfilerGroupConditionException("The limit must be int.")
  80. if limit < 1 or limit > 100:
  81. raise ProfilerGroupConditionException("The limit must in [1, 100].")
  82. if "offset" in group_condition:
  83. offset = group_condition.get("offset", 0)
  84. if isinstance(offset, bool) \
  85. or not isinstance(group_condition.get("offset"), int):
  86. log.error("The offset must be int.")
  87. raise ProfilerGroupConditionException("The offset must be int.")
  88. if offset < 0:
  89. raise ProfilerGroupConditionException("The offset must ge 0.")
  90. if offset > 1000000:
  91. raise ProfilerGroupConditionException("The offset must le 1000000.")
  92. def validate_sort_condition(search_condition, search_scope):
  93. """
  94. Verify the sort_condition in search_condition is valid or not.
  95. Args:
  96. search_condition (dict): The search condition.
  97. search_scope (list): The search scope.
  98. Raises:
  99. ProfilerSortConditionException: If the sort_condition param in search_condition is invalid.
  100. """
  101. sort_condition = search_condition.get("sort_condition")
  102. if not isinstance(sort_condition, dict):
  103. raise ProfilerSortConditionException("The sort condition must be dict.")
  104. if "name" in sort_condition:
  105. sorted_name = sort_condition.get("name", "")
  106. err_msg = "The sorted_name must be in {}".format(search_scope)
  107. if not isinstance(sorted_name, str):
  108. log.error("Wrong sorted name type.")
  109. raise ProfilerSortConditionException("Wrong sorted name type.")
  110. if sorted_name not in search_scope:
  111. log.error(err_msg)
  112. raise ProfilerSortConditionException(err_msg)
  113. if "type" in sort_condition:
  114. sorted_type_param = ['ascending', 'descending']
  115. sorted_type = sort_condition.get("type")
  116. if sorted_type and sorted_type not in sorted_type_param:
  117. err_msg = "The sorted type must be ascending or descending."
  118. log.error(err_msg)
  119. raise ProfilerSortConditionException(err_msg)
  120. def validate_filter_condition(search_condition):
  121. """
  122. Verify the filter_condition in search_condition is valid or not.
  123. Args:
  124. search_condition (dict): The search condition.
  125. Raises:
  126. ProfilerFilterConditionException: If the filter_condition param in search_condition is invalid.
  127. """
  128. def validate_op_filter_condition(op_condition):
  129. """
  130. Verify the op_condition in filter_condition is valid or not.
  131. Args:
  132. op_condition (dict): The op_condition in search_condition.
  133. Raises:
  134. ProfilerFilterConditionException: If the filter_condition param in search_condition is invalid.
  135. """
  136. if not isinstance(op_condition, dict):
  137. raise ProfilerFilterConditionException("Wrong op_type filter condition.")
  138. for key, value in op_condition.items():
  139. if not isinstance(key, str):
  140. raise ProfilerFilterConditionException("The filter key must be str")
  141. if not isinstance(value, list):
  142. raise ProfilerFilterConditionException("The filter value must be list")
  143. if key not in filter_key:
  144. raise ProfilerFilterConditionException("The filter key must in {}.".format(filter_key))
  145. for item in value:
  146. if not isinstance(item, str):
  147. raise ProfilerFilterConditionException("The item in filter value must be str")
  148. filter_condition = search_condition.get("filter_condition")
  149. if not isinstance(filter_condition, dict):
  150. raise ProfilerFilterConditionException("The filter condition must be dict.")
  151. filter_key = ["in", "not_in", "partial_match_str_in"]
  152. if filter_condition:
  153. if "op_type" in filter_condition:
  154. op_type_condition = filter_condition.get("op_type")
  155. validate_op_filter_condition(op_type_condition)
  156. if "op_name" in filter_condition:
  157. op_name_condition = filter_condition.get("op_name")
  158. validate_op_filter_condition(op_name_condition)
  159. if "op_type" not in filter_condition and "op_name" not in filter_condition:
  160. raise ProfilerFilterConditionException("The key of filter_condition is not support")