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

4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. auto context = MsContext::GetInstance();
  140. MS_EXCEPTION_IF_NULL(context);
  141. // Use the environment variable in priority
  142. auto env_flags = std::getenv("MS_GRAPH_KERNEL_FLAGS");
  143. std::string flags = env_flags ? std::string(env_flags) : context->get_param<std::string>(MS_CTX_GRAPH_KERNEL_FLAGS);
  144. return std::make_pair(flags, context->get_param<bool>(MS_CTX_ENABLE_GRAPH_KERNEL));
  145. }
  146. void GraphKernelFlags::Refresh() {
  147. auto flag_map = ParseFlags(flags_cache_);
  148. RegisterFlags(&flag_map);
  149. for (auto &item : flag_map) {
  150. MS_LOG(WARNING) << "Unknown GraphKernel flag: " << item.first;
  151. }
  152. if (IsEnableGraphKernel()) {
  153. auto context = MsContext::GetInstance();
  154. MS_EXCEPTION_IF_NULL(context);
  155. if (context->get_param<int>(MS_CTX_EXECUTION_MODE) != kGraphMode) {
  156. MS_LOG(WARNING) << "GraphKernel only support GRAPH_MODE";
  157. opt_level = OptLevel_0;
  158. }
  159. }
  160. // Dump flags so that people can check the setting.
  161. MS_LOG(INFO) << "graph_kernel_flags = \"" << flags_cache_ << "\", all flags: " << DumpAllFlags();
  162. }
  163. void GraphKernelFlags::RegisterFlags(std::map<std::string, std::string> *flag_map) {
  164. FlagRegister reg(flag_map);
  165. auto context_ptr = MsContext::GetInstance();
  166. MS_EXCEPTION_IF_NULL(context_ptr);
  167. bool is_ascend = (context_ptr->get_param<std::string>(MS_CTX_DEVICE_TARGET) == kAscendDevice);
  168. // Set opt_level first, some flags' default value depends on it.
  169. // Default optimization level is level 2 when enable graphkernel
  170. reg.AddFlag("opt_level", &opt_level, enable_graph_kernel_ ? OptLevel_2 : OptLevel_0);
  171. if (opt_level > OptLevel_3) {
  172. MS_LOG(WARNING) << "GraphKernelFlag: opt_level should be in the range [0,3] but got " << opt_level;
  173. opt_level = OptLevel_3;
  174. }
  175. // Boolean flags
  176. reg.AddFlag("dump_as_text", &dump_as_text);
  177. reg.AddFlag("enable_stitch_fusion", &enable_stitch_fusion, opt_level == OptLevel_3);
  178. reg.AddFlag("enable_recompute_fusion", &enable_recompute_fusion, opt_level >= OptLevel_2);
  179. reg.AddFlag("enable_parallel_fusion", &enable_parallel_fusion, opt_level == OptLevel_3);
  180. reg.AddFlag("enable_low_precision", &enable_low_precision);
  181. reg.AddFlag("enable_trans_op_optimize", &enable_trans_op_optimize);
  182. // Integer flags
  183. reg.AddFlag("online_tuning", &online_tuning);
  184. reg.AddFlag("fusion_ops_level", &fusion_ops_level, is_ascend ? OpLevel_0 : OpLevel_MAX);
  185. // String flags
  186. reg.AddFlag("repository_path", &repository_path);
  187. // String list flags
  188. reg.AddFlag("enable_expand_ops", &enable_expand_ops);
  189. reg.AddFlag("enable_expand_ops_only", &enable_expand_ops_only);
  190. reg.AddFlag("disable_expand_ops", &disable_expand_ops);
  191. reg.AddFlag("enable_cluster_ops", &enable_cluster_ops);
  192. reg.AddFlag("enable_cluster_ops_only", &enable_cluster_ops_only);
  193. reg.AddFlag("disable_cluster_ops", &disable_cluster_ops);
  194. reg.AddFlag("enable_simplify_exprs_only", &enable_simplify_exprs_only);
  195. reg.AddFlag("disable_simplify_exprs", &disable_simplify_exprs);
  196. reg.AddFlag("enable_pass", &enable_pass);
  197. reg.AddFlag("disable_pass", &disable_pass);
  198. }
  199. std::string GraphKernelFlags::DumpAllFlags() const {
  200. nlohmann::json json;
  201. json["dump_as_text"] = dump_as_text;
  202. json["enable_stitch_fusion"] = enable_stitch_fusion;
  203. json["enable_recompute_fusion"] = enable_recompute_fusion;
  204. json["enable_parallel_fusion"] = enable_parallel_fusion;
  205. json["enable_low_precision"] = enable_low_precision;
  206. json["enable_trans_op_optimize"] = enable_trans_op_optimize;
  207. json["opt_level"] = opt_level;
  208. json["fusion_ops_level"] = fusion_ops_level;
  209. json["online_tuning"] = online_tuning;
  210. json["repository_path"] = repository_path;
  211. json["enable_expand_ops"] = enable_expand_ops;
  212. json["enable_expand_ops_only"] = enable_expand_ops_only;
  213. json["disable_expand_ops"] = disable_expand_ops;
  214. json["enable_cluster_ops"] = enable_cluster_ops;
  215. json["enable_cluster_ops_only"] = enable_cluster_ops_only;
  216. json["disable_cluster_ops"] = disable_cluster_ops;
  217. json["enable_simplify_exprs_only"] = enable_simplify_exprs_only;
  218. json["disable_simplify_exprs"] = disable_simplify_exprs;
  219. json["enable_pass"] = enable_pass;
  220. json["disable_pass"] = disable_pass;
  221. return json.dump();
  222. }
  223. } // namespace mindspore::graphkernel