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 5.3 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 <limits>
  26. #include "debug/tensor_load.h"
  27. #include "debug/tensor_data.h"
  28. #include "ir/dtype.h"
  29. namespace mindspore {
  30. class DebugServices {
  31. public:
  32. DebugServices();
  33. DebugServices(const DebugServices &other);
  34. DebugServices &operator=(const DebugServices &other);
  35. ~DebugServices();
  36. enum CONDITION_TYPE {
  37. HAS_NAN,
  38. HAS_INF,
  39. IS_OVERFLOW,
  40. MAX_GT,
  41. MAX_LT,
  42. MIN_GT,
  43. MIN_LT,
  44. MAX_MIN_GT,
  45. MAX_MIN_LT,
  46. MEAN_GT,
  47. MEAN_LT,
  48. SD_GT,
  49. SD_LT
  50. };
  51. typedef struct condition {
  52. CONDITION_TYPE type;
  53. float parameter = 0;
  54. std::string comparison;
  55. } condition_t;
  56. typedef struct watchpoint {
  57. unsigned int id;
  58. condition_t condition;
  59. std::vector<std::tuple<std::string, bool>> check_node_list;
  60. size_t location = 0;
  61. bool IsNodeIncluded(const std::string &tensor_name) {
  62. std::string node_name = tensor_name.substr(0, tensor_name.find_first_of(':'));
  63. for (auto check_node : check_node_list) {
  64. std::string w_name = std::get<0>(check_node);
  65. bool w_type = std::get<1>(check_node);
  66. if ((w_type && (tensor_name.find(w_name) == location || w_name == "*")) || (!w_type && node_name == w_name)) {
  67. return true;
  68. }
  69. }
  70. return false;
  71. }
  72. bool min_max_enabled() {
  73. return condition.type == MAX_LT || condition.type == MAX_GT || condition.type == MIN_LT ||
  74. condition.type == MIN_GT || condition.type == MAX_MIN_LT || condition.type == MAX_MIN_GT;
  75. }
  76. // inf or nan related condition set
  77. bool inf_nan_enabled() { return condition.type == HAS_INF || condition.type == HAS_NAN; }
  78. // mean or sd related condition set
  79. bool mean_sd_enabled() {
  80. return condition.type == MEAN_LT || condition.type == MEAN_GT || condition.type == SD_LT ||
  81. condition.type == SD_GT;
  82. }
  83. } watchpoint_t;
  84. struct tensor_stats {
  85. double min = std::numeric_limits<double>::max();
  86. double max = std::numeric_limits<double>::lowest();
  87. bool has_inf = false;
  88. bool has_nan = false;
  89. unsigned int n = 0;
  90. double mean = 0.0;
  91. double m2 = 0.0;
  92. double statLookup(CONDITION_TYPE type) const {
  93. if (type == MAX_GT || type == MAX_LT) return max;
  94. if (type == MIN_GT || type == MIN_LT) return min;
  95. if (type == MAX_MIN_GT || type == MAX_MIN_LT) return (max - min);
  96. if (type == MEAN_GT || type == MEAN_LT) return mean;
  97. if (type == SD_GT || type == SD_LT) return getStandardDeviation();
  98. return std::numeric_limits<double>::quiet_NaN();
  99. }
  100. double getMean() const { return mean; }
  101. double getVariance() const {
  102. if (n > 1) {
  103. return m2 / (n - 1);
  104. } else {
  105. return 0.0;
  106. }
  107. }
  108. double getStandardDeviation() const { return sqrt(getVariance()); }
  109. };
  110. void AddWatchpoint(unsigned int id, unsigned int watch_condition, float parameter,
  111. const std::vector<std::tuple<std::string, bool>> &check_node_list);
  112. void RemoveWatchpoint(unsigned int id);
  113. void CheckWatchpoints(std::vector<std::string> *name, std::vector<std::string> *slot, std::vector<int> *condition,
  114. std::vector<unsigned int> *watchpoint_id, const std::vector<std::string> &op_overflows,
  115. const std::vector<std::shared_ptr<TensorData>> &tensor_list);
  116. void ReadNodesTensors(std::vector<std::string> name, std::vector<std::string> *ret_name,
  117. std::vector<char *> *data_ptr, std::vector<unsigned int> *data_size,
  118. std::vector<TypePtr> *dtype, std::vector<std::vector<int>> *shape);
  119. bool IsWatchPoint(std::string kernel_name);
  120. TensorLoader *tensor_loader() const;
  121. std::unordered_map<unsigned int, watchpoint_t> GetWatchpointTable();
  122. private:
  123. std::mutex lock_;
  124. std::unordered_map<unsigned int, watchpoint_t> watchpoint_table;
  125. std::vector<std::string> condition_label = {"HAS_NAN", "HAS_INF", "IS_OVERFLOW", "MAX_GT", "MAX_LT",
  126. "MIN_GT", "MIN_LT", "MAX_MIN_GT", "MAX_MIN_LT", "MEAN_GT",
  127. "MEAN_LT", "SD_GT", "SD_LT"};
  128. TensorLoader *tensor_loader_;
  129. template <typename T>
  130. static tensor_stats SummarizeTensor(const T *start, unsigned int n, bool need_min_max, bool need_mean_sd);
  131. };
  132. } // namespace mindspore
  133. #endif // MINDSPORE_CCSRC_DEBUG_DEBUG_SERVICES_H_