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