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

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