diff --git a/mindspore/ccsrc/debug/debug_services.cc b/mindspore/ccsrc/debug/debug_services.cc index 3d12ecd662..fbcc5ab388 100644 --- a/mindspore/ccsrc/debug/debug_services.cc +++ b/mindspore/ccsrc/debug/debug_services.cc @@ -21,11 +21,7 @@ namespace mindspore { -DebugServices::DebugServices() { - tensor_loader_ = new TensorLoader(); - uint32_t iter_num = -1; - tensor_loader_->set_iter_num(iter_num); -} +DebugServices::DebugServices() { tensor_loader_ = std::make_shared(); } DebugServices::DebugServices(const DebugServices &other) { tensor_loader_ = other.tensor_loader_; @@ -40,8 +36,6 @@ DebugServices &DebugServices::operator=(const DebugServices &other) { return *this; } -DebugServices::~DebugServices() { delete tensor_loader_; } - void DebugServices::AddWatchpoint(unsigned int id, unsigned int watch_condition, float parameter, const std::vector> &check_node_list, const std::vector ¶meter_list) { @@ -61,8 +55,9 @@ void DebugServices::RemoveWatchpoint(unsigned int id) { watchpoint_table.erase(id); } -std::unique_ptr GetSummaryPtr(const mindspore::tensor::TensorPtr &tensor_ptr, void *previous_tensor_ptr, - uint32_t num_elements, int tensor_dtype) { +std::unique_ptr GetSummaryPtr(const mindspore::tensor::TensorPtr &tensor_ptr, + void *const previous_tensor_ptr, uint32_t num_elements, + int tensor_dtype) { switch (tensor_dtype) { case kNumberTypeUInt8: { return std::make_unique>(tensor_ptr->data_c(), previous_tensor_ptr, num_elements); @@ -111,8 +106,8 @@ std::unique_ptr GetSummaryPtr(const mindspore::tensor::TensorPtr void DebugServices::AddWatchPointsToCheck(bool init_dbg_suspend, bool step_end, bool recheck, const std::string &tensor_name, const std::string &tensor_name_no_slot, - std::string *qualified_tensor_name, - std::vector *watchpoints_to_check) { + std::string *const qualified_tensor_name, + std::vector *const watchpoints_to_check) { for (auto w_table_item : watchpoint_table) { auto wp = std::get<1>(w_table_item); // check ONLY init conditions on initial suspended state. @@ -133,10 +128,11 @@ void DebugServices::AddWatchPointsToCheck(bool init_dbg_suspend, bool step_end, } } -void DebugServices::CheckWatchpoints(std::vector *name, std::vector *slot, - std::vector *condition, std::vector *const watchpoint_id, - std::vector> *parameters, - std::vector *error_codes, const std::vector &op_overflows, +void DebugServices::CheckWatchpoints(std::vector *const name, std::vector *const slot, + std::vector *const condition, std::vector *const watchpoint_id, + std::vector> *const parameters, + std::vector *const error_codes, + const std::vector &op_overflows, const std::vector> &tensor_list, const bool init_dbg_suspend, const bool step_end, const bool recheck) { std::lock_guard lg(lock_); @@ -200,9 +196,10 @@ void DebugServices::CheckWatchpoints(std::vector *name, std::vector } } -void DebugServices::ReadNodesTensors(std::vector name, std::vector *ret_name, - std::vector *data_ptr, std::vector *data_size, - std::vector *dtype, std::vector> *const shape) { +void DebugServices::ReadNodesTensors(const std::vector &name, std::vector *const ret_name, + std::vector *const data_ptr, std::vector *const data_size, + std::vector *const dtype, + std::vector> *const shape) { std::vector>> result_list; tensor_loader_->SearchTensors(name, &result_list); @@ -305,10 +302,10 @@ std::vector> DebugServices::GetNodeTensor(const CNod } return result; } -bool DebugServices::TensorExistsInCurrent(std::string tensor_name) { +bool DebugServices::TensorExistsInCurrent(const std::string &tensor_name) { return tensor_loader_->TensorExistsInCurrent(tensor_name); } -void DebugServices::MoveTensorCurrentToPrev(std::string tensor_name) { +void DebugServices::MoveTensorCurrentToPrev(const std::string &tensor_name) { tensor_loader_->MoveTensorCurrentToPrev(tensor_name); } diff --git a/mindspore/ccsrc/debug/debug_services.h b/mindspore/ccsrc/debug/debug_services.h index 271d6faaca..27c0bcad59 100644 --- a/mindspore/ccsrc/debug/debug_services.h +++ b/mindspore/ccsrc/debug/debug_services.h @@ -39,7 +39,7 @@ class DebugServices { DebugServices &operator=(const DebugServices &other); - ~DebugServices(); + ~DebugServices() = default; enum CONDITION_TYPE { HAS_NAN, @@ -106,7 +106,7 @@ class DebugServices { std::vector parameter_list; size_t location = 0; - std::string FindQualifiedTensorName(const std::string &tensor_name) { + std::string FindQualifiedTensorName(const std::string &tensor_name) const { std::string node_name = tensor_name.substr(0, tensor_name.find_first_of(':')); for (auto check_node : check_node_list) { std::string w_name = std::get<0>(check_node); @@ -120,17 +120,17 @@ class DebugServices { return {}; } - bool is_gt_wp() { + bool is_gt_wp() const { return condition.type == MAX_GT || condition.type == MIN_GT || condition.type == MEAN_GT || condition.type == SD_GT || condition.type == MAX_MIN_GT; } - bool is_lt_wp() { + bool is_lt_wp() const { return condition.type == MAX_LT || condition.type == MIN_LT || condition.type == MEAN_LT || condition.type == SD_LT || condition.type == MAX_MIN_LT; } - bool min_max_enabled() { + bool min_max_enabled() const { return condition.type == MAX_LT || condition.type == MAX_GT || condition.type == MIN_LT || condition.type == MIN_GT || condition.type == MAX_MIN_LT || condition.type == MAX_MIN_GT || (condition.type == INIT && (!parameter_list[1].disabled || !parameter_list[2].disabled)) || @@ -138,7 +138,7 @@ class DebugServices { (condition.type == TOO_SMALL && (!parameter_list[1].disabled || !parameter_list[2].disabled)); } // inf or nan related condition set - bool inf_nan_enabled() { + bool inf_nan_enabled() const { return condition.type == HAS_INF || condition.type == HAS_NAN || condition.type == GENERAL_OVERFLOW; } // mean or sd related condition set @@ -151,7 +151,7 @@ class DebugServices { return (condition.type == TOO_LARGE && !parameter_list[0].disabled) || (condition.type == TOO_SMALL && !parameter_list[0].disabled); } - bool zero_percentage_enabled() { return condition.type == ALL_ZERO || condition.type == INIT; } + bool zero_percentage_enabled() const { return condition.type == ALL_ZERO || condition.type == INIT; } bool tensor_update_ratio_mean_enabled() const { return condition.type == CHANGE_TOO_LARGE || condition.type == CHANGE_TOO_SMALL; @@ -184,7 +184,7 @@ class DebugServices { const std::string &tensor_name_no_slot, std::string *qualified_tensor_name, std::vector *watchpoints_to_check); - void ReadNodesTensors(std::vector name, std::vector *ret_name, + void ReadNodesTensors(const std::vector &name, std::vector *ret_name, std::vector *data_ptr, std::vector *data_size, std::vector *dtype, std::vector> *const shape); @@ -218,9 +218,9 @@ class DebugServices { std::vector> GetNodeTensor(const CNodePtr &kernel); - bool TensorExistsInCurrent(std::string tensor_name); + bool TensorExistsInCurrent(const std::string &tensor_name); - void MoveTensorCurrentToPrev(std::string tensor_name); + void MoveTensorCurrentToPrev(const std::string &tensor_name); private: std::mutex lock_; @@ -229,7 +229,7 @@ class DebugServices { std::unordered_map> wp_id_cache; std::unordered_map watchpoint_table; - TensorLoader *tensor_loader_; + std::shared_ptr tensor_loader_; }; } // namespace mindspore diff --git a/mindspore/ccsrc/debug/debugger/debugger.cc b/mindspore/ccsrc/debug/debugger/debugger.cc index 76e798aac8..1c21a1e969 100644 --- a/mindspore/ccsrc/debug/debugger/debugger.cc +++ b/mindspore/ccsrc/debug/debugger/debugger.cc @@ -182,7 +182,7 @@ void Debugger::SetOpOverflowBinPath(uint32_t graph_id) { d = opendir(overflow_bin_path.c_str()); if (d != nullptr) { struct dirent *dir; - while ((dir = readdir(d)) != NULL) { + while ((dir = readdir(d)) != nullptr) { if (dir->d_type == DT_REG) { std::string file_path = overflow_bin_path; file_path.append(dir->d_name); @@ -216,7 +216,7 @@ void Debugger::CheckDatasetSinkMode() { } } -bool Debugger::CheckDebuggerDumpEnabled() { +bool Debugger::CheckDebuggerDumpEnabled() const { // see if dump is enabled if (device_target_ == kGPUDevice) { return device::KernelRuntime::DumpDataEnabled(); @@ -224,7 +224,7 @@ bool Debugger::CheckDebuggerDumpEnabled() { return false; } -bool Debugger::CheckDebuggerEnabled() { +bool Debugger::CheckDebuggerEnabled() const { // get env variables to configure debugger const char *env_enable_char = std::getenv("ENABLE_MS_DEBUGGER"); if (env_enable_char != nullptr) { @@ -250,7 +250,7 @@ void Debugger::CheckDebuggerEnabledParam() { } } -bool Debugger::CheckDebuggerPartialMemoryEnabled() { +bool Debugger::CheckDebuggerPartialMemoryEnabled() const { const char *env_partial_mem_str = std::getenv("MS_DEBUGGER_PARTIAL_MEM"); if (env_partial_mem_str != nullptr) { MS_LOG(INFO) << "Getenv MS_DEBUGGER_PARTIAL_MEM: " << env_partial_mem_str; @@ -261,7 +261,7 @@ bool Debugger::CheckDebuggerPartialMemoryEnabled() { return false; } -bool Debugger::DebuggerBackendEnabled() { return CheckDebuggerDumpEnabled() || CheckDebuggerEnabled(); } +bool Debugger::DebuggerBackendEnabled() const { return CheckDebuggerDumpEnabled() || CheckDebuggerEnabled(); } void Debugger::Reset() { // access lock for public method @@ -315,7 +315,7 @@ void Debugger::PreExecute(const KernelGraphPtr &graph_ptr, uint32_t graph_sum) { LoadParametersAndConst(); // revert graph ptr to original value graph_ptr_ = dbg_graph_ptr; - SendMultiGraphsAndSuspend(graph_proto_list_, graph_sum); + SendMultiGraphsAndSuspend(graph_proto_list_); graph_proto_list_.clear(); } else if (graph_id == rungraph_id_list_.front() && device_target_ == kGPUDevice) { // stop only when receive the first sub run graph for each step @@ -353,7 +353,7 @@ void Debugger::PostExecute() { } } -bool Debugger::ReadNodeDataRequired(const CNodePtr &kernel) { +bool Debugger::ReadNodeDataRequired(const CNodePtr &kernel) const { if (debugger_enabled_ && !is_dataset_graph_) { auto is_watchpoint = debug_services_->IsWatchPoint(cur_name_, kernel); // if node has a watchpoint on it, is next_to node, or continue_to node then read the kernel tensor data @@ -470,7 +470,7 @@ void Debugger::CheckDatasetGraph() { GraphProto Debugger::GetGraphProto(const KernelGraphPtr &graph_ptr) const { // convert kernel graph to debugger modelproto - ModelProto model = GetDebuggerFuncGraphProto(graph_ptr_); + ModelProto model = GetDebuggerFuncGraphProto(graph_ptr); return model.graph(); } @@ -528,7 +528,7 @@ bool Debugger::SendMetadata(bool version_check) { return ret; } -void Debugger::SendMultiGraphsAndSuspend(const std::list &graph_proto_list, uint32_t graph_sum) { +void Debugger::SendMultiGraphsAndSuspend(const std::list &graph_proto_list) { if (!SendMetadata(true)) { return; } @@ -719,7 +719,7 @@ void Debugger::ProcessKViewCMD(const EventReply &reply) { } } -void AddTensorProtoInfo(TensorProto *tensor_item, TensorProto tensor) { +void AddTensorProtoInfo(TensorProto *tensor_item, const TensorProto &tensor) { tensor_item->set_node_name(tensor.node_name()); tensor_item->set_slot(tensor.slot()); tensor_item->set_iter(tensor.iter()); @@ -993,9 +993,9 @@ std::string GetTensorFullName(const TensorProto &tensor) { bool GetMiVersionMatched(const EventReply &reply) { return reply.version_matched(); } -bool Debugger::partial_memory() { return partial_memory_; } +bool Debugger::partial_memory() const { return partial_memory_; } -void Debugger::SetCurNode(std::string cur_name) { +void Debugger::SetCurNode(const std::string &cur_name) { // access lock for public method std::lock_guard a_lock(access_lock_); cur_name_ = cur_name; @@ -1031,7 +1031,7 @@ std::vector Debugger::CheckOpOverflow() { MS_LOG(INFO) << "processing bin file path " << overflow_bin_path << ", graph id " << graph_id; if (d != nullptr) { struct dirent *dir = nullptr; - while ((dir = readdir(d)) != NULL) { + while ((dir = readdir(d)) != nullptr) { if (dir->d_type == DT_REG) { std::string file_path = overflow_bin_path; file_path.append(dir->d_name); @@ -1111,7 +1111,7 @@ bool Debugger::CheckPort(const char *port) { return true; } -bool Debugger::CheckIp(const char *host) { +bool Debugger::CheckIp(const char *host) const { std::regex reg_ip( "(25[0-4]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])" "[.](25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])" @@ -1122,7 +1122,7 @@ bool Debugger::CheckIp(const char *host) { return std::regex_match(host_str, smat, reg_ip); } -uint32_t Debugger::GetFirstRunGraphId() { return rungraph_id_list_.front(); } +uint32_t Debugger::GetFirstRunGraphId() const { return rungraph_id_list_.front(); } void Debugger::LoadSingleAnfnode(const AnfNodePtr &anf_node, const size_t output_index) { MS_EXCEPTION_IF_NULL(anf_node); @@ -1189,7 +1189,6 @@ void Debugger::LoadGraphOutputs() { int exec_order = 1; for (const auto &node : apply_kernels) { MS_EXCEPTION_IF_NULL(node); - auto node_name = AnfAlgo::GetCNodeName(node); std::string kernel_name = node->fullname_with_scope(); auto output_size = AnfAlgo::GetOutputTensorNum(node); if (partial_memory_) { @@ -1238,7 +1237,7 @@ void Debugger::ClearCurrentData() { if (device_target_ == kGPUDevice && (debugger_enabled_ || device::KernelRuntime::DumpDataEnabledIteration())) debug_services_->EmptyCurrentTensor(); } -bool Debugger::TensorExistsInCurrent(std::string tensor_name) { +bool Debugger::TensorExistsInCurrent(const std::string &tensor_name) { return debug_services_->TensorExistsInCurrent(tensor_name); } diff --git a/mindspore/ccsrc/debug/debugger/debugger.h b/mindspore/ccsrc/debug/debugger/debugger.h index 3676ff4d78..0886ac3133 100644 --- a/mindspore/ccsrc/debug/debugger/debugger.h +++ b/mindspore/ccsrc/debug/debugger/debugger.h @@ -81,7 +81,7 @@ class Debugger : public std::enable_shared_from_this { // don't need a graph_ptr because it is saved during pre_execute void PostExecute(); - bool ReadNodeDataRequired(const CNodePtr &kernel); + bool ReadNodeDataRequired(const CNodePtr &kernel) const; void PostExecuteNode(const CNodePtr &kernel, bool last_kernel); @@ -106,9 +106,9 @@ class Debugger : public std::enable_shared_from_this { bool debugger_enabled() const; - bool partial_memory(); + bool partial_memory() const; - void SetCurNode(std::string cur_name); + void SetCurNode(const std::string &cur_name); std::string run_level() const; @@ -119,7 +119,7 @@ class Debugger : public std::enable_shared_from_this { void SetStreamTaskToOpnameMap(const std::map, std::string> &mapping); // check if any feature that uses the debugger backend is enabled - bool DebuggerBackendEnabled(); + bool DebuggerBackendEnabled() const; void SetTrainingDone(bool training_done); @@ -139,13 +139,13 @@ class Debugger : public std::enable_shared_from_this { void LoadGraphs(const KernelGraphPtr &graph_ptr); - uint32_t GetFirstRunGraphId(); + uint32_t GetFirstRunGraphId() const; void SetGraphPtr(const KernelGraphPtr &graph_ptr) { graph_ptr_ = graph_ptr; } std::list GetGraphPtrList() { return graph_ptr_list_; } - bool TensorExistsInCurrent(std::string tensor_name); + bool TensorExistsInCurrent(const std::string &tensor_name); private: // private constructor for singleton @@ -159,14 +159,14 @@ class Debugger : public std::enable_shared_from_this { void SetOpOverflowBinPath(uint32_t graph_id); // check if dump using debugger backend is enabled - bool CheckDebuggerDumpEnabled(); + bool CheckDebuggerDumpEnabled() const; // check if debugger enabled - bool CheckDebuggerEnabled(); + bool CheckDebuggerEnabled() const; void CheckDebuggerEnabledParam(); - bool CheckDebuggerPartialMemoryEnabled(); + bool CheckDebuggerPartialMemoryEnabled() const; // check and save graph pointer void CheckGraphPtr(const KernelGraphPtr &graph_ptr); @@ -180,7 +180,7 @@ class Debugger : public std::enable_shared_from_this { // send graph and enter command wait loop void SendGraphAndSuspend(const GraphProto &graph_proto); - void SendMultiGraphsAndSuspend(const std::list &graph_proto_list, uint32_t graph_sum); + void SendMultiGraphsAndSuspend(const std::list &graph_proto_list); // wait for command and process command // send command request and process reply in a loop @@ -222,7 +222,7 @@ class Debugger : public std::enable_shared_from_this { bool CheckPort(const char *port); // Check if the IP is valid - bool CheckIp(const char *host); + bool CheckIp(const char *host) const; void LoadSingleAnfnode(const AnfNodePtr &anf_node, const size_t output_index); diff --git a/mindspore/ccsrc/debug/debugger/tensor_summary.cc b/mindspore/ccsrc/debug/debugger/tensor_summary.cc index 144f91a253..4c979142ca 100644 --- a/mindspore/ccsrc/debug/debugger/tensor_summary.cc +++ b/mindspore/ccsrc/debug/debugger/tensor_summary.cc @@ -49,7 +49,7 @@ void AllCloseCalculator::ProcessElement(double current, double previous) { result = result && (std::abs(current - previous) <= (atol + rtol * std::abs(previous))); } -bool AllCloseCalculator::IsAllClose() { return result; } +bool AllCloseCalculator::IsAllClose() const { return result; } MeanCalculator::MeanCalculator() : mean(0.0), count(0) {} @@ -59,7 +59,7 @@ void MeanCalculator::ProcessElement(double value) { mean += delta / count; } -double MeanCalculator::GetMean() { return mean; } +double MeanCalculator::GetMean() const { return mean; } VarianceAndMeanCalculator::VarianceAndMeanCalculator() : mean(0.0), count(0), m2(0.0) {} @@ -70,9 +70,9 @@ void VarianceAndMeanCalculator::ProcessElement(double value) { m2 += delta * (value - mean); } -double VarianceAndMeanCalculator::GetMean() { return mean; } +double VarianceAndMeanCalculator::GetMean() const { return mean; } -double VarianceAndMeanCalculator::GetVariance() { +double VarianceAndMeanCalculator::GetVariance() const { if (count > 1) { return m2 / (count - 1); } else { diff --git a/mindspore/ccsrc/debug/debugger/tensor_summary.h b/mindspore/ccsrc/debug/debugger/tensor_summary.h index 39949fbd77..c9562f7ead 100644 --- a/mindspore/ccsrc/debug/debugger/tensor_summary.h +++ b/mindspore/ccsrc/debug/debugger/tensor_summary.h @@ -46,7 +46,7 @@ class AllCloseCalculator { AllCloseCalculator(); ~AllCloseCalculator() = default; void ProcessElement(double current, double previous); - bool IsAllClose(); + bool IsAllClose() const; void set_atol(double value) { atol = value; } void set_rtol(double value) { rtol = value; } @@ -61,7 +61,7 @@ class MeanCalculator { MeanCalculator(); ~MeanCalculator() = default; void ProcessElement(double value); - double GetMean(); + double GetMean() const; protected: double mean; @@ -74,8 +74,8 @@ class VarianceAndMeanCalculator { ~VarianceAndMeanCalculator() = default; void ProcessElement(double value); double GetStandardDeviation(); - double GetVariance(); - double GetMean(); + double GetVariance() const; + double GetMean() const; private: double mean; diff --git a/mindspore/ccsrc/debug/tensor_data.h b/mindspore/ccsrc/debug/tensor_data.h index 00af203208..afa0015bde 100644 --- a/mindspore/ccsrc/debug/tensor_data.h +++ b/mindspore/ccsrc/debug/tensor_data.h @@ -43,13 +43,13 @@ class TensorData { ~TensorData() {} - std::string GetName() { return this->name; } + std::string GetName() const { return this->name; } - mindspore::tensor::TensorPtr GetTensor() { return this->tensor_ptr; } + mindspore::tensor::TensorPtr GetTensor() const { return this->tensor_ptr; } - size_t GetSlot() { return this->slot; } + size_t GetSlot() const { return this->slot; } - int GetExecutionOrder() { return this->execution_order; } + int GetExecutionOrder() const { return this->execution_order; } void SetExecutionOrder(int execution_order) { this->execution_order = execution_order; } diff --git a/mindspore/ccsrc/debug/tensor_load.h b/mindspore/ccsrc/debug/tensor_load.h index af69519b80..673fe5c777 100644 --- a/mindspore/ccsrc/debug/tensor_load.h +++ b/mindspore/ccsrc/debug/tensor_load.h @@ -43,12 +43,12 @@ class TensorLoader { void SwapCurrentPrev() { tensor_list_map.swap(prev_tensor_list_map); } - bool TensorExistsInCurrent(std::string tensor_name) { + bool TensorExistsInCurrent(std::string tensor_name) const { return tensor_list_map.find(tensor_name) != tensor_list_map.end(); } // only parameters will return true - bool PrevTensorExistsInCurrent(std::string tensor_name) { return TensorExistsInCurrent(tensor_name + ":prev"); } + bool PrevTensorExistsInCurrent(std::string tensor_name) const { return TensorExistsInCurrent(tensor_name + ":prev"); } void MoveParametersCurrentToPrev() { MS_LOG(INFO) << "Moving parameters from current map to previous map"; @@ -65,7 +65,7 @@ class TensorLoader { } } - bool IsPrevTensor(std::string tensor_name) { + bool IsPrevTensor(std::string tensor_name) const { const std::string suffix = ":prev"; if (tensor_name.length() <= suffix.length()) return false; return std::equal(suffix.rbegin(), suffix.rend(), tensor_name.rbegin()); @@ -96,13 +96,13 @@ class TensorLoader { return tensor_list; } - std::shared_ptr GetTensor(const std::string &tensor_name) { + std::shared_ptr GetTensor(const std::string &tensor_name) const { auto iter = tensor_list_map.find(tensor_name); if (iter != tensor_list_map.end()) return iter->second; return nullptr; } - uint32_t GetIterNum() { return iter_num; } + uint32_t GetIterNum() const { return iter_num; } std::map> GetTensorMap() { return tensor_list_map; }