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