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 10 kB

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