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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. if (loc.type == mgb::CompNode::DeviceType::UNSPEC) {
  79. loc.type = mgb::CompNode::DeviceType::CUDA;
  80. }
  81. loc.device = 0;
  82. };
  83. }
  84. #endif
  85. if (enable_cpu_default) {
  86. mgb_log_warn("using cpu default device\n");
  87. model->get_mdl_config().comp_node_mapper = [](mgb::CompNode::Locator& loc) {
  88. loc.type = mgb::CompNode::DeviceType::CPU;
  89. loc.device = mgb::CompNode::Locator::DEVICE_CPU_DEFAULT;
  90. };
  91. }
  92. if (enable_multithread) {
  93. mgb_log_warn("using multithread device\n");
  94. model->get_mdl_config().comp_node_mapper =
  95. [&](mgb::CompNode::Locator& loc) {
  96. loc.type = mgb::CompNode::DeviceType::MULTITHREAD;
  97. loc.device = 0;
  98. loc.stream = thread_num;
  99. };
  100. }
  101. if (enable_multithread_default) {
  102. mgb_log_warn("using multithread default device\n");
  103. model->get_mdl_config().comp_node_mapper =
  104. [&](mgb::CompNode::Locator& loc) {
  105. loc.type = mgb::CompNode::DeviceType::MULTITHREAD;
  106. loc.device = mgb::CompNode::Locator::DEVICE_MULTITHREAD_DEFAULT;
  107. loc.stream = thread_num;
  108. };
  109. }
  110. if (enable_set_core_ids) {
  111. std::string core_str;
  112. for (auto id : core_ids) {
  113. core_str += std::to_string(id) + ",";
  114. }
  115. mgb_log_warn("set multi thread core ids:%s\n", core_str.c_str());
  116. auto affinity_callback = [&](size_t thread_id) {
  117. mgb::sys::set_cpu_affinity({core_ids[thread_id]});
  118. };
  119. mgb::CompNode::Locator loc;
  120. model->get_mdl_config().comp_node_mapper(loc);
  121. auto comp_node = mgb::CompNode::load(loc);
  122. mgb::CompNodeEnv::from_comp_node(comp_node).cpu_env().set_affinity(
  123. affinity_callback);
  124. }
  125. }
  126. }
  127. } // namespace lar
  128. XPUDeviceOption::XPUDeviceOption() {
  129. m_option_name = "xpu_device";
  130. enable_cpu = FLAGS_cpu;
  131. #if MGB_CUDA
  132. enable_cuda = FLAGS_cuda;
  133. #endif
  134. enable_cpu_default = FLAGS_cpu_default;
  135. if (FLAGS_multithread >= 0) {
  136. thread_num = FLAGS_multithread;
  137. enable_multithread = true;
  138. }
  139. if (FLAGS_multithread_default >= 0) {
  140. thread_num = FLAGS_multithread_default;
  141. enable_multithread_default = true;
  142. }
  143. if (!FLAGS_multi_thread_core_ids.empty()) {
  144. mgb_assert(
  145. enable_multithread || enable_multithread_default,
  146. "core ids should be set after --multithread or --multithread-default");
  147. std::stringstream id_stream(FLAGS_multi_thread_core_ids);
  148. std::string id;
  149. size_t thread_cnt = 0;
  150. while (getline(id_stream, id, ',')) {
  151. thread_cnt++;
  152. core_ids.push_back(atoi(id.c_str()));
  153. }
  154. mgb_assert(
  155. thread_cnt == thread_num,
  156. "core ids number should be same with thread number set before");
  157. enable_set_core_ids = true;
  158. }
  159. }
  160. bool XPUDeviceOption::is_valid() {
  161. bool ret = FLAGS_cpu || FLAGS_cpu_default;
  162. #if MGB_CUDA
  163. ret = ret || FLAGS_cuda;
  164. #endif
  165. ret = ret || FLAGS_multithread >= 0;
  166. ret = ret || FLAGS_multithread_default >= 0;
  167. ret = ret || !FLAGS_multi_thread_core_ids.empty();
  168. return ret;
  169. }
  170. std::shared_ptr<OptionBase> XPUDeviceOption::create_option() {
  171. static std::shared_ptr<lar::XPUDeviceOption> option(new XPUDeviceOption);
  172. if (XPUDeviceOption::is_valid()) {
  173. return std::static_pointer_cast<lar::OptionBase>(option);
  174. } else {
  175. return nullptr;
  176. }
  177. }
  178. void XPUDeviceOption::config_model(
  179. RuntimeParam& runtime_param, std::shared_ptr<ModelBase> model) {
  180. CONFIG_MODEL_FUN;
  181. }
  182. ///////////////////////// xpu gflags ////////////////////////////
  183. DEFINE_bool(cpu, false, "set CPU device as running device");
  184. #if MGB_CUDA || LITE_WITH_CUDA
  185. DEFINE_bool(cuda, false, "set CUDA device as running device ");
  186. #endif
  187. DEFINE_bool(cpu_default, false, "set running device as CPU device with inplace mode");
  188. DEFINE_int32(multithread, -1, "set multithread device as running device");
  189. DEFINE_int32(
  190. multithread_default, -1,
  191. "set multithread device as running device with inplace mode");
  192. DEFINE_string(multi_thread_core_ids, "", "set multithread core id");
  193. REGIST_OPTION_CREATOR(xpu_device, lar::XPUDeviceOption::create_option);