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.

device_options.cpp 7.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**
  2. * \file lite/load_and_run/src/options/device_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 <iostream>
  10. #include <sstream>
  11. #include "lite/global.h"
  12. #include "megbrain/comp_node_env.h"
  13. #include "misc.h"
  14. #include "device_options.h"
  15. #include "models/model_lite.h"
  16. #include "models/model_mdl.h"
  17. DECLARE_bool(weight_preprocess);
  18. using namespace lar;
  19. /////////////////// XPUDeviceOption //////////////////////
  20. namespace lar {
  21. template <>
  22. void XPUDeviceOption::config_model_internel<ModelLite>(
  23. RuntimeParam& runtime_param, std::shared_ptr<ModelLite> model) {
  24. if (runtime_param.stage == RunStage::BEFORE_MODEL_LOAD) {
  25. if ((enable_cpu) || (enable_cpu_default) || (enable_multithread) ||
  26. (enable_multithread_default)) {
  27. LITE_WARN("using cpu device\n");
  28. model->get_config().device_type = LiteDeviceType::LITE_CPU;
  29. }
  30. #if LITE_WITH_CUDA
  31. if (enable_cuda) {
  32. LITE_WARN("using cuda device\n");
  33. model->get_config().device_type = LiteDeviceType::LITE_CUDA;
  34. }
  35. #endif
  36. } else if (runtime_param.stage == RunStage::AFTER_MODEL_LOAD) {
  37. auto&& network = model->get_lite_network();
  38. if (enable_cpu_default) {
  39. LITE_WARN("using cpu default device\n");
  40. lite::Runtime::set_cpu_inplace_mode(network);
  41. }
  42. if (enable_multithread) {
  43. LITE_WARN("using multithread device\n");
  44. lite::Runtime::set_cpu_threads_number(network, thread_num);
  45. }
  46. if (enable_multithread_default) {
  47. LITE_WARN("using multithread default device\n");
  48. lite::Runtime::set_cpu_inplace_mode(network);
  49. lite::Runtime::set_cpu_threads_number(network, thread_num);
  50. }
  51. if (enable_set_core_ids) {
  52. std::string core_str;
  53. for (auto id : core_ids) {
  54. core_str += std::to_string(id) + ",";
  55. }
  56. LITE_WARN("multi thread core ids: %s\n", core_str.c_str());
  57. lite::ThreadAffinityCallback affinity_callback = [&](size_t thread_id) {
  58. mgb::sys::set_cpu_affinity({core_ids[thread_id]});
  59. };
  60. lite::Runtime::set_runtime_thread_affinity(network, affinity_callback);
  61. }
  62. }
  63. }
  64. template <>
  65. void XPUDeviceOption::config_model_internel<ModelMdl>(
  66. RuntimeParam& runtime_param, std::shared_ptr<ModelMdl> model) {
  67. if (runtime_param.stage == RunStage::BEFORE_MODEL_LOAD) {
  68. if (enable_cpu) {
  69. mgb_log_warn("using cpu device\n");
  70. model->get_mdl_config().comp_node_mapper = [](mgb::CompNode::Locator& loc) {
  71. loc.type = mgb::CompNode::DeviceType::CPU;
  72. };
  73. }
  74. #if MGB_CUDA
  75. if (enable_cuda) {
  76. mgb_log_warn("using cuda device\n");
  77. model->get_mdl_config().comp_node_mapper = [](mgb::CompNode::Locator& loc) {
  78. loc.type = mgb::CompNode::DeviceType::CUDA;
  79. loc.device = 0;
  80. };
  81. }
  82. #endif
  83. if (enable_cpu_default) {
  84. mgb_log_warn("using cpu default device\n");
  85. model->get_mdl_config().comp_node_mapper = [](mgb::CompNode::Locator& loc) {
  86. loc.type = mgb::CompNode::DeviceType::CPU;
  87. loc.device = mgb::CompNode::Locator::DEVICE_CPU_DEFAULT;
  88. };
  89. }
  90. if (enable_multithread) {
  91. mgb_log_warn("using multithread device\n");
  92. model->get_mdl_config().comp_node_mapper =
  93. [&](mgb::CompNode::Locator& loc) {
  94. loc.type = mgb::CompNode::DeviceType::MULTITHREAD;
  95. loc.device = 0;
  96. loc.stream = thread_num;
  97. };
  98. }
  99. if (enable_multithread_default) {
  100. mgb_log_warn("using multithread default device\n");
  101. model->get_mdl_config().comp_node_mapper =
  102. [&](mgb::CompNode::Locator& loc) {
  103. loc.type = mgb::CompNode::DeviceType::MULTITHREAD;
  104. loc.device = mgb::CompNode::Locator::DEVICE_MULTITHREAD_DEFAULT;
  105. loc.stream = thread_num;
  106. };
  107. }
  108. if (enable_set_core_ids) {
  109. std::string core_str;
  110. for (auto id : core_ids) {
  111. core_str += std::to_string(id) + ",";
  112. }
  113. mgb_log_warn("set multi thread core ids:%s\n", core_str.c_str());
  114. auto affinity_callback = [&](size_t thread_id) {
  115. mgb::sys::set_cpu_affinity({core_ids[thread_id]});
  116. };
  117. mgb::CompNode::Locator loc;
  118. model->get_mdl_config().comp_node_mapper(loc);
  119. auto comp_node = mgb::CompNode::load(loc);
  120. mgb::CompNodeEnv::from_comp_node(comp_node).cpu_env().set_affinity(
  121. affinity_callback);
  122. }
  123. }
  124. }
  125. } // namespace lar
  126. XPUDeviceOption::XPUDeviceOption() {
  127. m_option_name = "xpu_device";
  128. enable_cpu = FLAGS_cpu;
  129. #if MGB_CUDA
  130. enable_cuda = FLAGS_cuda;
  131. #endif
  132. enable_cpu_default = FLAGS_cpu_default;
  133. if (FLAGS_multithread >= 0) {
  134. thread_num = FLAGS_multithread;
  135. enable_multithread = true;
  136. }
  137. if (FLAGS_multithread_default >= 0) {
  138. thread_num = FLAGS_multithread_default;
  139. enable_multithread_default = true;
  140. }
  141. if (!FLAGS_multi_thread_core_ids.empty()) {
  142. mgb_assert(enable_multithread, "core ids should be set after --multithread");
  143. std::stringstream id_stream(FLAGS_multi_thread_core_ids);
  144. std::string id;
  145. size_t thread_cnt = 0;
  146. while (getline(id_stream, id, ',')) {
  147. thread_cnt++;
  148. core_ids.push_back(atoi(id.c_str()));
  149. }
  150. mgb_assert(
  151. thread_cnt == thread_num,
  152. "core ids number should be same with thread number set before");
  153. enable_set_core_ids = true;
  154. }
  155. }
  156. bool XPUDeviceOption::is_valid() {
  157. bool ret = FLAGS_cpu || FLAGS_cpu_default;
  158. #if MGB_CUDA
  159. ret = ret || FLAGS_cuda;
  160. #endif
  161. ret = ret || FLAGS_multithread >= 0;
  162. ret = ret || FLAGS_multithread_default >= 0;
  163. ret = ret || !FLAGS_multi_thread_core_ids.empty();
  164. return ret;
  165. }
  166. std::shared_ptr<OptionBase> XPUDeviceOption::create_option() {
  167. static std::shared_ptr<lar::XPUDeviceOption> option(new XPUDeviceOption);
  168. if (XPUDeviceOption::is_valid()) {
  169. return std::static_pointer_cast<lar::OptionBase>(option);
  170. } else {
  171. return nullptr;
  172. }
  173. }
  174. void XPUDeviceOption::config_model(
  175. RuntimeParam& runtime_param, std::shared_ptr<ModelBase> model) {
  176. CONFIG_MODEL_FUN;
  177. }
  178. ///////////////////////// xpu gflags ////////////////////////////
  179. DEFINE_bool(cpu, false, "set CPU device as running device");
  180. #if MGB_CUDA || LITE_WITH_CUDA
  181. DEFINE_bool(cuda, false, "set CUDA device as running device ");
  182. #endif
  183. DEFINE_bool(cpu_default, false, "set running device as CPU device with inplace mode");
  184. DEFINE_int32(multithread, -1, "set multithread device as running device");
  185. DEFINE_int32(
  186. multithread_default, -1,
  187. "set multithread device as running device with inplace mode");
  188. DEFINE_string(multi_thread_core_ids, "", "set multithread core id");
  189. REGIST_OPTION_CREATOR(xpu_device, lar::XPUDeviceOption::create_option);