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 13 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /**
  2. * Copyright 2020 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 "Eigen/Core"
  23. #include "Eigen/src/Core/arch/CUDA/Half.h"
  24. using float16 = Eigen::half;
  25. #include "debugger/offline_debug/offline_logger.h"
  26. #endif
  27. #include <math.h>
  28. #include <vector>
  29. #include <string>
  30. #include <memory>
  31. #include <tuple>
  32. #include <unordered_map>
  33. #include <set>
  34. #include <mutex>
  35. #include <map>
  36. #include <limits>
  37. #include <sstream>
  38. #include "debug/tensor_load.h"
  39. #include "debug/tensor_data.h"
  40. #ifdef ONLINE_DBG_MODE
  41. namespace mindspore {
  42. #endif
  43. class DebugServices {
  44. public:
  45. DebugServices();
  46. DebugServices(const DebugServices &other);
  47. DebugServices &operator=(const DebugServices &other);
  48. ~DebugServices() = default;
  49. enum CONDITION_TYPE {
  50. HAS_NAN,
  51. HAS_INF,
  52. IS_OVERFLOW,
  53. MAX_GT,
  54. MAX_LT,
  55. MIN_GT,
  56. MIN_LT,
  57. MAX_MIN_GT,
  58. MAX_MIN_LT,
  59. MEAN_GT,
  60. MEAN_LT,
  61. SD_GT,
  62. SD_LT,
  63. GENERAL_OVERFLOW,
  64. INIT,
  65. TOO_LARGE,
  66. TOO_SMALL,
  67. ALL_ZERO,
  68. CHANGE_TOO_LARGE,
  69. CHANGE_TOO_SMALL,
  70. NOT_CHANGED,
  71. RANGE
  72. };
  73. struct condition_t {
  74. CONDITION_TYPE type;
  75. float parameter = 0;
  76. };
  77. struct parameter_t {
  78. std::string name;
  79. bool disabled;
  80. double_t value;
  81. bool hit;
  82. double_t actual_value;
  83. void Evaluate(double_t actualValue, std::string inequality_type) {
  84. if (std::isnan(actualValue)) return;
  85. actual_value = actualValue;
  86. // if cannot extract inequality type from watchpoint
  87. // try extract from parameter name
  88. if (inequality_type.empty()) {
  89. auto pos = name.find_last_of('_');
  90. if (pos != std::string::npos) {
  91. inequality_type = name.substr(pos + 1);
  92. }
  93. }
  94. std::map<std::string, bool> condition_check{{"gt", actual_value > value},
  95. {"lt", actual_value < value},
  96. {"ge", actual_value >= value},
  97. {"le", actual_value <= value}};
  98. hit = condition_check[inequality_type];
  99. }
  100. };
  101. struct watchpoint_t {
  102. unsigned int id;
  103. condition_t condition;
  104. std::vector<std::tuple<std::string, bool>> check_node_list;
  105. std::vector<std::tuple<std::string, std::vector<uint32_t>>> check_node_device_list;
  106. std::vector<std::tuple<std::string, std::vector<uint32_t>>> check_node_graph_list;
  107. std::vector<parameter_t> parameter_list;
  108. size_t location = 0;
  109. std::string FindQualifiedTensorName(const std::string &tensor_name) const {
  110. std::string node_name = tensor_name.substr(0, tensor_name.find_first_of(':'));
  111. for (auto check_node : check_node_list) {
  112. std::string w_name = std::get<0>(check_node);
  113. bool w_type = std::get<1>(check_node);
  114. auto found = w_name.find_last_of('/');
  115. if (found != std::string::npos && w_name.substr(found + 1) == tensor_name) return w_name;
  116. if ((w_type && (tensor_name.find(w_name) == location || w_name == "*")) || (!w_type && node_name == w_name)) {
  117. return w_name;
  118. }
  119. }
  120. return {};
  121. }
  122. bool is_gt_wp() const {
  123. return condition.type == MAX_GT || condition.type == MIN_GT || condition.type == MEAN_GT ||
  124. condition.type == SD_GT || condition.type == MAX_MIN_GT;
  125. }
  126. bool is_lt_wp() const {
  127. return condition.type == MAX_LT || condition.type == MIN_LT || condition.type == MEAN_LT ||
  128. condition.type == SD_LT || condition.type == MAX_MIN_LT;
  129. }
  130. bool min_max_enabled() const {
  131. return condition.type == MAX_LT || condition.type == MAX_GT || condition.type == MIN_LT ||
  132. condition.type == MIN_GT || condition.type == MAX_MIN_LT || condition.type == MAX_MIN_GT ||
  133. (condition.type == INIT && (!parameter_list[1].disabled || !parameter_list[2].disabled)) ||
  134. (condition.type == TOO_LARGE && (!parameter_list[1].disabled || !parameter_list[2].disabled)) ||
  135. (condition.type == TOO_SMALL && (!parameter_list[1].disabled || !parameter_list[2].disabled));
  136. }
  137. // inf or nan related condition set
  138. bool inf_nan_enabled() const {
  139. return condition.type == HAS_INF || condition.type == HAS_NAN || condition.type == GENERAL_OVERFLOW;
  140. }
  141. // mean or sd related condition set
  142. bool mean_sd_enabled() const {
  143. return condition.type == MEAN_LT || condition.type == MEAN_GT || condition.type == SD_LT ||
  144. condition.type == SD_GT || (condition.type == TOO_LARGE && !parameter_list[3].disabled) ||
  145. (condition.type == TOO_SMALL && !parameter_list[3].disabled);
  146. }
  147. bool abs_mean_enabled() const {
  148. return (condition.type == TOO_LARGE && !parameter_list[0].disabled) ||
  149. (condition.type == TOO_SMALL && !parameter_list[0].disabled);
  150. }
  151. bool zero_percentage_enabled() const { return condition.type == ALL_ZERO || condition.type == INIT; }
  152. bool tensor_update_ratio_mean_enabled() const {
  153. return condition.type == CHANGE_TOO_LARGE || condition.type == CHANGE_TOO_SMALL;
  154. }
  155. bool allclose_enabled() const { return condition.type == NOT_CHANGED; }
  156. bool range_enabled() const {
  157. return condition.type == RANGE && (!parameter_list[0].disabled || !parameter_list[1].disabled);
  158. }
  159. bool change_condition() const {
  160. return condition.type == CHANGE_TOO_LARGE || condition.type == CHANGE_TOO_SMALL || condition.type == NOT_CHANGED;
  161. }
  162. };
  163. void AddWatchpoint(
  164. unsigned int id, unsigned int watch_condition, float parameter,
  165. const std::vector<std::tuple<std::string, bool>> &check_node_list, const std::vector<parameter_t> &parameter_list,
  166. const std::vector<std::tuple<std::string, std::vector<uint32_t>>> *check_node_device_list = nullptr,
  167. const std::vector<std::tuple<std::string, std::vector<uint32_t>>> *check_node_graph_list = nullptr);
  168. void RemoveWatchpoint(unsigned int id);
  169. void CheckWatchpoints(std::vector<std::string> *name, std::vector<std::string> *slot, std::vector<int> *condition,
  170. std::vector<unsigned int> *const watchpoint_id,
  171. std::vector<std::vector<parameter_t>> *parameters, std::vector<int32_t> *error_code,
  172. const std::vector<std::string> &op_overflows, const std::vector<std::string> &async_file_pool,
  173. std::vector<std::shared_ptr<TensorData>> *tensor_list, bool init_dbg_suspend,
  174. const bool step_end, const bool recheck, std::vector<unsigned int> *device_id = nullptr,
  175. std::vector<unsigned int> *root_graph_id = nullptr);
  176. void AddWatchPointsToCheck(bool init_dbg_suspend, bool step_end, bool recheck, const std::string &tensor_name,
  177. const std::string &tensor_name_no_slot, bool *previous_iter_tensor_needed,
  178. std::string *qualified_tensor_name, std::vector<watchpoint_t> *watchpoints_to_check);
  179. #ifdef OFFLINE_DBG_MODE
  180. void GetSlotInfo(const std::string &file_name, const std::string &dump_name, const std::string &specific_dump_dir,
  181. std::vector<size_t> *slot_list);
  182. std::size_t GetShapeTypeInfo(const std::string &specific_dump_dir, std::size_t slot,
  183. const std::string &prefix_dump_file_name, std::string *file_name, std::string *type_name,
  184. std::string *out_dir, std::vector<int64_t> *shape);
  185. void AddToTensorData(const std::string &backend_name, const std::size_t slot, const unsigned int iteration,
  186. const unsigned int device_id, const unsigned int root_graph_id, const std::size_t data_size,
  187. const std::string &type_name, const std::vector<int64_t> &shape, std::vector<char> *buffer,
  188. std::vector<std::shared_ptr<TensorData>> *result_list);
  189. void ReadDumpedTensor(std::vector<std::string> backend_name, std::vector<size_t> slot,
  190. std::vector<unsigned int> device_id, std::vector<unsigned int> iteration,
  191. std::vector<unsigned int> root_graph_id, const std::vector<std::string> &async_file_pool,
  192. std::vector<std::shared_ptr<TensorData>> *result_list);
  193. std::vector<std::shared_ptr<TensorData>> ReadNeededDumpedTensors(unsigned int iteration,
  194. std::vector<std::string> *async_file_pool);
  195. void *GetPrevTensor(const std::shared_ptr<TensorData> &tensor, bool previous_iter_tensor_needed);
  196. void ReadTensorFromNpy(const std::string &file_name, std::string *tensor_type, std::size_t *size,
  197. std::vector<int64_t> *shape, std::vector<char> **data_buffer);
  198. void ConvertToHostFormat(const std::map<std::string, std::vector<std::string>> &dir_to_files_map,
  199. std::vector<std::string> *result_list);
  200. void ConvertReadTensors(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, std::vector<std::string> *result_list);
  203. void ConvertWatchPointNodes(const std::vector<std::tuple<std::string, std::string>> &proto_dump,
  204. const std::string &specific_dump_dir, std::vector<std::string> *result_list);
  205. void GetTensorDataInfoAsync(const std::vector<std::tuple<std::string, std::string>> &proto_dump, uint32_t iteration,
  206. uint32_t device_id, uint32_t root_graph_id,
  207. const std::vector<std::string> &async_file_pool,
  208. std::vector<std::shared_ptr<TensorData>> *tensor_list);
  209. #endif
  210. void ReadNodesTensors(const std::vector<std::string> &name, std::vector<std::string> *ret_name,
  211. std::vector<char *> *data_ptr, std::vector<ssize_t> *data_size,
  212. std::vector<unsigned int> *dtype, std::vector<std::vector<int64_t>> *const shape);
  213. #ifdef ONLINE_DBG_MODE
  214. bool IsWatchPoint(const std::string &kernel_name, const CNodePtr &kernel = nullptr) const;
  215. bool IsWatchPointNodeInput(const std::string &w_name, const CNodePtr &kernel) const;
  216. #endif
  217. void EmptyTensor();
  218. std::vector<std::shared_ptr<TensorData>> GetTensor() const;
  219. void AddAnalyzedTensorToCache(const bool recheck, const unsigned int id, const std::string &tensor_name);
  220. std::vector<std::shared_ptr<TensorData>> GetNodeTensorMap(const std::string &node_name) const;
  221. uint32_t GetTensorLoaderIterNum() const;
  222. void SetTensorLoaderIterNum(uint32_t iter_num);
  223. void EmptyPrevTensor();
  224. void EmptyCurrentTensor();
  225. #ifdef ONLINE_DBG_MODE
  226. bool DumpTensorToFile(const std::string &tensor_name, bool trans_flag, const std::string &filepath,
  227. const std::string &host_fmt, const std::vector<int64_t> &host_shape, TypeId host_type,
  228. TypeId device_type, const std::string &addr_format, size_t slot) const;
  229. #endif
  230. bool LoadNewTensor(const std::shared_ptr<TensorData> &tensor, bool keep_prev);
  231. std::unordered_map<unsigned int, watchpoint_t> GetWatchpointTable();
  232. void ResetLoadedTensors();
  233. #ifdef ONLINE_DBG_MODE
  234. std::vector<std::shared_ptr<TensorData>> GetNodeTensor(const CNodePtr &kernel);
  235. #endif
  236. bool TensorExistsInCurrent(const std::string &tensor_name);
  237. void MoveTensorCurrentToPrev(const std::string &tensor_name);
  238. void SetNetName(std::string net_name);
  239. std::string GetNetName();
  240. void SetDumpDir(std::string dump_dir);
  241. std::string GetDumpDir();
  242. void SetSyncMode(bool is_sync_mode);
  243. bool GetSyncMode();
  244. private:
  245. std::mutex lock_;
  246. // to keep track of watchpoints that have been checked already for a tensor in current step
  247. std::unordered_map<std::string, std::set<int32_t>> wp_id_cache;
  248. std::unordered_map<unsigned int, watchpoint_t> watchpoint_table;
  249. std::string net_name;
  250. std::string dump_dir;
  251. bool is_sync_mode;
  252. std::shared_ptr<TensorLoader> tensor_loader_;
  253. };
  254. #ifdef ONLINE_DBG_MODE
  255. } // namespace mindspore
  256. #endif
  257. #endif // MINDSPORE_CCSRC_DEBUG_DEBUG_SERVICES_H_