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

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