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.

ge_profiling.cc 9.0 kB

4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 "framework/common/profiling/ge_profiling.h"
  17. #include "runtime/base.h"
  18. #include "common/profiling/profiling_manager.h"
  19. #include "framework/common/debug/ge_log.h"
  20. #include "framework/common/debug/log.h"
  21. #include "graph/load/graph_loader.h"
  22. #include "init/gelib.h"
  23. #include "framework/common/ge_inner_error_codes.h"
  24. namespace {
  25. const uint32_t kDeviceListIndex = 3;
  26. const std::string kDeviceNums = "devNums";
  27. const std::string kDeviceIdList = "devIdList";
  28. const std::string kProfilingInit = "prof_init";
  29. const std::string kProfilingFinalize = "prof_finalize";
  30. const std::string kProfilingStart = "prof_start";
  31. const std::string kProfilingStop = "prof_stop";
  32. const std::string kProfModelSubscribe = "prof_model_subscribe";
  33. const std::string kProfModelUnsubscribe = "prof_model_cancel_subscribe";
  34. const std::string kRtSetDeviceRegName = "profiling";
  35. const std::map<ProfCommandHandleType, std::string> kProfCommandTypeMap = {
  36. {kProfCommandhandleInit, kProfilingInit},
  37. {kProfCommandhandleStart, kProfilingStart},
  38. {kProfCommandhandleStop, kProfilingStop},
  39. {kProfCommandhandleFinalize, kProfilingFinalize},
  40. {kProfCommandhandleModelSubscribe, kProfModelSubscribe},
  41. {kProfCommandhandleModelUnsubscribe, kProfModelUnsubscribe}};
  42. } // namespace
  43. bool TransProfConfigToParam(const ProfCommandHandleData &profCommand, vector<string> &prof_config_params) {
  44. prof_config_params.clear();
  45. prof_config_params.emplace_back(kDeviceNums);
  46. prof_config_params.emplace_back(std::to_string(profCommand.devNums));
  47. prof_config_params.emplace_back(kDeviceIdList);
  48. std::string devID = "";
  49. if (profCommand.devNums == 0) {
  50. GELOGW("The device num is invalid.");
  51. return false;
  52. }
  53. for (uint32_t i = 0; i < profCommand.devNums; i++) {
  54. devID.append(std::to_string(profCommand.devIdList[i]));
  55. if (i != profCommand.devNums - 1) {
  56. devID.append(",");
  57. }
  58. }
  59. prof_config_params.push_back(devID);
  60. return true;
  61. }
  62. bool isProfConfigValid(const uint32_t *deviceid_list, uint32_t device_nums) {
  63. if (deviceid_list == nullptr) {
  64. GELOGE(ge::PARAM_INVALID, "[Check][DeviceIDList]Invalid, it is nullptr");
  65. REPORT_INNER_ERROR("E19999", "Device id list is nullptr");
  66. return false;
  67. }
  68. if (device_nums == 0 || device_nums > MAX_DEV_NUM) {
  69. GELOGE(ge::PARAM_INVALID, "[Check][DeviceNums]Invalid, device nums: %u", device_nums);
  70. REPORT_INNER_ERROR("E19999", "DeviceNums %u check invalid", device_nums);
  71. return false;
  72. }
  73. // real device num
  74. int32_t dev_count = 0;
  75. rtError_t rt_err = rtGetDeviceCount(&dev_count);
  76. if (rt_err != RT_ERROR_NONE) {
  77. GELOGE(ge::INTERNAL_ERROR, "[Get][DeviceCount]Failed, error_code %d", rt_err);
  78. REPORT_CALL_ERROR("E19999", "Get device count failed, error_code %d", rt_err);
  79. return false;
  80. }
  81. if (device_nums > static_cast<uint32_t>(dev_count)) {
  82. GELOGE(ge::PARAM_INVALID, "[Check][Param]Device num %u is not in range [1,%d]",
  83. device_nums, dev_count);
  84. REPORT_INNER_ERROR("E19999", "Device num %u check invalid, it is not in range [1,%d]",
  85. device_nums, dev_count);
  86. return false;
  87. }
  88. std::set<uint32_t> record;
  89. for (size_t i = 0; i < device_nums; ++i) {
  90. uint32_t dev_id = deviceid_list[i];
  91. if (dev_id >= static_cast<uint32_t>(dev_count)) {
  92. GELOGE(ge::PARAM_INVALID, "[Check][DeviceId]Device id %u is not in range [0,%d)",
  93. dev_id, dev_count);
  94. REPORT_CALL_ERROR("E19999", "Device id %u is not in range [0,%d)", dev_id, dev_count);
  95. return false;
  96. }
  97. if (record.count(dev_id) > 0) {
  98. GELOGE(ge::PARAM_INVALID, "[Check][DeviceId]Device id %u is duplicatedly set", dev_id);
  99. REPORT_CALL_ERROR("E19999", "Device id %u is not unique, duplicatedly set", dev_id);
  100. return false;
  101. }
  102. record.insert(dev_id);
  103. }
  104. return true;
  105. }
  106. ge::Status RegProfCtrlCallback(MsprofCtrlCallback func) {
  107. if (func == nullptr) {
  108. GELOGE(ge::PARAM_INVALID, "[Check][Param]Msprof ctrl callback is nullptr");
  109. REPORT_INNER_ERROR("E19999", "Msprof ctrl callback is nullptr");
  110. return ge::PARAM_INVALID;
  111. }
  112. if (ge::ProfilingManager::Instance().GetMsprofCallback().msprofCtrlCallback != nullptr) {
  113. GELOGW("Msprof ctrl callback is exist, just ignore it.");
  114. } else {
  115. ge::ProfilingManager::Instance().SetMsprofCtrlCallback(func);
  116. }
  117. return ge::SUCCESS;
  118. }
  119. ge::Status RegProfSetDeviceCallback(MsprofSetDeviceCallback func) {
  120. if (func == nullptr) {
  121. GELOGE(ge::PARAM_INVALID, "[Check][Param]MsprofSetDeviceCallback callback is nullptr");
  122. REPORT_INNER_ERROR("E19999", "MsprofSetDeviceCallback callback is nullptr");
  123. return ge::PARAM_INVALID;
  124. }
  125. // Pass MsprofSetDeviceCallback to runtime
  126. ge::Status rt_ret = rtRegDeviceStateCallback(kRtSetDeviceRegName.c_str(), static_cast<rtDeviceStateCallback>(func));
  127. if (rt_ret != ge::SUCCESS) {
  128. GELOGE(rt_ret, "[Pass][MsprofSetDeviceCallback]To runtime failed, ret 0x%X", rt_ret);
  129. REPORT_CALL_ERROR("E19999", "Pass MsprofSetDeviceCallback to runtime failed, ret 0x%X", rt_ret);
  130. return rt_ret;
  131. }
  132. return ge::SUCCESS;
  133. }
  134. ge::Status RegProfReporterCallback(MsprofReporterCallback func) {
  135. if (func == nullptr) {
  136. GELOGE(ge::PARAM_INVALID, "[Check][Param]MsprofReporterCallback callback is nullptr");
  137. REPORT_INNER_ERROR("E19999", "MsprofReporterCallback callback is nullptr");
  138. return ge::PARAM_INVALID;
  139. }
  140. if (ge::ProfilingManager::Instance().GetMsprofCallback().msprofReporterCallback != nullptr) {
  141. GELOGW("Msprof reporter callback is exist, just ignore it.");
  142. } else {
  143. GELOGI("GE register Msprof reporter callback.");
  144. ge::ProfilingManager::Instance().SetMsprofReporterCallback(func);
  145. // Pass MsprofReporterCallback to runtime
  146. ge::Status rt_ret = rtSetMsprofReporterCallback(func);
  147. if (rt_ret != ge::SUCCESS) {
  148. GELOGE(rt_ret, "[Pass][Param]Pass MsprofReporterCallback to runtime failed, error_code %u",
  149. rt_ret);
  150. REPORT_CALL_ERROR("E19999", "Pass MsprofReporterCallback to runtime failed, error_code %u",
  151. rt_ret);
  152. return rt_ret;
  153. }
  154. // Pass MsprofReporterCallback to hccl
  155. }
  156. return ge::SUCCESS;
  157. }
  158. ge::Status ProfCommandHandle(ProfCommandHandleType type, void *data, uint32_t len) {
  159. if (type != kProfCommandhandleFinalize) {
  160. GE_CHECK_NOTNULL(data);
  161. }
  162. ProfCommandHandleData *prof_config_param = reinterpret_cast<ProfCommandHandleData *>(data);
  163. auto iter = kProfCommandTypeMap.find(type);
  164. if (iter == kProfCommandTypeMap.end()) {
  165. GELOGW("The prof comand type is invalid.");
  166. return ge::PARAM_INVALID;
  167. }
  168. std::vector<string> prof_params;
  169. if (type == kProfCommandhandleStart || type == kProfCommandhandleStop) {
  170. if (!isProfConfigValid(prof_config_param->devIdList, prof_config_param->devNums)) {
  171. return ge::FAILED;
  172. }
  173. if (!TransProfConfigToParam(*prof_config_param, prof_params)) {
  174. GELOGE(ge::PARAM_INVALID, "[Check][Param]Transfer profilerConfig to string vector failed");
  175. REPORT_CALL_ERROR("E19999", "Transfer profilerConfig to string vector failed");
  176. return ge::PARAM_INVALID;
  177. }
  178. }
  179. ge::GraphLoader graph_loader;
  180. ge::Command command;
  181. command.cmd_params.clear();
  182. command.cmd_type = iter->second;
  183. command.cmd_params = prof_params;
  184. if (type != kProfCommandhandleFinalize) {
  185. command.module_index = prof_config_param->profSwitch;
  186. }
  187. GELOGI("GE commandhandle execute, Command Type: %s, data type config: 0x%lx", iter->second.c_str(),
  188. command.module_index);
  189. if (type == kProfCommandhandleStart || type == kProfCommandhandleStop) {
  190. GELOGI("Profiling device nums:%s , deviceID:[%s]", prof_params[0].c_str(), prof_params[kDeviceListIndex].c_str());
  191. }
  192. ge::Status ret = graph_loader.CommandHandle(command);
  193. if (ret != ge::SUCCESS) {
  194. GELOGE(ret, "[Handle][Command]Handle profiling command failed, command type %s, error_code %u",
  195. iter->second.c_str(), ret);
  196. REPORT_CALL_ERROR("E19999", "Handle profiling command failed, command type %s, error_code %u",
  197. iter->second.c_str(), ret);
  198. return ge::FAILED;
  199. }
  200. GELOGI("Successfully execute profiling command type: %d, command 0x%lx.", type, command.module_index);
  201. return ge::SUCCESS;
  202. }
  203. GE_FUNC_VISIBILITY ge::Status ProfSetStepInfo(uint64_t index_id, uint16_t tag_id, rtStream_t stream) {
  204. return ge::SUCCESS;
  205. }

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