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 8.4 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. #include <math.h>
  19. #include <vector>
  20. #include <string>
  21. #include <memory>
  22. #include <tuple>
  23. #include <unordered_map>
  24. #include <set>
  25. #include <mutex>
  26. #include <map>
  27. #include <limits>
  28. #include "debug/tensor_load.h"
  29. #include "debug/tensor_data.h"
  30. #include "ir/dtype.h"
  31. namespace mindspore {
  32. class DebugServices {
  33. public:
  34. DebugServices();
  35. DebugServices(const DebugServices &other);
  36. DebugServices &operator=(const DebugServices &other);
  37. ~DebugServices();
  38. enum CONDITION_TYPE {
  39. HAS_NAN,
  40. HAS_INF,
  41. IS_OVERFLOW,
  42. MAX_GT,
  43. MAX_LT,
  44. MIN_GT,
  45. MIN_LT,
  46. MAX_MIN_GT,
  47. MAX_MIN_LT,
  48. MEAN_GT,
  49. MEAN_LT,
  50. SD_GT,
  51. SD_LT,
  52. GENERAL_OVERFLOW,
  53. INIT,
  54. TOO_LARGE,
  55. TOO_SMALL,
  56. ALL_ZERO,
  57. CHANGE_TOO_LARGE,
  58. CHANGE_TOO_SMALL,
  59. NOT_CHANGED,
  60. RANGE
  61. };
  62. typedef struct condition {
  63. CONDITION_TYPE type;
  64. float parameter = 0;
  65. } condition_t;
  66. typedef struct parameter {
  67. std::string name;
  68. bool disabled;
  69. double_t value;
  70. bool hit;
  71. double_t actual_value;
  72. void Evaluate(double_t actualValue, std::string inequality_type) {
  73. if (std::isnan(actualValue)) return;
  74. actual_value = actualValue;
  75. // if cannot extract inequality type from watchpoint
  76. // try extract from parameter name
  77. if (inequality_type.empty()) {
  78. auto pos = name.find_last_of('_');
  79. if (pos != std::string::npos) {
  80. inequality_type = name.substr(pos + 1);
  81. }
  82. }
  83. std::map<std::string, bool> condition_check{{"gt", actual_value > value},
  84. {"lt", actual_value < value},
  85. {"ge", actual_value >= value},
  86. {"le", actual_value <= value}};
  87. hit = condition_check[inequality_type];
  88. }
  89. } parameter_t;
  90. typedef struct watchpoint {
  91. unsigned int id;
  92. condition_t condition;
  93. std::vector<std::tuple<std::string, bool>> check_node_list;
  94. std::vector<parameter_t> parameter_list;
  95. size_t location = 0;
  96. std::string FindQualifiedTensorName(const std::string &tensor_name) {
  97. std::string node_name = tensor_name.substr(0, tensor_name.find_first_of(':'));
  98. for (auto check_node : check_node_list) {
  99. std::string w_name = std::get<0>(check_node);
  100. bool w_type = std::get<1>(check_node);
  101. auto found = w_name.find_last_of('/');
  102. if (found != std::string::npos && w_name.substr(found + 1) == tensor_name) return w_name;
  103. if ((w_type && (tensor_name.find(w_name) == location || w_name == "*")) || (!w_type && node_name == w_name)) {
  104. return w_name;
  105. }
  106. }
  107. return {};
  108. }
  109. bool is_gt_wp() {
  110. return condition.type == MAX_GT || condition.type == MIN_GT || condition.type == MEAN_GT ||
  111. condition.type == SD_GT || condition.type == MAX_MIN_GT;
  112. }
  113. bool is_lt_wp() {
  114. return condition.type == MAX_LT || condition.type == MIN_LT || condition.type == MEAN_LT ||
  115. condition.type == SD_LT || condition.type == MAX_MIN_LT;
  116. }
  117. bool min_max_enabled() {
  118. return condition.type == MAX_LT || condition.type == MAX_GT || condition.type == MIN_LT ||
  119. condition.type == MIN_GT || condition.type == MAX_MIN_LT || condition.type == MAX_MIN_GT ||
  120. (condition.type == INIT && (!parameter_list[1].disabled || !parameter_list[2].disabled)) ||
  121. (condition.type == TOO_LARGE && (!parameter_list[1].disabled || !parameter_list[2].disabled)) ||
  122. (condition.type == TOO_SMALL && (!parameter_list[1].disabled || !parameter_list[2].disabled));
  123. }
  124. // inf or nan related condition set
  125. bool inf_nan_enabled() {
  126. return condition.type == HAS_INF || condition.type == HAS_NAN || condition.type == GENERAL_OVERFLOW;
  127. }
  128. // mean or sd related condition set
  129. bool mean_sd_enabled() const {
  130. return condition.type == MEAN_LT || condition.type == MEAN_GT || condition.type == SD_LT ||
  131. condition.type == SD_GT || (condition.type == TOO_LARGE && !parameter_list[3].disabled) ||
  132. (condition.type == TOO_SMALL && !parameter_list[3].disabled);
  133. }
  134. bool abs_mean_enabled() const {
  135. return (condition.type == TOO_LARGE && !parameter_list[0].disabled) ||
  136. (condition.type == TOO_SMALL && !parameter_list[0].disabled);
  137. }
  138. bool zero_percentage_enabled() { return condition.type == ALL_ZERO || condition.type == INIT; }
  139. bool tensor_update_ratio_mean_enabled() const {
  140. return condition.type == CHANGE_TOO_LARGE || condition.type == CHANGE_TOO_SMALL;
  141. }
  142. bool allclose_enabled() const { return condition.type == NOT_CHANGED; }
  143. bool range_enabled() const {
  144. return condition.type == RANGE && (!parameter_list[0].disabled || !parameter_list[1].disabled);
  145. }
  146. bool change_condition() const {
  147. return condition.type == CHANGE_TOO_LARGE || condition.type == CHANGE_TOO_SMALL || condition.type == NOT_CHANGED;
  148. }
  149. } watchpoint_t;
  150. void AddWatchpoint(unsigned int id, unsigned int watch_condition, float parameter,
  151. const std::vector<std::tuple<std::string, bool>> &check_node_list,
  152. const std::vector<parameter_t> &parameter_list);
  153. void RemoveWatchpoint(unsigned int id);
  154. void CheckWatchpoints(std::vector<std::string> *name, std::vector<std::string> *slot, std::vector<int> *condition,
  155. std::vector<unsigned int> *watchpoint_id, std::vector<std::vector<parameter_t>> *parameters,
  156. std::vector<int32_t> *error_code, const std::vector<std::string> &op_overflows,
  157. const std::vector<std::shared_ptr<TensorData>> &tensor_list, bool init_dbg_suspend,
  158. const bool step_end, const bool recheck);
  159. void ReadNodesTensors(std::vector<std::string> name, std::vector<std::string> *ret_name,
  160. std::vector<char *> *data_ptr, std::vector<ssize_t> *data_size, std::vector<TypePtr> *dtype,
  161. std::vector<std::vector<int64_t>> *shape);
  162. bool IsWatchPoint(const std::string &kernel_name, const CNodePtr &kernel = nullptr) const;
  163. bool IsWatchPointNodeInput(const std::string &w_name, const CNodePtr &kernel) const;
  164. void EmptyTensor();
  165. std::vector<std::shared_ptr<TensorData>> GetTensor() const;
  166. std::vector<std::shared_ptr<TensorData>> GetNodeTensorMap(const std::string &node_name) const;
  167. uint32_t GetTensorLoaderIterNum() const;
  168. void SetTensorLoaderIterNum(uint32_t iter_num);
  169. void EmptyPrevTensor();
  170. void EmptyCurrentTensor();
  171. bool DumpTensorToFile(const std::string &tensor_name, bool trans_flag, const std::string &filepath,
  172. const std::string &host_fmt, const std::vector<int64_t> &host_shape, TypeId host_type,
  173. TypeId addr_type_id, const std::string &addr_format, size_t slot) const;
  174. bool LoadNewTensor(const std::shared_ptr<TensorData> &tensor, bool keep_prev);
  175. std::unordered_map<unsigned int, watchpoint_t> GetWatchpointTable();
  176. void ResetLoadedTensors();
  177. std::vector<std::shared_ptr<TensorData>> GetNodeTensor(const CNodePtr &kernel);
  178. bool TensorExistsInCurrent(std::string tensor_name);
  179. void MoveTensorCurrentToPrev(std::string tensor_name);
  180. private:
  181. std::mutex lock_;
  182. // to keep track of watchpoints that have been checked already for a tensor in current step
  183. std::unordered_map<std::string, std::set<int32_t>> wp_id_cache;
  184. std::unordered_map<unsigned int, watchpoint_t> watchpoint_table;
  185. TensorLoader *tensor_loader_;
  186. };
  187. } // namespace mindspore
  188. #endif // MINDSPORE_CCSRC_DEBUG_DEBUG_SERVICES_H_