You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

profiling_manager.cc 33 kB

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

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