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.

mi_validators.py 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. # Copyright 2021 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. Validator Functions for Offline Debugger APIs.
  17. """
  18. from functools import wraps
  19. import mindspore.offline_debug.dbg_services as cds
  20. from mindspore.offline_debug.mi_validator_helpers import parse_user_args, type_check, type_check_list, check_dir, check_uint32, check_uint64, check_iteration
  21. def check_init(method):
  22. """Wrapper method to check the parameters of DbgServices init."""
  23. @wraps(method)
  24. def new_method(self, *args, **kwargs):
  25. [dump_file_path, verbose], _ = parse_user_args(method, *args, **kwargs)
  26. type_check(dump_file_path, (str,), "dump_file_path")
  27. type_check(verbose, (bool,), "verbose")
  28. check_dir(dump_file_path)
  29. return method(self, *args, **kwargs)
  30. return new_method
  31. def check_initialize(method):
  32. """Wrapper method to check the parameters of DbgServices Initialize method."""
  33. @wraps(method)
  34. def new_method(self, *args, **kwargs):
  35. [net_name, is_sync_mode], _ = parse_user_args(method, *args, **kwargs)
  36. type_check(net_name, (str,), "net_name")
  37. type_check(is_sync_mode, (bool,), "is_sync_mode")
  38. return method(self, *args, **kwargs)
  39. return new_method
  40. def check_add_watchpoint(method):
  41. """Wrapper method to check the parameters of DbgServices AddWatchpoint."""
  42. @wraps(method)
  43. def new_method(self, *args, **kwargs):
  44. [id_value, watch_condition, check_node_list, parameter_list], _ = parse_user_args(method, *args, **kwargs)
  45. check_uint32(id_value, "id")
  46. check_uint32(watch_condition, "watch_condition")
  47. type_check(check_node_list, (dict,), "check_node_list")
  48. for node_name, node_info in check_node_list.items():
  49. type_check(node_name, (str,), "node_name")
  50. type_check(node_info, (dict,), "node_info")
  51. for info_name, info_param in node_info.items():
  52. type_check(info_name, (str,), "node parameter name")
  53. if info_name in ["rank_id"]:
  54. if isinstance(info_param, str):
  55. if info_param not in ["*"]:
  56. raise ValueError("Node parameter {} only accepts '*' as string.".format(info_name))
  57. else:
  58. for param in info_param:
  59. check_uint32(param, "rank_id")
  60. elif info_name in ["root_graph_id"]:
  61. if isinstance(info_param, str):
  62. if info_param not in ["*"]:
  63. raise ValueError("Node parameter {} only accepts '*' as string.".format(info_name))
  64. else:
  65. for param in info_param:
  66. check_uint32(param, "root_graph_id")
  67. elif info_name in ["is_output"]:
  68. type_check(info_param, (bool,), "is_output")
  69. else:
  70. raise ValueError("Node parameter {} is not defined.".format(info_name))
  71. param_names = ["param_{0}".format(i) for i in range(len(parameter_list))]
  72. type_check_list(parameter_list, (cds.Parameter,), param_names)
  73. return method(self, *args, **kwargs)
  74. return new_method
  75. def check_remove_watchpoint(method):
  76. """Wrapper method to check the parameters of DbgServices RemoveWatchpoint."""
  77. @wraps(method)
  78. def new_method(self, *args, **kwargs):
  79. [id_value], _ = parse_user_args(method, *args, **kwargs)
  80. check_uint32(id_value, "id")
  81. return method(self, *args, **kwargs)
  82. return new_method
  83. def check_check_watchpoints(method):
  84. """Wrapper method to check the parameters of DbgServices CheckWatchpoint."""
  85. @wraps(method)
  86. def new_method(self, *args, **kwargs):
  87. [iteration], _ = parse_user_args(method, *args, **kwargs)
  88. check_iteration(iteration, "iteration")
  89. return method(self, *args, **kwargs)
  90. return new_method
  91. def check_read_tensor_info(method):
  92. """Wrapper method to check the parameters of DbgServices ReadTensors."""
  93. @wraps(method)
  94. def new_method(self, *args, **kwargs):
  95. [info_list], _ = parse_user_args(method, *args, **kwargs)
  96. info_names = ["info_{0}".format(i) for i in range(len(info_list))]
  97. type_check_list(info_list, (cds.TensorInfo,), info_names)
  98. return method(self, *args, **kwargs)
  99. return new_method
  100. def check_initialize_done(method):
  101. """Wrapper method to check if initlize is done for DbgServices."""
  102. @wraps(method)
  103. def new_method(self, *args, **kwargs):
  104. if not self.initialized:
  105. raise RuntimeError("Inilize should be called before any other methods of DbgServices!")
  106. return method(self, *args, **kwargs)
  107. return new_method
  108. def check_tensor_info_init(method):
  109. """Wrapper method to check the parameters of DbgServices TensorInfo init."""
  110. @wraps(method)
  111. def new_method(self, *args, **kwargs):
  112. [node_name, slot, iteration, rank_id, root_graph_id,
  113. is_output], _ = parse_user_args(method, *args, **kwargs)
  114. type_check(node_name, (str,), "node_name")
  115. check_uint32(slot, "slot")
  116. check_iteration(iteration, "iteration")
  117. check_uint32(rank_id, "rank_id")
  118. check_uint32(root_graph_id, "root_graph_id")
  119. type_check(is_output, (bool,), "is_output")
  120. return method(self, *args, **kwargs)
  121. return new_method
  122. def check_tensor_data_init(method):
  123. """Wrapper method to check the parameters of DbgServices TensorData init."""
  124. @wraps(method)
  125. def new_method(self, *args, **kwargs):
  126. [data_ptr, data_size, dtype, shape], _ = parse_user_args(method, *args, **kwargs)
  127. type_check(data_ptr, (bytes,), "data_ptr")
  128. check_uint64(data_size, "data_size")
  129. type_check(dtype, (int,), "dtype")
  130. shape_names = ["shape_{0}".format(i) for i in range(len(shape))]
  131. type_check_list(shape, (int,), shape_names)
  132. if len(data_ptr) != data_size:
  133. raise ValueError("data_ptr length ({0}) is not equal to data_size ({1}).".format(len(data_ptr), data_size))
  134. return method(self, *args, **kwargs)
  135. return new_method
  136. def check_tensor_base_data_init(method):
  137. """Wrapper method to check the parameters of DbgServices TensorBaseData init."""
  138. @wraps(method)
  139. def new_method(self, *args, **kwargs):
  140. [data_size, dtype, shape], _ = parse_user_args(method, *args, **kwargs)
  141. check_uint64(data_size, "data_size")
  142. type_check(dtype, (int,), "dtype")
  143. shape_names = ["shape_{0}".format(i) for i in range(len(shape))]
  144. type_check_list(shape, (int,), shape_names)
  145. return method(self, *args, **kwargs)
  146. return new_method
  147. def check_tensor_stat_data_init(method):
  148. """Wrapper method to check the parameters of DbgServices TensorBaseData init."""
  149. @wraps(method)
  150. def new_method(self, *args, **kwargs):
  151. [data_size, dtype, shape, is_bool, max_value, min_value,
  152. avg_value, count, neg_zero_count, pos_zero_count,
  153. nan_count, neg_inf_count, pos_inf_count,
  154. zero_count], _ = parse_user_args(method, *args, **kwargs)
  155. check_uint64(data_size, "data_size")
  156. type_check(dtype, (int,), "dtype")
  157. shape_names = ["shape_{0}".format(i) for i in range(len(shape))]
  158. type_check_list(shape, (int,), shape_names)
  159. type_check(is_bool, (bool,), "is_bool")
  160. type_check(max_value, (float,), "max_value")
  161. type_check(min_value, (float,), "min_value")
  162. type_check(avg_value, (float,), "avg_value")
  163. type_check(count, (int,), "count")
  164. type_check(neg_zero_count, (int,), "neg_zero_count")
  165. type_check(pos_zero_count, (int,), "pos_zero_count")
  166. type_check(nan_count, (int,), "nan_count")
  167. type_check(neg_inf_count, (int,), "neg_inf_count")
  168. type_check(pos_inf_count, (int,), "pos_inf_count")
  169. type_check(zero_count, (int,), "zero_count")
  170. return method(self, *args, **kwargs)
  171. return new_method
  172. def check_watchpoint_hit_init(method):
  173. """Wrapper method to check the parameters of DbgServices WatchpointHit init."""
  174. @wraps(method)
  175. def new_method(self, *args, **kwargs):
  176. [name, slot, condition, watchpoint_id,
  177. parameters, error_code, rank_id, root_graph_id], _ = parse_user_args(method, *args, **kwargs)
  178. type_check(name, (str,), "name")
  179. check_uint32(slot, "slot")
  180. type_check(condition, (int,), "condition")
  181. check_uint32(watchpoint_id, "watchpoint_id")
  182. param_names = ["param_{0}".format(i) for i in range(len(parameters))]
  183. type_check_list(parameters, (cds.Parameter,), param_names)
  184. type_check(error_code, (int,), "error_code")
  185. check_uint32(rank_id, "rank_id")
  186. check_uint32(root_graph_id, "root_graph_id")
  187. return method(self, *args, **kwargs)
  188. return new_method
  189. def check_parameter_init(method):
  190. """Wrapper method to check the parameters of DbgServices Parameter init."""
  191. @wraps(method)
  192. def new_method(self, *args, **kwargs):
  193. [name, disabled, value, hit, actual_value], _ = parse_user_args(method, *args, **kwargs)
  194. type_check(name, (str,), "name")
  195. type_check(disabled, (bool,), "disabled")
  196. type_check(value, (float,), "value")
  197. type_check(hit, (bool,), "hit")
  198. type_check(actual_value, (float,), "actual_value")
  199. return method(self, *args, **kwargs)
  200. return new_method