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

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