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

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

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