diff --git a/mindspore/ccsrc/profiler/device/common/memory_profiling.cc b/mindspore/ccsrc/profiler/device/common/memory_profiling.cc index c467915fcc..3ebe30e103 100644 --- a/mindspore/ccsrc/profiler/device/common/memory_profiling.cc +++ b/mindspore/ccsrc/profiler/device/common/memory_profiling.cc @@ -19,10 +19,14 @@ #include #include "utils/log_adapter.h" #include "utils/ms_context.h" +#include "utils/ms_utils.h" +#include "nlohmann/json.hpp" namespace mindspore { namespace profiler { +constexpr char kOutputPath[] = "result_path"; + std::shared_ptr MemoryProfiling::AddGraphMemoryNode(uint32_t graph_id) { std::shared_ptr node = std::make_shared(graph_id); graph_memory_[graph_id] = node; @@ -76,10 +80,45 @@ void MemoryProfiling::MemoryToPB() { return; } +std::string MemoryProfiling::GetOutputPath() const { + auto context = MsContext::GetInstance(); + MS_EXCEPTION_IF_NULL(context); + const std::string options_str = context->get_param(MS_CTX_PROFILING_OPTIONS); + nlohmann::json options_json; + try { + options_json = nlohmann::json::parse(options_str); + } catch (nlohmann::json::parse_error &e) { + MS_LOG(EXCEPTION) << "Parse profiling option json failed, error:" << e.what(); + } + auto iter = options_json.find(kOutputPath); + if (iter != options_json.end() && iter->is_string()) { + char real_path[PATH_MAX] = {0}; + if ((*iter).size() >= PATH_MAX) { + MS_LOG(ERROR) << "Path is invalid for memory profiling."; + return ""; + } +#if defined(_WIN32) || defined(_WIN64) + if (_fullpath(real_path, common::SafeCStr(*iter), PATH_MAX) == nullptr) { + MS_LOG(ERROR) << "Path is invalid for memory profiling."; + return ""; + } +#else + if (realpath(common::SafeCStr(*iter), real_path) == nullptr) { + MS_LOG(ERROR) << "Path is invalid for memory profiling."; + return ""; + } +#endif + return real_path; + } + + MS_LOG(ERROR) << "Output path is not found when save memory profiling data"; + return ""; +} + void MemoryProfiling::SaveMemoryProfiling() { auto context = MsContext::GetInstance(); MS_EXCEPTION_IF_NULL(context); - std::string dir_path = context->get_param(MS_CTX_PROFILING_DIR_PATH); + std::string dir_path = GetOutputPath(); auto device_id = context->get_param(MS_CTX_DEVICE_ID); std::string file = dir_path + std::string("/memory_usage_") + std::to_string(device_id) + std::string(".pb"); diff --git a/mindspore/ccsrc/profiler/device/common/memory_profiling.h b/mindspore/ccsrc/profiler/device/common/memory_profiling.h index 0e5470fd3e..aa3642ce4b 100644 --- a/mindspore/ccsrc/profiler/device/common/memory_profiling.h +++ b/mindspore/ccsrc/profiler/device/common/memory_profiling.h @@ -115,6 +115,8 @@ class MemoryProfiling { void SaveMemoryProfiling(); private: + std::string GetOutputPath() const; + MemoryProto memory_proto_; std::map> graph_memory_; uint64_t device_mem_size_; diff --git a/mindspore/ccsrc/pybind_api/utils/ms_context_py.cc b/mindspore/ccsrc/pybind_api/utils/ms_context_py.cc index 608908dac2..821b58958e 100644 --- a/mindspore/ccsrc/pybind_api/utils/ms_context_py.cc +++ b/mindspore/ccsrc/pybind_api/utils/ms_context_py.cc @@ -95,7 +95,6 @@ REGISTER_PYBIND_DEFINE(MsContextPy, ([](const py::module *m) { .value("variable_memory_max_size", MsCtxParam::MS_CTX_VARIABLE_MEMORY_MAX_SIZE) .value("device_id", MsCtxParam::MS_CTX_DEVICE_ID) .value("max_call_depth", MsCtxParam::MS_CTX_MAX_CALL_DEPTH) - .value("profiling_dir_path", MsCtxParam::MS_CTX_PROFILING_DIR_PATH) .value("env_config_path", MsCtxParam::MS_CTX_ENV_CONFIG_PATH); (void)py::class_>(*m, "MSContext") .def_static("get_instance", &mindspore::MsContext::GetInstance, "Get ms context instance.") diff --git a/mindspore/core/utils/ms_context.cc b/mindspore/core/utils/ms_context.cc index a2328059c3..7d3afd2310 100644 --- a/mindspore/core/utils/ms_context.cc +++ b/mindspore/core/utils/ms_context.cc @@ -74,7 +74,6 @@ MsContext::MsContext(const std::string &policy, const std::string &target) { set_param(MS_CTX_ENABLE_GRAPH_KERNEL, false); set_param(MS_CTX_ENABLE_SPARSE, false); set_param(MS_CTX_ENABLE_PARALLEL_SPLIT, false); - set_param(MS_CTX_PROFILING_DIR_PATH, ""); backend_policy_ = policy_map_[policy]; } diff --git a/mindspore/core/utils/ms_context.h b/mindspore/core/utils/ms_context.h index f82e787685..3beeabeb92 100644 --- a/mindspore/core/utils/ms_context.h +++ b/mindspore/core/utils/ms_context.h @@ -106,7 +106,6 @@ enum MsCtxParam : unsigned { MS_CTX_SAVE_GRAPHS_PATH, MS_CTX_VARIABLE_MEMORY_MAX_SIZE, MS_CTX_PYTHON_EXE_PATH, - MS_CTX_PROFILING_DIR_PATH, MS_CTX_ENV_CONFIG_PATH, MS_CTX_TYPE_STRING_END, diff --git a/mindspore/profiler/profiling.py b/mindspore/profiler/profiling.py index 8f65874ea9..fef25645e8 100644 --- a/mindspore/profiler/profiling.py +++ b/mindspore/profiler/profiling.py @@ -143,8 +143,7 @@ class Profiler: logger.error(msg) raise ValueError(msg) # use context interface to open profiling, for the new mindspore version(after 2020.5.21) - context.set_context(enable_profiling=True, profiling_options=profiling_options, - profiling_dir_path=self._output_path) + context.set_context(enable_profiling=True, profiling_options=profiling_options) base_profiling_container_path = os.path.join(self._output_path, "container") container_path = os.path.join(base_profiling_container_path, self._dev_id) data_path = os.path.join(container_path, "data")