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_manager.cc 33 kB

5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  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 "common/profiling/profiling_manager.h"
  17. #include "framework/common/debug/ge_log.h"
  18. #include "framework/common/debug/log.h"
  19. #include "framework/common/string_util.h"
  20. #include "graph/ge_context.h"
  21. #include "runtime/base.h"
  22. #include "graph/load/new_model_manager/davinci_model.h"
  23. namespace {
  24. const char *const kTrainingTrace = "training_trace";
  25. const char *const kFpPoint = "fp_point";
  26. const char *const kBpPoint = "bp_point";
  27. #ifdef DAVINCI_SUPPORT_PROFILING
  28. const size_t kReportMaxLen = 2048;
  29. const int32_t kMaxDeviceNum = 256;
  30. const std::string kConfigNumsdev = "devNums";
  31. const std::string kConfigDevIdList = "devIdList";
  32. const std::string kProfStart = "prof_start";
  33. const std::string kProfStop = "prof_stop";
  34. const std::string kProfModelSubscribe = "prof_model_subscribe";
  35. const std::string kProfModelUnsubscribe = "prof_model_cancel_subscribe";
  36. #endif
  37. } // namespace
  38. namespace ge {
  39. ProfilingManager::ProfilingManager()
  40. : is_load_profiling_(false), is_execute_profiling_(false), is_training_trace_(false), subscribe_count_(0) {
  41. prof_cb_.msprofCtrlCallback = nullptr;
  42. prof_cb_.msprofReporterCallback = nullptr;
  43. }
  44. ProfilingManager::~ProfilingManager() {}
  45. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ProfilingManager &ProfilingManager::Instance() {
  46. static ProfilingManager profiling_manager;
  47. return profiling_manager;
  48. }
  49. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ge::Status ProfilingManager::Init(const Options &options) {
  50. #ifdef DAVINCI_SUPPORT_PROFILING
  51. vector<int32_t>().swap(device_id_);
  52. subscribe_count_ = 0;
  53. GELOGI("ProfilingManager::Init job_id:%s", options.job_id.c_str());
  54. struct MsprofGeOptions prof_conf = {{ 0 }};
  55. Status ret = InitFromOptions(options, prof_conf);
  56. if (ret != SUCCESS) {
  57. GELOGE(ret, "Failed to init profiling.");
  58. return ret;
  59. }
  60. if (is_execute_profiling_) {
  61. if (prof_cb_.msprofCtrlCallback == nullptr) {
  62. GELOGE(ge::PARAM_INVALID, "MsprofCtrlCallback callback is nullptr.");
  63. return ge::PARAM_INVALID;
  64. }
  65. int32_t cb_ret = prof_cb_.msprofCtrlCallback(
  66. static_cast<uint32_t>(MsprofCtrlCallbackType::MSPROF_CTRL_INIT_GE_OPTIONS),
  67. static_cast<void *>(&prof_conf), sizeof(MsprofGeOptions));
  68. if (cb_ret != 0) {
  69. GELOGE(FAILED, "Call msprofCtrlCallback failed, type:%u, return:%d",
  70. static_cast<uint32_t>(MsprofCtrlCallbackType::MSPROF_CTRL_INIT_GE_OPTIONS), cb_ret);
  71. return FAILED;
  72. }
  73. GELOGI("Profiling init success");
  74. } else {
  75. GELOGI("The profiling is off, skip the initialization");
  76. }
  77. #endif
  78. return SUCCESS;
  79. }
  80. ge::Status ProfilingManager::InitFromOptions(const Options &options, MsprofGeOptions &prof_conf) {
  81. #ifdef DAVINCI_SUPPORT_PROFILING
  82. // enable profiling by env
  83. char env_profiling_mode[MMPA_MAX_PATH] = { 0x00 };
  84. is_execute_profiling_ = false;
  85. if (options.profiling_mode == "1" && !options.profiling_options.empty()) {
  86. // enable profiling by ge option
  87. if (strncpy_s(prof_conf.options, MSPROF_OPTIONS_DEF_LEN_MAX, options.profiling_options.c_str(),
  88. MSPROF_OPTIONS_DEF_LEN_MAX - 1) != EOK) {
  89. GELOGE(INTERNAL_ERROR, "copy profiling_options failed.");
  90. return INTERNAL_ERROR;
  91. }
  92. is_execute_profiling_ = true;
  93. GELOGI("The profiling in options is %s, %s. origin option: %s", options.profiling_mode.c_str(), prof_conf.options,
  94. options.profiling_options.c_str());
  95. } else {
  96. (void)mmGetEnv("PROFILING_MODE", env_profiling_mode, MMPA_MAX_PATH);
  97. (void)mmGetEnv("PROFILING_OPTIONS", prof_conf.options, MSPROF_OPTIONS_DEF_LEN_MAX);
  98. // The env is invalid
  99. if ((strcmp("true", env_profiling_mode) != 0) || (strcmp(prof_conf.options, "\0") == 0)) {
  100. return SUCCESS;
  101. }
  102. // enable profiling by env
  103. is_execute_profiling_ = true;
  104. GELOGI("The profiling in env is %s, %s", env_profiling_mode, prof_conf.options);
  105. }
  106. if (!is_execute_profiling_) {
  107. return SUCCESS;
  108. }
  109. // Parse json str for bp fp
  110. Status ret = ParseOptions(prof_conf.options);
  111. if (ret != ge::SUCCESS) {
  112. GELOGE(ge::PARAM_INVALID, "Parse training trace param failed.");
  113. return ge::PARAM_INVALID;
  114. }
  115. if (strncpy_s(prof_conf.jobId, MSPROF_OPTIONS_DEF_LEN_MAX, options.job_id.c_str(), MSPROF_OPTIONS_DEF_LEN_MAX - 1) !=
  116. EOK) {
  117. GELOGE(INTERNAL_ERROR, "copy job_id failed.");
  118. return INTERNAL_ERROR;
  119. }
  120. GELOGI("Job id: %s, original job id: %s.", prof_conf.jobId, options.job_id.c_str());
  121. #endif
  122. return ge::SUCCESS;
  123. }
  124. ge::Status ProfilingManager::ParseOptions(const std::string &options) {
  125. if (options.empty()) {
  126. GELOGE(ge::PARAM_INVALID, "Profiling options is empty.");
  127. return ge::PARAM_INVALID;
  128. }
  129. try {
  130. Json prof_options = Json::parse(options);
  131. if (options.find(kTrainingTrace) == std::string::npos) {
  132. return ge::SUCCESS;
  133. }
  134. const std::string training_trace = prof_options[kTrainingTrace];
  135. if (training_trace.empty()) {
  136. GELOGI("Training trace will not take effect.");
  137. return ge::SUCCESS;
  138. }
  139. GELOGI("GE profiling training trace:%s", training_trace.c_str());
  140. if (training_trace != "on") {
  141. GELOGE(ge::PARAM_INVALID, "Training trace param:%s is invalid.", training_trace.c_str());
  142. return ge::PARAM_INVALID;
  143. }
  144. fp_point_ = prof_options[kFpPoint];
  145. bp_point_ = prof_options[kBpPoint];
  146. if (!fp_point_.empty() && !bp_point_.empty()) {
  147. GELOGI("Training trace bp fp is set, bp_point:%s, fp_point:%s.", bp_point_.c_str(), fp_point_.c_str());
  148. }
  149. is_training_trace_ = true;
  150. } catch (...) {
  151. GELOGE(FAILED, "Json prof_conf options is invalid.");
  152. return ge::PARAM_INVALID;
  153. }
  154. return ge::SUCCESS;
  155. }
  156. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::StopProfiling() {
  157. #ifdef DAVINCI_SUPPORT_PROFILING
  158. uint64_t module = GetProfilingModule();
  159. // The following if case will not be executed in normal case, inc case of ProfStopProfiling is abnormal
  160. int32_t device_num = static_cast<int32_t>(device_id_.size());
  161. if (device_num != 0) {
  162. auto device_id_ptr = std::unique_ptr<uint32_t[]>(new (std::nothrow) uint32_t[device_num]);
  163. if (device_id_ptr == nullptr) {
  164. GELOGE(FAILED, "Stop profiling: device id ptr is null.");
  165. return;
  166. }
  167. for (int32_t i = 0; i < device_num; i++) {
  168. device_id_ptr[i] = static_cast<uint32_t>(device_id_[i]);
  169. }
  170. rtError_t rt_ret = rtProfilerStop(module, device_num, device_id_ptr.get());
  171. if (rt_ret != RT_ERROR_NONE) {
  172. GELOGW("Call rtProfilerStop failed, ret:%d", rt_ret);
  173. }
  174. }
  175. // stop profiling
  176. if (prof_cb_.msprofCtrlCallback == nullptr) {
  177. GELOGE(ge::PARAM_INVALID, "MsprofCtrlCallback callback is nullptr.");
  178. return;
  179. }
  180. int32_t cb_ret = prof_cb_.msprofCtrlCallback(static_cast<uint32_t>(MsprofCtrlCallbackType::MSPROF_CTRL_FINALIZE),
  181. nullptr, 0);
  182. if (cb_ret != 0) {
  183. GELOGW("call msprofCtrlCallback failed, type:%u, return:%d",
  184. static_cast<uint32_t>(MsprofCtrlCallbackType::MSPROF_CTRL_FINALIZE), cb_ret);
  185. return;
  186. }
  187. GELOGI("Stop Profiling success.");
  188. #endif
  189. }
  190. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::ProfilingTaskDescInfo(
  191. uint32_t model_id, const std::vector<TaskDescInfo> &task_desc_info, const int32_t &device_id) {
  192. #ifdef DAVINCI_SUPPORT_PROFILING
  193. std::string data;
  194. for (const auto &task : task_desc_info) {
  195. std::string model_name = task.model_name;
  196. std::string op_name = task.op_name;
  197. uint32_t block_dim = task.block_dim;
  198. uint32_t task_id = task.task_id;
  199. uint32_t stream_id = task.stream_id;
  200. std::string shape_type = task.shape_type;
  201. int64_t cur_iter_num = task.cur_iter_num;
  202. data = model_name.append(" ")
  203. .append(op_name).append(" ")
  204. .append(std::to_string(block_dim)).append(" ")
  205. .append(std::to_string(task_id)).append(" ")
  206. .append(std::to_string(stream_id)).append(" ")
  207. .append(std::to_string(model_id)).append(" ")
  208. .append(shape_type).append(" ")
  209. .append(std::to_string(cur_iter_num)).append("\n");
  210. ReporterData reporter_data{};
  211. reporter_data.deviceId = device_id;
  212. reporter_data.data = (unsigned char *)data.c_str();
  213. reporter_data.dataLen = data.size();
  214. int ret = memcpy_s(reporter_data.tag, MSPROF_ENGINE_MAX_TAG_LEN + 1, "task_desc_info", sizeof("task_desc_info"));
  215. if (ret != EOK) {
  216. GELOGE(ret, "Report data tag of task_desc_info memcpy error!");
  217. return;
  218. }
  219. int32_t cb_ret = CallMsprofReport(reporter_data);
  220. if (cb_ret != 0) {
  221. GELOGE(cb_ret, "Reporter data of task_desc_info failed, ret:%d", cb_ret);
  222. return;
  223. }
  224. }
  225. data.clear();
  226. #endif
  227. }
  228. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::ProfilingGraphDescInfo(
  229. uint32_t model_id, const std::vector<ComputeGraphDescInfo> &compute_graph_desc_info, const int32_t &device_id) {
  230. #ifdef DAVINCI_SUPPORT_PROFILING
  231. std::string data;
  232. for (const auto &graph : compute_graph_desc_info) {
  233. data.append("model_name:")
  234. .append(graph.model_name)
  235. .append(" op_name:")
  236. .append(graph.op_name)
  237. .append(" op_type:")
  238. .append(graph.op_type);
  239. for (size_t i = 0; i < graph.input_format.size(); ++i) {
  240. data.append(" input_id:")
  241. .append(std::to_string(i))
  242. .append(" input_format:")
  243. .append(std::to_string(graph.input_format.at(i)))
  244. .append(" input_data_type:")
  245. .append(std::to_string(graph.input_data_type.at(i)))
  246. .append(" input_shape:\"");
  247. size_t input_shape_len = graph.input_shape.at(i).size();
  248. if (input_shape_len == 0) {
  249. data.append("");
  250. } else if (input_shape_len == 1) {
  251. data.append(std::to_string(graph.input_shape.at(i).at(0)));
  252. } else {
  253. for (size_t j = 0; j < input_shape_len - 1; ++j) {
  254. data.append(std::to_string(graph.input_shape.at(i).at(j))).append(",");
  255. }
  256. data.append(std::to_string(graph.input_shape.at(i).at(input_shape_len - 1)));
  257. }
  258. data.append("\"");
  259. }
  260. for (size_t i = 0; i < graph.output_format.size(); ++i) {
  261. data.append(" output_id:")
  262. .append(std::to_string(i))
  263. .append(" output_format:")
  264. .append(std::to_string(graph.output_format.at(i)))
  265. .append(" output_data_type:")
  266. .append(std::to_string(graph.output_data_type.at(i)))
  267. .append(" output_shape:\"");
  268. size_t output_shape_len = graph.output_shape.at(i).size();
  269. if (output_shape_len == 0) {
  270. data.append("");
  271. } else if (output_shape_len == 1) {
  272. data.append(std::to_string(graph.output_shape.at(i).at(0)));
  273. } else {
  274. for (size_t j = 0; j < output_shape_len - 1; ++j) {
  275. data.append(std::to_string(graph.output_shape.at(i).at(j))).append(",");
  276. }
  277. data.append(std::to_string(graph.output_shape.at(i).at(output_shape_len - 1)));
  278. }
  279. data.append("\"");
  280. }
  281. data.append(" model_id:").append(std::to_string(model_id));
  282. data.append(" task_id:").append(std::to_string(graph.task_id));
  283. data.append(" stream_id:").append(std::to_string(graph.stream_id));
  284. data.append("\n");
  285. GraphDescReport(device_id, data);
  286. data.clear();
  287. }
  288. #endif
  289. }
  290. void ProfilingManager::GraphDescReport(const int32_t &device_id, const string &data) {
  291. #ifdef DAVINCI_SUPPORT_PROFILING
  292. ReporterData reporter_data{};
  293. int ret = -1;
  294. int32_t cb_ret = -1;
  295. size_t index = data.size() / kReportMaxLen;
  296. if (index >= 1) {
  297. reporter_data.deviceId = device_id;
  298. ret = memcpy_s(reporter_data.tag, MSPROF_ENGINE_MAX_TAG_LEN + 1, "graph_desc_info", sizeof("graph_desc_info"));
  299. GE_IF_BOOL_EXEC(ret != EOK, GELOGE(ret, "Report data tag of graph_desc_info memcpy error!"); return;);
  300. for (size_t i = 0; i < index; ++i) {
  301. reporter_data.data = (unsigned char *)data.c_str() + kReportMaxLen * i;
  302. reporter_data.dataLen = kReportMaxLen;
  303. cb_ret = CallMsprofReport(reporter_data);
  304. GE_IF_BOOL_EXEC(cb_ret != 0, GELOGE(cb_ret, "Reporter data of graph_desc_info failed, ret:%d", cb_ret); return;);
  305. }
  306. reporter_data.dataLen = data.size() - kReportMaxLen * index;
  307. if (reporter_data.dataLen != 0) {
  308. reporter_data.data = (unsigned char *)data.c_str() + kReportMaxLen * index;
  309. cb_ret = CallMsprofReport(reporter_data);
  310. GE_IF_BOOL_EXEC(cb_ret != 0, GELOGE(cb_ret, "Reporter data of graph_desc_info failed, ret:%d", cb_ret); return;);
  311. }
  312. } else {
  313. reporter_data.deviceId = device_id;
  314. reporter_data.data = (unsigned char *)data.c_str();
  315. reporter_data.dataLen = data.size();
  316. ret = memcpy_s(reporter_data.tag, MSPROF_ENGINE_MAX_TAG_LEN + 1, "graph_desc_info", sizeof("graph_desc_info"));
  317. GE_IF_BOOL_EXEC(ret != EOK, GELOGE(ret, "Report data tag of graph_desc_info memcpy error!"); return;);
  318. cb_ret = CallMsprofReport(reporter_data);
  319. GE_IF_BOOL_EXEC(cb_ret != 0, GELOGE(cb_ret, "Reporter data of graph_desc_info failed, ret:%d", cb_ret); return;);
  320. }
  321. #endif
  322. }
  323. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::ReportProfilingData(
  324. uint32_t model_id, const std::vector<TaskDescInfo> &task_desc_info,
  325. const std::vector<ComputeGraphDescInfo> &compute_graph_desc_info) {
  326. #ifdef DAVINCI_SUPPORT_PROFILING
  327. int32_t logic_device_id = 0;
  328. rtError_t rt_ret = rtGetDevice(&logic_device_id);
  329. if (rt_ret != RT_ERROR_NONE) {
  330. GELOGE(rt_ret, "runtime get logic_device_id failed, current logic_device_id:%d", logic_device_id);
  331. return;
  332. }
  333. GELOGD("current logic_device_id:%d", logic_device_id);
  334. GELOGD("start ProfilingTaskDescInfo.");
  335. ProfilingTaskDescInfo(model_id, task_desc_info, logic_device_id);
  336. GELOGD("start ProfilingGraphDescInfo.");
  337. ProfilingGraphDescInfo(model_id, compute_graph_desc_info, logic_device_id);
  338. GELOGD("Report profiling data for GE end.");
  339. #endif
  340. }
  341. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY uint64_t ProfilingManager::GetProfilingModule() {
  342. uint64_t module = PROF_MODEL_EXECUTE_MASK |
  343. PROF_RUNTIME_API_MASK |
  344. PROF_RUNTIME_TRACE_MASK |
  345. PROF_SCHEDULE_TIMELINE_MASK |
  346. PROF_SCHEDULE_TRACE_MASK |
  347. PROF_TASK_TIME_MASK |
  348. PROF_SUBTASK_TIME_MASK |
  349. PROF_AICPU_TRACE_MASK |
  350. PROF_AICORE_METRICS_MASK |
  351. PROF_AIVECTORCORE_METRICS_MASK |
  352. PROF_MODEL_LOAD_MASK;
  353. return module;
  354. }
  355. void ProfilingManager::UpdateSubscribeDeviceModuleMap(std::string prof_type, uint32_t device_id, uint64_t module) {
  356. #ifdef DAVINCI_SUPPORT_PROFILING
  357. if (prof_type == kProfModelSubscribe) {
  358. if (subs_dev_module_.find(device_id) != subs_dev_module_.end()) {
  359. subs_dev_module_[device_id].subscribe_count++;
  360. } else {
  361. DeviceSubsInfo dev_info;
  362. dev_info.module = module;
  363. dev_info.subscribe_count = 1;
  364. subs_dev_module_[device_id] = dev_info;
  365. }
  366. } else if (prof_type == kProfModelUnsubscribe) {
  367. auto iter = subs_dev_module_.find(device_id);
  368. if (iter != subs_dev_module_.end()) {
  369. if (iter->second.subscribe_count > 0) {
  370. iter->second.subscribe_count--;
  371. }
  372. if (iter->second.subscribe_count == 0) {
  373. subs_dev_module_.erase(iter);
  374. }
  375. }
  376. } else {
  377. GELOGI("No need to update device_id module map.");
  378. }
  379. #endif
  380. }
  381. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfModelSubscribe(
  382. uint64_t module, void *model) {
  383. #ifdef DAVINCI_SUPPORT_PROFILING
  384. std::lock_guard<std::mutex> lock(mutex_);
  385. uint64_t model_load_mask = module & PROF_MODEL_LOAD_MASK;
  386. if ((subscribe_count_ == 0) && (model_load_mask == PROF_MODEL_LOAD_MASK)) {
  387. // register framework to profiling
  388. // register Framework to profiling
  389. int32_t cb_ret = PluginInit();
  390. if (cb_ret != 0) {
  391. GELOGE(cb_ret, "profiling plugin init failed, ret:%d", cb_ret);
  392. return cb_ret;
  393. }
  394. GELOGI("Prof subscribe: model load profiling on.");
  395. }
  396. subscribe_count_++;
  397. auto davinci_model = static_cast<DavinciModel *>(model);
  398. int32_t device_num = 1;
  399. uint32_t device[1];
  400. device[0] = davinci_model->GetDeviceId();
  401. rtError_t rt_ret = rtProfilerStart(module, device_num, device);
  402. if (rt_ret != RT_ERROR_NONE) {
  403. GELOGE(FAILED, "Runtime profiler start failed.");
  404. return FAILED;
  405. }
  406. UpdateSubscribeDeviceModuleMap(kProfModelSubscribe, device[0], module);
  407. // Report profiling data
  408. Status p_ret = davinci_model->ReportProfilingData();
  409. if (p_ret != SUCCESS) {
  410. GELOGE(p_ret, "Report profiling data failed.");
  411. return p_ret;
  412. }
  413. #endif
  414. return SUCCESS;
  415. }
  416. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfModelUnsubscribe(
  417. void *model) {
  418. #ifdef DAVINCI_SUPPORT_PROFILING
  419. std::lock_guard<std::mutex> lock(mutex_);
  420. if (subscribe_count_ == 0) {
  421. GELOGW("The profiler has not been subscribed, you do not need to cannel the subscription.");
  422. return SUCCESS;
  423. }
  424. auto davinci_model = static_cast<DavinciModel *>(model);
  425. int32_t dev_num = 1;
  426. uint32_t device[1];
  427. device[0] = davinci_model->GetDeviceId();
  428. auto iter = subs_dev_module_.find(device[0]);
  429. if (iter != subs_dev_module_.end()) {
  430. if (subs_dev_module_[device[0]].subscribe_count == 1) {
  431. // The same device_id, only stop at last time
  432. rtError_t rt_ret = rtProfilerStop(subs_dev_module_[device[0]].module, dev_num, device);
  433. if (rt_ret != RT_ERROR_NONE) {
  434. GELOGE(FAILED, "Runtime profiler stop failed.");
  435. return FAILED;
  436. }
  437. }
  438. UpdateSubscribeDeviceModuleMap(kProfModelUnsubscribe, device[0], subs_dev_module_[device[0]].module);
  439. } else {
  440. GELOGE(FAILED, "The device_id:%u has not been subscribed, do not need to cancel.", device[0]);
  441. return FAILED;
  442. }
  443. subscribe_count_--;
  444. if (subscribe_count_ == 0) {
  445. // profiling plugin uninit at last subscription
  446. PluginUnInit();
  447. }
  448. #endif
  449. return SUCCESS;
  450. }
  451. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfInit(uint64_t module) {
  452. #ifdef DAVINCI_SUPPORT_PROFILING
  453. std::lock_guard<std::mutex> lock(mutex_);
  454. uint64_t model_load_mask = module & PROF_MODEL_LOAD_MASK;
  455. if (model_load_mask == PROF_MODEL_LOAD_MASK) {
  456. // register Framework to profiling
  457. int32_t cb_ret = PluginInit();
  458. if (cb_ret != 0) {
  459. GELOGE(cb_ret, "profiling plugin init failed, ret:%d", cb_ret);
  460. return cb_ret;
  461. }
  462. int32_t device_num = -1;
  463. rtError_t rt_ret = rtProfilerStart(model_load_mask, device_num, nullptr);
  464. if (rt_ret != RT_ERROR_NONE) {
  465. GELOGE(FAILED, "Runtime profiler start failed.");
  466. return FAILED;
  467. }
  468. is_load_profiling_ = true;
  469. GELOGI("Prof init: model load profiling on.");
  470. }
  471. uint64_t training_trace_mask = module & PROF_TRAINING_TRACE_MASK;
  472. if (training_trace_mask == PROF_TRAINING_TRACE_MASK) {
  473. is_training_trace_ = true;
  474. }
  475. GELOGI("Prof init success.");
  476. #endif
  477. return SUCCESS;
  478. }
  479. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfFinalize() {
  480. #ifdef DAVINCI_SUPPORT_PROFILING
  481. std::lock_guard<std::mutex> lock(mutex_);
  482. is_load_profiling_ = false;
  483. is_training_trace_ = false;
  484. is_execute_profiling_ = false;
  485. // profiling plugin uninit
  486. PluginUnInit();
  487. int32_t dev_num = -1;
  488. rtError_t rt_ret = rtProfilerStop(PROF_MODEL_LOAD_MASK, dev_num, nullptr);
  489. if (rt_ret != RT_ERROR_NONE) {
  490. GELOGE(FAILED, "Runtime profiler stop failed.");
  491. return FAILED;
  492. }
  493. for (auto device_id_module : device_id_module_map_) {
  494. if (device_id_module.second != 0) {
  495. uint32_t device_id = static_cast<uint32_t>(device_id_module.first);
  496. GELOGI("Prof finalize: device_id: %u, module: 0x%llx.", device_id, device_id_module.second);
  497. rt_ret = rtProfilerStop(device_id_module.second, 1, &device_id);
  498. if (rt_ret != RT_ERROR_NONE) {
  499. GELOGE(FAILED, "Runtime profiler stop failed.");
  500. return FAILED;
  501. }
  502. }
  503. }
  504. device_id_module_map_.clear();
  505. device_id_.clear();
  506. GELOGI("Prof finalize success.");
  507. #endif
  508. return SUCCESS;
  509. }
  510. Status ProfilingManager::ProfParseDeviceId(const std::map<std::string, std::string> &config_para,
  511. vector<int32_t> &device_list) {
  512. #ifdef DAVINCI_SUPPORT_PROFILING
  513. auto iter = config_para.find(kConfigDevIdList);
  514. if (iter != config_para.end()) {
  515. std::string device_id_list = iter->second;
  516. std::string temp;
  517. vector<std::string> decvice_id;
  518. for (uint32_t i = 0; i < device_id_list.size(); i++) {
  519. if (isdigit(device_id_list[i])) {
  520. temp.append(1, device_id_list[i]);
  521. } else {
  522. if (!temp.empty()) {
  523. decvice_id.emplace_back(temp);
  524. }
  525. temp.clear();
  526. }
  527. }
  528. if (!temp.empty()) {
  529. decvice_id.emplace_back(temp);
  530. }
  531. for (uint32_t i = 0; i < decvice_id.size(); i++) {
  532. try {
  533. int32_t dev_id = std::stoi(decvice_id[i]);
  534. device_list.push_back(dev_id);
  535. } catch (std::invalid_argument &) {
  536. GELOGE(FAILED, "Device id: %s is invalid.", decvice_id[i].c_str());
  537. return FAILED;
  538. } catch (std::out_of_range &) {
  539. GELOGE(FAILED, "Device id: %s is out of range.", decvice_id[i].c_str());
  540. return FAILED;
  541. } catch (...) {
  542. GELOGE(FAILED, "Device id: %s cannot change to int.", decvice_id[i].c_str());
  543. return FAILED;
  544. }
  545. }
  546. } else {
  547. GELOGE(FAILED, "Config para not contain device id list.");
  548. return FAILED;
  549. }
  550. #endif
  551. return SUCCESS;
  552. }
  553. Status ProfilingManager::ProfParseParam(const std::map<std::string, std::string> &config_para,
  554. int32_t &device_num, vector<int32_t> &device_list) {
  555. #ifdef DAVINCI_SUPPORT_PROFILING
  556. // device num
  557. auto iter = config_para.find(kConfigNumsdev);
  558. if (iter != config_para.end()) {
  559. try {
  560. device_num = std::stoi(iter->second);
  561. } catch (std::invalid_argument &) {
  562. GELOGE(FAILED, "Device nun: %s is invalid.", iter->second.c_str());
  563. return FAILED;
  564. } catch (std::out_of_range &) {
  565. GELOGE(FAILED, "Device num: %s is out of range.", iter->second.c_str());
  566. return FAILED;
  567. } catch (...) {
  568. GELOGE(FAILED, "Device num: %s cannot change to int.", iter->second.c_str());
  569. return FAILED;
  570. }
  571. } else {
  572. GELOGE(FAILED, "Config para not contain device num.");
  573. return FAILED;
  574. }
  575. // device id
  576. if (ProfParseDeviceId(config_para, device_list) != SUCCESS) {
  577. GELOGE(FAILED, "Parse config para device id failed.");
  578. return FAILED;
  579. }
  580. if (device_num == 0 || device_num > kMaxDeviceNum || device_num != static_cast<int32_t>(device_list.size())) {
  581. GELOGE(FAILED, "Config para device num: %d not equal to device list size: %d.", device_num, device_list.size());
  582. return FAILED;
  583. }
  584. #endif
  585. return SUCCESS;
  586. }
  587. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfStartProfiling(
  588. uint64_t module, const std::map<std::string, std::string> &config_para) {
  589. #ifdef DAVINCI_SUPPORT_PROFILING
  590. std::lock_guard<std::mutex> lock(mutex_);
  591. uint64_t training_trace_mask = module & PROF_TRAINING_TRACE_MASK;
  592. if (training_trace_mask == PROF_TRAINING_TRACE_MASK) {
  593. is_training_trace_ = true;
  594. }
  595. int32_t device_num = 0;
  596. vector<int32_t> device_list;
  597. if (ProfParseParam(config_para, device_num, device_list) != SUCCESS) {
  598. GELOGE(FAILED, "Prof start parse param failed.");
  599. return FAILED;
  600. }
  601. auto device_id_ptr = std::unique_ptr<uint32_t[]>(new (std::nothrow) uint32_t[device_num]);
  602. if (device_id_ptr == nullptr) {
  603. GELOGE(FAILED, "Prof start: device id ptr is null.");
  604. return FAILED;
  605. }
  606. for (int32_t i = 0; i < device_num; i++) {
  607. device_id_ptr[i] = static_cast<uint32_t>(device_list[i]);
  608. }
  609. GELOGI("Runtime config param: 0x%llx, device num: %d.", module, device_num);
  610. rtError_t rt_ret = rtProfilerStart(module, device_num, device_id_ptr.get());
  611. if (rt_ret != RT_ERROR_NONE) {
  612. GELOGE(FAILED, "Runtime profiler config proc failed.");
  613. return FAILED;
  614. }
  615. if ((module & PROF_MODEL_EXECUTE_MASK) == PROF_MODEL_EXECUTE_MASK) {
  616. for (int32_t i = 0; i < device_num; i++) {
  617. if (std::find(device_id_.begin(), device_id_.end(), device_list[i]) == device_id_.end()) {
  618. device_id_.push_back(device_list[i]);
  619. }
  620. }
  621. GELOGI("Prof start: ge execute model start profiling.");
  622. }
  623. if ((module & PROF_MODEL_LOAD_MASK) == PROF_MODEL_LOAD_MASK) {
  624. GELOGW("Prof start: load model module is invalid.");
  625. }
  626. UpdateDeviceIdModuleMap(kProfStart, module, device_list);
  627. GELOGI("Prof start profiling success.");
  628. #endif
  629. return SUCCESS;
  630. }
  631. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfStopProfiling(uint64_t module,
  632. const std::map<std::string, std::string> &config_para) {
  633. #ifdef DAVINCI_SUPPORT_PROFILING
  634. std::lock_guard<std::mutex> lock(mutex_);
  635. int32_t device_num = 0;
  636. vector<int32_t> device_list;
  637. if (ProfParseParam(config_para, device_num, device_list) != SUCCESS) {
  638. GELOGE(FAILED, "Prof stop parse param failed.");
  639. return FAILED;
  640. }
  641. auto device_id_ptr = std::unique_ptr<uint32_t[]>(new (std::nothrow) uint32_t[device_num]);
  642. if (device_id_ptr == nullptr) {
  643. GELOGE(FAILED, "Prof stop: device id ptr is null.");
  644. return FAILED;
  645. }
  646. for (int32_t i = 0; i < device_num; i++) {
  647. device_id_ptr[i] = static_cast<uint32_t>(device_list[i]);
  648. }
  649. GELOGI("Prof stop: runtime config param: 0x%llx, device num: %d", module, device_num);
  650. rtError_t rt_ret = rtProfilerStop(module, device_num, device_id_ptr.get());
  651. if (rt_ret != RT_ERROR_NONE) {
  652. GELOGE(FAILED, "Prof stop: runtime profiler config proc failed.");
  653. return FAILED;
  654. }
  655. uint64_t execute_model_mask = module & PROF_MODEL_EXECUTE_MASK;
  656. if (execute_model_mask == PROF_MODEL_EXECUTE_MASK) {
  657. for (int32_t i = 0; i < device_num; i++) {
  658. auto iter = std::find(device_id_.begin(), device_id_.end(), device_list[i]);
  659. if (iter != device_id_.end()) {
  660. device_id_.erase(iter);
  661. }
  662. }
  663. GELOGI("Prof stop: ge execute model stop profiling.");
  664. }
  665. if ((module & PROF_MODEL_LOAD_MASK) == PROF_MODEL_LOAD_MASK) {
  666. GELOGW("Prof stop: load model module is invalid.");
  667. }
  668. UpdateDeviceIdModuleMap(kProfStop, module, device_list);
  669. GELOGI("Prof stop profiling success.");
  670. #endif
  671. return SUCCESS;
  672. }
  673. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::UpdateDeviceIdModuleMap(string prof_type,
  674. uint64_t module, const vector<int32_t> &device_list) {
  675. #ifdef DAVINCI_SUPPORT_PROFILING
  676. if (prof_type == kProfStart) {
  677. for (uint32_t i = 0; i < device_list.size(); i++) {
  678. auto iter = device_id_module_map_.find(device_list[i]);
  679. if (iter != device_id_module_map_.end()) {
  680. uint64_t prof_on_module = device_id_module_map_[device_list[i]];
  681. // save all profiling on module of device
  682. device_id_module_map_[device_list[i]] = prof_on_module | module;
  683. } else {
  684. device_id_module_map_[device_list[i]] = module;
  685. }
  686. }
  687. } else if (prof_type == kProfStop) {
  688. for (uint32_t i = 0; i < device_list.size(); i++) {
  689. auto iter = device_id_module_map_.find(device_list[i]);
  690. if (iter != device_id_module_map_.end()) {
  691. uint64_t prof_on_module = device_id_module_map_[device_list[i]];
  692. uint64_t prof_off_module = prof_on_module & module;
  693. uint64_t prof_on_left_module = prof_on_module & (~prof_off_module);
  694. // stop profiling on module of device
  695. device_id_module_map_[device_list[i]] = prof_on_left_module;
  696. }
  697. }
  698. } else {
  699. GELOGI("No need to update device_id module map.");
  700. }
  701. #endif
  702. }
  703. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY bool ProfilingManager::ProfilingModelExecuteOn() const {
  704. int32_t logic_device_id = 0;
  705. rtError_t rt_ret = rtGetDevice(&logic_device_id);
  706. if (rt_ret != RT_ERROR_NONE) {
  707. GELOGE(rt_ret, "Runtime get logic_device_id failed, current logic_device_id:%d", logic_device_id);
  708. }
  709. GELOGI("Current logic_device_id:%d", logic_device_id);
  710. bool execute_model_prof_on = false;
  711. auto iter = std::find(device_id_.begin(), device_id_.end(), logic_device_id);
  712. if (iter != device_id_.end()) {
  713. execute_model_prof_on = true;
  714. }
  715. GELOGI("Flag is_execute_profiling: %d, execute_model_prof_on: %d", is_execute_profiling_, execute_model_prof_on);
  716. return execute_model_prof_on;
  717. }
  718. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::PluginInit() const {
  719. if (prof_cb_.msprofReporterCallback == nullptr) {
  720. GELOGE(ge::PARAM_INVALID, "MsprofReporterCallback callback is nullptr.");
  721. return ge::PARAM_INVALID;
  722. }
  723. return prof_cb_.msprofReporterCallback(
  724. static_cast<uint32_t>(MsprofReporterModuleId::MSPROF_MODULE_FRAMEWORK),
  725. static_cast<uint32_t>(MsprofReporterCallbackType::MSPROF_REPORTER_INIT),
  726. nullptr, 0);
  727. }
  728. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::PluginUnInit() const {
  729. #ifdef DAVINCI_SUPPORT_PROFILING
  730. if (prof_cb_.msprofReporterCallback == nullptr) {
  731. GELOGE(ge::PARAM_INVALID, "MsprofReporterCallback callback is nullptr.");
  732. return;
  733. }
  734. int32_t cb_ret = prof_cb_.msprofReporterCallback(
  735. static_cast<uint32_t>(MsprofReporterModuleId::MSPROF_MODULE_FRAMEWORK),
  736. static_cast<uint32_t>(MsprofReporterCallbackType::MSPROF_REPORTER_UNINIT),
  737. nullptr, 0);
  738. if (cb_ret != 0) {
  739. GELOGW("profiling plugin uninit failed, ret:%d", cb_ret);
  740. }
  741. #endif
  742. }
  743. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::CallMsprofReport(
  744. ReporterData &reporter_data) const {
  745. if (prof_cb_.msprofReporterCallback == nullptr) {
  746. GELOGE(ge::PARAM_INVALID, "MsprofReporterCallback callback is nullptr.");
  747. return ge::PARAM_INVALID;
  748. }
  749. return prof_cb_.msprofReporterCallback(
  750. static_cast<uint32_t>(MsprofReporterModuleId::MSPROF_MODULE_FRAMEWORK),
  751. static_cast<uint32_t>(MsprofReporterCallbackType::MSPROF_REPORTER_REPORT),
  752. static_cast<void *>(&reporter_data), sizeof(ReporterData));
  753. }
  754. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::GetFpBpPoint(
  755. std::string &fp_point, std::string &bp_point) {
  756. // Env or options mode, fp_point_/bp_point_ have initiliazed on profiling init
  757. if (!fp_point_.empty() && !bp_point_.empty()) {
  758. fp_point = fp_point_;
  759. bp_point = bp_point_;
  760. GELOGI("Bp Fp have been initialized in env or options. bp_point: %s, fp_point: %s", bp_point.c_str(),
  761. fp_point.c_str());
  762. return;
  763. }
  764. // ProfApi mode and training trace is set
  765. // Parse options first
  766. char env_profiling_options[MSPROF_OPTIONS_DEF_LEN_MAX] = { 0x00 };
  767. bool is_profiling_valid = false;
  768. std::string profiling_options;
  769. if (ge::GetContext().GetOption(OPTION_EXEC_PROFILING_OPTIONS, profiling_options) == SUCCESS &&
  770. !profiling_options.empty()) {
  771. is_profiling_valid = true;
  772. } else {
  773. INT32 ret = mmGetEnv("PROFILING_OPTIONS", env_profiling_options, MSPROF_OPTIONS_DEF_LEN_MAX);
  774. if (ret != EN_OK) {
  775. GELOGI("PROFILING_OPTIONS env is not exist.");
  776. return;
  777. }
  778. GELOGI("Parse env PROFILING_OPTIONS:%s.", env_profiling_options);
  779. profiling_options = env_profiling_options;
  780. is_profiling_valid = true;
  781. }
  782. if (is_profiling_valid) {
  783. try {
  784. Json prof_options = Json::parse(profiling_options);
  785. fp_point_ = prof_options[kFpPoint];
  786. bp_point_ = prof_options[kBpPoint];
  787. fp_point = fp_point_;
  788. bp_point = bp_point_;
  789. if (!fp_point_.empty() && !bp_point_.empty()) {
  790. GELOGI("Training trace bp fp is set, bp_point:%s, fp_point:%s.", bp_point_.c_str(), fp_point_.c_str());
  791. }
  792. } catch (...) {
  793. GELOGW("Json prof options is invalid.");
  794. return;
  795. }
  796. }
  797. return;
  798. }
  799. } // namespace ge

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