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 21 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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 <future>
  28. #include <string>
  29. #include <memory>
  30. #include <tuple>
  31. #include <unordered_map>
  32. #include <set>
  33. #include <mutex>
  34. #include <map>
  35. #include <limits>
  36. #include <sstream>
  37. #include "debug/tensor_load.h"
  38. #include "debug/tensor_data.h"
  39. #ifdef ONLINE_DBG_MODE
  40. namespace mindspore {
  41. #endif
  42. class DebugServices {
  43. public:
  44. DebugServices();
  45. DebugServices(const DebugServices &other);
  46. DebugServices &operator=(const DebugServices &other);
  47. ~DebugServices() = default;
  48. enum CONDITION_TYPE {
  49. HAS_NAN,
  50. HAS_INF,
  51. IS_OVERFLOW,
  52. MAX_GT,
  53. MAX_LT,
  54. MIN_GT,
  55. MIN_LT,
  56. MAX_MIN_GT,
  57. MAX_MIN_LT,
  58. MEAN_GT,
  59. MEAN_LT,
  60. SD_GT,
  61. SD_LT,
  62. GENERAL_OVERFLOW,
  63. INIT,
  64. TOO_LARGE,
  65. TOO_SMALL,
  66. ALL_ZERO,
  67. CHANGE_TOO_LARGE,
  68. CHANGE_TOO_SMALL,
  69. NOT_CHANGED,
  70. RANGE
  71. };
  72. struct condition_t {
  73. CONDITION_TYPE type;
  74. float parameter = 0;
  75. };
  76. struct parameter_t {
  77. std::string name;
  78. bool disabled;
  79. double_t value;
  80. bool hit;
  81. double_t actual_value;
  82. void Evaluate(double_t actualValue, std::string inequality_type) {
  83. if (std::isnan(actualValue)) return;
  84. actual_value = actualValue;
  85. // if cannot extract inequality type from watchpoint
  86. // try extract from parameter name
  87. if (inequality_type.empty()) {
  88. auto pos = name.find_last_of('_');
  89. if (pos != std::string::npos) {
  90. inequality_type = name.substr(pos + 1);
  91. }
  92. }
  93. std::map<std::string, bool> condition_check{{"gt", actual_value > value},
  94. {"lt", actual_value < value},
  95. {"ge", actual_value >= value},
  96. {"le", actual_value <= value}};
  97. hit = condition_check[inequality_type];
  98. }
  99. };
  100. typedef std::vector<std::vector<int>> partitioned_numbers;
  101. typedef std::vector<std::vector<std::string>> partitioned_names;
  102. typedef std::vector<std::vector<std::vector<parameter_t>>> partitioned_parameters;
  103. typedef std::vector<std::vector<int32_t>> partitioned_error_code;
  104. typedef std::vector<std::vector<unsigned int>> partitioned_id;
  105. struct watchpoint_t {
  106. unsigned int id;
  107. condition_t condition;
  108. std::vector<std::tuple<std::string, bool>> check_node_list;
  109. std::vector<std::tuple<std::string, std::vector<uint32_t>>> check_node_device_list;
  110. std::vector<std::tuple<std::string, std::vector<uint32_t>>> check_node_graph_list;
  111. std::vector<parameter_t> parameter_list;
  112. size_t location = 0;
  113. std::string FindQualifiedTensorName(const std::string &tensor_name, unsigned const int &tensor_device_id,
  114. unsigned const int &tensor_root_graph_id) const {
  115. int indx = 0;
  116. for (auto check_node : check_node_list) {
  117. std::string w_name = std::get<0>(check_node);
  118. bool w_type = std::get<1>(check_node);
  119. auto found = w_name.find_last_of('/');
  120. bool check_tensor_name = found != std::string::npos && w_name.substr(found + 1) == tensor_name;
  121. bool check_node_name =
  122. (w_type && (tensor_name == w_name || w_name == "*")) || (!w_type && tensor_name == w_name);
  123. if (check_tensor_name || check_node_name) {
  124. // online debugger only support single card
  125. if (check_node_device_list.empty()) {
  126. return w_name;
  127. }
  128. auto device_vec = std::get<1>(check_node_device_list[indx]);
  129. auto root_graph_vec = std::get<1>(check_node_graph_list[indx]);
  130. auto iter1 = std::find(device_vec.begin(), device_vec.end(), tensor_device_id);
  131. auto iter2 = std::find(root_graph_vec.begin(), root_graph_vec.end(), tensor_root_graph_id);
  132. if (iter1 != device_vec.end() && iter2 != root_graph_vec.end()) {
  133. return w_name;
  134. }
  135. }
  136. indx++;
  137. }
  138. return {};
  139. }
  140. bool is_gt_wp() const {
  141. return condition.type == MAX_GT || condition.type == MIN_GT || condition.type == MEAN_GT ||
  142. condition.type == SD_GT || condition.type == MAX_MIN_GT;
  143. }
  144. bool is_lt_wp() const {
  145. return condition.type == MAX_LT || condition.type == MIN_LT || condition.type == MEAN_LT ||
  146. condition.type == SD_LT || condition.type == MAX_MIN_LT;
  147. }
  148. bool min_max_enabled() const {
  149. return condition.type == MAX_LT || condition.type == MAX_GT || condition.type == MIN_LT ||
  150. condition.type == MIN_GT || condition.type == MAX_MIN_LT || condition.type == MAX_MIN_GT ||
  151. (condition.type == INIT && (!parameter_list[1].disabled || !parameter_list[2].disabled)) ||
  152. (condition.type == TOO_LARGE && (!parameter_list[1].disabled || !parameter_list[2].disabled)) ||
  153. (condition.type == TOO_SMALL && (!parameter_list[1].disabled || !parameter_list[2].disabled));
  154. }
  155. // inf or nan related condition set
  156. bool inf_nan_enabled() const {
  157. return condition.type == HAS_INF || condition.type == HAS_NAN || condition.type == GENERAL_OVERFLOW;
  158. }
  159. // mean or sd related condition set
  160. bool mean_sd_enabled() const {
  161. return condition.type == MEAN_LT || condition.type == MEAN_GT || condition.type == SD_LT ||
  162. condition.type == SD_GT || (condition.type == TOO_LARGE && !parameter_list[3].disabled) ||
  163. (condition.type == TOO_SMALL && !parameter_list[3].disabled);
  164. }
  165. bool abs_mean_enabled() const {
  166. return (condition.type == TOO_LARGE && !parameter_list[0].disabled) ||
  167. (condition.type == TOO_SMALL && !parameter_list[0].disabled);
  168. }
  169. bool zero_percentage_enabled() const { return condition.type == ALL_ZERO || condition.type == INIT; }
  170. bool tensor_update_ratio_mean_enabled() const {
  171. return condition.type == CHANGE_TOO_LARGE || condition.type == CHANGE_TOO_SMALL;
  172. }
  173. bool allclose_enabled() const { return condition.type == NOT_CHANGED; }
  174. bool range_enabled() const {
  175. return condition.type == RANGE && (!parameter_list[0].disabled || !parameter_list[1].disabled);
  176. }
  177. bool change_condition() const {
  178. return condition.type == CHANGE_TOO_LARGE || condition.type == CHANGE_TOO_SMALL || condition.type == NOT_CHANGED;
  179. }
  180. };
  181. struct TensorBase {
  182. TensorBase(uint64_t data_size, int dtype, const std::vector<int64_t> &shape)
  183. : data_size(data_size), dtype(dtype), shape(shape) {}
  184. TensorBase() = default;
  185. uint64_t data_size = 0;
  186. int dtype = 0;
  187. std::vector<int64_t> shape;
  188. };
  189. struct TensorStat {
  190. TensorStat(uint64_t data_size, int dtype, const std::vector<int64_t> &shape, bool is_bool, double max_value,
  191. double min_value, double avg_value, int count, int neg_zero_count, int pos_zero_count, int nan_count,
  192. int neg_inf_count, int pos_inf_count, int zero_count)
  193. : data_size(data_size),
  194. dtype(dtype),
  195. shape(shape),
  196. is_bool(is_bool),
  197. max_value(max_value),
  198. min_value(min_value),
  199. avg_value(avg_value),
  200. count(count),
  201. neg_zero_count(neg_zero_count),
  202. pos_zero_count(pos_zero_count),
  203. nan_count(nan_count),
  204. neg_inf_count(neg_inf_count),
  205. pos_inf_count(pos_inf_count),
  206. zero_count(zero_count) {}
  207. TensorStat() = default;
  208. uint64_t data_size = 0;
  209. int dtype = 0;
  210. std::vector<int64_t> shape;
  211. bool is_bool = false;
  212. double max_value = std::numeric_limits<double>::lowest();
  213. double min_value = std::numeric_limits<double>::max();
  214. double avg_value = 0.0;
  215. int count = 0;
  216. int neg_zero_count = 0;
  217. int pos_zero_count = 0;
  218. int nan_count = 0;
  219. int neg_inf_count = 0;
  220. int pos_inf_count = 0;
  221. int zero_count = 0;
  222. };
  223. TensorStat GetTensorStatistics(const std::shared_ptr<TensorData> &tensor);
  224. void AddWatchpoint(
  225. unsigned int id, unsigned int watch_condition, float parameter,
  226. const std::vector<std::tuple<std::string, bool>> &check_node_list, const std::vector<parameter_t> &parameter_list,
  227. const std::vector<std::tuple<std::string, std::vector<uint32_t>>> *check_node_device_list = nullptr,
  228. const std::vector<std::tuple<std::string, std::vector<uint32_t>>> *check_node_graph_list = nullptr);
  229. void RemoveWatchpoint(unsigned int id);
  230. void CheckWatchpointsForTensor(partitioned_names *chunk_names, partitioned_names *chunk_slots,
  231. partitioned_numbers *chunk_conditions, partitioned_id *const chunk_watchpoint_id,
  232. partitioned_parameters *chunk_parameters, partitioned_error_code *chunk_error_codes,
  233. const std::vector<std::string> &op_overflows,
  234. const std::vector<std::string> &async_file_pool,
  235. partitioned_numbers *chunk_exec_orders,
  236. std::vector<std::shared_ptr<TensorData>> *tensor_list, int begin, int end,
  237. int chunk_id, const bool init_dbg_suspend, const bool step_end, const bool recheck,
  238. partitioned_id *chunk_device_id, partitioned_id *chunk_root_graph_id,
  239. std::vector<uint64_t> *chunk_tensor_byte_size, partitioned_names *chunk_time_stamp,
  240. std::vector<unsigned int> *device_id, std::vector<unsigned int> *root_graph_id);
  241. void CheckWatchpoints(std::vector<std::string> *name, std::vector<std::string> *slot, std::vector<int> *condition,
  242. std::vector<unsigned int> *const watchpoint_id,
  243. std::vector<std::vector<parameter_t>> *parameters, std::vector<int32_t> *error_code,
  244. const std::vector<std::string> &op_overflows, const std::vector<std::string> &async_file_pool,
  245. std::vector<std::shared_ptr<TensorData>> *tensor_list, bool init_dbg_suspend,
  246. const bool step_end, const bool recheck, std::vector<unsigned int> *device_id = nullptr,
  247. std::vector<unsigned int> *root_graph_id = nullptr);
  248. void SortWatchpointsInfo(std::vector<std::future<void>> *tensor_future_vec, std::vector<int> *exec_order,
  249. std::vector<std::string> *time_stamps, uint64_t *tensor_list_byte_size,
  250. std::vector<std::string> *name, std::vector<std::string> *slot, std::vector<int> *condition,
  251. std::vector<unsigned int> *const watchpoint_id,
  252. std::vector<std::vector<parameter_t>> *parameters, std::vector<int32_t> *error_codes,
  253. partitioned_names *chunk_names, partitioned_names *chunk_slots,
  254. partitioned_numbers *chunk_conditions, partitioned_id *chunk_watchpoint_id,
  255. partitioned_parameters *chunk_parameters, partitioned_error_code *chunk_error_codes,
  256. partitioned_numbers *chunk_exec_orders, partitioned_names *chunk_time_stamp,
  257. std::vector<uint64_t> *chunk_tensor_byte_size, partitioned_id *chunk_device_id,
  258. partitioned_id *chunk_root_graph_id, std::vector<unsigned int> *device_id,
  259. std::vector<unsigned int> *root_graph_id);
  260. void AddWatchPointsToCheck(bool init_dbg_suspend, bool step_end, bool recheck,
  261. const std::shared_ptr<TensorData> &tensor, bool *previous_iter_tensor_needed,
  262. std::string *qualified_tensor_name, std::vector<watchpoint_t> *watchpoints_to_check);
  263. void SetCheckWatchpointsResult(const int chunk_id, partitioned_names *chunk_names, partitioned_names *chunk_slots,
  264. partitioned_numbers *chunk_conditions, partitioned_id *chunk_watchpoint_id,
  265. partitioned_parameters *chunk_parameters, partitioned_error_code *chunk_error_codes,
  266. partitioned_numbers *chunk_exec_orders, partitioned_names *chunk_time_stamp,
  267. partitioned_id *chunk_device_id, partitioned_id *chunk_root_graph_id,
  268. std::vector<unsigned int> *device_id, std::vector<unsigned int> *root_graph_id,
  269. const int exec_order, const std::string time_stamp,
  270. const std::string &qualified_tensor_name, const std::string &tensor_slot,
  271. const watchpoint_t &wp, const unsigned int device_id_val,
  272. const unsigned int root_graph_id_val, const std::vector<parameter_t> &parameter_list,
  273. const int32_t error_code);
  274. #ifdef OFFLINE_DBG_MODE
  275. void AddToTensorData(const std::string &backend_name, const std::string &time_stamp, const std::size_t slot,
  276. const unsigned int iteration, const unsigned int device_id, const unsigned int root_graph_id,
  277. const bool is_output, const std::size_t data_size, const std::string &type_name,
  278. const std::vector<int64_t> &shape, std::vector<char> *buffer,
  279. std::vector<std::shared_ptr<TensorData>> *result_list);
  280. void SetPrefixToCheck(std::string *prefix_dump_file_name, std::string *slot_string_to_check,
  281. std::string *dump_style_kernel_name, size_t slot, bool is_output);
  282. void ReadDumpedTensor(std::vector<std::string> backend_name, std::vector<size_t> slot,
  283. std::vector<unsigned int> device_id, std::vector<unsigned int> iteration,
  284. std::vector<unsigned int> root_graph_id, const std::vector<bool> &is_output,
  285. const std::vector<std::string> &async_file_pool,
  286. std::vector<std::shared_ptr<TensorData>> *result_list, bool *no_mem_to_read = nullptr);
  287. void ReadDumpedTensorSync(const std::string &prefix_dump_file_name, const std::string &specific_dump_dir,
  288. const std::string &backend_name, size_t slot, unsigned int device_id,
  289. unsigned int iteration, unsigned int root_graph_id, const bool &is_output,
  290. std::vector<std::shared_ptr<TensorData>> *result_list, bool *no_mem_to_read);
  291. void ReadDumpedTensorAsync(const std::string &specific_dump_dir, const std::string &prefix_dump_to_check,
  292. const std::string &slot_string_to_check, const std::string &backend_name, size_t slot,
  293. unsigned int device_id, unsigned int iteration, unsigned int root_graph_id,
  294. const bool &is_output, const std::vector<std::string> &async_file_pool,
  295. std::vector<std::shared_ptr<TensorData>> *result_list, bool *no_mem_to_read);
  296. std::vector<std::shared_ptr<TensorData>> ReadNeededDumpedTensors(unsigned int iteration,
  297. std::vector<std::string> *async_file_pool);
  298. void *GetPrevTensor(const std::shared_ptr<TensorData> &tensor, bool previous_iter_tensor_needed,
  299. uint32_t *prev_num_elements);
  300. void ReadTensorFromNpy(const std::string &tensor_name, const std::string &file_name, std::string *tensor_type,
  301. std::size_t *size, std::vector<int64_t> *shape, std::vector<char> **data_buffer,
  302. bool *no_mem_to_read);
  303. void ConvertToHostFormat(const std::map<std::string, std::vector<std::string>> &dir_to_files_map,
  304. std::vector<std::string> *result_list);
  305. void ConvertReadTensors(std::vector<std::string> backend_name, std::vector<size_t> slot,
  306. std::vector<unsigned int> device_id, std::vector<unsigned int> iteration,
  307. std::vector<unsigned int> root_graph_id, std::vector<std::string> *result_list);
  308. void ConvertWatchPointNodes(const std::vector<std::tuple<std::string, std::string>> &proto_dump,
  309. const std::string &specific_dump_dir, std::vector<std::string> *result_list);
  310. void GetTensorDataInfoAsync(const std::vector<std::tuple<std::string, std::string>> &proto_dump,
  311. const std::string &specific_dump_dir, uint32_t iteration, uint32_t device_id,
  312. uint32_t root_graph_id, const std::vector<std::string> &async_file_pool,
  313. std::vector<std::shared_ptr<TensorData>> *tensor_list);
  314. std::string GetStrippedFilename(const std::string &file_name);
  315. std::string IterationString(unsigned int iteration);
  316. #endif
  317. void ReadNodesTensors(const std::vector<std::string> &name, std::vector<std::string> *ret_name,
  318. std::vector<char *> *data_ptr, std::vector<ssize_t> *data_size,
  319. std::vector<unsigned int> *dtype, std::vector<std::vector<int64_t>> *const shape);
  320. void SearchNodesTensors(const std::vector<std::string> &name,
  321. std::vector<std::tuple<std::string, std::shared_ptr<TensorData>>> *result_list);
  322. #ifdef ONLINE_DBG_MODE
  323. bool IsWatchPoint(const std::string &kernel_name, const CNodePtr &kernel = nullptr) const;
  324. bool IsWatchPointNodeInput(const std::string &w_name, const CNodePtr &kernel) const;
  325. #endif
  326. void EmptyTensor();
  327. std::vector<std::shared_ptr<TensorData>> GetTensor() const;
  328. void AddAnalyzedTensorToCache(const bool recheck, const unsigned int id, const std::string &tensor_name);
  329. uint32_t GetTensorLoaderIterNum() const;
  330. void SetTensorLoaderIterNum(uint32_t iter_num);
  331. void EmptyCurrentTensor();
  332. #ifdef ONLINE_DBG_MODE
  333. bool DumpTensorToFile(const std::string &tensor_name, bool trans_flag, const std::string &filepath,
  334. const std::string &host_fmt, const std::vector<int64_t> &host_shape, TypeId host_type,
  335. TypeId device_type, const std::string &addr_format, size_t slot) const;
  336. #endif
  337. bool LoadNewTensor(const std::shared_ptr<TensorData> &tensor, bool keep_prev);
  338. std::unordered_map<unsigned int, watchpoint_t> GetWatchpointTable();
  339. void ResetLoadedTensors();
  340. #ifdef ONLINE_DBG_MODE
  341. std::vector<std::shared_ptr<TensorData>> GetNodeTensor(const CNodePtr &kernel);
  342. #endif
  343. // Find if any operation overflow happened on a particular node name
  344. bool CheckOpOverflow(std::string node_name_to_find, unsigned int device_id = 0, unsigned int root_graph_id = 0,
  345. unsigned int iteration = 0);
  346. bool GetAttrsFromAsyncFilename(const std::string &file_name, std::string *node_name, uint64_t *task_id,
  347. uint64_t *stream_id);
  348. std::string RealPath(const std::string &input_path);
  349. uint64_t BytestoUInt64(const std::vector<char> &buffer);
  350. bool TensorExistsInCurrent(const std::string &tensor_name);
  351. void MoveTensorCurrentToPrev(const std::string &tensor_name);
  352. void AppendToCacheEvictQueue(const std::string &tensor_name);
  353. void SetNetName(std::string net_name);
  354. std::string GetNetName();
  355. void SetDumpDir(std::string dump_dir);
  356. std::string GetDumpDir();
  357. void SetSyncMode(bool is_sync_mode);
  358. bool GetSyncMode();
  359. void SetMemLimit(uint64_t max_mem_size);
  360. private:
  361. std::mutex lock_;
  362. std::mutex wp_lock_;
  363. std::mutex overflow_wp_lock_;
  364. // to keep track of watchpoints that have been checked already for a tensor in current step
  365. std::unordered_map<std::string, std::set<int32_t>> wp_id_cache_;
  366. std::unordered_map<unsigned int, watchpoint_t> watchpoint_table_;
  367. // key is the iteration path, value is vector of op_names which have overflowed
  368. std::unordered_map<std::string, std::vector<std::string>> overflow_ops_;
  369. std::string net_name_;
  370. std::string dump_dir_;
  371. bool is_sync_mode_;
  372. std::shared_ptr<TensorLoader> tensor_loader_;
  373. };
  374. #ifdef ONLINE_DBG_MODE
  375. } // namespace mindspore
  376. #endif
  377. #endif // MINDSPORE_CCSRC_DEBUG_DEBUG_SERVICES_H_