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.

context_extends.cc 13 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /**
  2. * Copyright 2019 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/context_extends.h"
  17. #include <map>
  18. #include <string>
  19. #include <memory>
  20. #include <thread>
  21. #include <atomic>
  22. namespace mindspore {
  23. namespace context {
  24. #ifdef ENABLE_GE
  25. using mindspore::transform::DfGraphManager;
  26. #endif
  27. #ifndef NO_DLIB
  28. // Open tdt dataset
  29. bool OpenTsd(const std::shared_ptr<MsContext> &ms_context_ptr) {
  30. if (ms_context_ptr == nullptr) {
  31. MS_LOG(EXCEPTION) << "nullptr";
  32. }
  33. if (ms_context_ptr->is_pynative_ge_init()) {
  34. return true;
  35. }
  36. if (ms_context_ptr->tsd_ref()) {
  37. MS_LOG(DEBUG) << "TDT Dataset client is already opened.";
  38. ms_context_ptr->set_tsd_ref("++");
  39. return true;
  40. }
  41. auto role = common::GetEnv("MS_ROLE");
  42. if (strcmp(role.c_str(), "MS_SCHED") == 0 || strcmp(role.c_str(), "MS_PSERVER") == 0) {
  43. return true;
  44. }
  45. unsigned int device_id;
  46. unsigned int rank_size = 1;
  47. device_id = ms_context_ptr->device_id();
  48. auto rank_size_env = common::GetEnv("RANK_SIZE");
  49. if (rank_size_env.empty()) {
  50. MS_LOG(INFO) << "Should config rank size.";
  51. rank_size = 1;
  52. } else {
  53. int rank_env = std::stoi(rank_size_env);
  54. if (rank_env <= 0) {
  55. MS_LOG(EXCEPTION) << "Error rank size " << rank_env << ".";
  56. }
  57. rank_size = IntToUint(rank_env);
  58. }
  59. MS_LOG(INFO) << "Device id = " << device_id << ", rank size = " << rank_size << ".";
  60. TDT_StatusT status = TsdOpen(device_id, rank_size);
  61. if (status != TDT_OK) {
  62. MS_LOG(EXCEPTION) << "Device " << device_id << " is occupied, open tsd failed, status = " << status << ".";
  63. return false;
  64. }
  65. ms_context_ptr->set_tsd_ref("++");
  66. #ifdef ENABLE_TDTQUE
  67. int32_t initStatus = tdt::TdtHostInit(device_id);
  68. if (initStatus != TDT_OK_CODE) {
  69. MS_LOG(EXCEPTION) << "Init tsd failed, status = " << initStatus << ".";
  70. return false;
  71. }
  72. ms_context_ptr->tdt_print_ = std::thread(TensorPrint());
  73. #endif
  74. MS_LOG(INFO) << "Open and init tsd successful, tsd reference = " << ms_context_ptr->tsd_ref() << ".";
  75. return true;
  76. }
  77. bool CloseTsd(const std::shared_ptr<MsContext> &ms_context_ptr, bool force) {
  78. if (ms_context_ptr == nullptr) {
  79. MS_LOG(EXCEPTION) << "nullptr";
  80. }
  81. if (ms_context_ptr->tsd_ref() == 0) {
  82. return true;
  83. }
  84. ms_context_ptr->set_tsd_ref("--");
  85. if (force || ms_context_ptr->tsd_ref() == 0) {
  86. ms_context_ptr->set_tsd_ref(" ");
  87. #ifdef ENABLE_TDTQUE
  88. int32_t stopStatus = tdt::TdtHostStop(KNpuLog);
  89. if (stopStatus != TDT_OK_CODE) {
  90. MS_LOG(EXCEPTION) << "Stop tsd failed, status = " << stopStatus << ".";
  91. return false;
  92. }
  93. py::gil_scoped_release gil_release;
  94. int32_t destroyStatus = tdt::TdtHostDestroy();
  95. if (destroyStatus != TDT_OK_CODE) {
  96. MS_LOG(EXCEPTION) << "Destroy tsd failed, status = " << destroyStatus << ".";
  97. return false;
  98. }
  99. try {
  100. if (ms_context_ptr->tdt_print_.joinable()) {
  101. MS_LOG(INFO) << "join tdt host receive process";
  102. ms_context_ptr->tdt_print_.join();
  103. }
  104. } catch (const std::exception &e) {
  105. MS_LOG(ERROR) << "tdt thread join failed: " << e.what();
  106. }
  107. #endif
  108. auto device_id = ms_context_ptr->device_id();
  109. TDT_StatusT status = TsdClose(device_id);
  110. if (status != TDT_OK) {
  111. MS_LOG(EXCEPTION) << "Close tsd failed, status = " << status << ".";
  112. return false;
  113. }
  114. ms_context_ptr->set_pynative_ge_init(false);
  115. MS_LOG(INFO) << "Destroy and close tsd successful, status = " << status << ".";
  116. } else {
  117. MS_LOG(DEBUG) << "TDT Dataset client is used, no need to close, tsd reference = " << ms_context_ptr->tsd_ref()
  118. << ".";
  119. }
  120. return true;
  121. }
  122. #else
  123. bool OpenTsd(const std::shared_ptr<MsContext> &ms_context_ptr) { return true; }
  124. bool CloseTsd(const std::shared_ptr<MsContext> &ms_context_ptr, bool) { return true; }
  125. #endif
  126. void SetDisableReuseMemoryFlag(std::map<std::string, std::string> *ge_options) {
  127. auto env_disable_reuse_memory = common::GetEnv("DISABLE_REUSE_MEMORY");
  128. if (!env_disable_reuse_memory.empty()) {
  129. (*ge_options)["ge.exec.disableReuseMemory"] = env_disable_reuse_memory;
  130. } else {
  131. (*ge_options)["ge.exec.disableReuseMemory"] = "0";
  132. MS_LOG(WARNING) << "DISABLE_REUSE_MEMORY is not set in ENV. Now set to default value 0";
  133. }
  134. }
  135. void GetGeOptions(const std::shared_ptr<MsContext> &ms_context_ptr, std::map<std::string, std::string> *ge_options) {
  136. if (ms_context_ptr == nullptr) {
  137. MS_LOG(EXCEPTION) << "nullptr";
  138. }
  139. #ifdef ENABLE_GE
  140. (*ge_options)["device_id"] = "0";
  141. (*ge_options)["ge.exec.enableDump"] = std::to_string(ms_context_ptr->enable_dump());
  142. (*ge_options)["ge.exec.dumpPath"] = ms_context_ptr->save_dump_path();
  143. (*ge_options)["ge.exec.dumpMode"] = "output";
  144. MS_LOG(INFO) << "The enable dump state is " << std::to_string(ms_context_ptr->enable_dump())
  145. << " and save dump path is " << ms_context_ptr->save_dump_path() << ".";
  146. (*ge_options)["ge.exec.profilingMode"] = std::to_string(ms_context_ptr->enable_profiling());
  147. if (ms_context_ptr->enable_profiling()) {
  148. (*ge_options)["ge.exec.profilingOptions"] = ms_context_ptr->profiling_options();
  149. }
  150. (*ge_options)["rank_table_file"] = "";
  151. auto env_ddk_version = common::GetEnv("DDK_VERSION");
  152. if (!env_ddk_version.empty()) {
  153. (*ge_options)["ge.DDK_version"] = env_ddk_version;
  154. } else {
  155. (*ge_options)["ge.DDK_version"] = "1.60.T17.B830";
  156. }
  157. (*ge_options)["graphType"] = "1";
  158. if (ms_context_ptr->graph_memory_max_size() != "0") {
  159. (*ge_options)["ge.graphMemoryMaxSize"] = ms_context_ptr->graph_memory_max_size();
  160. }
  161. if (ms_context_ptr->variable_memory_max_size() != "0") {
  162. (*ge_options)["ge.variableMemoryMaxSize"] = ms_context_ptr->variable_memory_max_size();
  163. }
  164. #if ENABLE_TRAIN == 1
  165. (*ge_options)["ge.graphRunMode"] = "1";
  166. #endif
  167. SetDisableReuseMemoryFlag(ge_options);
  168. SetHcclOptions(ms_context_ptr, ge_options);
  169. auto env_job_id = common::GetEnv("JOB_ID");
  170. if (!env_job_id.empty()) {
  171. (*ge_options)["ge.exec.jobId"] = env_job_id;
  172. } else {
  173. (*ge_options)["ge.exec.jobId"] = "0";
  174. MS_LOG(WARNING) << "JOB_ID is not set in ENV. Now set to default value 0";
  175. }
  176. auto env_fe_flag = common::GetEnv("FE_FLAG");
  177. if (!env_fe_flag.empty()) {
  178. (*ge_options)["ge.feFlag"] = env_fe_flag;
  179. MS_LOG(INFO) << "Use FE, make sure fe lib is set in OPTION_EXEC_EXTERN_PLUGIN_PATH.";
  180. }
  181. auto env_aicpu_flag = common::GetEnv("AICPU_FLAG");
  182. if (!env_aicpu_flag.empty()) {
  183. (*ge_options)["ge.aicpuFlag"] = env_aicpu_flag;
  184. MS_LOG(INFO) << "Use AICPU, make sure aicpu lib is set in OPTION_EXEC_EXTERN_PLUGIN_PATH.";
  185. }
  186. auto proto_lib_path = common::GetEnv("OPTION_PROTO_LIB_PATH");
  187. if (!proto_lib_path.empty()) {
  188. char real_path[PATH_MAX] = {0};
  189. if (realpath(proto_lib_path.c_str(), real_path)) {
  190. proto_lib_path = real_path;
  191. (*ge_options)["ge.opsProtoLibPath"] = proto_lib_path;
  192. }
  193. } else {
  194. MS_LOG(WARNING) << "Set proto lib path failed!";
  195. }
  196. // Enable auto mixed precision according to the context options
  197. if (ms_context_ptr->auto_mixed_precision_flag()) {
  198. (*ge_options)["ge.exec.precision_mode"] = "allow_mix_precision";
  199. } else {
  200. (*ge_options)["ge.exec.precision_mode"] = "allow_fp32_to_fp16";
  201. }
  202. // Disable the global variable acc, only enable it whlie adding training graph in pipeline
  203. (*ge_options)["ge.exec.variable_acc"] = "0";
  204. #endif
  205. }
  206. void SetHcclOptions(const std::shared_ptr<MsContext> &ms_context_ptr, std::map<std::string, std::string> *ge_options) {
  207. if (ms_context_ptr == nullptr) {
  208. MS_LOG(EXCEPTION) << "nullptr";
  209. }
  210. auto env_table_file = common::GetEnv("RANK_TABLE_FILE");
  211. auto env_rank_id = common::GetEnv("RANK_ID");
  212. auto env_device_id = std::to_string(ms_context_ptr->device_id());
  213. if (!(env_table_file.empty() || env_rank_id.empty())) {
  214. MS_LOG(INFO) << "Initialize Ge for distribute parameter";
  215. MS_LOG(INFO) << "Use hccl, make sure hccl lib is set in OPTION_EXEC_EXTERN_PLUGIN_PATH.";
  216. auto env_hccl_flag = common::GetEnv("HCCL_FLAG");
  217. if (!env_hccl_flag.empty()) {
  218. (*ge_options)["ge.exec.hcclFlag"] = env_hccl_flag;
  219. }
  220. (*ge_options)["ge.exec.isUseHcom"] = "1";
  221. (*ge_options)["ge.exec.deviceId"] = env_device_id;
  222. (*ge_options)["ge.exec.rankId"] = env_rank_id;
  223. (*ge_options)["ge.exec.podName"] = env_rank_id;
  224. (*ge_options)["ge.exec.rankTableFile"] = env_table_file;
  225. (*ge_options)["ge.graphRunMode"] = "1";
  226. } else {
  227. // device id is still needed for non-distribute case
  228. (*ge_options)["ge.exec.deviceId"] = env_device_id;
  229. MS_LOG(INFO) << "No hccl mode. "
  230. "If use hccl, make sure [RANK_TABLE_FILE,RANK_ID,DEVICE_ID,DEPLOY_MODE] all be set in ENV.";
  231. }
  232. auto env_deploy_mode = common::GetEnv("DEPLOY_MODE");
  233. if (!env_deploy_mode.empty()) {
  234. (*ge_options)["ge.exec.deployMode"] = env_deploy_mode;
  235. } else {
  236. (*ge_options)["ge.exec.deployMode"] = "0";
  237. MS_LOG(WARNING) << "DEPLOY_MODE is not set in ENV. Now set to default value 0";
  238. }
  239. }
  240. bool InitGe(const std::shared_ptr<MsContext> &ms_context_ptr) {
  241. if (ms_context_ptr == nullptr) {
  242. MS_LOG(EXCEPTION) << "nullptr";
  243. }
  244. #ifdef ENABLE_GE
  245. if (ms_context_ptr->is_pynative_ge_init()) {
  246. return true;
  247. }
  248. if (ms_context_ptr->ge_ref()) {
  249. ms_context_ptr->set_ge_ref("++");
  250. return true;
  251. }
  252. std::map<std::string, std::string> ge_options;
  253. GetGeOptions(ms_context_ptr, &ge_options);
  254. {
  255. // Release GIL before calling into (potentially long-running) C++ code
  256. py::gil_scoped_release release;
  257. if (ge::GEInitialize(ge_options) != ge::GRAPH_SUCCESS) {
  258. MS_LOG(EXCEPTION) << "Initialize GE failed!";
  259. }
  260. }
  261. ms_context_ptr->set_ge_ref("++");
  262. MS_LOG(INFO) << "Init ge successful, ge reference = " << ms_context_ptr->ge_ref() << ".";
  263. #endif
  264. return true;
  265. }
  266. bool PynativeInitGe(const std::shared_ptr<MsContext> &ms_context_ptr) {
  267. if (ms_context_ptr == nullptr) {
  268. MS_LOG(EXCEPTION) << "nullptr";
  269. }
  270. if (ms_context_ptr->is_pynative_ge_init() || ms_context_ptr->ge_ref() || ms_context_ptr->tsd_ref()) {
  271. return true;
  272. }
  273. (void)OpenTsd(ms_context_ptr);
  274. (void)InitGe(ms_context_ptr);
  275. ms_context_ptr->set_pynative_ge_init(true);
  276. return true;
  277. }
  278. bool FinalizeGe(const std::shared_ptr<MsContext> &ms_context_ptr, bool force) {
  279. if (ms_context_ptr == nullptr) {
  280. MS_LOG(EXCEPTION) << "nullptr";
  281. }
  282. #ifdef ENABLE_GE
  283. if (ms_context_ptr->ge_ref() == 0) {
  284. return true;
  285. }
  286. ms_context_ptr->set_ge_ref("--");
  287. if (force || ms_context_ptr->ge_ref() == 0) {
  288. ms_context_ptr->set_ge_ref(" ");
  289. try {
  290. DfGraphManager::GetInstance().DeleteGraphRunner();
  291. DfGraphManager::GetInstance().DeleteGeSession();
  292. } catch (const std::exception &e) {
  293. MS_LOG(ERROR) << "Error occurred when deleting GE graph runner and session fail. Error: " << e.what();
  294. } catch (...) {
  295. std::string exName(abi::__cxa_current_exception_type()->name());
  296. MS_LOG(ERROR) << "Error occurred when deleting GE graph runner and session fail. Exception name: " << exName;
  297. }
  298. if (ge::GEFinalize() != ge::GRAPH_SUCCESS) {
  299. MS_LOG(WARNING) << "Finalize GE failed!";
  300. }
  301. ms_context_ptr->set_pynative_ge_init(false);
  302. } else {
  303. MS_LOG(INFO) << "Ge is used, no need to finalize, tsd reference = " << ms_context_ptr->ge_ref() << ".";
  304. }
  305. #endif
  306. return true;
  307. }
  308. bool IsTsdOpened(const std::shared_ptr<MsContext> &ms_context_ptr) {
  309. if (ms_context_ptr == nullptr) {
  310. MS_LOG(EXCEPTION) << "nullptr";
  311. }
  312. return ms_context_ptr->IsTsdOpened();
  313. }
  314. bool IsGeInited(const std::shared_ptr<MsContext> &ms_context_ptr) {
  315. if (ms_context_ptr == nullptr) {
  316. MS_LOG(EXCEPTION) << "nullptr";
  317. }
  318. return ms_context_ptr->IsGeInited();
  319. }
  320. // Register for device type.
  321. struct DeviceTypeSetRegister {
  322. DeviceTypeSetRegister() {
  323. MsContext::device_type_seter([](std::shared_ptr<MsContext> &device_type_seter) {
  324. #ifdef ENABLE_GE
  325. device_type_seter.reset(new (std::nothrow) MsContext("ge", kAscendDevice));
  326. #elif defined(ENABLE_D)
  327. device_type_seter.reset(new (std::nothrow) MsContext("ms", kAscendDevice));
  328. #elif defined(ENABLE_GPU)
  329. device_type_seter.reset(new (std::nothrow) MsContext("ms", kGPUDevice));
  330. #else
  331. device_type_seter.reset(new (std::nothrow) MsContext("vm", kCPUDevice));
  332. #endif
  333. });
  334. }
  335. ~DeviceTypeSetRegister() = default;
  336. } device_type_set_regsiter;
  337. } // namespace context
  338. } // namespace mindspore