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

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

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