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

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 "common/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. // Pass MsprofReporterCallback to runtime
  168. ge::Status rt_ret = rtSetMsprofReporterCallback(func);
  169. if (rt_ret != ge::SUCCESS) {
  170. GELOGE(rt_ret, "[Pass][Param]Pass MsprofReporterCallback to runtime failed, error_code %u",
  171. rt_ret);
  172. REPORT_CALL_ERROR("E19999", "Pass MsprofReporterCallback to runtime failed, error_code %u",
  173. rt_ret);
  174. return rt_ret;
  175. }
  176. // Pass MsprofReporterCallback to hccl
  177. }
  178. return ge::SUCCESS;
  179. }
  180. ge::Status ProfCommandHandle(ProfCommandHandleType type, void *data, uint32_t len) {
  181. if (type != kProfCommandhandleFinalize) {
  182. GE_CHECK_NOTNULL(data);
  183. }
  184. ProfCommandHandleData *prof_config_param = reinterpret_cast<ProfCommandHandleData *>(data);
  185. auto iter = kProfCommandTypeMap.find(type);
  186. if (iter == kProfCommandTypeMap.end()) {
  187. GELOGW("The prof comand type is invalid.");
  188. return ge::PARAM_INVALID;
  189. }
  190. std::vector<string> prof_params;
  191. if (type == kProfCommandhandleStart || type == kProfCommandhandleStop) {
  192. if (!isProfConfigValid(prof_config_param->devIdList, prof_config_param->devNums)) {
  193. return ge::FAILED;
  194. }
  195. if (!TransProfConfigToParam(*prof_config_param, prof_params)) {
  196. GELOGE(ge::PARAM_INVALID, "[Check][Param]Transfer profilerConfig to string vector failed");
  197. REPORT_CALL_ERROR("E19999", "Transfer profilerConfig to string vector failed");
  198. return ge::PARAM_INVALID;
  199. }
  200. }
  201. auto &profiling_manager = ge::ProfilingManager::Instance();
  202. auto is_train = domi::GetContext().train_flag;
  203. if (type == kProfCommandhandleModelSubscribe && is_train) {
  204. profiling_manager.SetSubscribeInfo(prof_config_param->profSwitch, prof_config_param->modelId, true);
  205. return ge::SUCCESS;
  206. }
  207. auto is_subscribe = profiling_manager.GetSubscribeInfo().is_subscribe;
  208. // GraphId is actually stored in prof_config_param
  209. auto graph_id = prof_config_param->modelId;
  210. ge::Status ret = NeedUnsubscribe(type, is_subscribe, graph_id, prof_params);
  211. if (ret != ge::SUCCESS) {
  212. GELOGE(ret, "graph_id:%u not not found", graph_id);
  213. REPORT_INPUT_ERROR("E10001", std::vector<std::string>({"value", "parameter", "reason"}),
  214. std::vector<std::string>({std::to_string(graph_id),
  215. "GraphToModelMap",
  216. "graph_id does not exist!"}));
  217. return ge::FAILED;
  218. }
  219. ge::GraphLoader graph_loader;
  220. ge::Command command;
  221. command.cmd_params.clear();
  222. command.cmd_type = iter->second;
  223. command.cmd_params = prof_params;
  224. if (type != kProfCommandhandleFinalize) {
  225. command.module_index = prof_config_param->profSwitch;
  226. }
  227. GELOGI("GE commandhandle execute, Command Type: %s, data type config: 0x%lx", iter->second.c_str(),
  228. command.module_index);
  229. if (type == kProfCommandhandleStart || type == kProfCommandhandleStop) {
  230. GELOGI("Profiling device nums:%s , deviceID:[%s]", prof_params[0].c_str(), prof_params[kDeviceListIndex].c_str());
  231. }
  232. ret = graph_loader.CommandHandle(command);
  233. if (ret != ge::SUCCESS) {
  234. GELOGE(ret, "[Handle][Command]Handle profiling command failed, command type %s, error_code %u",
  235. iter->second.c_str(), ret);
  236. REPORT_CALL_ERROR("E19999", "Handle profiling command failed, command type %s, error_code %u",
  237. iter->second.c_str(), ret);
  238. return ge::FAILED;
  239. }
  240. GELOGI("Successfully execute profiling command type: %d, command 0x%lx.", type, command.module_index);
  241. return ge::SUCCESS;
  242. }
  243. ge::Status ProfSetStepInfo(uint64_t index_id, uint16_t tag_id, rtStream_t stream) {
  244. static bool is_first_run = true;
  245. int32_t device_id = 0;
  246. rtError_t rt_ret = rtGetDevice(&device_id);
  247. if (rt_ret != RT_ERROR_NONE) {
  248. GELOGE(rt_ret, "[Get][LogicDeviceId]Failed, ret 0x%X", rt_ret);
  249. REPORT_CALL_ERROR("E19999", "Get logic device id failed, ret 0x%X", rt_ret);
  250. return ge::FAILED;
  251. }
  252. auto &profiling_manager = ge::ProfilingManager::Instance();
  253. profiling_manager.SetStepInfoIndex(index_id);
  254. if (is_first_run && tag_id == kStepStart) {
  255. GE_CHK_STATUS_RET_NOLOG(profiling_manager.ProfileStepInfo(index_id, kModelId, tag_id, stream, device_id));
  256. is_first_run = false;
  257. return ge::SUCCESS;
  258. }
  259. if (!is_first_run && tag_id == kStepEnd) {
  260. GE_CHK_STATUS_RET_NOLOG(profiling_manager.ProfileStepInfo(index_id, kModelId, tag_id, stream, device_id));
  261. is_first_run = true;
  262. return ge::SUCCESS;
  263. }
  264. GELOGE(ge::FAILED, "Param tag_id:%u invalid when is_first_run is %d", tag_id, is_first_run);
  265. REPORT_INPUT_ERROR("E10001", std::vector<std::string>({"value", "parameter", "reason"}),
  266. std::vector<std::string>({std::to_string(tag_id), "tag_id",
  267. "tag id must be 0 when first run, must be 1 when second run"}));
  268. return ge::FAILED;
  269. }
  270. ge::Status ProfGetDeviceFormGraphId(uint32_t graph_id, uint32_t &device_id) {
  271. return ge::ProfilingManager::Instance().GetDeviceIdFromGraph(graph_id, device_id);
  272. }

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