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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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) {
  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. if (iter->second.empty()) {
  89. MS_LOG(WARNING) << "Invalid GraphKernel flag: --" << iter->first;
  90. } else {
  91. MS_LOG(WARNING) << "Invalid GraphKernel flag: --" << iter->first << "=" << iter->second;
  92. }
  93. }
  94. flag_map_.erase(iter);
  95. }
  96. }
  97. private:
  98. bool ParseValue(const std::string &s, std::vector<std::string> *result) {
  99. *result = GetTokens(s, ",");
  100. return !result->empty();
  101. }
  102. bool ParseValue(const std::string &s, bool *result) {
  103. *result = (s.empty() || s == "true" || s == "on" || s == "1");
  104. return *result || s == "false" || s == "off" || s == "0";
  105. }
  106. template <typename T>
  107. bool ParseValue(const std::string &s, T *result) {
  108. if (s.empty()) {
  109. return false;
  110. }
  111. std::istringstream iss(s);
  112. iss >> (*result);
  113. return iss.eof();
  114. }
  115. template <typename T>
  116. bool ParseValue(const std::string &s, std::vector<T> *result) {
  117. result->clear();
  118. auto tokens = GetTokens(s, ",");
  119. if (tokens.empty()) {
  120. return false;
  121. }
  122. for (const auto &tok : tokens) {
  123. T temp;
  124. if (!ParseValue(tok, &temp)) {
  125. result->clear();
  126. return false;
  127. }
  128. result->emplace_back(temp);
  129. }
  130. return true;
  131. }
  132. std::map<std::string, std::string> &flag_map_;
  133. };
  134. } // namespace
  135. void GraphKernelFlags::Refresh() {
  136. auto flag_map = ParseFlags(flags_cache_);
  137. RegisterFlags(&flag_map);
  138. for (auto &item : flag_map) {
  139. MS_LOG(WARNING) << "Unknown GraphKernel flag: " << item.first;
  140. }
  141. if (opt_level > 0) {
  142. auto context = MsContext::GetInstance();
  143. MS_EXCEPTION_IF_NULL(context);
  144. if (context->get_param<int>(MS_CTX_EXECUTION_MODE) != kGraphMode) {
  145. MS_LOG(WARNING) << "GraphKernel only support GRAPH_MODE";
  146. opt_level = 0;
  147. }
  148. }
  149. }
  150. void GraphKernelFlags::RegisterFlags(std::map<std::string, std::string> *flag_map) {
  151. FlagRegister reg(flag_map);
  152. // Boolean flags
  153. reg.AddFlag("dump_as_text", &dump_as_text);
  154. reg.AddFlag("enable_stitch_fusion", &enable_stitch_fusion);
  155. reg.AddFlag("enable_parallel_fusion", &enable_parallel_fusion);
  156. // Integer flags
  157. reg.AddFlag("opt_level", &opt_level);
  158. reg.AddFlag("auto_tune", &auto_tune);
  159. reg.AddFlag("cluster_limit", &cluster_limit);
  160. // String list flags
  161. reg.AddFlag("enable_expand_ops", &enable_expand_ops);
  162. reg.AddFlag("enable_expand_ops_only", &enable_expand_ops_only);
  163. reg.AddFlag("disable_expand_ops", &disable_expand_ops);
  164. reg.AddFlag("enable_cluster_ops", &enable_cluster_ops);
  165. reg.AddFlag("enable_cluster_ops_only", &enable_cluster_ops_only);
  166. reg.AddFlag("disable_cluster_ops", &disable_cluster_ops);
  167. reg.AddFlag("enable_pass_only", &enable_pass_only);
  168. reg.AddFlag("disable_pass", &disable_pass);
  169. }
  170. std::string GraphKernelFlags::DumpAllFlags() const {
  171. nlohmann::json json;
  172. json["dump_as_text"] = dump_as_text;
  173. json["enable_stitch_fusion"] = enable_stitch_fusion;
  174. json["enable_parallel_fusion"] = enable_parallel_fusion;
  175. json["opt_level"] = opt_level;
  176. json["auto_tune"] = auto_tune;
  177. json["cluster_limit"] = cluster_limit;
  178. json["enable_expand_ops"] = enable_expand_ops;
  179. json["enable_expand_ops_only"] = enable_expand_ops_only;
  180. json["disable_expand_ops"] = disable_expand_ops;
  181. json["enable_cluster_ops"] = enable_cluster_ops;
  182. json["enable_cluster_ops_only"] = enable_cluster_ops_only;
  183. json["disable_cluster_ops"] = disable_cluster_ops;
  184. json["enable_pass_only"] = enable_pass_only;
  185. json["disable_pass"] = disable_pass;
  186. return json.dump();
  187. }
  188. } // namespace context
  189. } // namespace mindspore