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.

fastrun_options.cpp 8.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /**
  2. * \file lite/load_and_run/src/options/fastrun_options.cpp
  3. *
  4. * This file is part of MegEngine, a deep learning framework developed by
  5. * Megvii.
  6. *
  7. * \copyright Copyright (c) 2020-2021 Megvii Inc. All rights reserved.
  8. */
  9. #include <gflags/gflags.h>
  10. #if defined(_WIN32)
  11. #include <io.h>
  12. #define F_OK 0
  13. #define access(a, b) _access(a, b)
  14. #elif __linux__ || __unix__ || __APPLE__
  15. #include <unistd.h>
  16. #endif
  17. #include "fastrun_options.h"
  18. #include "megbrain/gopt/inference.h"
  19. #include "megbrain/utils/infile_persistent_cache.h"
  20. #include "misc.h"
  21. #include "models/model_lite.h"
  22. #include "models/model_mdl.h"
  23. namespace lar {
  24. template <>
  25. void FastRunOption::config_model_internel<ModelLite>(
  26. RuntimeParam& runtime_param, std::shared_ptr<ModelLite> model) {
  27. if (runtime_param.stage == RunStage::BEFORE_MODEL_LOAD) {
  28. //! set the algo policy before model load
  29. using Strategy = ModelLite::Strategy;
  30. uint32_t strategy = 0;
  31. #if MGB_ENABLE_FASTRUN
  32. if (enable_full_run) {
  33. LITE_WARN("enable full-run strategy for algo profile");
  34. strategy = static_cast<uint32_t>(Strategy::LITE_ALGO_PROFILE) | strategy;
  35. } else if (enable_fast_run) {
  36. LITE_WARN("enable fast-run strategy for algo profile");
  37. strategy = static_cast<uint32_t>(Strategy::LITE_ALGO_PROFILE) |
  38. static_cast<uint32_t>(Strategy::LITE_ALGO_OPTIMIZED) | strategy;
  39. } else {
  40. strategy = static_cast<uint32_t>(Strategy::LITE_ALGO_HEURISTIC) | strategy;
  41. }
  42. #else
  43. strategy = static_cast<uint32_t>(Strategy::LITE_ALGO_HEURISTIC) | strategy;
  44. #endif
  45. if (batch_binary_equal || enable_reproducible) {
  46. LITE_WARN("enable reproducible strategy for algo profile");
  47. if (batch_binary_equal)
  48. strategy = static_cast<uint32_t>(Strategy::LITE_ALGO_REPRODUCIBLE) |
  49. strategy;
  50. }
  51. auto lite_strategy = static_cast<Strategy>(strategy);
  52. model->set_lite_strategy(lite_strategy);
  53. } else if (runtime_param.stage == RunStage::AFTER_MODEL_LOAD) {
  54. auto&& lite_network = model->get_lite_network();
  55. auto&& lite_strategy = model->get_lite_strategy();
  56. //! set algo policy for model
  57. lite::Runtime::set_network_algo_policy(
  58. lite_network, lite_strategy, share_batch_size, batch_binary_equal);
  59. if (!m_fast_run_cache.empty()) {
  60. if (!access(m_fast_run_cache.c_str(), F_OK)) {
  61. lite::set_persistent_cache(m_fast_run_cache);
  62. } else {
  63. lite::set_persistent_cache(m_fast_run_cache, true);
  64. }
  65. //! TODO:this is from mdl model settings but not matched settings in
  66. //! lite model
  67. // if (!enable_full_run && !enable_fast_run)
  68. // mgb::gopt::enable_opr_use_profiling_cache_inplace(vars);
  69. }
  70. } else if (runtime_param.stage == RunStage::AFTER_MODEL_RUNNING) {
  71. #if MGB_ENABLE_FASTRUN
  72. //! dump algo cache
  73. if (!m_fast_run_cache.empty()) {
  74. lite::dump_persistent_cache(m_fast_run_cache);
  75. }
  76. #endif
  77. }
  78. }
  79. template <>
  80. void FastRunOption::config_model_internel<ModelMdl>(
  81. RuntimeParam& runtime_param, std::shared_ptr<ModelMdl> model) {
  82. if (runtime_param.stage == RunStage::BEFORE_MODEL_LOAD) {
  83. //! set the algo policy before model load
  84. using Strategy = ModelMdl::Strategy;
  85. auto strategy = static_cast<Strategy>(0);
  86. #if MGB_ENABLE_FASTRUN
  87. if (enable_full_run) {
  88. mgb_log_warn("enable full-run strategy for algo profile");
  89. strategy = Strategy::PROFILE | strategy;
  90. } else if (enable_fast_run) {
  91. mgb_log_warn("enable fast-run strategy for algo profile");
  92. strategy = Strategy::PROFILE | Strategy::OPTIMIZED | strategy;
  93. } else {
  94. strategy = Strategy::HEURISTIC | strategy;
  95. }
  96. #else
  97. strategy = Strategy::HEURISTIC | strategy;
  98. #endif
  99. if (batch_binary_equal || enable_reproducible) {
  100. mgb_log_warn("enable reproducible strategy for algo profile");
  101. strategy = Strategy::REPRODUCIBLE | strategy;
  102. }
  103. model->set_mdl_strategy(strategy);
  104. //! set binary_equal_between_batch and shared_batch_size
  105. if (batch_binary_equal) {
  106. mgb_log_warn("enable batch binary equal");
  107. model->get_mdl_config()
  108. .comp_graph->options()
  109. .fast_run_config.binary_equal_between_batch = true;
  110. }
  111. if (share_batch_size > 0) {
  112. mgb_log_warn("set shared shared batch");
  113. model->get_mdl_config()
  114. .comp_graph->options()
  115. .fast_run_config.shared_batch_size = share_batch_size;
  116. }
  117. } else if (runtime_param.stage == RunStage::AFTER_MODEL_LOAD) {
  118. auto& vars = model->get_mdl_load_result().output_var_list;
  119. auto&& strategy = model->get_mdl_strategy();
  120. mgb::gopt::modify_opr_algo_strategy_inplace(vars, strategy);
  121. // set algo cache path
  122. if (!m_fast_run_cache.empty()) {
  123. if (!access(m_fast_run_cache.c_str(), F_OK)) {
  124. mgb::PersistentCache::set_impl(
  125. std::make_shared<mgb::InFilePersistentCache>(
  126. m_fast_run_cache.c_str()));
  127. } else {
  128. mgb::PersistentCache::set_impl(
  129. std::make_shared<mgb::InFilePersistentCache>());
  130. }
  131. #if MGB_ENABLE_FASTRUN
  132. if (!enable_full_run && !enable_fast_run)
  133. #endif
  134. mgb::gopt::enable_opr_use_profiling_cache_inplace(vars);
  135. }
  136. } else if (runtime_param.stage == RunStage::AFTER_MODEL_RUNNING) {
  137. #if MGB_ENABLE_FASTRUN
  138. //! dump algo cache
  139. if (!m_fast_run_cache.empty()) {
  140. static_cast<mgb::InFilePersistentCache&>(mgb::PersistentCache::inst())
  141. .dump_cache(m_fast_run_cache.c_str());
  142. }
  143. #endif
  144. }
  145. }
  146. } // namespace lar
  147. using namespace lar;
  148. FastRunOption::FastRunOption() {
  149. m_option_name = "fastrun";
  150. #if MGB_ENABLE_FASTRUN
  151. enable_fast_run = FLAGS_fast_run;
  152. enable_full_run = FLAGS_full_run;
  153. #endif
  154. batch_binary_equal = FLAGS_binary_equal_between_batch;
  155. enable_reproducible = FLAGS_reproducible;
  156. m_fast_run_cache = FLAGS_fast_run_algo_policy;
  157. share_batch_size = FLAGS_fast_run_shared_batch_size;
  158. #if MGB_ENABLE_FASTRUN
  159. //! while fastrun cache file path is not empty and can't be accessed
  160. if (!m_fast_run_cache.empty() && access(m_fast_run_cache.c_str(), F_OK)) {
  161. mgb_assert(
  162. enable_full_run || enable_fast_run,
  163. "--fast-run or --full-run should be enabled");
  164. }
  165. if (share_batch_size) {
  166. mgb_assert(
  167. enable_full_run || enable_fast_run || !m_fast_run_cache.empty(),
  168. "--fast-run-shared-batch-size should be used with "
  169. "--fast-run|--full-run|--fast-run-algo-policy");
  170. }
  171. #endif
  172. }
  173. bool FastRunOption::is_valid() {
  174. bool ret = false;
  175. #if MGB_ENABLE_FASTRUN
  176. ret = ret || FLAGS_fast_run;
  177. ret = ret || FLAGS_full_run;
  178. #endif
  179. ret = ret || FLAGS_binary_equal_between_batch;
  180. ret = ret || FLAGS_fast_run_shared_batch_size > 0;
  181. ret = ret || FLAGS_reproducible;
  182. ret = ret || FLAGS_fast_run_algo_policy.size() > 0;
  183. return ret;
  184. }
  185. std::shared_ptr<OptionBase> FastRunOption::create_option() {
  186. static std::shared_ptr<FastRunOption> option(new FastRunOption);
  187. if (FastRunOption::is_valid()) {
  188. return std::static_pointer_cast<OptionBase>(option);
  189. } else {
  190. return nullptr;
  191. }
  192. }
  193. void FastRunOption::config_model(
  194. RuntimeParam& runtime_param, std::shared_ptr<ModelBase> model) {
  195. CONFIG_MODEL_FUN;
  196. }
  197. #if MGB_ENABLE_FASTRUN
  198. DEFINE_bool(fast_run, false, "whether to use fast-run in model run");
  199. DEFINE_bool(full_run, false, "whether to use full-run in model run");
  200. #endif
  201. DEFINE_bool(
  202. binary_equal_between_batch, false,
  203. "Each batch of output is promised binary equal if each batch of "
  204. "input is binary equal\n Note that if this option is turned on, "
  205. "`--reproducible` will also be turned on.");
  206. DEFINE_bool(
  207. reproducible, false,
  208. "Enable choose algo which is reproducible. It mainly used for "
  209. "cudnn algos.See "
  210. "https://docs.nvidia.com/deeplearning/sdk/cudnn-developer-guide/"
  211. "index.html#reproducibility"
  212. "for more details.");
  213. DEFINE_uint32(fast_run_shared_batch_size, 0, "Set the batch size used during fastrun");
  214. DEFINE_string(fast_run_algo_policy, "", "fast-run cache path.");
  215. REGIST_OPTION_CREATOR(fastrun, lar::FastRunOption::create_option);