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

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 "graph/ge_context.h"
  23. #include "init/gelib.h"
  24. #include "framework/common/ge_inner_error_codes.h"
  25. #include "model/ge_model.h"
  26. #include "framework/omg/omg_inner_types.h"
  27. namespace {
  28. const uint32_t kDeviceListIndex = 3;
  29. const std::string kDeviceNums = "devNums";
  30. const std::string kDeviceIdList = "devIdList";
  31. const std::string kProfilingInit = "prof_init";
  32. const std::string kProfilingFinalize = "prof_finalize";
  33. const std::string kProfilingStart = "prof_start";
  34. const std::string kProfilingStop = "prof_stop";
  35. const std::string kProfModelSubscribe = "prof_model_subscribe";
  36. const std::string kProfModelUnsubscribe = "prof_model_cancel_subscribe";
  37. const std::string kRtSetDeviceRegName = "profiling";
  38. const std::string kPofilingModelId = "modelId";
  39. const std::map<ProfCommandHandleType, std::string> kProfCommandTypeMap = {
  40. {kProfCommandhandleInit, kProfilingInit},
  41. {kProfCommandhandleStart, kProfilingStart},
  42. {kProfCommandhandleStop, kProfilingStop},
  43. {kProfCommandhandleFinalize, kProfilingFinalize},
  44. {kProfCommandhandleModelSubscribe, kProfModelSubscribe},
  45. {kProfCommandhandleModelUnsubscribe, kProfModelUnsubscribe}};
  46. const uint64_t kModelId = ge::INVALID_MODEL_ID;
  47. const uint16_t kStepStart = 0;
  48. const uint16_t kStepEnd = 1;
  49. ge::Status NeedUnsubscribe(ProfCommandHandleType type, bool is_subscribe,
  50. uint32_t graph_id, vector<string> &prof_params) {
  51. if (type == kProfCommandhandleModelUnsubscribe && is_subscribe) {
  52. prof_params.clear();
  53. prof_params.emplace_back(kPofilingModelId);
  54. uint32_t model_id = 0;
  55. auto ret = ge::ProfilingManager::Instance().GetModelIdFromGraph(graph_id, model_id);
  56. if (ret != ge::SUCCESS) {
  57. GELOGE(ret, "graph_id:%u not not found", graph_id);
  58. return ret;
  59. }
  60. prof_params.emplace_back(std::to_string(model_id));
  61. }
  62. return ge::SUCCESS;
  63. }
  64. } // namespace
  65. bool TransProfConfigToParam(const ProfCommandHandleData &profCommand, vector<string> &prof_config_params) {
  66. prof_config_params.clear();
  67. prof_config_params.emplace_back(kDeviceNums);
  68. prof_config_params.emplace_back(std::to_string(profCommand.devNums));
  69. prof_config_params.emplace_back(kDeviceIdList);
  70. std::string devID = "";
  71. if (profCommand.devNums == 0) {
  72. GELOGW("The device num is invalid.");
  73. return false;
  74. }
  75. for (uint32_t i = 0; i < profCommand.devNums; i++) {
  76. devID.append(std::to_string(profCommand.devIdList[i]));
  77. if (i != profCommand.devNums - 1) {
  78. devID.append(",");
  79. }
  80. }
  81. prof_config_params.push_back(devID);
  82. return true;
  83. }
  84. bool isProfConfigValid(const uint32_t *deviceid_list, uint32_t device_nums) {
  85. if (deviceid_list == nullptr) {
  86. GELOGE(ge::PARAM_INVALID, "[Check][DeviceIDList]Invalid, it is nullptr");
  87. REPORT_INNER_ERROR("E19999", "Device id list is nullptr");
  88. return false;
  89. }
  90. if (device_nums == 0 || device_nums > MAX_DEV_NUM) {
  91. GELOGE(ge::PARAM_INVALID, "[Check][DeviceNums]Invalid, device nums: %u", device_nums);
  92. REPORT_INNER_ERROR("E19999", "DeviceNums %u check invalid", device_nums);
  93. return false;
  94. }
  95. // real device num
  96. int32_t dev_count = 0;
  97. rtError_t rt_err = rtGetDeviceCount(&dev_count);
  98. if (rt_err != RT_ERROR_NONE) {
  99. GELOGE(ge::INTERNAL_ERROR, "[Get][DeviceCount]Failed, error_code %d", rt_err);
  100. REPORT_CALL_ERROR("E19999", "Get device count failed, error_code %d", rt_err);
  101. return false;
  102. }
  103. if (device_nums > static_cast<uint32_t>(dev_count)) {
  104. GELOGE(ge::PARAM_INVALID, "[Check][Param]Device num %u is not in range [1,%d]",
  105. device_nums, dev_count);
  106. REPORT_INNER_ERROR("E19999", "Device num %u check invalid, it is not in range [1,%d]",
  107. device_nums, dev_count);
  108. return false;
  109. }
  110. std::set<uint32_t> record;
  111. for (size_t i = 0; i < device_nums; ++i) {
  112. uint32_t dev_id = deviceid_list[i];
  113. if (dev_id >= static_cast<uint32_t>(dev_count)) {
  114. GELOGE(ge::PARAM_INVALID, "[Check][DeviceId]Device id %u is not in range [0,%d)",
  115. dev_id, dev_count);
  116. REPORT_CALL_ERROR("E19999", "Device id %u is not in range [0,%d)", dev_id, dev_count);
  117. return false;
  118. }
  119. if (record.count(dev_id) > 0) {
  120. GELOGE(ge::PARAM_INVALID, "[Check][DeviceId]Device id %u is duplicatedly set", dev_id);
  121. REPORT_CALL_ERROR("E19999", "Device id %u is not unique, duplicatedly set", dev_id);
  122. return false;
  123. }
  124. record.insert(dev_id);
  125. }
  126. return true;
  127. }
  128. ge::Status RegProfCtrlCallback(MsprofCtrlCallback func) {
  129. if (func == nullptr) {
  130. GELOGE(ge::PARAM_INVALID, "[Check][Param]Msprof ctrl callback is nullptr");
  131. REPORT_INNER_ERROR("E19999", "Msprof ctrl callback is nullptr");
  132. return ge::PARAM_INVALID;
  133. }
  134. if (ge::ProfilingManager::Instance().GetMsprofCallback().msprofCtrlCallback != nullptr) {
  135. GELOGW("Msprof ctrl callback is exist, just ignore it.");
  136. } else {
  137. ge::ProfilingManager::Instance().SetMsprofCtrlCallback(func);
  138. }
  139. return ge::SUCCESS;
  140. }
  141. ge::Status RegProfSetDeviceCallback(MsprofSetDeviceCallback func) {
  142. if (func == nullptr) {
  143. GELOGE(ge::PARAM_INVALID, "[Check][Param]MsprofSetDeviceCallback callback is nullptr");
  144. REPORT_INNER_ERROR("E19999", "MsprofSetDeviceCallback callback is nullptr");
  145. return ge::PARAM_INVALID;
  146. }
  147. // Pass MsprofSetDeviceCallback to runtime
  148. ge::Status rt_ret = rtRegDeviceStateCallback(kRtSetDeviceRegName.c_str(), static_cast<rtDeviceStateCallback>(func));
  149. if (rt_ret != ge::SUCCESS) {
  150. GELOGE(rt_ret, "[Pass][MsprofSetDeviceCallback]To runtime failed, ret 0x%X", rt_ret);
  151. REPORT_CALL_ERROR("E19999", "Pass MsprofSetDeviceCallback to runtime failed, ret 0x%X", rt_ret);
  152. return rt_ret;
  153. }
  154. return ge::SUCCESS;
  155. }
  156. ge::Status RegProfReporterCallback(MsprofReporterCallback func) {
  157. if (func == nullptr) {
  158. GELOGE(ge::PARAM_INVALID, "[Check][Param]MsprofReporterCallback callback is nullptr");
  159. REPORT_INNER_ERROR("E19999", "MsprofReporterCallback callback is nullptr");
  160. return ge::PARAM_INVALID;
  161. }
  162. if (ge::ProfilingManager::Instance().GetMsprofCallback().msprofReporterCallback != nullptr) {
  163. GELOGW("Msprof reporter callback is exist, just ignore it.");
  164. } else {
  165. GELOGI("GE register Msprof reporter callback.");
  166. ge::ProfilingManager::Instance().SetMsprofReporterCallback(func);
  167. auto ctrl_handle = ge::ProfilingManager::Instance().GetMsprofCtrlHandle();
  168. if (ctrl_handle != nullptr) {
  169. void *report_func = reinterpret_cast<void *>(func);
  170. ctrl_handle(0, report_func, sizeof(report_func));
  171. }
  172. // Pass MsprofReporterCallback to runtime
  173. ge::Status rt_ret = rtSetMsprofReporterCallback(func);
  174. if (rt_ret != ge::SUCCESS) {
  175. GELOGE(rt_ret, "[Pass][Param]Pass MsprofReporterCallback to runtime failed, error_code %u",
  176. rt_ret);
  177. REPORT_CALL_ERROR("E19999", "Pass MsprofReporterCallback to runtime failed, error_code %u",
  178. rt_ret);
  179. return rt_ret;
  180. }
  181. // Pass MsprofReporterCallback to hccl
  182. }
  183. return ge::SUCCESS;
  184. }
  185. ge::Status ProfCommandHandle(ProfCommandHandleType type, void *data, uint32_t len) {
  186. if (type != kProfCommandhandleFinalize) {
  187. GE_CHECK_NOTNULL(data);
  188. }
  189. ProfCommandHandleData *prof_config_param = reinterpret_cast<ProfCommandHandleData *>(data);
  190. auto iter = kProfCommandTypeMap.find(type);
  191. if (iter == kProfCommandTypeMap.end()) {
  192. GELOGW("The prof comand type is invalid.");
  193. return ge::PARAM_INVALID;
  194. }
  195. auto ctrl_handle = ge::ProfilingManager::Instance().GetMsprofCtrlHandle();
  196. if (ctrl_handle != nullptr) {
  197. if (type == kProfCommandhandleStart) {
  198. ctrl_handle(1, data, len);
  199. }
  200. if (type == kProfCommandhandleStop) {
  201. ctrl_handle(2, data, len);
  202. }
  203. }
  204. std::vector<string> prof_params;
  205. if (type == kProfCommandhandleStart || type == kProfCommandhandleStop) {
  206. if (!isProfConfigValid(prof_config_param->devIdList, prof_config_param->devNums)) {
  207. return ge::FAILED;
  208. }
  209. if (!TransProfConfigToParam(*prof_config_param, prof_params)) {
  210. GELOGE(ge::PARAM_INVALID, "[Check][Param]Transfer profilerConfig to string vector failed");
  211. REPORT_CALL_ERROR("E19999", "Transfer profilerConfig to string vector failed");
  212. return ge::PARAM_INVALID;
  213. }
  214. }
  215. auto &profiling_manager = ge::ProfilingManager::Instance();
  216. auto is_train = domi::GetContext().train_flag;
  217. if (type == kProfCommandhandleModelSubscribe && is_train) {
  218. profiling_manager.SetSubscribeInfo(prof_config_param->profSwitch, prof_config_param->modelId, true);
  219. return ge::SUCCESS;
  220. }
  221. auto is_subscribe = profiling_manager.GetSubscribeInfo().is_subscribe;
  222. // GraphId is actually stored in prof_config_param
  223. auto graph_id = prof_config_param->modelId;
  224. ge::Status ret = NeedUnsubscribe(type, is_subscribe, graph_id, prof_params);
  225. if (ret != ge::SUCCESS) {
  226. GELOGE(ret, "graph_id:%u not not found", graph_id);
  227. REPORT_INPUT_ERROR("E10001", std::vector<std::string>({"value", "parameter", "reason"}),
  228. std::vector<std::string>({std::to_string(graph_id),
  229. "GraphToModelMap",
  230. "graph_id does not exist!"}));
  231. return ge::FAILED;
  232. }
  233. ge::GraphLoader graph_loader;
  234. ge::Command command;
  235. command.cmd_params.clear();
  236. command.cmd_type = iter->second;
  237. command.cmd_params = prof_params;
  238. if (type != kProfCommandhandleFinalize) {
  239. command.module_index = prof_config_param->profSwitch;
  240. }
  241. GELOGI("GE commandhandle execute, Command Type: %s, data type config: 0x%lx", iter->second.c_str(),
  242. command.module_index);
  243. if (type == kProfCommandhandleStart || type == kProfCommandhandleStop) {
  244. GELOGI("Profiling device nums:%s , deviceID:[%s]", prof_params[0].c_str(), prof_params[kDeviceListIndex].c_str());
  245. }
  246. ret = graph_loader.CommandHandle(command);
  247. if (ret != ge::SUCCESS) {
  248. GELOGE(ret, "[Handle][Command]Handle profiling command failed, command type %s, error_code %u",
  249. iter->second.c_str(), ret);
  250. REPORT_CALL_ERROR("E19999", "Handle profiling command failed, command type %s, error_code %u",
  251. iter->second.c_str(), ret);
  252. return ge::FAILED;
  253. }
  254. GELOGI("Successfully execute profiling command type: %d, command 0x%lx.", type, command.module_index);
  255. return ge::SUCCESS;
  256. }
  257. ge::Status ProfSetStepInfo(uint64_t index_id, uint16_t tag_id, rtStream_t stream) {
  258. static bool is_first_run = true;
  259. int32_t device_id = 0;
  260. rtError_t rt_ret = rtGetDevice(&device_id);
  261. if (rt_ret != RT_ERROR_NONE) {
  262. GELOGE(rt_ret, "[Get][LogicDeviceId]Failed, ret 0x%X", rt_ret);
  263. REPORT_CALL_ERROR("E19999", "Get logic device id failed, ret 0x%X", rt_ret);
  264. return ge::FAILED;
  265. }
  266. auto &profiling_manager = ge::ProfilingManager::Instance();
  267. profiling_manager.SetStepInfoIndex(index_id);
  268. if (is_first_run && tag_id == kStepStart) {
  269. GE_CHK_STATUS_RET_NOLOG(profiling_manager.ProfileStepInfo(index_id, kModelId, tag_id, stream, device_id));
  270. is_first_run = false;
  271. return ge::SUCCESS;
  272. }
  273. if (!is_first_run && tag_id == kStepEnd) {
  274. GE_CHK_STATUS_RET_NOLOG(profiling_manager.ProfileStepInfo(index_id, kModelId, tag_id, stream, device_id));
  275. is_first_run = true;
  276. return ge::SUCCESS;
  277. }
  278. GELOGE(ge::FAILED, "Param tag_id:%u invalid when is_first_run is %d", tag_id, is_first_run);
  279. REPORT_INPUT_ERROR("E10001", std::vector<std::string>({"value", "parameter", "reason"}),
  280. std::vector<std::string>({std::to_string(tag_id), "tag_id",
  281. "tag id must be 0 when first run, must be 1 when second run"}));
  282. return ge::FAILED;
  283. }
  284. ge::Status ProfGetDeviceFormGraphId(uint32_t graph_id, uint32_t &device_id) {
  285. return ge::ProfilingManager::Instance().GetDeviceIdFromGraph(graph_id, device_id);
  286. }
  287. ge::Status ProfRegisterCtrlCallback(MsprofCtrlHandle func) {
  288. if (func == nullptr) {
  289. GELOGE(ge::PARAM_INVALID, "[Check][Param]Msprof ctrl callback is nullptr");
  290. REPORT_INNER_ERROR("E19999", "Msprof ctrl callback is nullptr");
  291. return ge::PARAM_INVALID;
  292. }
  293. if (ge::ProfilingManager::Instance().GetMsprofCtrlHandle() != nullptr) {
  294. GELOGW("Msprof ctrl callback is exist, just ignore it.");
  295. } else {
  296. ge::ProfilingManager::Instance().SetMsprofCtrlHandle(func);
  297. }
  298. return ge::SUCCESS;
  299. }

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