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.h 7.2 kB

4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. #ifndef MINDSPORE_CCSRC_UTILS_GRAPH_KERNEL_FLAGS_H
  17. #define MINDSPORE_CCSRC_UTILS_GRAPH_KERNEL_FLAGS_H
  18. #include <map>
  19. #include <memory>
  20. #include <string>
  21. #include <vector>
  22. #include <utility>
  23. #include "utils/ms_context.h"
  24. namespace mindspore {
  25. namespace context {
  26. constexpr unsigned int OptLevel_0 = 0; // Disabled
  27. constexpr unsigned int OptLevel_1 = 1; // Basic functions
  28. constexpr unsigned int OptLevel_2 = 2; // Default functions
  29. constexpr unsigned int OptLevel_3 = 3; // Experimental functions
  30. constexpr unsigned int OptLevel_MAX = 4;
  31. class GraphKernelFlags {
  32. public:
  33. static const GraphKernelFlags &GetInstance() {
  34. static std::unique_ptr<GraphKernelFlags> flags(nullptr);
  35. auto contexts = GetGraphKernelContext();
  36. if (flags == nullptr || contexts.first != flags->flags_cache_ || contexts.second != flags->enable_graph_kernel_) {
  37. flags.reset(new GraphKernelFlags(contexts.first, contexts.second));
  38. flags->Refresh();
  39. }
  40. return *flags;
  41. }
  42. // Dump all flags to json-format string
  43. std::string DumpAllFlags() const;
  44. // Check whether graph_kernel is enabled
  45. bool IsEnableGraphKernel() const { return opt_level > OptLevel_0; }
  46. GraphKernelFlags(const GraphKernelFlags &flags) = delete;
  47. ~GraphKernelFlags() = default;
  48. public:
  49. /**
  50. * Dump info as human-readable text.
  51. * A directory "graph_kernel_dump" will be created, and all information will be dumped in this directory.
  52. */
  53. bool dump_as_text{false};
  54. /**
  55. * Enable stitch fusion in graph kernel fusion strategy.
  56. *
  57. * Experimental feature, enabled by default when opt_level=3
  58. */
  59. bool enable_stitch_fusion;
  60. /**
  61. * Enable recompute fusion in graph kernel fusion strategy, enabled when op_level>=2.
  62. */
  63. bool enable_recompute_fusion;
  64. /**
  65. * Enable parallel fusion in graph kernel fusion strategy.
  66. *
  67. * Experimental feature, enabled by default when opt_level=3
  68. */
  69. bool enable_parallel_fusion;
  70. /**
  71. * Enable low precision in data transferring between graph kernel and computing in graph kernel
  72. * in graph kernel.
  73. * Experimental feature, enabled by the enable_low_precision flag
  74. */
  75. bool enable_low_precision;
  76. /**
  77. * Optimization level, value from 0 to 3.
  78. * 0: Disable GraphKernel
  79. * 1: Enable GraphKernel with basic features only.
  80. * 2: Enable GraphKernel with all stable features.
  81. * 3: Enable GraphKernel with all experimental features.
  82. * The default value is OptLevel_2 when the context "enable_graph_kernel" is set,
  83. * but if it's also changed in "graph_kernel_flags", then the "graph_kernel_flags" will prevail.
  84. */
  85. unsigned int opt_level; // defaults 0 or 2
  86. /**
  87. * Online tuning level, value from 0 to 3.
  88. * 0: Disable online tuning
  89. * 1-3: The higher level, the larger tuning space, and the more time it takes.
  90. */
  91. unsigned int online_tuning{0};
  92. /**
  93. * AKG's operator repository file path.
  94. */
  95. std::string repository_path;
  96. /**
  97. * Additional expanding operators (case sensitive).
  98. * The operators to be added into the default expanding operator list.
  99. */
  100. std::vector<std::string> enable_expand_ops;
  101. /**
  102. * Expanding operators to be enabled (case sensitive).
  103. * Unlike the "enable_expand_ops", the default list will be overwritten by this list.
  104. * Note that the "enable_expand_ops" and "disable_expand_ops" will be ignored if this flag is set.
  105. */
  106. std::vector<std::string> enable_expand_ops_only;
  107. /**
  108. * Expanding operators to be disabled (case sensitive).
  109. * The behavior is undefined when this list overlaps with "enable_expand_ops".
  110. */
  111. std::vector<std::string> disable_expand_ops;
  112. /**
  113. * Additional clustering operators (case sensitive).
  114. * The operators to be added into the default clustering operator list.
  115. */
  116. std::vector<std::string> enable_cluster_ops;
  117. /**
  118. * Clustering operators to be enabled (case sensitive).
  119. * Unlike the "enable_cluster_ops", the default list will be overwritten by this list.
  120. * Note that the "enable_cluster_ops" and "disable_cluster_ops" will be ignored if this flag is set.
  121. */
  122. std::vector<std::string> enable_cluster_ops_only;
  123. /**
  124. * Clustering operators to be disabled (case sensitive).
  125. * The behavior is undefined when this list overlaps with "enable_cluster_ops".
  126. */
  127. std::vector<std::string> disable_cluster_ops;
  128. /**
  129. * Arithmetic simplify expressions to be enabled (case sensitive).
  130. * The default list will be overwritten by this list.
  131. * Note that "disable_simplify_exprs" will be ignored if this flag is set.
  132. */
  133. std::vector<std::string> enable_simplify_exprs_only;
  134. /**
  135. * Arithmetic simplify expressions to be disabled (case sensitive).
  136. */
  137. std::vector<std::string> disable_simplify_exprs;
  138. /**
  139. * Passes to be enabled.
  140. * By default, the passes is controlled by "opt_level" and target device,
  141. * user can manually enable some passes by setting this flag.
  142. * The format is "stage_id.pass_id" or "stage_name.pass_name", which corresponds to the ir filename.
  143. */
  144. std::vector<std::string> enable_pass;
  145. /**
  146. * Passes to be disabled.
  147. * By default, the passes is controlled by "opt_level" and target device,
  148. * user can manually disable some passes by setting this flag.
  149. * The format is "stage_id.pass_id" or "stage_name.pass_name", which corresponds to the ir filename.
  150. */
  151. std::vector<std::string> disable_pass;
  152. private:
  153. GraphKernelFlags(const std::string &graph_kernel_flags, bool enable_graph_kernel)
  154. : flags_cache_(graph_kernel_flags), enable_graph_kernel_(enable_graph_kernel) {}
  155. // get the `graph_kernel_flags` and `enable_graph_kernel`
  156. static std::pair<std::string, bool> GetGraphKernelContext() {
  157. auto context = MsContext::GetInstance();
  158. MS_EXCEPTION_IF_NULL(context);
  159. // Use the environment variable in priority
  160. auto env_flags = std::getenv("MS_GRAPH_KERNEL_FLAGS");
  161. std::string flags = env_flags ? std::string(env_flags) : context->get_param<std::string>(MS_CTX_GRAPH_KERNEL_FLAGS);
  162. return std::make_pair(flags, context->get_param<bool>(MS_CTX_ENABLE_GRAPH_KERNEL));
  163. }
  164. // parse and refresh the flags
  165. void Refresh();
  166. // register the flags defined above
  167. void RegisterFlags(std::map<std::string, std::string> *flag_map);
  168. // cache the flag string to check whether the flags is changed.
  169. std::string flags_cache_;
  170. // cache the enable_graph_kernel value to check whether the context is changed.
  171. bool enable_graph_kernel_;
  172. };
  173. } // namespace context
  174. } // namespace mindspore
  175. #endif // MINDSPORE_CCSRC_UTILS_GRAPH_KERNEL_FLAGS_H