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.

csa_interact.cc 8.2 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /**
  2. * Copyright 2019-2020 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 "omm/csa_interact.h"
  17. #include "framework/common/debug/ge_log.h"
  18. #include "framework/common/debug/log.h"
  19. #include "framework/common/util.h"
  20. #include "graph/ge_context.h"
  21. #include "graph/manager/graph_var_manager.h"
  22. #include "graph/utils/tensor_utils.h"
  23. #include "mmpa/mmpa_api.h"
  24. #include "nlohmann/json.hpp"
  25. namespace ge {
  26. namespace {
  27. const char FMK_STATUS_FILE_DIR_ENV[] = "FMK_STATUS_FILE_DIR";
  28. const char JOBSTATE_FILE_NAME[] = "jobstateupdate_framework";
  29. const char HCOM_DETECT_FILE_NAME[] = "hcom_detection_result";
  30. const char FILE_SEPARATE[] = "/";
  31. } // namespace
  32. ///
  33. /// @brief Obtain CsaInteract instance
  34. /// @return CsaInteract instance
  35. ///
  36. CsaInteract &CsaInteract::GetInstance() {
  37. static CsaInteract instance;
  38. return instance;
  39. }
  40. ///
  41. /// @brief CsaInteract instance initialization
  42. /// @param [in] dev_index device index
  43. /// @param [in] job_id job id
  44. /// @return void
  45. ///
  46. void CsaInteract::Init(int32_t dev_index, int64_t job_id) {
  47. if (!is_init_) {
  48. dev_index_ = dev_index;
  49. job_id_ = job_id;
  50. char *file_dir_env = std::getenv(FMK_STATUS_FILE_DIR_ENV);
  51. string csa_path_prefix;
  52. if (file_dir_env != nullptr) {
  53. csa_path_prefix = file_dir_env;
  54. }
  55. if (!csa_path_prefix.empty()) {
  56. job_state_file_ = csa_path_prefix + std::to_string(dev_index_) + FILE_SEPARATE + JOBSTATE_FILE_NAME;
  57. hcom_detect_file_ = csa_path_prefix + std::to_string(dev_index_) + FILE_SEPARATE + HCOM_DETECT_FILE_NAME;
  58. }
  59. is_init_ = true;
  60. }
  61. }
  62. ///
  63. /// @brief Update job state file
  64. /// @param [in] job_state job state
  65. /// @param [in] job_sub_state detailed job state
  66. /// @param [in] module_ret_errcode sub module training failure error code
  67. /// @param [in] error_module error module identified by FMK
  68. /// @return Status
  69. ///
  70. Status CsaInteract::WriteJobState(JobState job_state, JobSubState job_sub_state, uint32_t module_ret_errcode,
  71. ErrorModule error_module) {
  72. if (!is_init_) {
  73. GELOGE(INTERNAL_ERROR, "CsaInteract has not init, can't WriteJobState");
  74. return INTERNAL_ERROR;
  75. }
  76. if ((curr_state_ == JOBSTATE_FAILED) || (curr_state_ == JOBSTATE_KILLED)) {
  77. return SUCCESS;
  78. }
  79. if (job_state_file_.empty()) {
  80. return SUCCESS;
  81. }
  82. std::string content;
  83. try {
  84. nlohmann::json content_json;
  85. content_json["job_id"] = job_id_;
  86. content_json["jobstate"] = job_state;
  87. // Only the running or running failure state has a job sub state
  88. if ((job_state == JOBSTATE_RUNNING) || (job_state == JOBSTATE_FAILED)) {
  89. content_json["job_sub_state"] = job_sub_state;
  90. }
  91. content_json["time"] = CurrentTimeInStr();
  92. // Write error code only if run failed
  93. if (job_state == JOBSTATE_FAILED) {
  94. content_json["errorcode"] = module_ret_errcode;
  95. content_json["errmodule"] = error_module;
  96. }
  97. content = content_json.dump();
  98. } catch (const nlohmann::json::exception &e) {
  99. GELOGE(INTERNAL_ERROR, "build jobstate content json string failed, exception:%s job_state:%u", e.what(), job_state);
  100. return INTERNAL_ERROR;
  101. }
  102. if (WriteFile(job_state_file_, content) != SUCCESS) {
  103. // The error log subfunction has been printed and will not print again
  104. return INTERNAL_ERROR;
  105. }
  106. curr_state_ = job_state;
  107. return SUCCESS;
  108. }
  109. ///
  110. /// @brief Update error code in the job state file
  111. /// @param [in] module_ret_errcode sub module training failure error code
  112. /// @param [in] error_module error module identified by FMK
  113. /// @param [in] job_sub_state detailed job state
  114. /// @return void
  115. ///
  116. void CsaInteract::WriteErrorCode(uint32_t module_ret_errcode, ErrorModule error_module, JobSubState job_sub_state) {
  117. // The error log subfunction has been printed and will not print again
  118. Status ret = WriteJobState(JOBSTATE_FAILED, job_sub_state, module_ret_errcode, error_module);
  119. if (ret != SUCCESS) {
  120. GELOGW("write error code fail. ret_code: %u, status: %u", module_ret_errcode, job_sub_state);
  121. }
  122. }
  123. ///
  124. /// @brief Record errors that occurred durning the training
  125. /// @param [in] module_ret_errcode sub module training failure error code
  126. /// @param [in] error_module error module identified by FMK
  127. /// @param [in] job_sub_state detailed job state
  128. /// @return void
  129. ///
  130. void CsaInteract::StoreInternalErrorCode(uint32_t module_ret_errcode, ErrorModule error_module,
  131. JobSubState job_sub_state) {
  132. is_have_internal_error_ = true;
  133. csa_error_code_.module_ret_errcode = module_ret_errcode;
  134. csa_error_code_.error_module = error_module;
  135. csa_error_code_.job_sub_state = job_sub_state;
  136. }
  137. ///
  138. /// @brief Update training error code in the job state file
  139. /// @return void
  140. ///
  141. void CsaInteract::WriteInternalErrorCode() {
  142. if (is_have_internal_error_) {
  143. WriteErrorCode(csa_error_code_.module_ret_errcode, csa_error_code_.error_module, csa_error_code_.job_sub_state);
  144. }
  145. }
  146. ///
  147. /// @brief Update network connectivity detect file
  148. /// @param [in] content network connectivity content
  149. /// @return Status
  150. ///
  151. Status CsaInteract::WriteHcomDetection(const std::string &content) {
  152. if (!is_init_) {
  153. GELOGE(INTERNAL_ERROR, "CsaInteract has not init, can't WriteJobState");
  154. return INTERNAL_ERROR;
  155. }
  156. if (hcom_detect_file_.empty()) {
  157. return SUCCESS;
  158. }
  159. return WriteFile(hcom_detect_file_, content);
  160. }
  161. ///
  162. /// @ingroup WriteFile
  163. /// @brief Write the content into the file. If the file does not exist, create the file
  164. /// @param [in] file_name: File name to be written
  165. /// @param [in] content: Contents to be written
  166. /// @return Status
  167. ///
  168. Status CsaInteract::WriteFile(const std::string &file_name, const std::string &content) {
  169. // if file path is not exist, then make path
  170. INT32 flags = O_WRONLY | O_TRUNC | O_CREAT;
  171. int32_t fd = mmOpen2(file_name.c_str(), flags, M_IRUSR | M_IWUSR | S_IRGRP);
  172. if (fd == EN_ERROR) {
  173. if (MakePath(file_name) != SUCCESS) {
  174. GELOGE(INTERNAL_ERROR, "csainteract create file path fail, errno is %d", errno);
  175. return INTERNAL_ERROR;
  176. }
  177. fd = mmOpen2(file_name.c_str(), flags, M_IRUSR | M_IWUSR | S_IRGRP);
  178. if (fd == EN_ERROR) {
  179. GELOGE(INTERNAL_ERROR, "open file fail, errno is %d", errno);
  180. return INTERNAL_ERROR;
  181. }
  182. }
  183. ssize_t ret = write(fd, content.c_str(), content.length());
  184. if (ret == EN_ERROR) {
  185. GELOGE(INTERNAL_ERROR, "write file fail, errno is %d", errno);
  186. ret = mmClose(fd);
  187. if (ret == EN_ERROR) {
  188. GELOGE(INTERNAL_ERROR, "close file fail, error is %d", errno);
  189. }
  190. return INTERNAL_ERROR;
  191. }
  192. ret = mmClose(fd);
  193. if (ret == EN_ERROR) {
  194. GELOGE(INTERNAL_ERROR, "close file fail, error is %d", errno);
  195. return INTERNAL_ERROR;
  196. }
  197. return SUCCESS;
  198. }
  199. ///
  200. /// @ingroup MakePath
  201. /// @brief Verify whether the file path exists, if not, recursively create the folder
  202. /// @param [in] file_name: File name to be verified
  203. /// @return Status
  204. ///
  205. Status CsaInteract::MakePath(const std::string &file_name) {
  206. std::size_t found = file_name.find_last_of("/");
  207. if (found == std::string::npos) {
  208. return PARAM_INVALID;
  209. }
  210. std::string file_path = file_name.substr(0, found + 1);
  211. if (mmAccess(file_path.c_str()) == EN_OK) {
  212. return SUCCESS;
  213. }
  214. found = file_path.find_first_of("/");
  215. while (found != std::string::npos) {
  216. std::string pre_path = file_path.substr(0, found + 1);
  217. if (mmAccess(pre_path.c_str()) != EN_OK) {
  218. if (mmMkdir(pre_path.c_str(), S_IRWXU) != EN_OK) {
  219. GELOGE(INTERNAL_ERROR, "csainteract mkdir fail, errno is %d", errno);
  220. return INTERNAL_ERROR;
  221. }
  222. }
  223. found = file_path.find_first_of("/", found + 1);
  224. }
  225. return SUCCESS;
  226. }
  227. } // namespace ge

图引擎模块(GE)是MindSpore的一个子模块,其代码由C++实现,位于前端模块ME和底层硬件之间,起到承接作用。图引擎模块以ME下发的图作为输入,然后进行一系列的深度图优化操作,最后输出一张可以在底层硬件上高效运行的图。GE针对昇腾AI处理器的硬件结构特点,做了特定的优化工作,以此来充分发挥出昇腾AI处理器的强大算力。在进行模型训练/推理时,GE会被自动调用而用户并不感知。GE主要由GE API和GE Core两部分组成,详细的架构图如下所示