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.

tensor_summary.cc 10 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. #include <math.h>
  17. #include <algorithm>
  18. #include <limits>
  19. #include <memory>
  20. #include <bitset>
  21. #include <tuple>
  22. #include "debug/debugger/tensor_summary.h"
  23. namespace mindspore {
  24. using CONDITION_TYPE = DebugServices::CONDITION_TYPE;
  25. RangeCountCalculator::RangeCountCalculator()
  26. : range_start_inclusive(-std::numeric_limits<double>::infinity()),
  27. range_end_inclusive(std::numeric_limits<double>::infinity()),
  28. count(0),
  29. total(0) {}
  30. void RangeCountCalculator::ProcessElement(double element) {
  31. count += (element >= range_start_inclusive && element <= range_end_inclusive);
  32. total += 1;
  33. }
  34. double RangeCountCalculator::GetPercentInRange() const {
  35. if (total == 0) {
  36. return 0.0;
  37. }
  38. return 100.0 * count / total;
  39. }
  40. AllCloseCalculator::AllCloseCalculator() : atol(1.0e-8), rtol(1.0e-5), result(true) {}
  41. void AllCloseCalculator::ProcessElement(double current, double previous) {
  42. result = result && (std::abs(current - previous) <= (atol + rtol * std::abs(previous)));
  43. }
  44. bool AllCloseCalculator::IsAllClose() { return result; }
  45. MeanCalculator::MeanCalculator() : mean(0.0), count(0) {}
  46. void MeanCalculator::ProcessElement(double value) {
  47. count += 1;
  48. double delta = value - mean;
  49. mean += delta / count;
  50. }
  51. double MeanCalculator::GetMean() { return mean; }
  52. VarianceAndMeanCalculator::VarianceAndMeanCalculator() : mean(0.0), count(0), m2(0.0) {}
  53. void VarianceAndMeanCalculator::ProcessElement(double value) {
  54. count += 1;
  55. double delta = value - mean;
  56. mean += delta / count;
  57. m2 += delta * (value - mean);
  58. }
  59. double VarianceAndMeanCalculator::GetMean() { return mean; }
  60. double VarianceAndMeanCalculator::GetVariance() {
  61. if (count > 1) {
  62. return m2 / (count - 1);
  63. } else {
  64. return 0.0;
  65. }
  66. }
  67. double VarianceAndMeanCalculator::GetStandardDeviation() { return sqrt(GetVariance()); }
  68. template <typename T>
  69. TensorSummary<T>::TensorSummary(void *current_tensor_ptr, void *previous_tensor_ptr, uint32_t num_elements)
  70. : current_tensor_ptr(reinterpret_cast<T *>(current_tensor_ptr)),
  71. prev_tensor_ptr(reinterpret_cast<T *>(previous_tensor_ptr)),
  72. num_elements(num_elements),
  73. min(std::numeric_limits<double>::max()),
  74. max(std::numeric_limits<double>::lowest()),
  75. inf_count(0),
  76. nan_count(0),
  77. zero_count(0),
  78. epsilon(1.0e-9),
  79. mean_sd_cal_enabled(false) {}
  80. template <typename T>
  81. void TensorSummary<T>::SummarizeTensor(const std::vector<DebugServices::watchpoint_t> &wps) {
  82. InitCalculators(wps);
  83. for (size_t i = 0; i < num_elements; ++i) {
  84. auto current_value = static_cast<double>(current_tensor_ptr[i]);
  85. double previous_value =
  86. prev_tensor_ptr ? static_cast<double>(prev_tensor_ptr[i]) : std::numeric_limits<double>::quiet_NaN();
  87. inf_count += std::isinf(current_value);
  88. nan_count += std::isnan(current_value);
  89. zero_count += (current_value == 0);
  90. max = std::max(max, current_value);
  91. min = std::min(min, current_value);
  92. if (mean_sd_cal_enabled) {
  93. current_mean_variance.ProcessElement(current_value);
  94. }
  95. for (auto &it : all_close) {
  96. it.second->ProcessElement(current_value, previous_value);
  97. }
  98. for (auto &range_count : range_counts) {
  99. range_count.second->ProcessElement(current_value);
  100. }
  101. for (auto &mean : means) {
  102. if (mean.first == "curr_prev_diff_mean") {
  103. mean.second->ProcessElement(std::abs(current_value - previous_value));
  104. } else if (mean.first == "abs_prev_mean") {
  105. mean.second->ProcessElement(std::abs(previous_value));
  106. } else if (mean.first == "abs_current_mean") {
  107. mean.second->ProcessElement(std::abs(current_value));
  108. }
  109. }
  110. }
  111. }
  112. template <typename T>
  113. std::tuple<bool, int, std::vector<DebugServices::parameter_t>> TensorSummary<T>::IsWatchpointHit(
  114. DebugServices::watchpoint_t wp) {
  115. auto parameter_list = wp.parameter_list;
  116. bool hit = false;
  117. std::bitset<32> error_code;
  118. CONDITION_TYPE type = wp.condition.type;
  119. // bit 0 denotes presence of nan
  120. error_code.set(0, nan_count > 0);
  121. // bit 1 denotes presence of inf
  122. error_code.set(1, inf_count > 0);
  123. if (type == CONDITION_TYPE::HAS_NAN) {
  124. error_code.reset();
  125. hit = nan_count > 0;
  126. } else if (type == CONDITION_TYPE::HAS_INF) {
  127. error_code.reset();
  128. hit = inf_count > 0;
  129. } else if (type == CONDITION_TYPE::GENERAL_OVERFLOW) {
  130. error_code.reset();
  131. hit = (nan_count + inf_count) > 0;
  132. } else if (type == CONDITION_TYPE::NOT_CHANGED && prev_tensor_ptr && error_code.none()) {
  133. hit = all_close[wp.id]->IsAllClose();
  134. } else if ((type == CONDITION_TYPE::NOT_CHANGED || type == CONDITION_TYPE::CHANGE_TOO_LARGE ||
  135. type == CONDITION_TYPE::CHANGE_TOO_SMALL) &&
  136. !prev_tensor_ptr) {
  137. // bit 2 denotes absence of previous tensor
  138. error_code.set(2, true);
  139. }
  140. if (error_code.none()) {
  141. for (auto &parameter : parameter_list) {
  142. if (parameter.disabled || error_code.any()) {
  143. continue;
  144. }
  145. // extract inequality type from watchpoint for backward compatibility
  146. std::string inequality_type;
  147. if (wp.is_gt_wp()) {
  148. inequality_type = "gt";
  149. } else if (wp.is_lt_wp()) {
  150. inequality_type = "lt";
  151. }
  152. parameter.Evaluate(StatLookup(parameter.name, wp), inequality_type);
  153. hit = hit || parameter.hit;
  154. }
  155. }
  156. return std::make_tuple(hit, static_cast<int32_t>(error_code.to_ulong()), parameter_list);
  157. }
  158. template <typename T>
  159. double_t TensorSummary<T>::StatLookup(const std::string &parameter_name, const DebugServices::watchpoint_t &wp) {
  160. if (parameter_name == "param") return StatLookup(wp);
  161. std::string param_type;
  162. auto pos = parameter_name.find_last_of('_');
  163. if (pos != std::string::npos) {
  164. param_type = parameter_name.substr(0, pos);
  165. }
  166. if (param_type == "max") {
  167. return max;
  168. } else if (param_type == "min") {
  169. return min;
  170. } else if (param_type == "max_min") {
  171. return max - min;
  172. } else if (param_type == "mean") {
  173. return current_mean_variance.GetMean();
  174. } else if (param_type == "sd") {
  175. return current_mean_variance.GetStandardDeviation();
  176. } else if (param_type == "abs_mean") {
  177. if (means.find("abs_current_mean") != means.end()) {
  178. return means["abs_current_mean"]->GetMean();
  179. }
  180. } else if (param_type == "abs_mean_update_ratio" && prev_tensor_ptr) {
  181. if (means.find("curr_prev_diff_mean") != means.end() && means.find("abs_prev_mean") != means.end()) {
  182. return means["curr_prev_diff_mean"]->GetMean() / (means["abs_prev_mean"]->GetMean() + epsilon);
  183. }
  184. } else if (param_type == "range_percentage") {
  185. if (range_counts.find(wp.id) != range_counts.end()) {
  186. return range_counts[wp.id]->GetPercentInRange();
  187. }
  188. } else if (param_type == "zero_percentage") {
  189. return GetZeroValPercent();
  190. }
  191. return std::numeric_limits<double_t>::quiet_NaN();
  192. }
  193. template <typename T>
  194. double_t TensorSummary<T>::StatLookup(const DebugServices::watchpoint_t &wp) {
  195. CONDITION_TYPE type = wp.condition.type;
  196. if (type == CONDITION_TYPE::MAX_LT || type == CONDITION_TYPE::MAX_GT) {
  197. return max;
  198. } else if (type == CONDITION_TYPE::MIN_LT || type == CONDITION_TYPE::MIN_GT) {
  199. return min;
  200. } else if (type == CONDITION_TYPE::MEAN_LT || type == CONDITION_TYPE::MEAN_GT) {
  201. return current_mean_variance.GetMean();
  202. } else if (type == CONDITION_TYPE::SD_LT || type == CONDITION_TYPE::SD_GT) {
  203. return current_mean_variance.GetStandardDeviation();
  204. } else if (type == CONDITION_TYPE::MAX_MIN_GT || type == CONDITION_TYPE::MAX_MIN_LT) {
  205. return max - min;
  206. }
  207. return std::numeric_limits<double_t>::quiet_NaN();
  208. }
  209. template <typename T>
  210. double_t TensorSummary<T>::GetZeroValPercent() {
  211. if (num_elements == 0) {
  212. return 0;
  213. }
  214. return (zero_count * 100.0) / num_elements;
  215. }
  216. template <typename T>
  217. void TensorSummary<T>::InitCalculators(const std::vector<DebugServices::watchpoint_t> &wps) {
  218. for (auto &wp : wps) {
  219. auto wp_id = wp.id;
  220. mean_sd_cal_enabled = mean_sd_cal_enabled || wp.mean_sd_enabled();
  221. if (wp.allclose_enabled() && prev_tensor_ptr) {
  222. all_close[wp_id] = std::make_unique<AllCloseCalculator>();
  223. if (!wp.parameter_list[0].disabled) {
  224. all_close[wp_id]->set_atol(wp.parameter_list[0].value);
  225. }
  226. if (!wp.parameter_list[1].disabled) {
  227. all_close[wp_id]->set_rtol(wp.parameter_list[1].value);
  228. }
  229. } else if (wp.range_enabled()) {
  230. range_counts[wp_id] = std::make_unique<RangeCountCalculator>();
  231. if (!wp.parameter_list[0].disabled) {
  232. range_counts[wp_id]->set_range_start_inclusive(wp.parameter_list[0].value);
  233. }
  234. if (!wp.parameter_list[1].disabled) {
  235. range_counts[wp_id]->set_range_end_inclusive(wp.parameter_list[1].value);
  236. }
  237. } else if (wp.tensor_update_ratio_mean_enabled() && prev_tensor_ptr) {
  238. means.insert({"curr_prev_diff_mean", std::make_unique<MeanCalculator>()});
  239. means.insert({"abs_prev_mean", std::make_unique<MeanCalculator>()});
  240. } else if (wp.abs_mean_enabled()) {
  241. means.insert({"abs_current_mean", std::make_unique<MeanCalculator>()});
  242. }
  243. }
  244. }
  245. template class TensorSummary<uint8_t>;
  246. template class TensorSummary<int8_t>;
  247. template class TensorSummary<uint16_t>;
  248. template class TensorSummary<int16_t>;
  249. template class TensorSummary<uint32_t>;
  250. template class TensorSummary<int32_t>;
  251. template class TensorSummary<uint64_t>;
  252. template class TensorSummary<int64_t>;
  253. template class TensorSummary<float16>;
  254. template class TensorSummary<float>;
  255. template class TensorSummary<double>;
  256. template class TensorSummary<bool>;
  257. } // namespace mindspore