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