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