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 8.9 kB

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

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