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.6 kB

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