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.

graph_kernel_flags.cc 9.3 kB

4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 "utils/context/graph_kernel_flags.h"
  17. #include <map>
  18. #include <string>
  19. #include <cstring>
  20. #include <vector>
  21. #include <utility>
  22. #include "nlohmann/json.hpp"
  23. #include "utils/ms_context.h"
  24. namespace mindspore::graphkernel {
  25. namespace {
  26. // Split string to tokens
  27. std::vector<std::string> GetTokens(const std::string &str, const std::string &delim) {
  28. std::vector<std::string> tokens;
  29. std::vector<char> c_str(str.begin(), str.end());
  30. c_str.push_back('\0');
  31. char *saveptr = nullptr;
  32. char *pch = strtok_r(&c_str[0], delim.c_str(), &saveptr);
  33. while (pch != nullptr) {
  34. tokens.emplace_back(pch);
  35. pch = strtok_r(nullptr, delim.c_str(), &saveptr);
  36. }
  37. return tokens;
  38. }
  39. // Parse flag string to key-value pair.
  40. // Flag format: "--key=value", bool flag's value can be implicit, the "--key" means "--key=true"
  41. std::pair<std::string, std::string> ParseFlag(const std::string &flag) {
  42. auto i = flag.find("--");
  43. // check the string starts with "--".
  44. constexpr size_t leading_size = 2;
  45. if (flag.size() <= leading_size || i != 0) {
  46. return std::pair<std::string, std::string>();
  47. }
  48. i += leading_size;
  49. auto j = flag.find('=', i + 1); // the key should not be empty, "--=" is invalid
  50. if (j >= flag.size()) {
  51. // no value, treated as bool flag.
  52. return std::make_pair(flag.substr(i), "");
  53. } else if (j + 1 < flag.size() && flag.find('=', j + 1) == std::string::npos) {
  54. // normal "--key=value" format
  55. return std::make_pair(flag.substr(i, j - i), flag.substr(j + 1));
  56. }
  57. // string with two "=" is invalid.
  58. return std::pair<std::string, std::string>();
  59. }
  60. std::map<std::string, std::string> ParseFlags(const std::string &flags) {
  61. std::map<std::string, std::string> flag_map;
  62. auto tokens = GetTokens(flags, " ");
  63. for (const auto &token : tokens) {
  64. auto flag = ParseFlag(token);
  65. if (flag.first != "") {
  66. if (!flag_map.insert(flag).second) {
  67. MS_LOG(WARNING) << "Repeated GraphKernel flag: " << flag.first;
  68. }
  69. } else {
  70. MS_LOG(WARNING) << "Invalid GraphKernel flag: " << token;
  71. }
  72. }
  73. return flag_map;
  74. }
  75. class FlagRegister {
  76. public:
  77. explicit FlagRegister(std::map<std::string, std::string> *flag_map) : flag_map_(*flag_map) {}
  78. ~FlagRegister() = default;
  79. template <typename T>
  80. void AddFlag(const std::string &flag_name, T *const flag_var, T default_value = T()) const {
  81. auto iter = flag_map_.find(flag_name);
  82. if (iter != flag_map_.end()) {
  83. T var;
  84. bool ret = ParseValue(iter->second, &var);
  85. if (ret) {
  86. *flag_var = std::move(var);
  87. } else {
  88. *flag_var = std::move(default_value);
  89. if (iter->second.empty()) {
  90. MS_LOG(WARNING) << "Invalid GraphKernel flag: --" << iter->first;
  91. } else {
  92. MS_LOG(WARNING) << "Invalid GraphKernel flag: --" << iter->first << "=" << iter->second;
  93. }
  94. }
  95. flag_map_.erase(iter);
  96. } else {
  97. *flag_var = std::move(default_value);
  98. }
  99. }
  100. private:
  101. bool ParseValue(const std::string &s, std::vector<std::string> *result) const {
  102. *result = GetTokens(s, ",");
  103. return !result->empty();
  104. }
  105. bool ParseValue(const std::string &s, bool *result) const {
  106. *result = (s.empty() || s == "true" || s == "on" || s == "1");
  107. return *result || s == "false" || s == "off" || s == "0";
  108. }
  109. template <typename T>
  110. bool ParseValue(const std::string &s, T *result) const {
  111. if (s.empty()) {
  112. return false;
  113. }
  114. std::istringstream iss(s);
  115. iss >> (*result);
  116. return iss.eof();
  117. }
  118. template <typename T>
  119. bool ParseValue(const std::string &s, std::vector<T> *result) const {
  120. result->clear();
  121. auto tokens = GetTokens(s, ",");
  122. if (tokens.empty()) {
  123. return false;
  124. }
  125. for (const auto &tok : tokens) {
  126. T temp;
  127. if (!ParseValue(tok, &temp)) {
  128. result->clear();
  129. return false;
  130. }
  131. result->emplace_back(temp);
  132. }
  133. return true;
  134. }
  135. std::map<std::string, std::string> &flag_map_;
  136. };
  137. } // namespace
  138. std::pair<std::string, bool> GraphKernelFlags::GetGraphKernelContext() {
  139. // Use the environment variable in priority and not in lite
  140. auto env_flags = std::getenv("MS_GRAPH_KERNEL_FLAGS");
  141. std::string flags = env_flags ? std::string(env_flags) : "";
  142. bool enable_context{false};
  143. #ifndef MSLITE_ENABLE_GRAPH_KERNEL
  144. auto context = MsContext::GetInstance();
  145. MS_EXCEPTION_IF_NULL(context);
  146. if (flags.empty()) {
  147. flags = context->get_param<std::string>(MS_CTX_GRAPH_KERNEL_FLAGS);
  148. }
  149. enable_context = context->get_param<bool>(MS_CTX_ENABLE_GRAPH_KERNEL);
  150. #endif
  151. return std::make_pair(flags, enable_context);
  152. }
  153. void GraphKernelFlags::Refresh() {
  154. auto flag_map = ParseFlags(flags_cache_);
  155. RegisterFlags(&flag_map);
  156. for (auto &item : flag_map) {
  157. MS_LOG(WARNING) << "Unknown GraphKernel flag: " << item.first;
  158. }
  159. #ifndef MSLITE_ENABLE_GRAPH_KERNEL
  160. if (IsEnableGraphKernel()) {
  161. auto context = MsContext::GetInstance();
  162. MS_EXCEPTION_IF_NULL(context);
  163. if (context->get_param<int>(MS_CTX_EXECUTION_MODE) != kGraphMode) {
  164. MS_LOG(WARNING) << "GraphKernel only support GRAPH_MODE";
  165. opt_level = OptLevel_0;
  166. }
  167. }
  168. #endif
  169. // Dump flags so that people can check the setting.
  170. MS_LOG(INFO) << "graph_kernel_flags = \"" << flags_cache_ << "\", all flags: " << DumpAllFlags();
  171. }
  172. void GraphKernelFlags::RegisterFlags(std::map<std::string, std::string> *flag_map) {
  173. FlagRegister reg(flag_map);
  174. bool is_ascend{false};
  175. #ifndef MSLITE_ENABLE_GRAPH_KERNEL
  176. auto context_ptr = MsContext::GetInstance();
  177. MS_EXCEPTION_IF_NULL(context_ptr);
  178. is_ascend = (context_ptr->get_param<std::string>(MS_CTX_DEVICE_TARGET) == kAscendDevice);
  179. #endif
  180. // Set opt_level first, some flags' default value depends on it.
  181. // Default optimization level is level 2 when enable graphkernel
  182. reg.AddFlag("opt_level", &opt_level, enable_graph_kernel_ ? OptLevel_2 : OptLevel_0);
  183. if (opt_level > OptLevel_3) {
  184. MS_LOG(WARNING) << "GraphKernelFlag: opt_level should be in the range [0,3] but got " << opt_level;
  185. opt_level = OptLevel_3;
  186. }
  187. // Boolean flags
  188. reg.AddFlag("dump_as_text", &dump_as_text);
  189. reg.AddFlag("enable_stitch_fusion", &enable_stitch_fusion, opt_level == OptLevel_3);
  190. reg.AddFlag("enable_recompute_fusion", &enable_recompute_fusion, opt_level >= OptLevel_2);
  191. reg.AddFlag("enable_parallel_fusion", &enable_parallel_fusion, opt_level == OptLevel_3);
  192. reg.AddFlag("enable_low_precision", &enable_low_precision);
  193. reg.AddFlag("enable_trans_op_optimize", &enable_trans_op_optimize);
  194. // Integer flags
  195. reg.AddFlag("online_tuning", &online_tuning);
  196. reg.AddFlag("fusion_ops_level", &fusion_ops_level, is_ascend ? OpLevel_0 : OpLevel_MAX);
  197. reg.AddFlag("parallel_ops_level", &parallel_ops_level);
  198. // String flags
  199. reg.AddFlag("repository_path", &repository_path);
  200. // String list flags
  201. reg.AddFlag("enable_expand_ops", &enable_expand_ops);
  202. reg.AddFlag("enable_expand_ops_only", &enable_expand_ops_only);
  203. reg.AddFlag("disable_expand_ops", &disable_expand_ops);
  204. reg.AddFlag("enable_cluster_ops", &enable_cluster_ops);
  205. reg.AddFlag("enable_cluster_ops_only", &enable_cluster_ops_only);
  206. reg.AddFlag("disable_cluster_ops", &disable_cluster_ops);
  207. reg.AddFlag("enable_simplify_exprs_only", &enable_simplify_exprs_only);
  208. reg.AddFlag("disable_simplify_exprs", &disable_simplify_exprs);
  209. reg.AddFlag("enable_pass", &enable_pass);
  210. reg.AddFlag("disable_pass", &disable_pass);
  211. }
  212. std::string GraphKernelFlags::DumpAllFlags() const {
  213. nlohmann::json json;
  214. json["dump_as_text"] = dump_as_text;
  215. json["enable_stitch_fusion"] = enable_stitch_fusion;
  216. json["enable_recompute_fusion"] = enable_recompute_fusion;
  217. json["enable_parallel_fusion"] = enable_parallel_fusion;
  218. json["enable_low_precision"] = enable_low_precision;
  219. json["enable_trans_op_optimize"] = enable_trans_op_optimize;
  220. json["opt_level"] = opt_level;
  221. json["fusion_ops_level"] = fusion_ops_level;
  222. json["parallel_ops_level"] = parallel_ops_level;
  223. json["online_tuning"] = online_tuning;
  224. json["repository_path"] = repository_path;
  225. json["enable_expand_ops"] = enable_expand_ops;
  226. json["enable_expand_ops_only"] = enable_expand_ops_only;
  227. json["disable_expand_ops"] = disable_expand_ops;
  228. json["enable_cluster_ops"] = enable_cluster_ops;
  229. json["enable_cluster_ops_only"] = enable_cluster_ops_only;
  230. json["disable_cluster_ops"] = disable_cluster_ops;
  231. json["enable_simplify_exprs_only"] = enable_simplify_exprs_only;
  232. json["disable_simplify_exprs"] = disable_simplify_exprs;
  233. json["enable_pass"] = enable_pass;
  234. json["disable_pass"] = disable_pass;
  235. return json.dump();
  236. }
  237. } // namespace mindspore::graphkernel