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

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. void ProcessElement(double element);
  29. double GetPercentInRange();
  30. void set_range_start_inclusive(double value) { range_start_inclusive = value; }
  31. void set_range_end_inclusive(double value) { range_end_inclusive = value; }
  32. private:
  33. double range_start_inclusive;
  34. double range_end_inclusive;
  35. int count;
  36. int total;
  37. };
  38. class AllCloseCalculator {
  39. public:
  40. AllCloseCalculator();
  41. void ProcessElement(double current, double previous);
  42. bool IsAllClose();
  43. void set_atol(double value) { atol = value; }
  44. void set_rtol(double value) { rtol = value; }
  45. private:
  46. double atol;
  47. double rtol;
  48. bool result;
  49. };
  50. class MeanCalculator {
  51. public:
  52. MeanCalculator();
  53. void ProcessElement(double value);
  54. double GetMean();
  55. protected:
  56. double mean;
  57. int count;
  58. };
  59. class VarianceAndMeanCalculator {
  60. public:
  61. VarianceAndMeanCalculator();
  62. void ProcessElement(double value);
  63. double GetStandardDeviation();
  64. double GetVariance();
  65. double GetMean();
  66. private:
  67. double mean;
  68. int count;
  69. double m2;
  70. };
  71. class ITensorSummary {
  72. public:
  73. virtual ~ITensorSummary() = default;
  74. virtual void SummarizeTensor(const std::vector<DebugServices::watchpoint_t> &) = 0;
  75. virtual std::tuple<bool, int32_t, std::vector<DebugServices::parameter_t>> IsWatchpointHit(
  76. DebugServices::watchpoint_t) = 0;
  77. };
  78. template <typename T>
  79. class TensorSummary : public ITensorSummary {
  80. public:
  81. TensorSummary() = default;
  82. TensorSummary(void *, void *, uint32_t);
  83. void SummarizeTensor(const std::vector<DebugServices::watchpoint_t> &) override;
  84. // returns hit, error_code, parameter_list
  85. std::tuple<bool, int, std::vector<DebugServices::parameter_t>> IsWatchpointHit(DebugServices::watchpoint_t) override;
  86. private:
  87. T *current_tensor_ptr;
  88. T *prev_tensor_ptr;
  89. uint32_t num_elements;
  90. double min;
  91. double max;
  92. uint32_t inf_count;
  93. uint32_t nan_count;
  94. uint32_t zero_count;
  95. double epsilon;
  96. bool mean_sd_cal_enabled;
  97. VarianceAndMeanCalculator current_mean_variance;
  98. std::unordered_map<std::string, std::unique_ptr<MeanCalculator>> means;
  99. std::unordered_map<uint32_t, std::unique_ptr<AllCloseCalculator>> all_close;
  100. std::unordered_map<uint32_t, std::unique_ptr<RangeCountCalculator>> range_counts;
  101. double_t StatLookup(const DebugServices::watchpoint_t &);
  102. double_t StatLookup(const std::string &, const DebugServices::watchpoint_t &);
  103. double_t GetZeroValPercent();
  104. void InitCalculators(const std::vector<DebugServices::watchpoint_t> &);
  105. };
  106. } // namespace mindspore
  107. #endif // MINDSPORE_TENSOR_SUMMARY_H