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

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

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