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.

debug_services.h 15 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /**
  2. * Copyright 2020-2021 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MINDSPORE_CCSRC_DEBUG_DEBUG_SERVICES_H_
  17. #define MINDSPORE_CCSRC_DEBUG_DEBUG_SERVICES_H_
  18. #ifndef OFFLINE_DBG_MODE
  19. #define ONLINE_DBG_MODE
  20. #endif
  21. #ifdef OFFLINE_DBG_MODE
  22. #include "base/float16.h"
  23. #include "debugger/offline_debug/offline_logger.h"
  24. #endif
  25. #include <math.h>
  26. #include <vector>
  27. #include <string>
  28. #include <memory>
  29. #include <tuple>
  30. #include <unordered_map>
  31. #include <set>
  32. #include <mutex>
  33. #include <map>
  34. #include <limits>
  35. #include <sstream>
  36. #include "debug/tensor_load.h"
  37. #include "debug/tensor_data.h"
  38. #ifdef ONLINE_DBG_MODE
  39. namespace mindspore {
  40. #endif
  41. class DebugServices {
  42. public:
  43. DebugServices();
  44. DebugServices(const DebugServices &other);
  45. DebugServices &operator=(const DebugServices &other);
  46. ~DebugServices() = default;
  47. enum CONDITION_TYPE {
  48. HAS_NAN,
  49. HAS_INF,
  50. IS_OVERFLOW,
  51. MAX_GT,
  52. MAX_LT,
  53. MIN_GT,
  54. MIN_LT,
  55. MAX_MIN_GT,
  56. MAX_MIN_LT,
  57. MEAN_GT,
  58. MEAN_LT,
  59. SD_GT,
  60. SD_LT,
  61. GENERAL_OVERFLOW,
  62. INIT,
  63. TOO_LARGE,
  64. TOO_SMALL,
  65. ALL_ZERO,
  66. CHANGE_TOO_LARGE,
  67. CHANGE_TOO_SMALL,
  68. NOT_CHANGED,
  69. RANGE
  70. };
  71. struct condition_t {
  72. CONDITION_TYPE type;
  73. float parameter = 0;
  74. };
  75. struct parameter_t {
  76. std::string name;
  77. bool disabled;
  78. double_t value;
  79. bool hit;
  80. double_t actual_value;
  81. void Evaluate(double_t actualValue, std::string inequality_type) {
  82. if (std::isnan(actualValue)) return;
  83. actual_value = actualValue;
  84. // if cannot extract inequality type from watchpoint
  85. // try extract from parameter name
  86. if (inequality_type.empty()) {
  87. auto pos = name.find_last_of('_');
  88. if (pos != std::string::npos) {
  89. inequality_type = name.substr(pos + 1);
  90. }
  91. }
  92. std::map<std::string, bool> condition_check{{"gt", actual_value > value},
  93. {"lt", actual_value < value},
  94. {"ge", actual_value >= value},
  95. {"le", actual_value <= value}};
  96. hit = condition_check[inequality_type];
  97. }
  98. };
  99. typedef std::vector<std::vector<int>> partitioned_numbers;
  100. typedef std::vector<std::vector<std::string>> partitioned_names;
  101. typedef std::vector<std::vector<std::vector<parameter_t>>> partitioned_parameters;
  102. typedef std::vector<std::vector<int32_t>> partitioned_error_code;
  103. typedef std::vector<std::vector<unsigned int>> partitioned_id;
  104. struct watchpoint_t {
  105. unsigned int id;
  106. condition_t condition;
  107. std::vector<std::tuple<std::string, bool>> check_node_list;
  108. std::vector<std::tuple<std::string, std::vector<uint32_t>>> check_node_device_list;
  109. std::vector<std::tuple<std::string, std::vector<uint32_t>>> check_node_graph_list;
  110. std::vector<parameter_t> parameter_list;
  111. size_t location = 0;
  112. std::string FindQualifiedTensorName(const std::string &tensor_name) const {
  113. std::string node_name = tensor_name.substr(0, tensor_name.find_first_of(':'));
  114. for (auto check_node : check_node_list) {
  115. std::string w_name = std::get<0>(check_node);
  116. bool w_type = std::get<1>(check_node);
  117. auto found = w_name.find_last_of('/');
  118. if (found != std::string::npos && w_name.substr(found + 1) == tensor_name) return w_name;
  119. if ((w_type && (tensor_name.find(w_name) == location || w_name == "*")) || (!w_type && node_name == w_name)) {
  120. return w_name;
  121. }
  122. }
  123. return {};
  124. }
  125. bool is_gt_wp() const {
  126. return condition.type == MAX_GT || condition.type == MIN_GT || condition.type == MEAN_GT ||
  127. condition.type == SD_GT || condition.type == MAX_MIN_GT;
  128. }
  129. bool is_lt_wp() const {
  130. return condition.type == MAX_LT || condition.type == MIN_LT || condition.type == MEAN_LT ||
  131. condition.type == SD_LT || condition.type == MAX_MIN_LT;
  132. }
  133. bool min_max_enabled() const {
  134. return condition.type == MAX_LT || condition.type == MAX_GT || condition.type == MIN_LT ||
  135. condition.type == MIN_GT || condition.type == MAX_MIN_LT || condition.type == MAX_MIN_GT ||
  136. (condition.type == INIT && (!parameter_list[1].disabled || !parameter_list[2].disabled)) ||
  137. (condition.type == TOO_LARGE && (!parameter_list[1].disabled || !parameter_list[2].disabled)) ||
  138. (condition.type == TOO_SMALL && (!parameter_list[1].disabled || !parameter_list[2].disabled));
  139. }
  140. // inf or nan related condition set
  141. bool inf_nan_enabled() const {
  142. return condition.type == HAS_INF || condition.type == HAS_NAN || condition.type == GENERAL_OVERFLOW;
  143. }
  144. // mean or sd related condition set
  145. bool mean_sd_enabled() const {
  146. return condition.type == MEAN_LT || condition.type == MEAN_GT || condition.type == SD_LT ||
  147. condition.type == SD_GT || (condition.type == TOO_LARGE && !parameter_list[3].disabled) ||
  148. (condition.type == TOO_SMALL && !parameter_list[3].disabled);
  149. }
  150. bool abs_mean_enabled() const {
  151. return (condition.type == TOO_LARGE && !parameter_list[0].disabled) ||
  152. (condition.type == TOO_SMALL && !parameter_list[0].disabled);
  153. }
  154. bool zero_percentage_enabled() const { return condition.type == ALL_ZERO || condition.type == INIT; }
  155. bool tensor_update_ratio_mean_enabled() const {
  156. return condition.type == CHANGE_TOO_LARGE || condition.type == CHANGE_TOO_SMALL;
  157. }
  158. bool allclose_enabled() const { return condition.type == NOT_CHANGED; }
  159. bool range_enabled() const {
  160. return condition.type == RANGE && (!parameter_list[0].disabled || !parameter_list[1].disabled);
  161. }
  162. bool change_condition() const {
  163. return condition.type == CHANGE_TOO_LARGE || condition.type == CHANGE_TOO_SMALL || condition.type == NOT_CHANGED;
  164. }
  165. };
  166. void AddWatchpoint(
  167. unsigned int id, unsigned int watch_condition, float parameter,
  168. const std::vector<std::tuple<std::string, bool>> &check_node_list, const std::vector<parameter_t> &parameter_list,
  169. const std::vector<std::tuple<std::string, std::vector<uint32_t>>> *check_node_device_list = nullptr,
  170. const std::vector<std::tuple<std::string, std::vector<uint32_t>>> *check_node_graph_list = nullptr);
  171. void RemoveWatchpoint(unsigned int id);
  172. void CheckWatchpointsForTensor(partitioned_names *chunk_names, partitioned_names *chunk_slots,
  173. partitioned_numbers *chunk_conditions, partitioned_id *const chunk_watchpoint_id,
  174. partitioned_parameters *chunk_parameters, partitioned_error_code *chunk_error_codes,
  175. const std::vector<std::string> &op_overflows,
  176. const std::vector<std::string> &async_file_pool,
  177. partitioned_numbers *chunk_exec_orders,
  178. std::vector<std::shared_ptr<TensorData>> *tensor_list, int begin, int end,
  179. int chunk_id, const bool init_dbg_suspend, const bool step_end, const bool recheck,
  180. partitioned_id *chunk_device_id, partitioned_id *chunk_root_graph_id,
  181. std::vector<uint64_t> *chunk_tensor_byte_size, std::vector<unsigned int> *device_id,
  182. std::vector<unsigned int> *root_graph_id);
  183. void CheckWatchpoints(std::vector<std::string> *name, std::vector<std::string> *slot, std::vector<int> *condition,
  184. std::vector<unsigned int> *const watchpoint_id,
  185. std::vector<std::vector<parameter_t>> *parameters, std::vector<int32_t> *error_code,
  186. const std::vector<std::string> &op_overflows, const std::vector<std::string> &async_file_pool,
  187. std::vector<std::shared_ptr<TensorData>> *tensor_list, bool init_dbg_suspend,
  188. const bool step_end, const bool recheck, std::vector<unsigned int> *device_id = nullptr,
  189. std::vector<unsigned int> *root_graph_id = nullptr);
  190. void AddWatchPointsToCheck(bool init_dbg_suspend, bool step_end, bool recheck, const std::string &tensor_name,
  191. const std::string &tensor_name_no_slot, bool *previous_iter_tensor_needed,
  192. std::string *qualified_tensor_name, std::vector<watchpoint_t> *watchpoints_to_check);
  193. #ifdef OFFLINE_DBG_MODE
  194. void AddToTensorData(const std::string &backend_name, const std::size_t slot, const unsigned int iteration,
  195. const unsigned int device_id, const unsigned int root_graph_id, const bool is_output,
  196. const std::size_t data_size, const std::string &type_name, const std::vector<int64_t> &shape,
  197. std::vector<char> *buffer, std::vector<std::shared_ptr<TensorData>> *result_list);
  198. void SetPrefixToCheck(std::string *prefix_dump_file_name, std::string *slot_string_to_check,
  199. std::string *dump_style_kernel_name, size_t slot, bool is_output);
  200. void ReadDumpedTensor(std::vector<std::string> backend_name, std::vector<size_t> slot,
  201. std::vector<unsigned int> device_id, std::vector<unsigned int> iteration,
  202. std::vector<unsigned int> root_graph_id, const std::vector<bool> &is_output,
  203. const std::vector<std::string> &async_file_pool,
  204. std::vector<std::shared_ptr<TensorData>> *result_list);
  205. std::vector<std::shared_ptr<TensorData>> ReadNeededDumpedTensors(unsigned int iteration,
  206. std::vector<std::string> *async_file_pool);
  207. void *GetPrevTensor(const std::shared_ptr<TensorData> &tensor, bool previous_iter_tensor_needed);
  208. void ReadTensorFromNpy(const std::string &file_name, std::string *tensor_type, std::size_t *size,
  209. std::vector<int64_t> *shape, std::vector<char> **data_buffer);
  210. void ConvertToHostFormat(const std::map<std::string, std::vector<std::string>> &dir_to_files_map,
  211. std::vector<std::string> *result_list);
  212. void ConvertReadTensors(std::vector<std::string> backend_name, std::vector<size_t> slot,
  213. std::vector<unsigned int> device_id, std::vector<unsigned int> iteration,
  214. std::vector<unsigned int> root_graph_id, std::vector<std::string> *result_list);
  215. void ConvertWatchPointNodes(const std::vector<std::tuple<std::string, std::string>> &proto_dump,
  216. const std::string &specific_dump_dir, std::vector<std::string> *result_list);
  217. void GetTensorDataInfoAsync(const std::vector<std::tuple<std::string, std::string>> &proto_dump,
  218. const std::string &specific_dump_dir, uint32_t iteration, uint32_t device_id,
  219. uint32_t root_graph_id, const std::vector<std::string> &async_file_pool,
  220. std::vector<std::shared_ptr<TensorData>> *tensor_list);
  221. std::string GetStrippedFilename(const std::string &file_name);
  222. std::string IterationString(unsigned int iteration);
  223. #endif
  224. void ReadNodesTensors(const std::vector<std::string> &name, std::vector<std::string> *ret_name,
  225. std::vector<char *> *data_ptr, std::vector<ssize_t> *data_size,
  226. std::vector<unsigned int> *dtype, std::vector<std::vector<int64_t>> *const shape);
  227. #ifdef ONLINE_DBG_MODE
  228. bool IsWatchPoint(const std::string &kernel_name, const CNodePtr &kernel = nullptr) const;
  229. bool IsWatchPointNodeInput(const std::string &w_name, const CNodePtr &kernel) const;
  230. #endif
  231. void EmptyTensor();
  232. std::vector<std::shared_ptr<TensorData>> GetTensor() const;
  233. void AddAnalyzedTensorToCache(const bool recheck, const unsigned int id, const std::string &tensor_name);
  234. std::vector<std::shared_ptr<TensorData>> GetNodeTensorMap(const std::string &node_name) const;
  235. uint32_t GetTensorLoaderIterNum() const;
  236. void SetTensorLoaderIterNum(uint32_t iter_num);
  237. void EmptyPrevTensor();
  238. void EmptyCurrentTensor();
  239. #ifdef ONLINE_DBG_MODE
  240. bool DumpTensorToFile(const std::string &tensor_name, bool trans_flag, const std::string &filepath,
  241. const std::string &host_fmt, const std::vector<int64_t> &host_shape, TypeId host_type,
  242. TypeId device_type, const std::string &addr_format, size_t slot) const;
  243. #endif
  244. bool LoadNewTensor(const std::shared_ptr<TensorData> &tensor, bool keep_prev);
  245. std::unordered_map<unsigned int, watchpoint_t> GetWatchpointTable();
  246. void ResetLoadedTensors();
  247. #ifdef ONLINE_DBG_MODE
  248. std::vector<std::shared_ptr<TensorData>> GetNodeTensor(const CNodePtr &kernel);
  249. #endif
  250. // Find if any operation overflow happened on a particular node name
  251. bool CheckOpOverflow(std::string node_name_to_find, unsigned int device_id = 0, unsigned int root_graph_id = 0,
  252. unsigned int iteration = 0);
  253. bool GetAttrsFromAsyncFilename(const std::string &file_name, std::string *node_name, uint64_t *task_id,
  254. uint64_t *stream_id);
  255. std::string RealPath(const std::string &input_path);
  256. uint64_t BytestoUInt64(const std::vector<char> &buffer);
  257. bool TensorExistsInCurrent(const std::string &tensor_name);
  258. void MoveTensorCurrentToPrev(const std::string &tensor_name);
  259. void SetNetName(std::string net_name);
  260. std::string GetNetName();
  261. void SetDumpDir(std::string dump_dir);
  262. std::string GetDumpDir();
  263. void SetSyncMode(bool is_sync_mode);
  264. bool GetSyncMode();
  265. private:
  266. std::mutex lock_;
  267. std::mutex wp_lock_;
  268. std::mutex overflow_wp_lock_;
  269. // to keep track of watchpoints that have been checked already for a tensor in current step
  270. std::unordered_map<std::string, std::set<int32_t>> wp_id_cache;
  271. std::unordered_map<unsigned int, watchpoint_t> watchpoint_table;
  272. // key is the iteration path, value is vector of op_names which have overflowed
  273. std::unordered_map<std::string, std::vector<std::string>> overflow_ops;
  274. std::string net_name;
  275. std::string dump_dir;
  276. bool is_sync_mode;
  277. std::shared_ptr<TensorLoader> tensor_loader_;
  278. };
  279. #ifdef ONLINE_DBG_MODE
  280. } // namespace mindspore
  281. #endif
  282. #endif // MINDSPORE_CCSRC_DEBUG_DEBUG_SERVICES_H_