You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

env_config_parser.cc 8.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /**
  2. * Copyright 2021 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. #include "debug/env_config_parser.h"
  17. #include <algorithm>
  18. #include <fstream>
  19. #include "nlohmann/json.hpp"
  20. #include "utils/log_adapter.h"
  21. #include "debug/common.h"
  22. #include "utils/ms_context.h"
  23. #include "utils/convert_utils_base.h"
  24. namespace {
  25. #ifdef ENABLE_DUMP_IR
  26. constexpr auto ENV_RDR_ENABLE = "MS_RDR_ENABLE";
  27. constexpr auto ENV_RDR_PATH = "MS_RDR_PATH";
  28. constexpr auto KEY_RDR_SETTINGS = "rdr";
  29. constexpr auto KEY_PATH = "path";
  30. constexpr auto KEY_ENABLE = "enable";
  31. #endif
  32. constexpr auto KEY_MEM_REUSE_SETTINGS = "sys";
  33. constexpr auto KEY_MEM_REUSE = "mem_reuse";
  34. } // namespace
  35. namespace mindspore {
  36. #ifdef ENABLE_DUMP_IR
  37. std::optional<bool> GetRdrEnableFromEnv() {
  38. // get environment variable to configure RDR
  39. std::string env_enable_str = common::GetEnv(ENV_RDR_ENABLE);
  40. if (!env_enable_str.empty()) {
  41. (void)std::transform(env_enable_str.begin(), env_enable_str.end(), env_enable_str.begin(), ::tolower);
  42. if (env_enable_str != "0" && env_enable_str != "1") {
  43. MS_LOG(WARNING) << "The environment variable '" << ENV_RDR_ENABLE << "' should be 0 or 1.";
  44. }
  45. if (env_enable_str == "1") {
  46. return true;
  47. }
  48. return false;
  49. }
  50. return std::nullopt;
  51. }
  52. std::optional<std::string> GetRdrPathFromEnv() {
  53. // get environment variable to configure RDR
  54. std::string path = common::GetEnv(ENV_RDR_PATH);
  55. if (!path.empty()) {
  56. std::string err_msg = "RDR path parse from environment variable failed. Please check the settings about '" +
  57. std::string(ENV_RDR_PATH) + "' in environment variables.";
  58. if (!Common::IsPathValid(path, MAX_DIRECTORY_LENGTH, err_msg)) {
  59. return std::string("");
  60. }
  61. return path;
  62. }
  63. return std::nullopt;
  64. }
  65. #endif
  66. bool EnvConfigParser::CheckJsonStringType(const nlohmann::json &content, const std::string &setting_key,
  67. const std::string &key) const {
  68. if (!content.is_string()) {
  69. MS_LOG(WARNING) << "Json Parse Failed. The '" << key << "' in '" << setting_key << "' should be string."
  70. << " Please check the config file '" << config_file_ << "' set by 'env_config_path' in context.";
  71. return false;
  72. }
  73. return true;
  74. }
  75. std::optional<nlohmann::detail::iter_impl<const nlohmann::json>> EnvConfigParser::CheckJsonKeyExist(
  76. const nlohmann::json &content, const std::string &setting_key, const std::string &key) const {
  77. auto iter = content.find(key);
  78. if (iter == content.end()) {
  79. MS_LOG(WARNING) << "Check json failed, '" << key << "' not found in '" << setting_key << "'."
  80. << " Please check the config file '" << config_file_ << "' set by 'env_config_path' in context.";
  81. return std::nullopt;
  82. }
  83. return iter;
  84. }
  85. std::string EnvConfigParser::GetIfstreamString(const std::ifstream &ifstream) const {
  86. std::stringstream buffer;
  87. buffer << ifstream.rdbuf();
  88. return buffer.str();
  89. }
  90. void EnvConfigParser::ParseFromEnv() {
  91. #ifdef ENABLE_DUMP_IR
  92. // Get RDR seetings from environment variables
  93. auto rdr_enable_env = GetRdrEnableFromEnv();
  94. if (rdr_enable_env.has_value()) {
  95. has_rdr_setting_ = true;
  96. rdr_enabled_ = rdr_enable_env.value();
  97. }
  98. auto path_env = GetRdrPathFromEnv();
  99. if (path_env.has_value()) {
  100. has_rdr_setting_ = true;
  101. std::string path = path_env.value();
  102. if (!path.empty()) {
  103. if (path.back() != '/') {
  104. path += '/';
  105. }
  106. rdr_path_ = path;
  107. }
  108. }
  109. #endif
  110. }
  111. void EnvConfigParser::ParseFromFile() {
  112. auto context = MsContext::GetInstance();
  113. MS_EXCEPTION_IF_NULL(context);
  114. auto config_file = context->get_param<std::string>(MS_CTX_ENV_CONFIG_PATH);
  115. if (config_file.empty()) {
  116. MS_LOG(INFO) << "The 'env_config_path' in 'mindspore.context.set_context(env_config_path={path})' is empty.";
  117. return;
  118. }
  119. config_file_ = config_file;
  120. std::ifstream json_file(config_file_);
  121. if (!json_file.is_open()) {
  122. MS_LOG(WARNING) << "Env config file:" << config_file_ << " open failed."
  123. << " Please check the config file '" << config_file_ << "' set by 'env_config_path' in context."
  124. << " Errno:" << errno << " ErrInfo:" << strerror(errno);
  125. return;
  126. }
  127. nlohmann::json j;
  128. try {
  129. json_file >> j;
  130. } catch (nlohmann::json::parse_error &e) {
  131. MS_LOG(WARNING) << "Env config json contents '" << GetIfstreamString(json_file) << "' in config file '"
  132. << config_file_ << "' set by 'env_config_path' in context.";
  133. return;
  134. }
  135. // convert json to string
  136. std::stringstream ss;
  137. ss << j;
  138. std::string cfg = ss.str();
  139. MS_LOG(INFO) << "Env config json:" << cfg;
  140. #ifdef ENABLE_DUMP_IR
  141. ParseRdrSetting(j);
  142. #endif
  143. ParseMemReuseSetting(j);
  144. ConfigToString();
  145. }
  146. void EnvConfigParser::Parse() {
  147. std::lock_guard<std::mutex> guard(lock_);
  148. if (already_parsed_) {
  149. return;
  150. }
  151. already_parsed_ = true;
  152. ParseFromEnv();
  153. ParseFromFile();
  154. }
  155. void EnvConfigParser::ParseMemReuseSetting(const nlohmann::json &content) {
  156. auto sys_setting = content.find(KEY_MEM_REUSE_SETTINGS);
  157. if (sys_setting == content.end()) {
  158. MS_LOG(INFO) << "The '" << KEY_MEM_REUSE_SETTINGS << "' isn't existed. Please check the config file '"
  159. << config_file_ << "' set by 'env_config_path' in context.";
  160. return;
  161. }
  162. auto sys_memreuse = CheckJsonKeyExist(*sys_setting, KEY_MEM_REUSE_SETTINGS, KEY_MEM_REUSE);
  163. if (sys_memreuse.has_value()) {
  164. ParseSysMemReuse(**sys_memreuse);
  165. }
  166. }
  167. void EnvConfigParser::ParseSysMemReuse(const nlohmann::json &content) {
  168. if (!content.is_boolean()) {
  169. MS_LOG(INFO) << "the json object parses failed. 'enable' in " << KEY_MEM_REUSE_SETTINGS << " should be boolean."
  170. << " Please check the config file '" << config_file_ << "' set by 'env_config_path' in context.";
  171. return;
  172. }
  173. sys_memreuse_ = content;
  174. }
  175. #ifdef ENABLE_DUMP_IR
  176. void EnvConfigParser::ParseRdrSetting(const nlohmann::json &content) {
  177. auto rdr_setting = content.find(KEY_RDR_SETTINGS);
  178. if (rdr_setting == content.end()) {
  179. MS_LOG(WARNING) << "The '" << KEY_RDR_SETTINGS << "' not exists. Please check the config file '" << config_file_
  180. << "' set by 'env_config_path' in context.";
  181. return;
  182. }
  183. has_rdr_setting_ = true;
  184. auto rdr_enable = CheckJsonKeyExist(*rdr_setting, KEY_RDR_SETTINGS, KEY_ENABLE);
  185. if (rdr_enable.has_value()) {
  186. ParseRdrEnable(**rdr_enable);
  187. }
  188. auto rdr_path = CheckJsonKeyExist(*rdr_setting, KEY_RDR_SETTINGS, KEY_PATH);
  189. if (rdr_path.has_value()) {
  190. ParseRdrPath(**rdr_path);
  191. }
  192. }
  193. void EnvConfigParser::ParseRdrPath(const nlohmann::json &content) {
  194. std::string err_msg = "RDR path parse failed. The RDR path will be a default value: '" + rdr_path_ +
  195. "'. Please check the settings about '" + KEY_RDR_SETTINGS + "' in config file '" +
  196. config_file_ + "' set by 'env_config_path' in context.";
  197. if (!CheckJsonStringType(content, KEY_RDR_SETTINGS, KEY_PATH)) {
  198. MS_LOG(WARNING) << err_msg;
  199. return;
  200. }
  201. std::string path = content;
  202. if (!Common::IsPathValid(path, MAX_DIRECTORY_LENGTH, err_msg)) {
  203. return;
  204. }
  205. if (path.back() != '/') {
  206. path += '/';
  207. }
  208. rdr_path_ = path;
  209. }
  210. void EnvConfigParser::ParseRdrEnable(const nlohmann::json &content) {
  211. if (!content.is_boolean()) {
  212. MS_LOG(WARNING) << "Json parse failed. 'enable' in " << KEY_RDR_SETTINGS << " should be boolean."
  213. << " Please check the config file '" << config_file_ << "' set by 'env_config_path' in context.";
  214. return;
  215. }
  216. rdr_enabled_ = content;
  217. }
  218. #endif
  219. void EnvConfigParser::ConfigToString() {
  220. std::string cur_config;
  221. #ifdef ENABLE_DUMP_IR
  222. cur_config.append("After parsed, rdr path: ");
  223. cur_config.append(rdr_path_);
  224. cur_config.append(", rdr_enable: ");
  225. std::string rdr_enable_flag = rdr_enabled_ ? "1" : "0";
  226. (void)cur_config.append(rdr_enable_flag);
  227. #endif
  228. MS_LOG(INFO) << cur_config;
  229. }
  230. } // namespace mindspore