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 13 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. import os
  17. import sys
  18. from mindinsight.datavisual.utils.tools import to_int
  19. from mindinsight.profiler.common.exceptions.exceptions import ProfilerParamTypeErrorException, \
  20. ProfilerDeviceIdException, ProfilerOpTypeException, \
  21. ProfilerSortConditionException, ProfilerFilterConditionException, \
  22. ProfilerGroupConditionException, ProfilerParamValueErrorException
  23. from mindinsight.profiler.common.log import logger as log
  24. AICORE_TYPE_COL = ["op_type", "execution_time", "execution_frequency", "precent"]
  25. AICORE_DETAIL_COL = ["op_name", "op_type", "avg_execution_time", "subgraph", "full_op_name"]
  26. AICPU_COL = ["serial_number", "op_type", "total_time", "dispatch_time", "run_start",
  27. "run_end"]
  28. MINDDATA_PIPELINE_COL = [
  29. 'op_id', 'op_type', 'num_workers', 'output_queue_average_size',
  30. 'output_queue_length', 'output_queue_usage_rate', 'sample_interval',
  31. 'parent_id'
  32. ]
  33. def validate_condition(search_condition):
  34. """
  35. Verify the param in search_condition is valid or not.
  36. Args:
  37. search_condition (dict): The search condition.
  38. Raises:
  39. ProfilerParamTypeErrorException: If the type of the param in search_condition is invalid.
  40. ProfilerDeviceIdException: If the device_id param in search_condition is invalid.
  41. ProfilerOpTypeException: If the op_type param in search_condition is invalid.
  42. ProfilerGroupConditionException: If the group_condition param in search_condition is invalid.
  43. ProfilerSortConditionException: If the sort_condition param in search_condition is invalid.
  44. ProfilerFilterConditionException: If the filter_condition param in search_condition is invalid.
  45. """
  46. if not isinstance(search_condition, dict):
  47. log.error("Invalid search_condition type, it should be dict.")
  48. raise ProfilerParamTypeErrorException(
  49. "Invalid search_condition type, it should be dict.")
  50. if "device_id" in search_condition:
  51. device_id = search_condition.get("device_id")
  52. if not isinstance(device_id, str):
  53. raise ProfilerDeviceIdException("Invalid device_id type, it should be str.")
  54. if "op_type" in search_condition:
  55. op_type = search_condition.get("op_type")
  56. if op_type == "aicpu":
  57. search_scope = AICPU_COL
  58. elif op_type == "aicore_type":
  59. search_scope = AICORE_TYPE_COL
  60. elif op_type == "aicore_detail":
  61. search_scope = AICORE_DETAIL_COL
  62. else:
  63. raise ProfilerOpTypeException("The op_type must in ['aicpu', 'aicore_type', 'aicore_detail']")
  64. else:
  65. raise ProfilerOpTypeException("The op_type must in ['aicpu', 'aicore_type', 'aicore_detail']")
  66. if "group_condition" in search_condition:
  67. validate_group_condition(search_condition)
  68. if "sort_condition" in search_condition:
  69. validate_sort_condition(search_condition, search_scope)
  70. if "filter_condition" in search_condition:
  71. validate_filter_condition(search_condition)
  72. def validate_group_condition(search_condition):
  73. """
  74. Verify the group_condition in search_condition is valid or not.
  75. Args:
  76. search_condition (dict): The search condition.
  77. Raises:
  78. ProfilerGroupConditionException: If the group_condition param in search_condition is invalid.
  79. """
  80. group_condition = search_condition.get("group_condition")
  81. if not isinstance(group_condition, dict):
  82. raise ProfilerGroupConditionException("The group condition must be dict.")
  83. if "limit" in group_condition:
  84. limit = group_condition.get("limit", 10)
  85. if isinstance(limit, bool) \
  86. or not isinstance(group_condition.get("limit"), int):
  87. log.error("The limit must be int.")
  88. raise ProfilerGroupConditionException("The limit must be int.")
  89. if limit < 1 or limit > 100:
  90. raise ProfilerGroupConditionException("The limit must in [1, 100].")
  91. if "offset" in group_condition:
  92. offset = group_condition.get("offset", 0)
  93. if isinstance(offset, bool) \
  94. or not isinstance(group_condition.get("offset"), int):
  95. log.error("The offset must be int.")
  96. raise ProfilerGroupConditionException("The offset must be int.")
  97. if offset < 0:
  98. raise ProfilerGroupConditionException("The offset must ge 0.")
  99. if offset > 1000000:
  100. raise ProfilerGroupConditionException("The offset must le 1000000.")
  101. def validate_sort_condition(search_condition, search_scope):
  102. """
  103. Verify the sort_condition in search_condition is valid or not.
  104. Args:
  105. search_condition (dict): The search condition.
  106. search_scope (list): The search scope.
  107. Raises:
  108. ProfilerSortConditionException: If the sort_condition param in search_condition is invalid.
  109. """
  110. sort_condition = search_condition.get("sort_condition")
  111. if not isinstance(sort_condition, dict):
  112. raise ProfilerSortConditionException("The sort condition must be dict.")
  113. if "name" in sort_condition:
  114. sorted_name = sort_condition.get("name", "")
  115. err_msg = "The sorted_name must be in {}".format(search_scope)
  116. if not isinstance(sorted_name, str):
  117. log.error("Wrong sorted name type.")
  118. raise ProfilerSortConditionException("Wrong sorted name type.")
  119. if sorted_name not in search_scope:
  120. log.error(err_msg)
  121. raise ProfilerSortConditionException(err_msg)
  122. if "type" in sort_condition:
  123. sorted_type_param = ['ascending', 'descending']
  124. sorted_type = sort_condition.get("type")
  125. if sorted_type and sorted_type not in sorted_type_param:
  126. err_msg = "The sorted type must be ascending or descending."
  127. log.error(err_msg)
  128. raise ProfilerSortConditionException(err_msg)
  129. def validate_op_filter_condition(op_condition, value_type=str, value_type_msg='str'):
  130. """
  131. Verify the op_condition in filter_condition is valid or not.
  132. Args:
  133. op_condition (dict): The op_condition in search_condition.
  134. value_type (type): The value type. Default: str.
  135. value_type_msg (str): The value type message. Default: 'str'.
  136. Raises:
  137. ProfilerFilterConditionException: If the filter_condition param in search_condition is invalid.
  138. """
  139. filter_key = ["in", "not_in", "partial_match_str_in"]
  140. if not isinstance(op_condition, dict):
  141. raise ProfilerFilterConditionException("The filter condition value must be dict.")
  142. for key, value in op_condition.items():
  143. if not isinstance(key, str):
  144. raise ProfilerFilterConditionException("The filter key must be str")
  145. if not isinstance(value, list):
  146. raise ProfilerFilterConditionException("The filter value must be list")
  147. if key not in filter_key:
  148. raise ProfilerFilterConditionException("The filter key must in {}.".format(filter_key))
  149. for item in value:
  150. if not isinstance(item, value_type):
  151. raise ProfilerFilterConditionException(
  152. "The item in filter value must be {}.".format(value_type_msg)
  153. )
  154. def validate_filter_condition(search_condition):
  155. """
  156. Verify the filter_condition in search_condition is valid or not.
  157. Args:
  158. search_condition (dict): The search condition.
  159. Raises:
  160. ProfilerFilterConditionException: If the filter_condition param in search_condition is invalid.
  161. """
  162. filter_condition = search_condition.get("filter_condition")
  163. if not isinstance(filter_condition, dict):
  164. raise ProfilerFilterConditionException("The filter condition must be dict.")
  165. if filter_condition:
  166. if "op_type" in filter_condition:
  167. op_type_condition = filter_condition.get("op_type")
  168. validate_op_filter_condition(op_type_condition)
  169. if "op_name" in filter_condition:
  170. op_name_condition = filter_condition.get("op_name")
  171. validate_op_filter_condition(op_name_condition)
  172. if "op_type" not in filter_condition and "op_name" not in filter_condition:
  173. raise ProfilerFilterConditionException("The key of filter_condition is not support")
  174. def validate_and_set_job_id_env(job_id_env):
  175. """
  176. Validate the job id and set it in environment.
  177. Args:
  178. job_id_env (str): The id that to be set in environment parameter `JOB_ID`.
  179. Returns:
  180. int, the valid job id env.
  181. """
  182. if job_id_env is None:
  183. return job_id_env
  184. # get job_id_env in int type
  185. valid_id = to_int(job_id_env, 'job_id_env')
  186. # check the range of valid_id
  187. if valid_id and 255 < valid_id < sys.maxsize:
  188. os.environ['JOB_ID'] = job_id_env
  189. else:
  190. log.warning("Invalid job_id_env %s. The value should be int and between 255 and %s. Use"
  191. "default job id env instead.",
  192. job_id_env, sys.maxsize)
  193. return valid_id
  194. def validate_ui_proc(proc_name):
  195. """
  196. Validate proc name in restful request.
  197. Args:
  198. proc_name (str): The proc name to query. Acceptable value is in
  199. [`iteration_interval`, `fp_and_bp`, `tail`].
  200. Raises:
  201. ProfilerParamValueErrorException: If the proc_name is invalid.
  202. """
  203. accept_names = ['iteration_interval', 'fp_and_bp', 'tail']
  204. if proc_name not in accept_names:
  205. log.error("Invalid proc_name. The proc_name for restful api is in %s", accept_names)
  206. raise ProfilerParamValueErrorException(f'proc_name should be in {accept_names}.')
  207. def validate_minddata_pipeline_condition(condition):
  208. """
  209. Verify the minddata pipeline search condition is valid or not.
  210. Args:
  211. condition (dict): The minddata pipeline search condition.
  212. Raises:
  213. ProfilerParamTypeErrorException: If the type of the search condition is
  214. invalid.
  215. ProfilerDeviceIdException: If the device_id param in the search
  216. condition is invalid.
  217. ProfilerGroupConditionException: If the group_condition param in the
  218. search condition is invalid.
  219. ProfilerSortConditionException: If the sort_condition param in the
  220. search condition is invalid.
  221. ProfilerFilterConditionException: If the filter_condition param in the
  222. search condition is invalid.
  223. """
  224. if not isinstance(condition, dict):
  225. log.error("Invalid condition type, it should be dict.")
  226. raise ProfilerParamTypeErrorException(
  227. "Invalid condition type, it should be dict."
  228. )
  229. if "device_id" in condition:
  230. device_id = condition.get("device_id")
  231. if not isinstance(device_id, str):
  232. raise ProfilerDeviceIdException(
  233. "Invalid device_id type, it should be str."
  234. )
  235. if "group_condition" in condition:
  236. validate_group_condition(condition)
  237. if "sort_condition" in condition:
  238. validate_sort_condition(condition, MINDDATA_PIPELINE_COL)
  239. if "filter_condition" in condition:
  240. filter_condition = condition.get('filter_condition')
  241. if not isinstance(filter_condition, dict):
  242. raise ProfilerFilterConditionException(
  243. "The filter condition must be dict."
  244. )
  245. for key, value in filter_condition.items():
  246. if key == 'op_id':
  247. validate_op_filter_condition(
  248. value, value_type=int, value_type_msg='int'
  249. )
  250. elif key == 'op_type':
  251. validate_op_filter_condition(value)
  252. elif key == 'is_display_op_detail':
  253. if not isinstance(key, bool):
  254. raise ProfilerFilterConditionException(
  255. "The condition must be bool."
  256. )
  257. else:
  258. raise ProfilerFilterConditionException(
  259. "The key {} of filter_condition is not support.".format(key)
  260. )