From 9aa51c64fab59d2557b374af3ce4754bb8920626 Mon Sep 17 00:00:00 2001 From: LaiYongqiang Date: Wed, 2 Jun 2021 15:46:29 +0800 Subject: [PATCH] fix static check warning in SOMAS --- .../optimizer/mem_reuse/mem_swap_manager.cc | 3 +- .../ccsrc/backend/optimizer/somas/somas.cc | 196 ++++++------------ .../ccsrc/backend/optimizer/somas/somas.h | 5 +- .../optimizer/somas/somas_solver_alg.cc | 4 +- .../optimizer/somas/somas_solver_core.cc | 8 +- .../optimizer/somas/somas_solver_pre.cc | 90 ++++---- mindspore/ccsrc/debug/common.cc | 23 ++ mindspore/ccsrc/debug/common.h | 1 + 8 files changed, 132 insertions(+), 198 deletions(-) diff --git a/mindspore/ccsrc/backend/optimizer/mem_reuse/mem_swap_manager.cc b/mindspore/ccsrc/backend/optimizer/mem_reuse/mem_swap_manager.cc index 442d1e6d351..dfad9c914ef 100644 --- a/mindspore/ccsrc/backend/optimizer/mem_reuse/mem_swap_manager.cc +++ b/mindspore/ccsrc/backend/optimizer/mem_reuse/mem_swap_manager.cc @@ -108,7 +108,8 @@ void MemSwapManager::RetreatSwapThreshold() { if (update_one_decay_step) { distance_threshold_ -= distance_decay_step_; } else if (distance_threshold_ >= kDistanceLowerBound) { - size_t new_distance_decay_step = (distance_threshold_ - kDistanceLowerBound) / 4; + static constexpr size_t kDistanceDecayStepFactor = 4; + size_t new_distance_decay_step = (distance_threshold_ - kDistanceLowerBound) / kDistanceDecayStepFactor; if (new_distance_decay_step < 1) { new_distance_decay_step = 1; } diff --git a/mindspore/ccsrc/backend/optimizer/somas/somas.cc b/mindspore/ccsrc/backend/optimizer/somas/somas.cc index 99a705e5b4d..0835a81b07d 100644 --- a/mindspore/ccsrc/backend/optimizer/somas/somas.cc +++ b/mindspore/ccsrc/backend/optimizer/somas/somas.cc @@ -22,7 +22,6 @@ #include #include #include -#include #include "backend/optimizer/somas/somas_node.h" #include "backend/optimizer/somas/somas_solver_pre.h" @@ -47,6 +46,8 @@ using mindspore::profiler::TensorMemory; namespace mindspore { namespace somas { constexpr auto kGapSize = 512; +constexpr auto kRetryIntervalSeconds = 500; +constexpr size_t kRefNodeTensorNum = 2; constexpr auto kGraphId = "graph_id"; constexpr auto kHashId = "hash_id"; @@ -130,31 +131,12 @@ bool Somas::CalcSomasModelHash(const session::KernelGraph *graph) { MS_LOG(INFO) << "Graph " << graph->graph_id() << "'s SOMAS Model hash id is " << hash_id_; std::string filename = save_graphs_path_ + "/somas_meta/" + "somas_graph" + std::to_string(graph->graph_id()) + "_" + hash_id_ + ".info"; - if (filename.size() > PATH_MAX) { - MS_LOG(WARNING) << "File path " << filename << " is too long."; - return false; - } - auto real_path = Common::GetRealPath(filename); - if (!real_path.has_value()) { - MS_LOG(WARNING) << "Get real path failed. path=" << filename; - return false; + std::ofstream ofs; + if (Common::OpenFile(filename, ofs)) { + ofs << model_str << std::endl; + ofs.close(); } - std::ifstream ifs(real_path.value()); - if (ifs) { - MS_LOG(INFO) << "Graph " << graph->graph_id() << "'s SOMAS Model file " << real_path.value() << " is exist."; - ifs.close(); - return true; - } - ChangeFileMode(real_path.value(), S_IRWXU); - std::ofstream ofs(real_path.value()); - - if (!ofs.is_open()) { - MS_LOG(WARNING) << "Open file '" << real_path.value() << "' failed!"; - return false; - } - ofs << model_str << std::endl; - ofs.close(); return true; } @@ -185,26 +167,11 @@ bool Somas::SaveSomasResult(const session::KernelGraph *graph) { std::string filename = save_graphs_path_ + "/somas_meta/" + "somas_graph" + std::to_string(graph->graph_id()) + "_" + hash_id_ + ".json"; - if (filename.size() > PATH_MAX) { - MS_LOG(WARNING) << "File path " << filename << " is too long."; - return false; + std::ofstream ofs; + if (Common::OpenFile(filename, ofs)) { + ofs << somas_json.dump() << std::endl; + ofs.close(); } - auto real_path = Common::GetRealPath(filename); - if (!real_path.has_value()) { - MS_LOG(WARNING) << "Get real path failed. path=" << filename; - return false; - } - - ChangeFileMode(real_path.value(), S_IRWXU); - std::ofstream ofs(real_path.value()); - - if (!ofs.is_open()) { - MS_LOG(WARNING) << "Open file '" << real_path.value() << "' failed!"; - return false; - } - - ofs << somas_json.dump() << std::endl; - ofs.close(); return true; } @@ -225,7 +192,7 @@ bool Somas::LoadSomasResult(const session::KernelGraph *graph, const string file } catch (std::exception &e) { MS_LOG(WARNING) << "Parse json file error: " << filename << ", sleep 500ms and retry again."; somas_json_fs.close(); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); + std::this_thread::sleep_for(std::chrono::milliseconds(kRetryIntervalSeconds)); std::ifstream retry_tmp(filename); if (!retry_tmp.is_open()) { MS_LOG(INFO) << "Open json file: " << filename << " error, please check kernel_meta."; @@ -544,7 +511,7 @@ void Somas::InitCommonNodeInputs(bool is_all_nop_node, const CNodePtr &kernel) { if ((op_name == kDynamicRNNOpName || op_name == kDynamicGRUV2OpName) && input_origin_type == kMetaTypeNone) { continue; } - auto parameter = GetSomasParameters(prenode_index.first, prenode_index.second); + auto parameter = GetSomasParameter(prenode_index.first, prenode_index.second); node->input_parameters_map_[real_input_index] = parameter; real_input_index++; MS_LOG(DEBUG) << "Input [" << prenode_index.first->fullname_with_scope() << "] is not a real cnode kernel."; @@ -644,7 +611,7 @@ void Somas::InitAtomicCleanInputs(bool enable_fusion_clear, const CNodePtr &kern } } -SomasParameterPtr Somas::CreateSomasParameters(AnfNodePtr node, size_t index) { +SomasParameterPtr Somas::CreateSomasParameter(AnfNodePtr node, size_t index) { auto id = parameters_list_.size(); auto device_addr = AnfAlgo::GetOutputAddr(node, index); if (device_addr == nullptr) { @@ -656,7 +623,7 @@ SomasParameterPtr Somas::CreateSomasParameters(AnfNodePtr node, size_t index) { return param; } -SomasParameterPtr Somas::GetSomasParameters(AnfNodePtr node, size_t index) { +SomasParameterPtr Somas::GetSomasParameter(AnfNodePtr node, size_t index) { auto key = node.get(); auto iter = parameters_map_.find(key); if (iter != parameters_map_.end()) { @@ -665,14 +632,14 @@ SomasParameterPtr Somas::GetSomasParameters(AnfNodePtr node, size_t index) { if (it != iter->second.end()) { return *it; } else { - auto new_param = CreateSomasParameters(node, index); + auto new_param = CreateSomasParameter(node, index); iter->second.push_back(new_param); return new_param; } } else { - auto new_param = CreateSomasParameters(node, index); - parameters_map_[key].push_back(new_param); - return new_param; + auto param = CreateSomasParameter(node, index); + parameters_map_[key].push_back(param); + return param; } } @@ -1304,15 +1271,15 @@ std::map Somas::GetRefTensorsInContiguousList() { size_t contiguous_in_ref_list = std::count_if(ref_node_list.begin(), ref_node_list.end(), [this](size_t tid) { return tensors_map_[tid]->contiguous_; }); // Keep info about contiguous and check for errors - if (ref_node_list.size() > 2 && contiguous_in_ref_list > 0) { + if (ref_node_list.size() > kRefNodeTensorNum && contiguous_in_ref_list > 0) { MS_LOG(WARNING) << "Ref node of size greater than two with at least one contiguous tensor in"; } - if (ref_node_list.size() == 2 && contiguous_in_ref_list == 1) { + if (ref_node_list.size() == kRefNodeTensorNum && contiguous_in_ref_list == 1) { MS_LOG(WARNING) << "Ref node of size two with only one contiguous tensor" << ref_node_list[0] << ":" << tensors_map_[ref_node_list[0]]->contiguous_ << ", " << ref_node_list[1] << ":" << tensors_map_[ref_node_list[1]]->contiguous_; } - if (ref_node_list.size() == 2 && contiguous_in_ref_list == 2) { + if (ref_node_list.size() == kRefNodeTensorNum && contiguous_in_ref_list == kRefNodeTensorNum) { ref_tensors_in_contiguous_map[ref_node_list[0]] = ref_node_list[1]; } } @@ -1389,12 +1356,12 @@ void Somas::UpdateRefTensorsConflict() { } std::string Somas::GetSplitName(const std::string &scope_name) const { - auto indx = scope_name.rfind('/'); - if (indx == std::string::npos) { + auto index = scope_name.rfind('/'); + if (index == std::string::npos) { return scope_name; } else { - if (indx < scope_name.size() - 1) { - auto split_name = scope_name.substr(indx + 1); + if (index < scope_name.size() - 1) { + auto split_name = scope_name.substr(index + 1); return split_name; } return scope_name; @@ -1517,26 +1484,11 @@ void Somas::DumpParameters(std::ostringstream &oss) const { } void Somas::DumpSomasInfoIR(const string filename) const { - if (filename.size() > PATH_MAX) { - MS_LOG(ERROR) << "File path " << filename << " is too long."; - return; + std::ofstream ofs; + if (Common::OpenFile(filename, ofs)) { + ofs << SomasInfo(); + ofs.close(); } - auto real_path = Common::GetRealPath(filename); - if (!real_path.has_value()) { - MS_LOG(ERROR) << "Get real path failed. path=" << filename; - return; - } - - ChangeFileMode(real_path.value(), S_IRWXU); - std::ofstream ofs(real_path.value()); - - if (!ofs.is_open()) { - MS_LOG(ERROR) << "Open dump file '" << real_path.value() << "' failed!"; - return; - } - - ofs << SomasInfo(); - ofs.close(); } std::string Somas::Offline() const { @@ -1585,27 +1537,11 @@ std::string Somas::Offline() const { void Somas::DumpOfflineIR(const string filename) const { MS_LOG(INFO) << "Printing somas-log-from-graph log: " << filename; - if (filename.size() > PATH_MAX) { - MS_LOG(ERROR) << "File path " << filename << " is too long."; - return; + std::ofstream ofs; + if (Common::OpenFile(filename, ofs)) { + ofs << Offline(); + ofs.close(); } - - auto real_path = Common::GetRealPath(filename); - if (!real_path.has_value()) { - MS_LOG(ERROR) << "Get real path failed. path=" << filename; - return; - } - - ChangeFileMode(real_path.value(), S_IRWXU); - std::ofstream ofs(real_path.value()); - - if (!ofs.is_open()) { - MS_LOG(ERROR) << "Open dump file '" << real_path.value() << "' failed!"; - return; - } - - ofs << Offline(); - ofs.close(); } std::string Somas::SomasMemory() const { @@ -1623,15 +1559,15 @@ std::string Somas::SomasMemory() const { std::map> mem_list; - for (const auto &tensor : tensors_list_) { - size_t key = tensor->offset_; + for (const auto &output_tensor : tensors_list_) { + size_t key = output_tensor->offset_; auto iter = mem_list.find(key); if (iter == mem_list.end()) { std::map id_tensor_map; - id_tensor_map[tensor->GetId()] = tensor; + id_tensor_map[output_tensor->GetId()] = output_tensor; mem_list[key] = id_tensor_map; } else { - iter->second[tensor->GetId()] = tensor; + iter->second[output_tensor->GetId()] = output_tensor; } } @@ -1652,51 +1588,35 @@ std::string Somas::SomasMemory() const { for (const auto &mem : mem_list) { auto id_tensor_map = mem.second; for (const auto &id_tensor : id_tensor_map) { - auto tensor = id_tensor.second; + auto place_tensor = id_tensor.second; std::string scope_name; size_t src_stm_id = 0xffff; - if (tensor->GetSourceNode() != nullptr) { - scope_name = tensor->GetSourceNode()->scope_full_name_; - src_stm_id = tensor->GetSourceNode()->GetStream()->GetId(); + if (place_tensor->GetSourceNode() != nullptr) { + scope_name = place_tensor->GetSourceNode()->scope_full_name_; + src_stm_id = place_tensor->GetSourceNode()->GetStream()->GetId(); } else { scope_name = "Somas Tensor"; } std::string split_name = GetSplitName(scope_name); - oss << "#" << mem_map[tensor->GetOffset()] << "\t" << tensor->GetOffset() << "\t" - << tensor->GetOffset() + tensor->GetAlignedSize() << "\t%" << tensor->GetId() << "T\t" - << tensor->GetOriginalSize() << "\t" << tensor->GetAlignedSize() << "\t&" - << static_cast(tensor->GetOffset() + mem_base_addr_) << "\t&" - << static_cast(tensor->GetOffset() + mem_base_addr_ + tensor->GetAlignedSize()) << "\t" - << tensor_type_name_map[tensor->type_] << "\t" << split_name << "\tstm" << src_stm_id << "\t" - << tensor->lifetime_.start_ << "\t" << tensor->lifetime_.end_ << "\n"; + oss << "#" << mem_map[place_tensor->GetOffset()] << "\t" << place_tensor->GetOffset() << "\t" + << place_tensor->GetOffset() + place_tensor->GetAlignedSize() << "\t%" << place_tensor->GetId() << "T\t" + << place_tensor->GetOriginalSize() << "\t" << place_tensor->GetAlignedSize() << "\t&" + << static_cast(place_tensor->GetOffset() + mem_base_addr_) << "\t&" + << static_cast(place_tensor->GetOffset() + mem_base_addr_ + place_tensor->GetAlignedSize()) << "\t" + << tensor_type_name_map[place_tensor->type_] << "\t" << split_name << "\tstm" << src_stm_id << "\t" + << place_tensor->lifetime_.start_ << "\t" << place_tensor->lifetime_.end_ << "\n"; } } return oss.str(); } void Somas::DumpSomasMemoryIR(const string filename) const { - if (filename.size() > PATH_MAX) { - MS_LOG(ERROR) << "File path " << filename << " is too long."; - return; + std::ofstream ofs; + if (Common::OpenFile(filename, ofs)) { + ofs << SomasMemory(); + ofs.close(); } - - auto real_path = Common::GetRealPath(filename); - if (!real_path.has_value()) { - MS_LOG(ERROR) << "Get real path failed. path=" << filename; - return; - } - - ChangeFileMode(real_path.value(), S_IRWXU); - std::ofstream ofs(real_path.value()); - - if (!ofs.is_open()) { - MS_LOG(ERROR) << "Open dump file '" << real_path.value() << "' failed!"; - return; - } - - ofs << SomasMemory(); - ofs.close(); } size_t Somas::CalcLowerBound() const { @@ -1822,14 +1742,14 @@ void Somas::ConvertToProfilingNode(uint32_t graph_id) { std::string name = GetSplitName(node->scope_full_name_); node_memory.SetNodeName(name); node_memory.SetNodeId(node->GetId()); - for (const auto &tensor : node->input_tensors_) { - node_memory.AddInputTensorId(tensor->GetId()); + for (const auto &input_tensor : node->input_tensors_) { + node_memory.AddInputTensorId(input_tensor->GetId()); } - for (const auto &tensor : node->output_tensors_) { - node_memory.AddOutputTensorId(tensor->GetId()); + for (const auto &output_tensor : node->output_tensors_) { + node_memory.AddOutputTensorId(output_tensor->GetId()); } - for (const auto &tensor : node->workspace_tensors_) { - node_memory.AddWorkSpaceTensorId(tensor->GetId()); + for (const auto &workspace_tensor : node->workspace_tensors_) { + node_memory.AddWorkSpaceTensorId(workspace_tensor->GetId()); } graph_node->AddNodeMemory(node_memory); } diff --git a/mindspore/ccsrc/backend/optimizer/somas/somas.h b/mindspore/ccsrc/backend/optimizer/somas/somas.h index 47cdba48561..c9f5fe77680 100644 --- a/mindspore/ccsrc/backend/optimizer/somas/somas.h +++ b/mindspore/ccsrc/backend/optimizer/somas/somas.h @@ -129,8 +129,8 @@ class Somas { std::string GetSplitName(const string &scope_name) const; size_t CalcLowerBound() const; void GenGraphStatisticInfo(); - SomasParameterPtr GetSomasParameters(AnfNodePtr node, size_t index); - SomasParameterPtr CreateSomasParameters(AnfNodePtr node, size_t index); + SomasParameterPtr GetSomasParameter(AnfNodePtr node, size_t index); + SomasParameterPtr CreateSomasParameter(AnfNodePtr node, size_t index); void InitCommonNodeInputs(bool is_all_nop_node, const CNodePtr &kernel); void InitAtomicCleanInputs(bool is_all_nop_node, const CNodePtr &kernel); void ComputeOneTensorConflicts(const std::shared_ptr &calc_tensor, @@ -156,6 +156,7 @@ class Somas { bool LoadSomasResult(const session::KernelGraph *graph, const string filename); bool UpdateTensorsOffset(const std::vector &tensors_json); bool CalcSomasModelHash(const session::KernelGraph *graph); + void UpdateInputTensor(SomasNodePtr node, SomasNodePtr pre_somas_node, SomasTensorPtr input_somas_tensor) const; }; using SomasPtr = std::shared_ptr; diff --git a/mindspore/ccsrc/backend/optimizer/somas/somas_solver_alg.cc b/mindspore/ccsrc/backend/optimizer/somas/somas_solver_alg.cc index f39389d71a0..db7c71f3e62 100644 --- a/mindspore/ccsrc/backend/optimizer/somas/somas_solver_alg.cc +++ b/mindspore/ccsrc/backend/optimizer/somas/somas_solver_alg.cc @@ -51,7 +51,7 @@ size_t FootPrint::Result() { std::shared_ptr foot_print = shared_from_this(); size_t upperbound = 0; uint32_t total_footprints = 0; - while (NULL != foot_print) { + while (foot_print != NULL) { foot_print->printStats(); upperbound = foot_print->getOffset(); @@ -249,7 +249,7 @@ bool FastHeuristic::Eval(vector *block_tensors_v, std::shared_ptrNext()) { + if (p->Next() != NULL) { p = p->Next(); } else if (bpushed == false) { // something went wrong MS_LOG(WARNING) << "Could not allocate memory for tensor: " << tensor->index_; diff --git a/mindspore/ccsrc/backend/optimizer/somas/somas_solver_core.cc b/mindspore/ccsrc/backend/optimizer/somas/somas_solver_core.cc index 04b6a8b0dba..a8c11307ba2 100644 --- a/mindspore/ccsrc/backend/optimizer/somas/somas_solver_core.cc +++ b/mindspore/ccsrc/backend/optimizer/somas/somas_solver_core.cc @@ -206,7 +206,7 @@ void SomasSolverCore::BuildBlocks() { pTensor->blocked_ = true; pTensor = pTensor->right_; tensors_block_count++; - } while (NULL != pTensor); + } while (pTensor != NULL); // add to the list this->block_tensors_.emplace_back(bTensor); @@ -355,14 +355,12 @@ size_t SomasSolverCore::FindSolutions() { } void SomasSolverCore::Destroy(std::shared_ptr &pFootprint) { - while (NULL != pFootprint) { - if (NULL != pFootprint->Next()) { + while (pFootprint != NULL) { + if (pFootprint->Next() != NULL) { std::shared_ptr &p = pFootprint; pFootprint = pFootprint->Next(); - // delete p; p = NULL; } else { - // delete pFootprint; pFootprint = NULL; } } diff --git a/mindspore/ccsrc/backend/optimizer/somas/somas_solver_pre.cc b/mindspore/ccsrc/backend/optimizer/somas/somas_solver_pre.cc index ccd2093f53e..565e52f6630 100644 --- a/mindspore/ccsrc/backend/optimizer/somas/somas_solver_pre.cc +++ b/mindspore/ccsrc/backend/optimizer/somas/somas_solver_pre.cc @@ -30,11 +30,11 @@ namespace somas { constexpr auto kSolNumThresholdMultiThread = 8; Status SomasSolverPre::CheckTensors(const TensorsDescMap *pTensors, uint32_t index1, uint32_t index2) { auto tensors = *pTensors; - if (nullptr == tensors[index1]) { + if (tensors[index1] == nullptr) { MS_LOG(WARNING) << "NULL tensor received in continuous constraint (tensor index " << index1 << ")"; return FAILED; } - if (nullptr == tensors[index2]) { + if (tensors[index2] == nullptr) { MS_LOG(WARNING) << "NULL tensor received in continuous constraint (tensor index " << index2 << ")"; return FAILED; } @@ -99,7 +99,7 @@ Status SomasSolverPre::Solving(const session::KernelGraph *graph, TensorsDescMap const std::vector *pConstraints, const vector> &continuous_v, bool bVerifySolution, bool ball, SortingType sorting, FittingType fitting, AlgorithmType algorithm) { - Status retval = SUCCESS; + Status ret = SUCCESS; try { TensorsDescMap &tensors = *ptensors; size_t total_sol = kNumSortingTypes * kNumFittingTypes * kNumAlgorithmTypes; @@ -156,6 +156,7 @@ Status SomasSolverPre::Solving(const session::KernelGraph *graph, TensorsDescMap *(tensor.second.get()) = *(vecTensorsMap[best_sol][tensor.first]); } max_offset_ = best_solver->GetUpperbound(); + constexpr float kFloatPresent = 100.0; MS_LOG(INFO) << "SOMAS SOLVER RESUME:"; MS_LOG(INFO) << "Best Solution:[" << 1 + best_sol << "/" << total_sol << "] "; MS_LOG(INFO) << "Best result:" << best << " Bytes " << (best) / (giga) << " GB (" @@ -166,7 +167,8 @@ Status SomasSolverPre::Solving(const session::KernelGraph *graph, TensorsDescMap MS_LOG(INFO) << "Best sorting strategy: " << sortingNames[best_solver->sort_strategy_]; MS_LOG(INFO) << "Best offset strategy: " << branchingNames[best_solver->branching_strategy_]; MS_LOG(INFO) << "Time elapsed: " << total_time << " ms"; - MS_LOG(INFO) << "Spread:" << static_cast((worst - best) / static_cast(best * 100.0)) << " %%"; + MS_LOG(INFO) << "Spread:" << static_cast((worst - best) / static_cast(best * kFloatPresent)) + << " %%"; } else { if (AddContiguousInfoInMap(continuous_v, ptensors) == FAILED) { return FAILED; @@ -183,21 +185,22 @@ Status SomasSolverPre::Solving(const session::KernelGraph *graph, TensorsDescMap MS_LOG(INFO) << "SomasSolver::Solving RESULT: " << max_offset_ << " (" << max_offset_ / (giga) << " GB)"; } } - auto context_ptr = MsContext::GetInstance(); - MS_EXCEPTION_IF_NULL(context_ptr); - bool save_graphs = context_ptr->get_param(MS_CTX_SAVE_GRAPHS_FLAG); - if (save_graphs) { - Log(graph, tensors, pConstraints, continuous_v); - } + Log(graph, tensors, pConstraints, continuous_v); } catch (const std::exception &e) { MS_LOG(EXCEPTION) << "SomasSolver::Solving FAILED: " << e.what(); - retval = FAILED; + ret = FAILED; } - return retval; + return ret; } void SomasSolverPre::Log(const session::KernelGraph *graph, const TensorsDescMap &tensors, const std::vector *pConstraints, const vector> &continuous_v) { + auto context_ptr = MsContext::GetInstance(); + MS_EXCEPTION_IF_NULL(context_ptr); + bool save_graphs = context_ptr->get_param(MS_CTX_SAVE_GRAPHS_FLAG); + if (!save_graphs) { + return; + } SolverInputLog(graph, tensors, pConstraints, continuous_v); SolverOutputLog(graph, tensors); TensorRelationLog(pConstraints, graph); @@ -247,46 +250,30 @@ void SomasSolverPre::SolverInputLog(const session::KernelGraph *graph, const Ten MS_EXCEPTION_IF_NULL(context_ptr); auto save_graphs_path = context_ptr->get_param(MS_CTX_SAVE_GRAPHS_PATH); std::string filename = save_graphs_path + "/" + "somas_solver_input_" + std::to_string(graph->graph_id()) + ".ir"; - if (filename.size() > PATH_MAX) { - MS_LOG(ERROR) << "File path " << filename << " is too long."; - return; - } - auto real_path = Common::GetRealPath(filename); - if (!real_path.has_value()) { - MS_LOG(ERROR) << "Get real path failed. path=" << filename; - return; - } + std::ofstream ofs; + if (Common::OpenFile(filename, ofs)) { + for (auto &t : tensors) { + ofs << "T " << t.second->index_ << " " << t.second->size_ << " " << t.second->lifelong_ << std::endl; + } - ChangeFileMode(real_path.value(), S_IRWXU); - std::ofstream ofs(real_path.value()); - - if (!ofs.is_open()) { - MS_LOG(ERROR) << "Open log file '" << real_path.value() << "' failed!"; - return; - } - - for (auto &t : tensors) { - ofs << "T " << t.second->index_ << " " << t.second->size_ << " " << t.second->lifelong_ << std::endl; - } - - for (auto &t1 : tensors) { - for (auto &t2 : tensors) { - size_t idx1 = t1.first; - size_t idx2 = t2.first; - if ((idx1 != idx2) && (*pConstraints)[idx1].IsBitTrue(idx2) == false) { - ofs << "C " << idx1 << " " << idx2 << std::endl; + for (auto &t1 : tensors) { + for (auto &t2 : tensors) { + size_t idx1 = t1.first; + size_t idx2 = t2.first; + if ((idx1 != idx2) && (*pConstraints)[idx1].IsBitTrue(idx2) == false) { + ofs << "C " << idx1 << " " << idx2 << std::endl; + } } } - } - for (auto &s : continuous_v) { - ofs << "S"; - for (auto idx : s) { - ofs << " " << idx; + for (auto &s : continuous_v) { + ofs << "S"; + for (auto idx : s) { + ofs << " " << idx; + } + ofs << std::endl; } - ofs << std::endl; + ofs.close(); } - ofs.close(); - MS_LOG(INFO) << "SomasSolver input Log done"; } @@ -315,15 +302,18 @@ void SomasSolverPre::SolverOutputLog(const session::KernelGraph *graph, const Te return; } + constexpr size_t contiguous_left = 1; + constexpr size_t contiguous_mid = 2; + constexpr size_t contiguous_right = 3; for (auto &t : tensors) { SomasSolverTensorDescPtr tensor = t.second; int continuous = 0; if (tensor->left_ == nullptr && tensor->right_ != nullptr) - continuous = 1; + continuous = contiguous_left; else if (tensor->left_ != nullptr && tensor->right_ != nullptr) - continuous = 2; + continuous = contiguous_mid; else if (tensor->left_ != nullptr && tensor->right_ == nullptr) - continuous = 3; + continuous = contiguous_right; const size_t alignment = 512; bool size_aligned = tensor->size_ % alignment == 0; bool offset_aligned = tensor->offset_ % alignment == 0; diff --git a/mindspore/ccsrc/debug/common.cc b/mindspore/ccsrc/debug/common.cc index 05291fc2f2c..37f7001e6c4 100644 --- a/mindspore/ccsrc/debug/common.cc +++ b/mindspore/ccsrc/debug/common.cc @@ -19,10 +19,12 @@ #include #include #include +#include #include "utils/ms_context.h" #include "utils/system/env.h" #include "utils/system/file_system.h" #include "utils/log_adapter.h" +#include "utils/utils.h" namespace mindspore { std::optional Common::GetRealPath(const std::string &input_path) { @@ -287,4 +289,25 @@ std::string Common::AddId(const std::string &filename, const std::string &suffix g_id++; return s.str(); } + +bool Common::OpenFile(const std::string filename, std::ofstream &ofs) { + if (filename.size() > PATH_MAX) { + MS_LOG(ERROR) << "File path " << filename << " is too long."; + return false; + } + auto real_path = GetRealPath(filename); + if (!real_path.has_value()) { + MS_LOG(ERROR) << "Get real path failed. path=" << filename; + return false; + } + + ChangeFileMode(real_path.value(), S_IRUSR | S_IWUSR); + ofs.open(real_path.value()); + + if (!ofs.is_open()) { + MS_LOG(ERROR) << "Open dump file '" << real_path.value() << "' failed!"; + return false; + } + return true; +} } // namespace mindspore diff --git a/mindspore/ccsrc/debug/common.h b/mindspore/ccsrc/debug/common.h index aaa250e7192..dbc951af481 100644 --- a/mindspore/ccsrc/debug/common.h +++ b/mindspore/ccsrc/debug/common.h @@ -38,6 +38,7 @@ class Common { static bool CreateNotExistDirs(const std::string &path); static std::string AddId(const std::string &filename, const std::string &suffix); + static bool OpenFile(const std::string filename, std::ofstream &ofs); private: static bool IsEveryFilenameValid(const std::string &path, size_t length_limit, const std::string &error_message);