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