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 37 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
4 years ago
4 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
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
4 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
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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 kJobID = "jobID";
  25. const char *const kDeviceID = "deviceID";
  26. const char *const kStartCfg = "startCfg";
  27. const char *const kFeatures = "features";
  28. const char *const kConf = "conf";
  29. const char *const kEvents = "events";
  30. const char *const kAiCoreEvents = "ai_core_events";
  31. const char *const kName = "name";
  32. const char *const kTraceID = "traceId";
  33. const char *const kProfDir = "resultPath";
  34. const size_t kReportMaxLen = 2048;
  35. const int32_t kMaxDeviceNum = 256;
  36. const std::string kConfigNumsdev = "devNums";
  37. const std::string kConfigDevIdList = "devIdList";
  38. const std::string kProfStart = "prof_start";
  39. const std::string kProfStop = "prof_stop";
  40. const std::string kProfModelSubscribe = "prof_model_subscribe";
  41. const std::string kProfModelUnsubscribe = "prof_model_cancel_subscribe";
  42. } // namespace
  43. namespace ge {
  44. ProfilingManager::ProfilingManager() : subscribe_count_(0) {}
  45. ProfilingManager::~ProfilingManager() {}
  46. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ProfilingManager &ProfilingManager::Instance() {
  47. static ProfilingManager profiling_manager;
  48. return profiling_manager;
  49. }
  50. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ge::Status ProfilingManager::Init(const Options &options) {
  51. #ifdef DAVINCI_SUPPORT_PROFILING
  52. vector<int32_t>().swap(device_id_);
  53. subscribe_count_ = 0;
  54. job_id_ = options.job_id;
  55. GELOGI("ProfilingManager::Init job_id:%s", job_id_.c_str());
  56. Status ret;
  57. if (!recv_profiling_config_.empty()) {
  58. GELOGI("Profiling json config from acl:%s", recv_profiling_config_.c_str());
  59. ret = InitFromAclCfg(recv_profiling_config_);
  60. } else {
  61. ret = InitFromOptions(options);
  62. if (ret == SUCCESS && is_load_profiling_) {
  63. device_id_.push_back(options.device_id);
  64. }
  65. }
  66. if (ret != SUCCESS) {
  67. GELOGE(ret, "Failed to init profiling.");
  68. return ret;
  69. }
  70. if (is_load_profiling_) {
  71. // register Framework to profiling
  72. int result = Msprof::Engine::Init(GE_PROFILING_MODULE, &engine_);
  73. if (result != 0) {
  74. GELOGE(FAILED, "Register profiling engine failed.");
  75. return FAILED;
  76. }
  77. // profiling startup first time
  78. GELOGI("Begin to init profiling, device num %zu", device_id_.size());
  79. for (size_t i = 0; i < device_id_.size(); ++i) {
  80. ret = StartProfiling(0, device_id_[i]);
  81. if (ret != SUCCESS) {
  82. GELOGW("Profiling start failed on device %d.", device_id_[i]);
  83. continue;
  84. }
  85. GELOGI("Profiling init succ on device %d.", device_id_[i]);
  86. }
  87. } else {
  88. GELOGI("The profiling is off, skip the initialization");
  89. }
  90. #endif
  91. return SUCCESS;
  92. }
  93. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ge::Status ProfilingManager::InitFromAclCfg(
  94. const std::string &config) {
  95. #ifdef DAVINCI_SUPPORT_PROFILING
  96. try {
  97. is_load_profiling_ = false;
  98. is_execute_profiling_ = false;
  99. profiling_opts_.clear();
  100. op_trace_conf_.clear();
  101. Json start_prof_conf = Json::parse(config);
  102. Json &prof_conf = start_prof_conf[kStartCfg][0];
  103. job_id_ = prof_conf[kJobID];
  104. auto iter = prof_conf.find(kProfDir);
  105. if (iter != prof_conf.end()) {
  106. prof_dir_ = prof_conf[kProfDir];
  107. }
  108. Json &device_id = prof_conf[kDeviceID];
  109. if (device_id.size() != 0) {
  110. vector<int32_t>().swap(device_id_);
  111. bool is_all = false;
  112. for (size_t i = 0; i < device_id.size(); i++) {
  113. std::string device_id_str = device_id[i].get<std::string>();
  114. if (device_id_str == "all") {
  115. is_all = true;
  116. break;
  117. }
  118. device_id_.push_back(std::stoi(device_id_str));
  119. }
  120. if (is_all) {
  121. int32_t count = 0;
  122. rtError_t rt_err = rtGetDeviceCount(&count);
  123. if (rt_err != RT_ERROR_NONE) {
  124. GELOGE(FAILED, "Call rtGetDeviceCount to get device failed.");
  125. }
  126. vector<int32_t>().swap(device_id_);
  127. for (int32_t i = 0; i < count; ++i) {
  128. device_id_.push_back(i);
  129. }
  130. }
  131. }
  132. Json &features = prof_conf[kFeatures];
  133. if (ParseFeaturesFromAclCfg(features) != SUCCESS) {
  134. GELOGE(FAILED, "Parse feature from acl cfg failed.");
  135. return FAILED;
  136. }
  137. is_load_profiling_ = true;
  138. is_execute_profiling_ = true;
  139. } catch (...) {
  140. GELOGE(FAILED, "Json conf is not invalid !");
  141. return ge::PARAM_INVALID;
  142. }
  143. #endif
  144. return ge::SUCCESS;
  145. }
  146. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ge::Status ProfilingManager::ParseFeaturesFromAclCfg(
  147. const Json &features) {
  148. #ifdef DAVINCI_SUPPORT_PROFILING
  149. try {
  150. for (size_t i = 0; i < features.size(); ++i) {
  151. const Json &feature = features[i];
  152. if ((feature.find(kName) == feature.end()) || feature[kName].is_null()) {
  153. continue;
  154. }
  155. const std::string &name = feature[kName];
  156. if (name == "op_trace") {
  157. const Json &conf = feature[kConf];
  158. const Json &events = conf[0][kEvents];
  159. const std::string &ai_core_events = events[0][kAiCoreEvents];
  160. GELOGI("Op trace config from acl ai_core_events:%s", ai_core_events.c_str());
  161. is_op_trace_ = true;
  162. ProfMgrConf prof_mgr_conf;
  163. int result = ProfMgrGetConf(ai_core_events, &prof_mgr_conf);
  164. if (result != 0) {
  165. GELOGE(FAILED, "ProfMgrGetConf failed.");
  166. return FAILED;
  167. }
  168. op_trace_conf_ = prof_mgr_conf.conf;
  169. op_trace_iter_num_ = static_cast<int32_t>(op_trace_conf_.size());
  170. GELOGI("Op trace profiling iter num %d,", op_trace_iter_num_);
  171. } else if (name == "task_trace") {
  172. is_op_trace_ = false;
  173. if (feature.find(kConf) != feature.end()) {
  174. const Json &conf = feature[kConf];
  175. std::stringstream task_trace_conf;
  176. task_trace_conf << conf;
  177. task_trace_conf_ = task_trace_conf.str();
  178. }
  179. GELOGI("Task trace config from acl");
  180. } else if (name == "system_trace") {
  181. is_op_trace_ = false;
  182. const Json &conf = feature[kConf];
  183. std::stringstream system_trace_conf;
  184. system_trace_conf << conf;
  185. system_trace_conf_ = system_trace_conf.str();
  186. GELOGI("System trace config from acl");
  187. }
  188. profiling_opts_.push_back(name);
  189. }
  190. } catch (...) {
  191. GELOGE(ge::PARAM_INVALID, "Json conf feature is not invalid !");
  192. return ge::PARAM_INVALID;
  193. }
  194. #endif
  195. return ge::SUCCESS;
  196. }
  197. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ge::Status ProfilingManager::InitFromOptions(const Options &options) {
  198. #ifdef DAVINCI_SUPPORT_PROFILING
  199. // enable profiling support two ways: env and front end
  200. char profiling_mode_temp[MMPA_MAX_PATH] = { 0x00 };
  201. char prof_options_temp[MMPA_MAX_PATH] = { 0x00 };
  202. (void)mmGetEnv("PROFILING_MODE", profiling_mode_temp, MMPA_MAX_PATH);
  203. (void)mmGetEnv("PROFILING_OPTIONS", prof_options_temp, MMPA_MAX_PATH );
  204. const char *profiling_mode = profiling_mode_temp;
  205. const char *prof_options = prof_options_temp;
  206. if ((profiling_mode == nullptr) || (strcmp("true", profiling_mode) != 0) || (prof_options == nullptr)) {
  207. is_load_profiling_ = false;
  208. is_execute_profiling_ = false;
  209. } else {
  210. std::string prof_options_str = std::string(prof_options);
  211. profiling_opts_ = StringUtils::Split(prof_options_str, ':');
  212. is_load_profiling_ = true;
  213. is_execute_profiling_ = true;
  214. GELOGI("The profiling in env is %s, %s", profiling_mode, prof_options);
  215. }
  216. if (!is_load_profiling_) {
  217. const std::string enable_profiling = "1";
  218. if (options.profiling_mode != enable_profiling || options.profiling_options.empty()) {
  219. is_load_profiling_ = false;
  220. is_execute_profiling_ = false;
  221. return SUCCESS;
  222. } else {
  223. profiling_opts_ = StringUtils::Split(options.profiling_options, ':');
  224. is_load_profiling_ = true;
  225. is_execute_profiling_ = true;
  226. GELOGI("The profiling in options is %s, %s", options.profiling_mode.c_str(), options.profiling_options.c_str());
  227. }
  228. }
  229. // features:'training_trace', 'task_trace' or 'op_trace' etc
  230. if (!profiling_opts_.empty()) {
  231. if (profiling_opts_[0] == "op_trace") {
  232. is_op_trace_ = true;
  233. // op trace get conf
  234. ProfMgrConf prof_mgr_conf;
  235. int result = ProfMgrGetConf("", &prof_mgr_conf);
  236. if (result != 0) {
  237. GELOGE(FAILED, "ProfMgrGetConf failed.");
  238. return FAILED;
  239. }
  240. op_trace_conf_ = prof_mgr_conf.conf;
  241. op_trace_iter_num_ = static_cast<int32_t>(op_trace_conf_.size());
  242. GELOGI("op trace profiling iter num %d,", op_trace_iter_num_);
  243. } else {
  244. is_op_trace_ = false;
  245. op_trace_iter_num_ = 1;
  246. }
  247. }
  248. #endif
  249. return ge::SUCCESS;
  250. }
  251. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ge::Status ProfilingManager::StartProfiling(int32_t iter_num,
  252. int32_t device_id) {
  253. #ifdef DAVINCI_SUPPORT_PROFILING
  254. if (!profiling_opts_.empty()) {
  255. GELOGI("Start profiling index is %d", iter_num);
  256. // current one docker only use one device
  257. Json p_device;
  258. try {
  259. // profiling need physical_device_id
  260. p_device[kDeviceID] = std::to_string(device_id);
  261. p_device[kJobID] = job_id_;
  262. p_device[kTraceID] = std::to_string(GetContext().TraceId());
  263. if (!prof_dir_.empty()) {
  264. p_device[kProfDir] = prof_dir_;
  265. GELOGI("Prof dir: %s.", prof_dir_.c_str());
  266. }
  267. Json features;
  268. if (is_op_trace_) {
  269. Json f;
  270. f[kName] = "op_trace";
  271. Json conf;
  272. if (op_trace_conf_.size() <= static_cast<size_t>(iter_num)) {
  273. GELOGE(FAILED, "Op trace iter num is invalid!");
  274. return FAILED;
  275. }
  276. Json events;
  277. events[0] = nlohmann::json::parse(op_trace_conf_[iter_num]);
  278. conf[0][kEvents] = events;
  279. f[kConf] = conf;
  280. features[0] = f;
  281. if (iter_num == 0) {
  282. is_load_ = true;
  283. }
  284. } else {
  285. for (std::vector<std::string>::size_type i = 0; i < profiling_opts_.size(); i++) {
  286. Json f;
  287. if (profiling_opts_[i] == "system_trace") {
  288. f[kConf] = nlohmann::json::parse(system_trace_conf_);
  289. } else if (profiling_opts_[i] == "task_trace") {
  290. if (!task_trace_conf_.empty()) {
  291. f[kConf] = nlohmann::json::parse(task_trace_conf_);
  292. }
  293. }
  294. f[kName] = profiling_opts_[i];
  295. features[i] = f;
  296. }
  297. is_load_ = true;
  298. }
  299. p_device[kFeatures] = features;
  300. // only one device, but sProfMgrStartUp API require for device list
  301. Json devices;
  302. devices[0] = p_device;
  303. Json start_cfg;
  304. start_cfg[kStartCfg] = devices;
  305. // convert json to string
  306. std::stringstream ss;
  307. ss << start_cfg;
  308. send_profiling_config_ = ss.str();
  309. GELOGI("Profiling config %s\n", send_profiling_config_.c_str());
  310. } catch (...) {
  311. GELOGE(FAILED, "Op trace json conf is not invalid !");
  312. return FAILED;
  313. }
  314. // runtime startup for profiling
  315. uint64_t module = GetProfilingModule();
  316. int32_t device_num = 1;
  317. uint32_t device_id_rt = static_cast<uint32_t>(device_id);
  318. GE_CHK_RT_RET(rtProfilerStart(module, device_num, &device_id_rt));
  319. // call profiling startup API
  320. ProfMgrCfg prof_cfg = {send_profiling_config_};
  321. void *prof_handle = ProfMgrStartUp(&prof_cfg);
  322. if (prof_handle == nullptr) {
  323. GELOGW("ProfMgrStartUp failed on device %d ", device_id);
  324. return FAILED;
  325. }
  326. GELOGD("StartProfiling, prof_handle: %p", prof_handle);
  327. prof_handle_vec_.push_back(prof_handle);
  328. }
  329. #endif
  330. return SUCCESS;
  331. }
  332. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::StopProfiling() {
  333. #ifdef DAVINCI_SUPPORT_PROFILING
  334. Msprof::Engine::Reporter *reporter = PluginImpl::GetPluginReporter();
  335. if (reporter != nullptr) {
  336. int ret = reporter->Flush();
  337. GELOGI("Report data end, ret is %d", ret);
  338. }
  339. uint64_t module = GetProfilingModule();
  340. int32_t device_num = static_cast<int32_t>(device_id_.size());
  341. auto device_id_ptr = std::unique_ptr<uint32_t[]>(new (std::nothrow) uint32_t[device_num]);
  342. if (device_id_ptr == nullptr) {
  343. GELOGE(FAILED, "Stop profiling: device id ptr is null.");
  344. return;
  345. }
  346. for (int32_t i = 0; i < device_num; i++) {
  347. device_id_ptr[i] = static_cast<uint32_t>(device_id_[i]);
  348. }
  349. rtError_t rt_ret = rtProfilerStop(module, device_num, device_id_ptr.get());
  350. if (rt_ret != RT_ERROR_NONE) {
  351. GELOGW("Call rtProfilerStop failed, ret:%d", rt_ret);
  352. }
  353. for (size_t i = 0; i < prof_handle_vec_.size(); ++i) {
  354. int result = ProfMgrStop(prof_handle_vec_[i]);
  355. if (result != 0) {
  356. GELOGW("ProfMgr stop return fail:%d, handle:%p", result, prof_handle_vec_[i]);
  357. }
  358. }
  359. vector<void *>().swap(prof_handle_vec_);
  360. is_load_ = false;
  361. recv_profiling_config_ = "";
  362. GELOGI("Stop Profiling success.");
  363. #endif
  364. }
  365. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::ProfilingTaskDescInfo(
  366. uint32_t model_id, const std::vector<TaskDescInfo> &task_desc_info, const int32_t &device_id) {
  367. #ifdef DAVINCI_SUPPORT_PROFILING
  368. Msprof::Engine::Reporter *reporter = PluginImpl::GetPluginReporter();
  369. if (reporter == nullptr) {
  370. GELOGI("Profiling report is nullptr!");
  371. return;
  372. }
  373. std::string data;
  374. for (const auto &task : task_desc_info) {
  375. std::string model_name = task.model_name;
  376. std::string op_name = task.op_name;
  377. uint32_t block_dim = task.block_dim;
  378. uint32_t task_id = task.task_id;
  379. uint32_t stream_id = task.stream_id;
  380. data = model_name.append(" ")
  381. .append(op_name).append(" ")
  382. .append(std::to_string(block_dim).append(" ")
  383. .append(std::to_string(task_id)).append(" ")
  384. .append(std::to_string(stream_id)).append(" ")
  385. .append(std::to_string(model_id)).append("\n"));
  386. Msprof::Engine::ReporterData reporter_data{};
  387. reporter_data.deviceId = device_id;
  388. reporter_data.data = (unsigned char *)data.c_str();
  389. reporter_data.dataLen = data.size();
  390. int ret = memcpy_s(reporter_data.tag, MSPROF_ENGINE_MAX_TAG_LEN + 1, "task_desc_info", sizeof("task_desc_info"));
  391. if (ret != EOK) {
  392. GELOGE(ret, "Report data tag of task_desc_info memcpy error!");
  393. return;
  394. }
  395. ret = reporter->Report(&reporter_data);
  396. if (ret != SUCCESS) {
  397. GELOGE(ret, "Reporter data of task_desc_info fail!");
  398. return;
  399. }
  400. }
  401. data.clear();
  402. #endif
  403. }
  404. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::ProfilingGraphDescInfo(
  405. uint32_t model_id, const std::vector<ComputeGraphDescInfo> &compute_graph_desc_info, const int32_t &device_id) {
  406. #ifdef DAVINCI_SUPPORT_PROFILING
  407. Msprof::Engine::Reporter *reporter = PluginImpl::GetPluginReporter();
  408. GE_IF_BOOL_EXEC(reporter == nullptr, GELOGI("Profiling report is nullptr!"); return;);
  409. std::string data;
  410. for (const auto &graph : compute_graph_desc_info) {
  411. data.append("model_name:")
  412. .append(graph.model_name)
  413. .append(" op_name:")
  414. .append(graph.op_name)
  415. .append(" op_type:")
  416. .append(graph.op_type);
  417. for (size_t i = 0; i < graph.input_format.size(); ++i) {
  418. data.append(" input_id:")
  419. .append(std::to_string(i))
  420. .append(" input_format:")
  421. .append(std::to_string(graph.input_format.at(i)))
  422. .append(" input_data_type:")
  423. .append(std::to_string(graph.input_data_type.at(i)))
  424. .append(" input_shape:\"");
  425. size_t input_shape_len = graph.input_shape.at(i).size();
  426. if (input_shape_len == 0) {
  427. data.append("");
  428. } else if (input_shape_len == 1) {
  429. data.append(std::to_string(graph.input_shape.at(i).at(0)));
  430. } else {
  431. for (size_t j = 0; j < input_shape_len - 1; ++j) {
  432. data.append(std::to_string(graph.input_shape.at(i).at(j))).append(",");
  433. }
  434. data.append(std::to_string(graph.input_shape.at(i).at(input_shape_len - 1)));
  435. }
  436. data.append("\"");
  437. }
  438. for (size_t i = 0; i < graph.output_format.size(); ++i) {
  439. data.append(" output_id:")
  440. .append(std::to_string(i))
  441. .append(" output_format:")
  442. .append(std::to_string(graph.output_format.at(i)))
  443. .append(" output_data_type:")
  444. .append(std::to_string(graph.output_data_type.at(i)))
  445. .append(" output_shape:\"");
  446. size_t output_shape_len = graph.output_shape.at(i).size();
  447. if (output_shape_len == 0) {
  448. data.append("");
  449. } else if (output_shape_len == 1) {
  450. data.append(std::to_string(graph.output_shape.at(i).at(0)));
  451. } else {
  452. for (size_t j = 0; j < output_shape_len - 1; ++j) {
  453. data.append(std::to_string(graph.output_shape.at(i).at(j))).append(",");
  454. }
  455. data.append(std::to_string(graph.output_shape.at(i).at(output_shape_len - 1)));
  456. }
  457. data.append("\"");
  458. }
  459. data.append(" model_id:").append(std::to_string(model_id));
  460. data.append("\n");
  461. Msprof::Engine::ReporterData reporter_data{};
  462. Report(device_id, data, *reporter, reporter_data);
  463. data.clear();
  464. }
  465. #endif
  466. }
  467. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::Report(
  468. const int32_t &device_id, const string &data, Msprof::Engine::Reporter &reporter,
  469. Msprof::Engine::ReporterData &reporter_data) {
  470. #ifdef DAVINCI_SUPPORT_PROFILING
  471. size_t index = data.size() / kReportMaxLen;
  472. if (index >= 1) {
  473. reporter_data.deviceId = device_id;
  474. int ret = memcpy_s(reporter_data.tag, MSPROF_ENGINE_MAX_TAG_LEN + 1, "graph_desc_info", sizeof("graph_desc_info"));
  475. GE_IF_BOOL_EXEC(ret != EOK, GELOGE(ret, "Report data tag of graph_desc_info memcpy error!"); return;);
  476. for (size_t i = 0; i < index; ++i) {
  477. reporter_data.data = (unsigned char *)data.c_str() + kReportMaxLen * i;
  478. reporter_data.dataLen = kReportMaxLen;
  479. ret = reporter.Report(&reporter_data);
  480. GE_IF_BOOL_EXEC(ret != SUCCESS, GELOGE(ret, "Reporter data of graph_desc_info fail!"); return;);
  481. }
  482. reporter_data.dataLen = data.size() - kReportMaxLen * index;
  483. if (reporter_data.dataLen != 0) {
  484. reporter_data.data = (unsigned char *)data.c_str() + kReportMaxLen * index;
  485. ret = reporter.Report(&reporter_data);
  486. GE_IF_BOOL_EXEC(ret != SUCCESS, GELOGE(ret, "Reporter data of graph_desc_info fail!"); return;);
  487. }
  488. } else {
  489. reporter_data.deviceId = device_id;
  490. reporter_data.data = (unsigned char *)data.c_str();
  491. reporter_data.dataLen = data.size();
  492. int ret = memcpy_s(reporter_data.tag, MSPROF_ENGINE_MAX_TAG_LEN + 1, "graph_desc_info", sizeof("graph_desc_info"));
  493. GE_IF_BOOL_EXEC(ret != EOK, GELOGE(ret, "Report data tag of graph_desc_info memcpy error!"); return;);
  494. ret = reporter.Report(&reporter_data);
  495. GE_IF_BOOL_EXEC(ret != SUCCESS, GELOGE(ret, "Reporter data of graph_desc_info fail!"); return;);
  496. }
  497. #endif
  498. }
  499. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::PluginUnInit(const std::string &module) const {
  500. #ifdef DAVINCI_SUPPORT_PROFILING
  501. int ret = Msprof::Engine::UnInit(module);
  502. if (ret != SUCCESS) {
  503. GELOGE(ret, "profiling plugin uninit failed, ret:%d", ret);
  504. }
  505. #endif
  506. }
  507. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::ReportProfilingData(
  508. uint32_t model_id, const std::vector<TaskDescInfo> &task_desc_info,
  509. const std::vector<ComputeGraphDescInfo> &compute_graph_desc_info,
  510. bool check_device) {
  511. #ifdef DAVINCI_SUPPORT_PROFILING
  512. int32_t logic_device_id = 0;
  513. rtError_t rt_ret = rtGetDevice(&logic_device_id);
  514. if (rt_ret != RT_ERROR_NONE) {
  515. GELOGE(rt_ret, "runtime get logic_device_id failed, current logic_device_id:%d", logic_device_id);
  516. return;
  517. }
  518. GELOGD("current logic_device_id:%d", logic_device_id);
  519. if (check_device) {
  520. auto ret = std::find(device_id_.begin(), device_id_.end(), logic_device_id);
  521. if (ret == device_id_.end()) {
  522. GELOGE(FAILED, "get valid phy_device_id failed, profiling report failed.");
  523. return;
  524. }
  525. }
  526. GELOGD("start ProfilingTaskDescInfo.");
  527. ProfilingTaskDescInfo(model_id, task_desc_info, logic_device_id);
  528. GELOGD("start ProfilingGraphDescInfo.");
  529. ProfilingGraphDescInfo(model_id, compute_graph_desc_info, logic_device_id);
  530. GELOGD("Report profiling data for GE end.");
  531. #endif
  532. }
  533. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::SetProfilingConfig(
  534. const std::string &profiling_cfg) {
  535. recv_profiling_config_ = profiling_cfg;
  536. }
  537. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY uint64_t ProfilingManager::GetProfilingModule() {
  538. uint64_t module = PROF_MODEL_EXECUTE_MASK |
  539. PROF_RUNTIME_API_MASK |
  540. PROF_RUNTIME_TRACE_MASK |
  541. PROF_SCHEDULE_TIMELINE_MASK |
  542. PROF_SCHEDULE_TRACE_MASK |
  543. PROF_TASK_TIME_MASK |
  544. PROF_SUBTASK_TIME_MASK |
  545. PROF_AICPU_TRACE_MASK |
  546. PROF_AICORE_METRICS_MASK |
  547. PROF_AIVECTORCORE_METRICS_MASK |
  548. PROF_MODEL_LOAD_MASK;
  549. return module;
  550. }
  551. void ProfilingManager::UpdateSubscribeDeviceModuleMap(std::string prof_type,
  552. uint32_t device_id,
  553. uint64_t module) {
  554. #ifdef DAVINCI_SUPPORT_PROFILING
  555. if (prof_type == kProfModelSubscribe) {
  556. if (subs_dev_module_.find(device_id) != subs_dev_module_.end()) {
  557. subs_dev_module_[device_id].subscribe_count++;
  558. } else {
  559. DeviceSubsInfo dev_info;
  560. dev_info.module = module;
  561. dev_info.subscribe_count = 1;
  562. subs_dev_module_[device_id] = dev_info;
  563. }
  564. } else if (prof_type == kProfModelUnsubscribe) {
  565. if (subs_dev_module_.find(device_id) != subs_dev_module_.end()) {
  566. if (subs_dev_module_[device_id].subscribe_count > 0) {
  567. subs_dev_module_[device_id].subscribe_count--;
  568. }
  569. }
  570. } else {
  571. GELOGI("No need to update device_id module map.");
  572. }
  573. #endif
  574. }
  575. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfModelSubscribe(
  576. uint64_t module, void *model) {
  577. #ifdef DAVINCI_SUPPORT_PROFILING
  578. std::lock_guard<std::mutex> lock(mutex_);
  579. uint64_t model_load_mask = module & PROF_MODEL_LOAD_MASK;
  580. if ((subscribe_count_ == 0) && (model_load_mask == PROF_MODEL_LOAD_MASK)) {
  581. // register framework to profiling
  582. int32_t result = Msprof::Engine::Init(GE_PROFILING_MODULE, &engine_);
  583. if (result != SUCCESS) {
  584. GELOGE(FAILED, "Register profiling engine failed.");
  585. return FAILED;
  586. }
  587. GELOGI("Prof subscribe: model load profiling on.");
  588. }
  589. subscribe_count_++;
  590. auto davinci_model = static_cast<DavinciModel *>(model);
  591. int32_t device_num = 1;
  592. uint32_t device[1];
  593. device[0] = davinci_model->GetDeviceId();
  594. rtError_t rt_ret = rtProfilerStart(module, device_num, device);
  595. if (rt_ret != RT_ERROR_NONE) {
  596. GELOGE(FAILED, "Runtime profiler start failed.");
  597. return FAILED;
  598. }
  599. UpdateSubscribeDeviceModuleMap(kProfModelSubscribe, device[0], module);
  600. // Report profiling data
  601. Status p_ret = davinci_model->ReportProfilingData(false);
  602. if (p_ret != SUCCESS) {
  603. GELOGE(p_ret, "Report profiling data failed.");
  604. return p_ret;
  605. }
  606. #endif
  607. return SUCCESS;
  608. }
  609. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfModelUnsubscribe(
  610. void *model) {
  611. #ifdef DAVINCI_SUPPORT_PROFILING
  612. std::lock_guard<std::mutex> lock(mutex_);
  613. if (subscribe_count_ == 0) {
  614. GELOGW("The profiler has not been subscribed, you do not need to cannel the subscription.");
  615. return SUCCESS;
  616. }
  617. auto davinci_model = static_cast<DavinciModel *>(model);
  618. int32_t dev_num = 1;
  619. uint32_t device[1];
  620. device[0] = davinci_model->GetDeviceId();
  621. auto iter = subs_dev_module_.find(device[0]);
  622. if (iter != subs_dev_module_.end()) {
  623. if (subs_dev_module_[device[0]].subscribe_count == 1) {
  624. rtError_t rt_ret = rtProfilerStop(subs_dev_module_[device[0]].module, dev_num, device);
  625. if (rt_ret != RT_ERROR_NONE) {
  626. GELOGE(FAILED, "Runtime profiler stop failed.");
  627. return FAILED;
  628. }
  629. }
  630. UpdateSubscribeDeviceModuleMap(kProfModelUnsubscribe, device[0], subs_dev_module_[device[0]].module);
  631. }
  632. subscribe_count_--;
  633. if (subscribe_count_ == 0) {
  634. int32_t ret = Msprof::Engine::UnInit(GE_PROFILING_MODULE);
  635. if (ret != SUCCESS) {
  636. GELOGE(ret, "Profiling plugin uninit failed, ret:%d", ret);
  637. return ret;
  638. }
  639. }
  640. #endif
  641. return SUCCESS;
  642. }
  643. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfInit(uint64_t module) {
  644. #ifdef DAVINCI_SUPPORT_PROFILING
  645. std::lock_guard<std::mutex> lock(mutex_);
  646. uint64_t model_load_mask = module & PROF_MODEL_LOAD_MASK;
  647. if (model_load_mask == PROF_MODEL_LOAD_MASK) {
  648. // register Framework to profiling
  649. int32_t result = Msprof::Engine::Init(GE_PROFILING_MODULE, &engine_);
  650. if (result != SUCCESS) {
  651. GELOGE(FAILED, "Register profiling engine failed.");
  652. return FAILED;
  653. }
  654. int32_t device_num = -1;
  655. rtError_t rt_ret = rtProfilerStart(model_load_mask, device_num, nullptr);
  656. if (rt_ret != RT_ERROR_NONE) {
  657. GELOGE(FAILED, "Runtime profiler start failed.");
  658. return FAILED;
  659. }
  660. is_load_profiling_ = true;
  661. GELOGI("Prof init: model load profiling on.");
  662. }
  663. uint64_t training_trace_mask = module & PROF_TRAINING_TRACE_MASK;
  664. if (training_trace_mask == PROF_TRAINING_TRACE_MASK) {
  665. is_training_trace_ = true;
  666. }
  667. is_acl_api_mode_ = true;
  668. GELOGI("Prof init success.");
  669. #endif
  670. return SUCCESS;
  671. }
  672. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfFinalize() {
  673. #ifdef DAVINCI_SUPPORT_PROFILING
  674. std::lock_guard<std::mutex> lock(mutex_);
  675. is_load_profiling_ = false;
  676. is_training_trace_ = false;
  677. is_acl_api_mode_ = false;
  678. int32_t ret = Msprof::Engine::UnInit(GE_PROFILING_MODULE);
  679. if (ret != SUCCESS) {
  680. GELOGE(ret, "Profiling plugin uninit failed, ret:%d", ret);
  681. }
  682. int32_t dev_num = -1;
  683. rtError_t rt_ret = rtProfilerStop(PROF_MODEL_LOAD_MASK, dev_num, nullptr);
  684. if (rt_ret != RT_ERROR_NONE) {
  685. GELOGE(FAILED, "Runtime profiler stop failed.");
  686. return FAILED;
  687. }
  688. for (auto device_id_module : device_id_module_map_) {
  689. if (device_id_module.second != 0) {
  690. uint32_t device_id = static_cast<uint32_t>(device_id_module.first);
  691. GELOGI("Prof finalize: device_id: %u, module: 0x%llx.", device_id, device_id_module.second);
  692. rt_ret = rtProfilerStop(device_id_module.second, 1, &device_id);
  693. if (rt_ret != RT_ERROR_NONE) {
  694. GELOGE(FAILED, "Runtime profiler stop failed.");
  695. return FAILED;
  696. }
  697. }
  698. }
  699. device_id_module_map_.clear();
  700. device_id_.clear();
  701. GELOGI("Prof finalize success.");
  702. #endif
  703. return SUCCESS;
  704. }
  705. Status ProfilingManager::ProfParseDeviceId(const std::map<std::string, std::string> &config_para,
  706. vector<int32_t> &device_list) {
  707. #ifdef DAVINCI_SUPPORT_PROFILING
  708. auto iter = config_para.find(kConfigDevIdList);
  709. if (iter != config_para.end()) {
  710. std::string device_id_list = iter->second;
  711. std::string temp;
  712. vector<std::string> decvice_id;
  713. for (uint32_t i = 0; i < device_id_list.size(); i++) {
  714. if (isdigit(device_id_list[i])) {
  715. temp.append(1, device_id_list[i]);
  716. } else {
  717. if (!temp.empty()) {
  718. decvice_id.emplace_back(temp);
  719. }
  720. temp.clear();
  721. }
  722. }
  723. if (!temp.empty()) {
  724. decvice_id.emplace_back(temp);
  725. }
  726. for (uint32_t i = 0; i < decvice_id.size(); i++) {
  727. try {
  728. int32_t dev_id = std::stoi(decvice_id[i]);
  729. device_list.push_back(dev_id);
  730. } catch (std::invalid_argument &) {
  731. GELOGE(FAILED, "Device id: %s is invalid.", decvice_id[i].c_str());
  732. return FAILED;
  733. } catch (std::out_of_range &) {
  734. GELOGE(FAILED, "Device id: %s is out of range.", decvice_id[i].c_str());
  735. } catch (...) {
  736. GELOGE(FAILED, "Device id: %s cannot change to int.", decvice_id[i].c_str());
  737. return FAILED;
  738. }
  739. }
  740. } else {
  741. GELOGE(FAILED, "Config para not contain device id list.");
  742. return FAILED;
  743. }
  744. #endif
  745. return SUCCESS;
  746. }
  747. Status ProfilingManager::ProfParseParam(const std::map<std::string, std::string> &config_para,
  748. int32_t &device_num, vector<int32_t> &device_list) {
  749. #ifdef DAVINCI_SUPPORT_PROFILING
  750. // device num
  751. auto iter = config_para.find(kConfigNumsdev);
  752. if (iter != config_para.end()) {
  753. try {
  754. device_num = std::stoi(iter->second);
  755. } catch (std::invalid_argument &) {
  756. GELOGE(FAILED, "Device nun: %s is invalid.", iter->second.c_str());
  757. return FAILED;
  758. } catch (std::out_of_range &) {
  759. GELOGE(FAILED, "Device num: %s is out of range.", iter->second.c_str());
  760. } catch (...) {
  761. GELOGE(FAILED, "Device num: %s cannot change to int.", iter->second.c_str());
  762. return FAILED;
  763. }
  764. } else {
  765. GELOGE(FAILED, "Config para not contain device num.");
  766. return FAILED;
  767. }
  768. // device id
  769. if (ProfParseDeviceId(config_para, device_list) != SUCCESS) {
  770. GELOGE(FAILED, "Parse config para device id failed.");
  771. return FAILED;
  772. }
  773. if (device_num == 0 || device_num > kMaxDeviceNum || device_num != static_cast<int32_t>(device_list.size())) {
  774. GELOGE(FAILED, "Config para device num: %d not equal to device list size: %d.", device_num, device_list.size());
  775. return FAILED;
  776. }
  777. #endif
  778. return SUCCESS;
  779. }
  780. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfStartProfiling(
  781. uint64_t module, const std::map<std::string, std::string> &config_para) {
  782. #ifdef DAVINCI_SUPPORT_PROFILING
  783. std::lock_guard<std::mutex> lock(mutex_);
  784. int32_t device_num = 0;
  785. vector<int32_t> device_list;
  786. if (ProfParseParam(config_para, device_num, device_list) != SUCCESS) {
  787. GELOGE(FAILED, "Prof start parse param failed.");
  788. return FAILED;
  789. }
  790. auto device_id_ptr = std::unique_ptr<uint32_t[]>(new (std::nothrow) uint32_t[device_num]);
  791. if (device_id_ptr == nullptr) {
  792. GELOGE(FAILED, "Prof start: device id ptr is null.");
  793. return FAILED;
  794. }
  795. for (int32_t i = 0; i < device_num; i++) {
  796. device_id_ptr[i] = static_cast<uint32_t>(device_list[i]);
  797. }
  798. GELOGD("Runtime config param: 0x%llx, device num: %d.", module, device_num);
  799. rtError_t rt_ret = rtProfilerStart(module, device_num, device_id_ptr.get());
  800. if (rt_ret != RT_ERROR_NONE) {
  801. GELOGE(FAILED, "Runtime profiler config proc failed.");
  802. return FAILED;
  803. }
  804. if ((module & PROF_MODEL_EXECUTE_MASK) == PROF_MODEL_EXECUTE_MASK) {
  805. for (int32_t i = 0; i < device_num; i++) {
  806. if (std::find(device_id_.begin(), device_id_.end(), device_list[i]) == device_id_.end()) {
  807. device_id_.push_back(device_list[i]);
  808. }
  809. }
  810. GELOGI("Prof start: ge execute model start profiling.");
  811. }
  812. if ((module & PROF_MODEL_LOAD_MASK) == PROF_MODEL_LOAD_MASK) {
  813. GELOGW("Prof start: load model module is invalid.");
  814. }
  815. UpdateDeviceIdModuleMap(kProfStart, module, device_list);
  816. GELOGD("Prof start profiling success.");
  817. #endif
  818. return SUCCESS;
  819. }
  820. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfStopProfiling(uint64_t module,
  821. const std::map<std::string, std::string> &config_para) {
  822. #ifdef DAVINCI_SUPPORT_PROFILING
  823. std::lock_guard<std::mutex> lock(mutex_);
  824. int32_t device_num = 0;
  825. vector<int32_t> device_list;
  826. if (ProfParseParam(config_para, device_num, device_list) != SUCCESS) {
  827. GELOGE(FAILED, "Prof stop parse param failed.");
  828. return FAILED;
  829. }
  830. auto device_id_ptr = std::unique_ptr<uint32_t[]>(new (std::nothrow) uint32_t[device_num]);
  831. if (device_id_ptr == nullptr) {
  832. GELOGE(FAILED, "Prof stop: device id ptr is null.");
  833. return FAILED;
  834. }
  835. for (int32_t i = 0; i < device_num; i++) {
  836. device_id_ptr[i] = static_cast<uint32_t>(device_list[i]);
  837. }
  838. GELOGD("Prof stop: runtime config param: 0x%llx, device num: %d", module, device_num);
  839. rtError_t rt_ret = rtProfilerStop(module, device_num, device_id_ptr.get());
  840. if (rt_ret != RT_ERROR_NONE) {
  841. GELOGE(FAILED, "Prof stop: runtime profiler config proc failed.");
  842. return FAILED;
  843. }
  844. uint64_t execute_model_mask = module & PROF_MODEL_EXECUTE_MASK;
  845. if (execute_model_mask == PROF_MODEL_EXECUTE_MASK) {
  846. for (int32_t i = 0; i < device_num; i++) {
  847. auto iter = std::find(device_id_.begin(), device_id_.end(), device_list[i]);
  848. if (iter != device_id_.end()) {
  849. device_id_.erase(iter);
  850. }
  851. }
  852. GELOGI("Prof stop: ge execute model stop profiling.");
  853. }
  854. if ((module & PROF_MODEL_LOAD_MASK) == PROF_MODEL_LOAD_MASK) {
  855. GELOGW("Prof stop: load model module is invalid.");
  856. }
  857. UpdateDeviceIdModuleMap(kProfStop, module, device_list);
  858. GELOGD("Prof stop profiling success.");
  859. #endif
  860. return SUCCESS;
  861. }
  862. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::UpdateDeviceIdModuleMap(string prof_type,
  863. uint64_t module, const vector<int32_t> &device_list) {
  864. #ifdef DAVINCI_SUPPORT_PROFILING
  865. if (prof_type == kProfStart) {
  866. for (uint32_t i = 0; i < device_list.size(); i++) {
  867. auto iter = device_id_module_map_.find(device_list[i]);
  868. if (iter != device_id_module_map_.end()) {
  869. uint64_t prof_on_module = device_id_module_map_[device_list[i]];
  870. // save all profiling on module of device
  871. device_id_module_map_[device_list[i]] = prof_on_module | module;
  872. } else {
  873. device_id_module_map_[device_list[i]] = module;
  874. }
  875. }
  876. } else if (prof_type == kProfStop) {
  877. for (uint32_t i = 0; i < device_list.size(); i++) {
  878. auto iter = device_id_module_map_.find(device_list[i]);
  879. if (iter != device_id_module_map_.end()) {
  880. uint64_t prof_on_module = device_id_module_map_[device_list[i]];
  881. uint64_t prof_off_module = prof_on_module & module;
  882. uint64_t prof_on_left_module = prof_on_module & (~prof_off_module);
  883. // stop profiling on module of device
  884. device_id_module_map_[device_list[i]] = prof_on_left_module;
  885. }
  886. }
  887. } else {
  888. GELOGI("No need to update device_id module map.");
  889. }
  890. #endif
  891. }
  892. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY bool ProfilingManager::ProfilingModelExecuteOn() const {
  893. int32_t logic_device_id = 0;
  894. rtError_t rt_ret = rtGetDevice(&logic_device_id);
  895. if (rt_ret != RT_ERROR_NONE) {
  896. GELOGE(rt_ret, "Runtime get logic_device_id failed, current logic_device_id:%d", logic_device_id);
  897. }
  898. GELOGD("Current logic_device_id:%d", logic_device_id);
  899. bool execute_model_prof_on = false;
  900. auto iter = std::find(device_id_.begin(), device_id_.end(), logic_device_id);
  901. if (iter != device_id_.end()) {
  902. execute_model_prof_on = true;
  903. }
  904. GELOGD("Flag is_execute_profiling: %d, execute_model_prof_on: %d", is_execute_profiling_, execute_model_prof_on);
  905. return is_execute_profiling_ || execute_model_prof_on;
  906. }
  907. /**
  908. * @brief Profiling PluginImpl
  909. */
  910. // PluginImpl static variable init
  911. Msprof::Engine::Reporter *PluginImpl::reporter_ = nullptr;
  912. PluginImpl::PluginImpl(const std::string &module) : module_(module) { GELOGI("Create PluginImpl\n"); }
  913. int PluginImpl::Init(const Msprof::Engine::Reporter *reporter) {
  914. GELOGI("PluginImpl init");
  915. reporter_ = const_cast<Msprof::Engine::Reporter *>(reporter);
  916. return 0;
  917. }
  918. int PluginImpl::UnInit() {
  919. GELOGI("PluginImpl Uninit");
  920. reporter_ = nullptr;
  921. return 0;
  922. }
  923. Msprof::Engine::PluginIntf *ProfilingEngineImpl::CreatePlugin() {
  924. GELOGI(" Create Plugin");
  925. return new (std::nothrow) PluginImpl(GE_PROFILING_MODULE);
  926. }
  927. int ProfilingEngineImpl::ReleasePlugin(Msprof::Engine::PluginIntf *plugin) {
  928. if (plugin != nullptr) {
  929. delete plugin;
  930. plugin = nullptr;
  931. }
  932. return 0;
  933. }
  934. } // namespace ge

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