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 7.1 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 <mutex>
  25. #include <map>
  26. #include <limits>
  27. #include "debug/tensor_load.h"
  28. #include "debug/tensor_data.h"
  29. #include "ir/dtype.h"
  30. namespace mindspore {
  31. class DebugServices {
  32. public:
  33. DebugServices();
  34. DebugServices(const DebugServices &other);
  35. DebugServices &operator=(const DebugServices &other);
  36. ~DebugServices();
  37. enum CONDITION_TYPE {
  38. HAS_NAN,
  39. HAS_INF,
  40. IS_OVERFLOW,
  41. MAX_GT,
  42. MAX_LT,
  43. MIN_GT,
  44. MIN_LT,
  45. MAX_MIN_GT,
  46. MAX_MIN_LT,
  47. MEAN_GT,
  48. MEAN_LT,
  49. SD_GT,
  50. SD_LT,
  51. GENERAL_OVERFLOW,
  52. INIT,
  53. TOO_LARGE,
  54. TOO_SMALL,
  55. ALL_ZERO,
  56. CHANGE_TOO_LARGE,
  57. CHANGE_TOO_SMALL,
  58. NOT_CHANGED,
  59. RANGE
  60. };
  61. typedef struct condition {
  62. CONDITION_TYPE type;
  63. float parameter = 0;
  64. } condition_t;
  65. typedef struct parameter {
  66. std::string name;
  67. bool disabled;
  68. double_t value;
  69. bool hit;
  70. double_t actual_value;
  71. void Evaluate(double_t actualValue, std::string inequality_type) {
  72. if (std::isnan(actualValue)) return;
  73. actual_value = actualValue;
  74. if (inequality_type.empty()) {
  75. auto pos = name.find_last_of('_');
  76. if (pos != std::string::npos) {
  77. inequality_type = name.substr(pos + 1);
  78. }
  79. }
  80. std::map<std::string, bool> condition_check{{"gt", actual_value > value},
  81. {"lt", actual_value < value},
  82. {"ge", actual_value >= value},
  83. {"le", actual_value <= value}};
  84. hit = condition_check[inequality_type];
  85. }
  86. } parameter_t;
  87. typedef struct watchpoint {
  88. unsigned int id;
  89. condition_t condition;
  90. std::vector<std::tuple<std::string, bool>> check_node_list;
  91. std::vector<parameter_t> parameter_list;
  92. size_t location = 0;
  93. std::string FindQualifiedTensorName(const std::string &tensor_name) {
  94. std::string node_name = tensor_name.substr(0, tensor_name.find_first_of(':'));
  95. for (auto check_node : check_node_list) {
  96. std::string w_name = std::get<0>(check_node);
  97. bool w_type = std::get<1>(check_node);
  98. auto found = w_name.find_last_of('/');
  99. if (found != std::string::npos && w_name.substr(found + 1) == tensor_name) return w_name;
  100. if ((w_type && (tensor_name.find(w_name) == location || w_name == "*")) || (!w_type && node_name == w_name)) {
  101. return w_name;
  102. }
  103. }
  104. return {};
  105. }
  106. bool is_gt_wp() {
  107. return condition.type == MAX_GT || condition.type == MIN_GT || condition.type == MEAN_GT ||
  108. condition.type == SD_GT || condition.type == MAX_MIN_GT;
  109. }
  110. bool is_lt_wp() {
  111. return condition.type == MAX_LT || condition.type == MIN_LT || condition.type == MEAN_LT ||
  112. condition.type == SD_LT || condition.type == MAX_MIN_LT;
  113. }
  114. bool min_max_enabled() {
  115. return condition.type == MAX_LT || condition.type == MAX_GT || condition.type == MIN_LT ||
  116. condition.type == MIN_GT || condition.type == MAX_MIN_LT || condition.type == MAX_MIN_GT ||
  117. (condition.type == INIT && (!parameter_list[1].disabled || !parameter_list[2].disabled)) ||
  118. (condition.type == TOO_LARGE && (!parameter_list[1].disabled || !parameter_list[2].disabled)) ||
  119. (condition.type == TOO_SMALL && (!parameter_list[1].disabled || !parameter_list[2].disabled));
  120. }
  121. // inf or nan related condition set
  122. bool inf_nan_enabled() {
  123. return condition.type == HAS_INF || condition.type == HAS_NAN || condition.type == GENERAL_OVERFLOW;
  124. }
  125. // mean or sd related condition set
  126. bool mean_sd_enabled() const {
  127. return condition.type == MEAN_LT || condition.type == MEAN_GT || condition.type == SD_LT ||
  128. condition.type == SD_GT || (condition.type == TOO_LARGE && !parameter_list[3].disabled) ||
  129. (condition.type == TOO_SMALL && !parameter_list[3].disabled);
  130. }
  131. bool abs_mean_enabled() const {
  132. return (condition.type == TOO_LARGE && !parameter_list[0].disabled) ||
  133. (condition.type == TOO_SMALL && !parameter_list[0].disabled);
  134. }
  135. bool zero_percentage_enabled() { return condition.type == ALL_ZERO || condition.type == INIT; }
  136. bool tensor_update_ratio_mean_enabled() const {
  137. return condition.type == CHANGE_TOO_LARGE || condition.type == CHANGE_TOO_SMALL;
  138. }
  139. bool allclose_enabled() const { return condition.type == NOT_CHANGED; }
  140. bool range_enabled() const {
  141. return condition.type == RANGE && (!parameter_list[0].disabled || !parameter_list[1].disabled);
  142. }
  143. } watchpoint_t;
  144. void AddWatchpoint(unsigned int id, unsigned int watch_condition, float parameter,
  145. const std::vector<std::tuple<std::string, bool>> &check_node_list,
  146. const std::vector<parameter_t> &parameter_list);
  147. void RemoveWatchpoint(unsigned int id);
  148. void CheckWatchpoints(std::vector<std::string> *name, std::vector<std::string> *slot, std::vector<int> *condition,
  149. std::vector<unsigned int> *watchpoint_id, std::vector<std::vector<parameter_t>> *parameters,
  150. std::vector<int32_t> *error_code, const std::vector<std::string> &op_overflows,
  151. const std::vector<std::shared_ptr<TensorData>> &tensor_list, bool init_dbg_suspend);
  152. void ReadNodesTensors(std::vector<std::string> name, std::vector<std::string> *ret_name,
  153. std::vector<char *> *data_ptr, std::vector<unsigned int> *data_size,
  154. std::vector<TypePtr> *dtype, std::vector<std::vector<int64_t>> *shape);
  155. bool IsWatchPoint(std::string kernel_name, const CNodePtr &kernel = nullptr);
  156. bool IsWatchPointNodeInput(std::string w_name, const CNodePtr &kernel);
  157. void AddWeightsBiasInputs(std::vector<std::shared_ptr<TensorData>> *tensor_list, const CNodePtr &kernel);
  158. TensorLoader *tensor_loader() const;
  159. std::unordered_map<unsigned int, watchpoint_t> GetWatchpointTable();
  160. private:
  161. std::mutex lock_;
  162. std::unordered_map<unsigned int, watchpoint_t> watchpoint_table;
  163. TensorLoader *tensor_loader_;
  164. };
  165. } // namespace mindspore
  166. #endif // MINDSPORE_CCSRC_DEBUG_DEBUG_SERVICES_H_