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 11 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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();
  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. typedef struct condition {
  74. CONDITION_TYPE type;
  75. float parameter = 0;
  76. } condition_t;
  77. typedef struct parameter {
  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. } parameter_t;
  101. typedef struct watchpoint {
  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) {
  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() {
  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() {
  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() {
  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() {
  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() { 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. } watchpoint_t;
  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> *watchpoint_id, std::vector<std::vector<parameter_t>> *parameters,
  171. std::vector<int32_t> *error_code, const std::vector<std::string> &op_overflows,
  172. std::vector<std::shared_ptr<TensorData>> *tensor_list, bool init_dbg_suspend,
  173. const bool step_end, const bool recheck, std::vector<unsigned int> *device_id = nullptr,
  174. std::vector<unsigned int> *root_graph_id = nullptr);
  175. void AddWatchPointsToCheck(bool init_dbg_suspend, bool step_end, bool recheck, const std::string &tensor_name,
  176. const std::string &tensor_name_no_slot, bool *previous_iter_tensor_needed,
  177. std::string *qualified_tensor_name, std::vector<watchpoint_t> *watchpoints_to_check);
  178. #ifdef OFFLINE_DBG_MODE
  179. void GetSlotInfo(const std::string &file_name, const std::string &dump_name, const std::string &specific_dump_dir,
  180. std::vector<size_t> *slot_list);
  181. std::size_t GetShapeTypeInfo(const std::string &specific_dump_dir, std::size_t slot,
  182. const std::string &prefix_dump_file_name, std::string *file_name, std::string *type_name,
  183. std::string *out_dir, std::vector<int64_t> *shape);
  184. void ReadDumpedTensor(std::vector<std::string> backend_name, std::vector<size_t> slot,
  185. std::vector<unsigned int> device_id, std::vector<unsigned int> iteration,
  186. std::vector<unsigned int> root_graph_id, std::vector<std::shared_ptr<TensorData>> *result_list);
  187. std::vector<std::shared_ptr<TensorData>> ReadNeededDumpedTensors(unsigned int iteration);
  188. void *GetPrevTensor(const std::shared_ptr<TensorData> &tensor, bool previous_iter_tensor_needed);
  189. #endif
  190. void ReadNodesTensors(std::vector<std::string> name, std::vector<std::string> *ret_name,
  191. std::vector<char *> *data_ptr, std::vector<ssize_t> *data_size,
  192. std::vector<unsigned int> *dtype, std::vector<std::vector<int64_t>> *shape);
  193. #ifdef ONLINE_DBG_MODE
  194. bool IsWatchPoint(const std::string &kernel_name, const CNodePtr &kernel = nullptr) const;
  195. bool IsWatchPointNodeInput(const std::string &w_name, const CNodePtr &kernel) const;
  196. #endif
  197. void EmptyTensor();
  198. std::vector<std::shared_ptr<TensorData>> GetTensor() const;
  199. void AddAnalyzedTensorToCache(const bool recheck, const unsigned int id, const std::string &tensor_name);
  200. std::vector<std::shared_ptr<TensorData>> GetNodeTensorMap(const std::string &node_name) const;
  201. uint32_t GetTensorLoaderIterNum() const;
  202. void SetTensorLoaderIterNum(uint32_t iter_num);
  203. void EmptyPrevTensor();
  204. void EmptyCurrentTensor();
  205. #ifdef ONLINE_DBG_MODE
  206. bool DumpTensorToFile(const std::string &tensor_name, bool trans_flag, const std::string &filepath,
  207. const std::string &host_fmt, const std::vector<int64_t> &host_shape, TypeId host_type,
  208. TypeId addr_type_id, const std::string &addr_format, size_t slot) const;
  209. #endif
  210. bool LoadNewTensor(const std::shared_ptr<TensorData> &tensor, bool keep_prev);
  211. std::unordered_map<unsigned int, watchpoint_t> GetWatchpointTable();
  212. void ResetLoadedTensors();
  213. #ifdef ONLINE_DBG_MODE
  214. std::vector<std::shared_ptr<TensorData>> GetNodeTensor(const CNodePtr &kernel);
  215. #endif
  216. bool TensorExistsInCurrent(std::string tensor_name);
  217. void MoveTensorCurrentToPrev(std::string tensor_name);
  218. void SetNetName(std::string net_name);
  219. std::string GetNetName();
  220. void SetDumpDir(std::string dump_dir);
  221. std::string GetDumpDir();
  222. void SetSyncMode(bool is_sync_mode);
  223. bool GetSyncMode();
  224. private:
  225. std::mutex lock_;
  226. // to keep track of watchpoints that have been checked already for a tensor in current step
  227. std::unordered_map<std::string, std::set<int32_t>> wp_id_cache;
  228. std::unordered_map<unsigned int, watchpoint_t> watchpoint_table;
  229. std::string net_name;
  230. std::string dump_dir;
  231. bool is_sync_mode;
  232. TensorLoader *tensor_loader_;
  233. };
  234. #ifdef ONLINE_DBG_MODE
  235. } // namespace mindspore
  236. #endif
  237. #endif // MINDSPORE_CCSRC_DEBUG_DEBUG_SERVICES_H_