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.2 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
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_TENSOR_SUMMARY_H
  17. #define MINDSPORE_TENSOR_SUMMARY_H
  18. #include <vector>
  19. #include <unordered_map>
  20. #include <tuple>
  21. #include <memory>
  22. #include <string>
  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. virtual ~ITensorSummary() = default;
  81. virtual void SummarizeTensor(const std::vector<DebugServices::watchpoint_t> &) = 0;
  82. virtual std::tuple<bool, int32_t, std::vector<DebugServices::parameter_t>> IsWatchpointHit(
  83. DebugServices::watchpoint_t) = 0;
  84. virtual void TensorStatistics(DbgDataType) = 0;
  85. virtual const bool is_bool() const = 0;
  86. virtual const double max_value() const = 0;
  87. virtual const double min_value() const = 0;
  88. virtual const double avg_value() const = 0;
  89. virtual const int count() const = 0;
  90. virtual const int neg_zero_count() const = 0;
  91. virtual const int pos_zero_count() const = 0;
  92. virtual const int nan_count() const = 0;
  93. virtual const int neg_inf_count() const = 0;
  94. virtual const int pos_inf_count() const = 0;
  95. virtual const int zero_count() const = 0;
  96. };
  97. template <typename T>
  98. class TensorSummary : public ITensorSummary {
  99. public:
  100. TensorSummary() = default;
  101. ~TensorSummary() override = default;
  102. TensorSummary(void *, void *, uint32_t);
  103. void SummarizeTensor(const std::vector<DebugServices::watchpoint_t> &) override;
  104. // returns hit, error_code, parameter_list
  105. std::tuple<bool, int, std::vector<DebugServices::parameter_t>> IsWatchpointHit(DebugServices::watchpoint_t) override;
  106. void TensorStatistics(DbgDataType) override;
  107. const bool is_bool() const override { return is_bool_; }
  108. const double max_value() const override { return max_; }
  109. const double min_value() const override { return min_; }
  110. const double avg_value() const override { return avg_; }
  111. const int count() const override { return num_elements_; }
  112. const int neg_zero_count() const override { return neg_zero_count_; }
  113. const int pos_zero_count() const override { return pos_zero_count_; }
  114. const int nan_count() const override { return nan_count_; }
  115. const int neg_inf_count() const override { return neg_inf_count_; }
  116. const int pos_inf_count() const override { return pos_inf_count_; }
  117. const int zero_count() const override { return zero_count_; }
  118. private:
  119. T *current_tensor_ptr_;
  120. T *prev_tensor_ptr_;
  121. uint32_t num_elements_;
  122. double min_;
  123. double max_;
  124. double avg_;
  125. bool is_bool_;
  126. uint32_t neg_zero_count_;
  127. uint32_t pos_zero_count_;
  128. uint32_t pos_inf_count_;
  129. uint32_t neg_inf_count_;
  130. uint32_t inf_count_;
  131. uint32_t nan_count_;
  132. uint32_t zero_count_;
  133. double epsilon_;
  134. bool mean_sd_cal_enabled_;
  135. VarianceAndMeanCalculator current_mean_variance_;
  136. std::unordered_map<std::string, std::unique_ptr<MeanCalculator>> means_;
  137. std::unordered_map<uint32_t, std::unique_ptr<AllCloseCalculator>> all_close_;
  138. std::unordered_map<uint32_t, std::unique_ptr<RangeCountCalculator>> range_counts_;
  139. double_t StatLookup(const DebugServices::watchpoint_t &);
  140. double_t StatLookup(const std::string &, const DebugServices::watchpoint_t &);
  141. double_t GetZeroValPercent();
  142. void InitCalculators(const std::vector<DebugServices::watchpoint_t> &);
  143. };
  144. #ifdef ONLINE_DBG_MODE
  145. } // namespace mindspore
  146. #endif
  147. #endif // MINDSPORE_TENSOR_SUMMARY_H