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.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. if (i != 0 || flag.size() == 2) {
  46. return std::pair<std::string, std::string>();
  47. }
  48. i += 2;
  49. auto j = flag.find('=', i + 1); // the key should not be empty, "--=" is invalid
  50. if (j == std::string::npos) {
  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(std::string flag_name, T *flag_var, T default_value = T()) {
  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) {
  102. *result = GetTokens(s, ",");
  103. return !result->empty();
  104. }
  105. bool ParseValue(const std::string &s, bool *result) {
  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) {
  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) {
  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. void GraphKernelFlags::Refresh() {
  139. auto flag_map = ParseFlags(flags_cache_);
  140. RegisterFlags(&flag_map);
  141. for (auto &item : flag_map) {
  142. MS_LOG(WARNING) << "Unknown GraphKernel flag: " << item.first;
  143. }
  144. if (IsEnableGraphKernel()) {
  145. auto context = MsContext::GetInstance();
  146. MS_EXCEPTION_IF_NULL(context);
  147. if (context->get_param<int>(MS_CTX_EXECUTION_MODE) != kGraphMode) {
  148. MS_LOG(WARNING) << "GraphKernel only support GRAPH_MODE";
  149. opt_level = OptLevel_0;
  150. }
  151. }
  152. // Dump flags so that people can check the setting.
  153. MS_LOG(INFO) << "GraphKernelFlags info: " << DumpAllFlags();
  154. }
  155. void GraphKernelFlags::RegisterFlags(std::map<std::string, std::string> *flag_map) {
  156. FlagRegister reg(flag_map);
  157. // Set opt_level first, some flags' default value depends on it.
  158. // Default optimization level is level 2 when enable graphkernel
  159. reg.AddFlag("opt_level", &opt_level, enable_graph_kernel_ ? OptLevel_2 : OptLevel_0);
  160. if (opt_level > OptLevel_3) {
  161. MS_LOG(WARNING) << "GraphKernelFlag: opt_level should be in the range [0,3] but got " << opt_level;
  162. opt_level = OptLevel_3;
  163. }
  164. // Boolean flags
  165. reg.AddFlag("dump_as_text", &dump_as_text);
  166. reg.AddFlag("enable_stitch_fusion", &enable_stitch_fusion, opt_level == OptLevel_3);
  167. reg.AddFlag("enable_parallel_fusion", &enable_parallel_fusion, opt_level == OptLevel_3);
  168. // Integer flags
  169. reg.AddFlag("auto_tune", &auto_tune);
  170. reg.AddFlag("cluster_limit", &cluster_limit);
  171. // String list flags
  172. reg.AddFlag("enable_expand_ops", &enable_expand_ops);
  173. reg.AddFlag("enable_expand_ops_only", &enable_expand_ops_only);
  174. reg.AddFlag("disable_expand_ops", &disable_expand_ops);
  175. reg.AddFlag("enable_cluster_ops", &enable_cluster_ops);
  176. reg.AddFlag("enable_cluster_ops_only", &enable_cluster_ops_only);
  177. reg.AddFlag("disable_cluster_ops", &disable_cluster_ops);
  178. reg.AddFlag("enable_pass", &enable_pass);
  179. reg.AddFlag("disable_pass", &disable_pass);
  180. }
  181. std::string GraphKernelFlags::DumpAllFlags() const {
  182. nlohmann::json json;
  183. json["dump_as_text"] = dump_as_text;
  184. json["enable_stitch_fusion"] = enable_stitch_fusion;
  185. json["enable_parallel_fusion"] = enable_parallel_fusion;
  186. json["opt_level"] = opt_level;
  187. json["auto_tune"] = auto_tune;
  188. json["cluster_limit"] = cluster_limit;
  189. json["enable_expand_ops"] = enable_expand_ops;
  190. json["enable_expand_ops_only"] = enable_expand_ops_only;
  191. json["disable_expand_ops"] = disable_expand_ops;
  192. json["enable_cluster_ops"] = enable_cluster_ops;
  193. json["enable_cluster_ops_only"] = enable_cluster_ops_only;
  194. json["disable_cluster_ops"] = disable_cluster_ops;
  195. json["enable_pass"] = enable_pass;
  196. json["disable_pass"] = disable_pass;
  197. return json.dump();
  198. }
  199. } // namespace context
  200. } // namespace mindspore