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

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