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.

profiling_init.cc 9.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /**
  2. * Copyright 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 "profiling_init.h"
  17. #include "common/properties_manager.h"
  18. #include "framework/common/debug/ge_log.h"
  19. #include "framework/common/debug/log.h"
  20. #include "common/profiling/profiling_properties.h"
  21. #include "runtime/base.h"
  22. #include "profiling_manager.h"
  23. namespace {
  24. const char *const kTrainingTrace = "training_trace";
  25. const char *const kFpPoint = "fp_point";
  26. const char *const kBpPoint = "bp_point";
  27. }
  28. namespace ge {
  29. ProfilingInit &ProfilingInit::Instance() {
  30. static ProfilingInit profiling_init;
  31. return profiling_init;
  32. }
  33. ge::Status ProfilingInit::Init(const Options &options) {
  34. GELOGI("ProfilingInit::Init job_id:%s", options.job_id.c_str());
  35. struct MsprofGeOptions prof_conf = {{0}};
  36. bool is_execute_profiling = false;
  37. Status ret = InitFromOptions(options, prof_conf, is_execute_profiling);
  38. if (ret != SUCCESS) {
  39. GELOGE(ret, "[Init][Profiling]Failed, error_code %u", ret);
  40. REPORT_CALL_ERROR("E19999", "Init profiling failed, error_code %u", ret);
  41. return ret;
  42. }
  43. if (is_execute_profiling) {
  44. int32_t cb_ret = MsprofInit(static_cast<uint32_t>(MsprofCtrlCallbackType::MSPROF_CTRL_INIT_GE_OPTIONS),
  45. static_cast<void *>(&prof_conf), sizeof(MsprofGeOptions));
  46. if (cb_ret != 0) {
  47. GELOGE(FAILED, "[Call][msprofCtrlCallback]Failed, type %u, return %d",
  48. static_cast<uint32_t>(MsprofCtrlCallbackType::MSPROF_CTRL_INIT_GE_OPTIONS), cb_ret);
  49. REPORT_CALL_ERROR("E19999", "Call msprofCtrlCallback failed, type %u, return %d",
  50. static_cast<uint32_t>(MsprofCtrlCallbackType::MSPROF_CTRL_INIT_GE_OPTIONS), cb_ret);
  51. return FAILED;
  52. }
  53. GELOGI("Profiling init success");
  54. }
  55. else {
  56. GELOGI("The profiling is off, skip the initialization");
  57. }
  58. return SUCCESS;
  59. }
  60. ge::Status ProfilingInit::ProfRegisterCtrlCallback() {
  61. auto &prof_manager = ge::ProfilingManager::Instance();
  62. MsprofCtrlHandle callback = prof_manager.CommandHandle;
  63. rtError_t rt_ret = rtProfRegisterCtrlCallback(GE,callback);
  64. if (rt_ret != RT_ERROR_NONE) {
  65. GELOGE(FAILED, "register func failed");
  66. return FAILED;
  67. }
  68. return SUCCESS;
  69. }
  70. ge::Status ProfilingInit::InitFromOptions(const Options &options, MsprofGeOptions &prof_conf,
  71. bool &is_execute_profiling) {
  72. // enable profiling by env
  73. char env_profiling_mode[MMPA_MAX_PATH] = {0x00};
  74. if (options.profiling_mode == "1" && !options.profiling_options.empty()) {
  75. // enable profiling by ge option
  76. if (strncpy_s(prof_conf.options, MSPROF_OPTIONS_DEF_LEN_MAX, options.profiling_options.c_str(),
  77. MSPROF_OPTIONS_DEF_LEN_MAX - 1) != EOK) {
  78. GELOGE(INTERNAL_ERROR, "[copy][ProfilingOptions]Failed, options %s", options.profiling_options.c_str());
  79. REPORT_CALL_ERROR("E19999", "Copy profiling_options %s failed", options.profiling_options.c_str());
  80. return INTERNAL_ERROR;
  81. }
  82. is_execute_profiling = true;
  83. GELOGI("The profiling in options is %s, %s. origin option: %s", options.profiling_mode.c_str(), prof_conf.options,
  84. options.profiling_options.c_str());
  85. } else {
  86. (void)mmGetEnv("PROFILING_MODE", env_profiling_mode, MMPA_MAX_PATH);
  87. (void)mmGetEnv("PROFILING_OPTIONS", prof_conf.options, MSPROF_OPTIONS_DEF_LEN_MAX);
  88. // The env is invalid
  89. if ((strcmp("true", env_profiling_mode) != 0) || (strcmp(prof_conf.options, "\0") == 0)) {
  90. return SUCCESS;
  91. }
  92. // enable profiling by env
  93. is_execute_profiling = true;
  94. GELOGI("The profiling in env is %s, %s", env_profiling_mode, prof_conf.options);
  95. }
  96. ProfilingProperties::Instance().SetExecuteProfiling(is_execute_profiling);
  97. if (!is_execute_profiling) {
  98. return SUCCESS;
  99. }
  100. // Parse json str for bp fp
  101. Status ret = ParseOptions(prof_conf.options);
  102. if (ret != ge::SUCCESS) {
  103. GELOGE(ge::PARAM_INVALID, "[Parse][Options]Parse training trace param %s failed, error_code %u", prof_conf.options,
  104. ret);
  105. REPORT_CALL_ERROR("E19999", "Parse training trace param %s failed, error_code %u", prof_conf.options, ret);
  106. return ge::PARAM_INVALID;
  107. }
  108. if (strncpy_s(prof_conf.jobId, MSPROF_OPTIONS_DEF_LEN_MAX, options.job_id.c_str(), MSPROF_OPTIONS_DEF_LEN_MAX - 1) !=
  109. EOK) {
  110. GELOGE(INTERNAL_ERROR, "[Copy][JobId]Failed, original job_id %s", options.job_id.c_str());
  111. REPORT_CALL_ERROR("E19999", "Copy job_id %s failed", options.job_id.c_str());
  112. return INTERNAL_ERROR;
  113. }
  114. GELOGI("Job id: %s, original job id: %s.", prof_conf.jobId, options.job_id.c_str());
  115. return ge::SUCCESS;
  116. }
  117. ge::Status ProfilingInit::ParseOptions(const std::string &options) {
  118. if (options.empty()) {
  119. GELOGE(ge::PARAM_INVALID, "[Check][Param]Profiling options is empty");
  120. REPORT_INNER_ERROR("E19999", "Profiling options is empty");
  121. return ge::PARAM_INVALID;
  122. }
  123. try {
  124. Json prof_options = Json::parse(options);
  125. if (options.find(kTrainingTrace) == std::string::npos) {
  126. return ge::SUCCESS;
  127. }
  128. std::string training_trace;
  129. if (prof_options.contains(kTrainingTrace)) {
  130. training_trace = prof_options[kTrainingTrace];
  131. }
  132. if (training_trace.empty()) {
  133. GELOGI("Training trace will not take effect.");
  134. return ge::SUCCESS;
  135. }
  136. GELOGI("GE profiling training trace:%s", training_trace.c_str());
  137. if (training_trace != "on") {
  138. GELOGE(ge::PARAM_INVALID, "[Check][Param]Training trace param:%s is invalid.", training_trace.c_str());
  139. REPORT_INNER_ERROR("E19999", "Training trace param:%s is invalid.", training_trace.c_str());
  140. return ge::PARAM_INVALID;
  141. }
  142. string fp_point;
  143. string bp_point;
  144. if (prof_options.contains(kFpPoint)) {
  145. fp_point = prof_options[kFpPoint];
  146. }
  147. if (prof_options.contains(kBpPoint)) {
  148. bp_point = prof_options[kBpPoint];
  149. }
  150. if (!fp_point.empty() && !bp_point.empty()) {
  151. GELOGI("Training trace bp fp is set, bp_point:%s, fp_point:%s.", bp_point.c_str(), fp_point.c_str());
  152. }
  153. ProfilingProperties::Instance().SetTrainingTrace(true);
  154. ProfilingProperties::Instance().SetFpBpPoint(fp_point,bp_point);
  155. } catch (...) {
  156. GELOGE(FAILED, "[Check][Param]Json prof_conf options is invalid");
  157. REPORT_INNER_ERROR("E19999", "Json prof_conf options is invalid");
  158. return ge::PARAM_INVALID;
  159. }
  160. return ge::SUCCESS;
  161. }
  162. void ProfilingInit::StopProfiling() {
  163. uint64_t module = GetProfilingModule();
  164. // The following if case will not be executed in normal case, inc case of ProfStopProfiling is abnormal
  165. auto device_id = ProfilingProperties::Instance().GetDeviceID();
  166. int32_t device_num = static_cast<int32_t>(device_id.size());
  167. if (device_num != 0) {
  168. auto device_id_ptr = std::unique_ptr<uint32_t[]>(new (std::nothrow) uint32_t[device_num]);
  169. if (device_id_ptr == nullptr) {
  170. GELOGE(FAILED, "[Stop][Profiling]Device id ptr is null.");
  171. REPORT_INNER_ERROR("E19999", "Stop profiling, device id ptr is null");
  172. return;
  173. }
  174. for (int32_t i = 0; i < device_num; i++) {
  175. device_id_ptr[i] = static_cast<uint32_t>(device_id[i]);
  176. }
  177. rtError_t rt_ret = rtProfilerStop(module, device_num, device_id_ptr.get());
  178. if (rt_ret != RT_ERROR_NONE) {
  179. GELOGW("Call rtProfilerStop failed, ret:%d", rt_ret);
  180. }
  181. }
  182. // stop profiling
  183. int32_t cb_ret = MsprofFinalize();
  184. if (cb_ret != 0) {
  185. GELOGW("call msprofCtrlCallback failed, type:%u, return:%d",
  186. static_cast<uint32_t>(MsprofCtrlCallbackType::MSPROF_CTRL_FINALIZE), cb_ret);
  187. return;
  188. }
  189. GELOGI("Stop Profiling success.");
  190. }
  191. void ProfilingInit::ShutDownProfiling() {
  192. StopProfiling();
  193. ProfilingManager::Instance().PluginUnInit();
  194. }
  195. uint64_t ProfilingInit::GetProfilingModule() {
  196. uint64_t module = PROF_MODEL_EXECUTE_MASK |
  197. PROF_RUNTIME_API_MASK |
  198. PROF_RUNTIME_TRACE_MASK |
  199. PROF_SCHEDULE_TIMELINE_MASK |
  200. PROF_SCHEDULE_TRACE_MASK |
  201. PROF_TASK_TIME_MASK |
  202. PROF_SUBTASK_TIME_MASK |
  203. PROF_AICPU_TRACE_MASK |
  204. PROF_AICORE_METRICS_MASK |
  205. PROF_AIVECTORCORE_METRICS_MASK |
  206. PROF_MODEL_LOAD_MASK;
  207. return module;
  208. }
  209. Status ProfilingInit::SetDeviceIdByModelId(uint32_t model_id, uint32_t &device_id) {
  210. auto rt_ret = rtSetDeviceIdByModelIdx(model_id, device_id);
  211. if (rt_ret != RT_ERROR_NONE) {
  212. GELOGE(ge::FAILED, "[Set][Device]Set Device id failed");
  213. return ge::FAILED;
  214. }
  215. return ge::SUCCESS;
  216. }
  217. Status ProfilingInit::UnsetDeviceIdByModelId(uint32_t model_id, uint32_t &device_id) {
  218. auto rt_ret = rtSetDeviceIdByModelIdx(model_id, device_id);
  219. if (rt_ret != RT_ERROR_NONE) {
  220. GELOGE(ge::FAILED, "[Unset][Device]Set Device id failed");
  221. return ge::FAILED;
  222. }
  223. return ge::SUCCESS;
  224. }
  225. } // namespace ge

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