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 3.7 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. virtual ~ITensorSummary() = default;
  80. virtual void SummarizeTensor(const std::vector<DebugServices::watchpoint_t> &) = 0;
  81. virtual std::tuple<bool, int32_t, std::vector<DebugServices::parameter_t>> IsWatchpointHit(
  82. DebugServices::watchpoint_t) = 0;
  83. };
  84. template <typename T>
  85. class TensorSummary : public ITensorSummary {
  86. public:
  87. TensorSummary() = default;
  88. ~TensorSummary() override = default;
  89. TensorSummary(void *, void *, uint32_t);
  90. void SummarizeTensor(const std::vector<DebugServices::watchpoint_t> &) override;
  91. // returns hit, error_code, parameter_list
  92. std::tuple<bool, int, std::vector<DebugServices::parameter_t>> IsWatchpointHit(DebugServices::watchpoint_t) override;
  93. private:
  94. T *current_tensor_ptr;
  95. T *prev_tensor_ptr;
  96. uint32_t num_elements;
  97. double min;
  98. double max;
  99. uint32_t inf_count;
  100. uint32_t nan_count;
  101. uint32_t zero_count;
  102. double epsilon;
  103. bool mean_sd_cal_enabled;
  104. VarianceAndMeanCalculator current_mean_variance;
  105. std::unordered_map<std::string, std::unique_ptr<MeanCalculator>> means;
  106. std::unordered_map<uint32_t, std::unique_ptr<AllCloseCalculator>> all_close;
  107. std::unordered_map<uint32_t, std::unique_ptr<RangeCountCalculator>> range_counts;
  108. double_t StatLookup(const DebugServices::watchpoint_t &);
  109. double_t StatLookup(const std::string &, const DebugServices::watchpoint_t &);
  110. double_t GetZeroValPercent();
  111. void InitCalculators(const std::vector<DebugServices::watchpoint_t> &);
  112. };
  113. #ifdef ONLINE_DBG_MODE
  114. } // namespace mindspore
  115. #endif
  116. #endif // MINDSPORE_TENSOR_SUMMARY_H