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.h 5.5 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #ifndef MINDSPORE_TENSOR_SUMMARY_H
  17. #define MINDSPORE_TENSOR_SUMMARY_H
  18. #include <vector>
  19. #include <tuple>
  20. #include <memory>
  21. #include <string>
  22. #include "utils/hash_map.h"
  23. #include "debug/debug_services.h"
  24. #ifdef ONLINE_DBG_MODE
  25. namespace mindspore {
  26. #endif
  27. class RangeCountCalculator {
  28. public:
  29. RangeCountCalculator();
  30. ~RangeCountCalculator() = default;
  31. void ProcessElement(double element);
  32. double GetPercentInRange() const;
  33. void set_range_start_inclusive(double value) { range_start_inclusive = value; }
  34. void set_range_end_inclusive(double value) { range_end_inclusive = value; }
  35. private:
  36. double range_start_inclusive;
  37. double range_end_inclusive;
  38. int count;
  39. int total;
  40. };
  41. class AllCloseCalculator {
  42. public:
  43. AllCloseCalculator();
  44. ~AllCloseCalculator() = default;
  45. void ProcessElement(double current, double previous);
  46. bool IsAllClose() const;
  47. void set_atol(double value) { atol = value; }
  48. void set_rtol(double value) { rtol = value; }
  49. private:
  50. double atol;
  51. double rtol;
  52. bool result;
  53. };
  54. class MeanCalculator {
  55. public:
  56. MeanCalculator();
  57. ~MeanCalculator() = default;
  58. void ProcessElement(double value);
  59. double GetMean() const;
  60. protected:
  61. double mean;
  62. int count;
  63. };
  64. class VarianceAndMeanCalculator {
  65. public:
  66. VarianceAndMeanCalculator();
  67. ~VarianceAndMeanCalculator() = default;
  68. void ProcessElement(double value);
  69. double GetStandardDeviation();
  70. double GetVariance() const;
  71. double GetMean() const;
  72. private:
  73. double mean;
  74. int count;
  75. double m2;
  76. };
  77. class ITensorSummary {
  78. public:
  79. enum WatchpointPos { eHitPos = 0, eErrorCodePos = 1, eParamListPos = 2 };
  80. enum ErrorCode {
  81. NAN_TENSOR = 0,
  82. INF_TENSOR = 2,
  83. NULL_PREV_TENSOR = 4,
  84. OUT_OF_MEMORY = 8,
  85. HISTORY_NOT_FOUND = 16,
  86. NO_VALUE = 32
  87. };
  88. virtual ~ITensorSummary() = default;
  89. virtual void SummarizeTensor(const std::vector<DebugServices::watchpoint_t> &) = 0;
  90. virtual std::tuple<bool, int32_t, std::vector<DebugServices::parameter_t>> IsWatchpointHit(
  91. DebugServices::watchpoint_t) = 0;
  92. virtual void TensorStatistics(DbgDataType) = 0;
  93. virtual const bool is_bool() const = 0;
  94. virtual const double max_value() const = 0;
  95. virtual const double min_value() const = 0;
  96. virtual const double avg_value() const = 0;
  97. virtual const int count() const = 0;
  98. virtual const int neg_zero_count() const = 0;
  99. virtual const int pos_zero_count() const = 0;
  100. virtual const int nan_count() const = 0;
  101. virtual const int neg_inf_count() const = 0;
  102. virtual const int pos_inf_count() const = 0;
  103. virtual const int zero_count() const = 0;
  104. };
  105. template <typename T>
  106. class TensorSummary : public ITensorSummary {
  107. public:
  108. TensorSummary() = default;
  109. ~TensorSummary() override = default;
  110. TensorSummary(const void *, const void *, uint32_t, uint32_t);
  111. void SummarizeTensor(const std::vector<DebugServices::watchpoint_t> &) override;
  112. // returns hit, error_code, parameter_list
  113. std::tuple<bool, int, std::vector<DebugServices::parameter_t>> IsWatchpointHit(DebugServices::watchpoint_t) override;
  114. void TensorStatistics(DbgDataType) override;
  115. const bool is_bool() const override { return is_bool_; }
  116. const double max_value() const override { return max_; }
  117. const double min_value() const override { return min_; }
  118. const double avg_value() const override { return avg_; }
  119. const int count() const override { return num_elements_; }
  120. const int neg_zero_count() const override { return neg_zero_count_; }
  121. const int pos_zero_count() const override { return pos_zero_count_; }
  122. const int nan_count() const override { return nan_count_; }
  123. const int neg_inf_count() const override { return neg_inf_count_; }
  124. const int pos_inf_count() const override { return pos_inf_count_; }
  125. const int zero_count() const override { return zero_count_; }
  126. private:
  127. const T *current_tensor_ptr_;
  128. const T *prev_tensor_ptr_;
  129. uint32_t num_elements_;
  130. uint32_t prev_num_elements_;
  131. double min_;
  132. double max_;
  133. double avg_;
  134. bool is_bool_;
  135. uint32_t neg_zero_count_;
  136. uint32_t pos_zero_count_;
  137. uint32_t pos_inf_count_;
  138. uint32_t neg_inf_count_;
  139. uint32_t inf_count_;
  140. uint32_t nan_count_;
  141. uint32_t zero_count_;
  142. double epsilon_;
  143. bool mean_sd_cal_enabled_;
  144. VarianceAndMeanCalculator current_mean_variance_;
  145. mindspore::HashMap<std::string, std::unique_ptr<MeanCalculator>> means_;
  146. mindspore::HashMap<uint32_t, std::unique_ptr<AllCloseCalculator>> all_close_;
  147. mindspore::HashMap<uint32_t, std::unique_ptr<RangeCountCalculator>> range_counts_;
  148. double_t StatLookup(const DebugServices::watchpoint_t &);
  149. double_t StatLookup(const std::string &, const DebugServices::watchpoint_t &);
  150. double_t GetZeroValPercent();
  151. void InitCalculators(const std::vector<DebugServices::watchpoint_t> &);
  152. };
  153. #ifdef ONLINE_DBG_MODE
  154. } // namespace mindspore
  155. #endif
  156. #endif // MINDSPORE_TENSOR_SUMMARY_H