diff --git a/mindspore/ccsrc/debug/debugger/debugger.cc b/mindspore/ccsrc/debug/debugger/debugger.cc index 5e9a5b4ae2..56a19566ba 100644 --- a/mindspore/ccsrc/debug/debugger/debugger.cc +++ b/mindspore/ccsrc/debug/debugger/debugger.cc @@ -33,6 +33,7 @@ #include "runtime/device/kernel_runtime.h" #include "debug/data_dump/e2e_dump.h" #include "utils/config_manager.h" +#include "debug/env_config_parser.h" using debugger::Chunk; using debugger::EventReply; @@ -81,10 +82,8 @@ Debugger::Debugger() // configure partial memory reuse partial_memory_ = CheckDebuggerPartialMemoryEnabled(); - auto context_ptr = MsContext::GetInstance(); - MS_EXCEPTION_IF_NULL(context_ptr); // switch memory reuse on or off - context_ptr->set_param(MS_CTX_ENABLE_MEM_REUSE, partial_memory_); + EnvConfigParser::GetInstance().SetSysMemreuse(partial_memory_); // print some message about memory reuse to user if (partial_memory_) { MS_LOG(WARNING) diff --git a/mindspore/ccsrc/debug/env_config_parser.cc b/mindspore/ccsrc/debug/env_config_parser.cc index 8c90e997fe..803ad930f7 100644 --- a/mindspore/ccsrc/debug/env_config_parser.cc +++ b/mindspore/ccsrc/debug/env_config_parser.cc @@ -28,6 +28,8 @@ constexpr auto ENV_RDR_PATH = "MS_RDR_PATH"; constexpr auto KEY_RDR_SETTINGS = "rdr"; constexpr auto KEY_PATH = "path"; constexpr auto KEY_ENABLE = "enable"; +constexpr auto kSysSettings = "sys"; +constexpr auto kMemReuse = "mem_reuse"; } // namespace namespace mindspore { @@ -140,6 +142,7 @@ void EnvConfigParser::ParseFromFile() { // Parse rdr seetings from file ParseRdrSetting(j); + ParseMemReuseSetting(j); ConfigToString(); } @@ -154,6 +157,28 @@ void EnvConfigParser::Parse() { ParseFromFile(); } +void EnvConfigParser::ParseMemReuseSetting(const nlohmann::json &content) { + auto sys_setting = content.find(kSysSettings); + if (sys_setting == content.end()) { + MS_LOG(INFO) << "The '" << kSysSettings << "' not exists. Please check the config file '" << config_file_ + << "' set by 'env_config_path' in context."; + return; + } + auto sys_memreuse = CheckJsonKeyExist(*sys_setting, kSysSettings, kMemReuse); + if (sys_memreuse.has_value()) { + ParseSysMemReuse(**sys_memreuse); + } +} + +void EnvConfigParser::ParseSysMemReuse(const nlohmann::json &content) { + if (!content.is_boolean()) { + MS_LOG(WARNING) << "Json parse failed. 'enable' in " << kSysSettings << " should be boolean." + << " Please check the config file '" << config_file_ << "' set by 'env_config_path' in context."; + return; + } + sys_memreuse_ = content; +} + void EnvConfigParser::ParseRdrSetting(const nlohmann::json &content) { auto rdr_setting = content.find(KEY_RDR_SETTINGS); if (rdr_setting == content.end()) { diff --git a/mindspore/ccsrc/debug/env_config_parser.h b/mindspore/ccsrc/debug/env_config_parser.h index be4f76753a..49bac32216 100644 --- a/mindspore/ccsrc/debug/env_config_parser.h +++ b/mindspore/ccsrc/debug/env_config_parser.h @@ -37,6 +37,8 @@ class EnvConfigParser { bool HasRdrSetting() const { return has_rdr_setting_; } bool RdrEnabled() const { return rdr_enabled_; } std::string RdrPath() const { return rdr_path_; } + bool GetSysMemreuse() { return sys_memreuse_; } + void SetSysMemreuse(bool set_memreuse) { sys_memreuse_ = set_memreuse; } private: EnvConfigParser() {} @@ -50,6 +52,7 @@ class EnvConfigParser { bool has_rdr_setting_{false}; std::string rdr_path_{"./rdr/"}; + bool sys_memreuse_{true}; void ParseFromFile(); void ParseFromEnv(); std::string GetIfstreamString(const std::ifstream &ifstream); @@ -62,6 +65,8 @@ class EnvConfigParser { void ParseRdrPath(const nlohmann::json &content); void ParseRdrEnable(const nlohmann::json &content); + void ParseMemReuseSetting(const nlohmann::json &content); + void ParseSysMemReuse(const nlohmann::json &content); void ConfigToString(); }; diff --git a/mindspore/ccsrc/runtime/device/ascend/ascend_kernel_runtime.cc b/mindspore/ccsrc/runtime/device/ascend/ascend_kernel_runtime.cc index 207260afa2..316845f0aa 100644 --- a/mindspore/ccsrc/runtime/device/ascend/ascend_kernel_runtime.cc +++ b/mindspore/ccsrc/runtime/device/ascend/ascend_kernel_runtime.cc @@ -43,6 +43,7 @@ #include "debug/anf_ir_dump.h" #ifdef MEM_REUSE_DEBUG #include "backend/optimizer/mem_reuse/mem_reuse_checker.h" +#include "debug/env_config_parser.h" #endif #include "runtime/device/ascend/executor/tiling/op_tiling_calculater.h" #include "runtime/device/executor/executor_callback.h" @@ -395,9 +396,7 @@ bool AscendKernelRuntime::GenTask(const session::KernelGraph *graph) { MS_LOG(INFO) << "GenTask start. GraphId:" << graph->graph_id(); DumpJsonParser::GetInstance().UpdateNeedDumpKernels(NOT_NULL(graph)); #ifdef MEM_REUSE_DEBUG - auto context_ptr = MsContext::GetInstance(); - MS_EXCEPTION_IF_NULL(context_ptr); - if (!context_ptr->get_param(MS_CTX_ENABLE_MEM_REUSE)) { + if (!EnvConfigParser::GetInstance().GetSysMemreuse()) { // Get normal graph ir for memreuse mindspore::memreuse::MemReuseChecker::GetInstance().CheckNormalIR(graph); } diff --git a/mindspore/ccsrc/runtime/device/cpu/cpu_kernel_runtime.cc b/mindspore/ccsrc/runtime/device/cpu/cpu_kernel_runtime.cc index 4cb5a481a7..954ec8909c 100644 --- a/mindspore/ccsrc/runtime/device/cpu/cpu_kernel_runtime.cc +++ b/mindspore/ccsrc/runtime/device/cpu/cpu_kernel_runtime.cc @@ -35,6 +35,7 @@ #include "utils/profile.h" #include "utils/trace_base.h" #include "debug/data_dump/cpu_e2e_dump.h" +#include "debug/env_config_parser.h" #ifdef MEM_REUSE_DEBUG #include "backend/optimizer/mem_reuse/mem_reuse_checker.h" #endif @@ -59,7 +60,7 @@ void CPUKernelRuntime::AssignKernelAddress(session::KernelGraph *kernel_graph) { AssignInputNodeAddress(kernel_graph); auto context_ptr = MsContext::GetInstance(); MS_EXCEPTION_IF_NULL(context_ptr); - bool is_enable_mem_reuse = context_ptr->get_param(MS_CTX_ENABLE_MEM_REUSE); + bool is_enable_mem_reuse = EnvConfigParser::GetInstance().GetSysMemreuse(); if (context_ptr->get_param(MS_CTX_EXECUTION_MODE) == kPynativeMode) { // disable mem reuse for kPynativeMode is_enable_mem_reuse = false; diff --git a/mindspore/ccsrc/runtime/device/kernel_runtime.cc b/mindspore/ccsrc/runtime/device/kernel_runtime.cc index afb8181bab..72e246e39b 100644 --- a/mindspore/ccsrc/runtime/device/kernel_runtime.cc +++ b/mindspore/ccsrc/runtime/device/kernel_runtime.cc @@ -31,6 +31,7 @@ #include "utils/shape_utils.h" #include "utils/utils.h" #include "frontend/parallel/context.h" +#include "debug/env_config_parser.h" #if (ENABLE_CPU && (ENABLE_D || ENABLE_GPU)) #include "ps/ps_cache/ps_cache_manager.h" #endif @@ -717,11 +718,11 @@ void KernelRuntime::AssignDynamicMemory(session::KernelGraph *graph) { MS_EXCEPTION_IF_NULL(mem_manager_); auto context_ptr = MsContext::GetInstance(); MS_EXCEPTION_IF_NULL(context_ptr); - bool is_enable_mem_reuse = context_ptr->get_param(MS_CTX_ENABLE_MEM_REUSE); + bool is_enable_mem_reuse = EnvConfigParser::GetInstance().GetSysMemreuse(); auto mem_type = kDynamicMem; auto &dump_json_parser = DumpJsonParser::GetInstance(); if (dump_json_parser.e2e_dump_enabled() && dump_json_parser.dump_mode() == 0) { - context_ptr->set_param(MS_CTX_ENABLE_MEM_REUSE, false); + EnvConfigParser::GetInstance().SetSysMemreuse(false); is_enable_mem_reuse = false; MS_LOG(INFO) << "Disable Memory Reuse when e2e dump is enable and dump mode is set to dump all kernels"; }