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
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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. job_id_ = options.job_id;
  51. GELOGI("ProfilingManager::Init job_id:%s", job_id_.c_str());
  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. device_id_.push_back(options.device_id);
  60. }
  61. }
  62. if (ret != SUCCESS) {
  63. GELOGE(ret, "Failed to init profiling.");
  64. return ret;
  65. }
  66. if (is_load_profiling_) {
  67. // register Framework to profiling
  68. int result = Msprof::Engine::Init(GE_PROFILING_MODULE, &engine_);
  69. if (result != 0) {
  70. GELOGE(FAILED, "Register profiling engine failed.");
  71. return FAILED;
  72. }
  73. // profiling startup first time
  74. GELOGI("Begin to init profiling, device num %zu", device_id_.size());
  75. for (size_t i = 0; i < device_id_.size(); ++i) {
  76. ret = StartProfiling(0, device_id_[i]);
  77. if (ret != SUCCESS) {
  78. GELOGW("Profiling start failed on device %d.", device_id_[i]);
  79. continue;
  80. }
  81. GELOGI("Profiling init succ on device %d.", device_id_[i]);
  82. }
  83. } else {
  84. GELOGI("The profiling is off, skip the initialization");
  85. }
  86. #endif
  87. return SUCCESS;
  88. }
  89. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ge::Status ProfilingManager::InitFromAclCfg(
  90. const std::string &config) {
  91. #ifdef DAVINCI_SUPPORT_PROFILING
  92. try {
  93. is_load_profiling_ = false;
  94. is_execute_profiling_ = false;
  95. profiling_opts_.clear();
  96. op_trace_conf_.clear();
  97. Json start_prof_conf = Json::parse(config);
  98. Json &prof_conf = start_prof_conf[kStartCfg][0];
  99. job_id_ = prof_conf[kJobID];
  100. auto iter = prof_conf.find(kProfDir);
  101. if (iter != prof_conf.end()) {
  102. prof_dir_ = prof_conf[kProfDir];
  103. }
  104. Json &device_id = prof_conf[kDeviceID];
  105. if (device_id.size() != 0) {
  106. vector<int32_t>().swap(device_id_);
  107. bool is_all = false;
  108. for (size_t i = 0; i < device_id.size(); i++) {
  109. std::string device_id_str = device_id[i].get<std::string>();
  110. if (device_id_str == "all") {
  111. is_all = true;
  112. break;
  113. }
  114. device_id_.push_back(std::stoi(device_id_str));
  115. }
  116. if (is_all) {
  117. int32_t count = 0;
  118. rtError_t rt_err = rtGetDeviceCount(&count);
  119. if (rt_err != RT_ERROR_NONE) {
  120. GELOGE(FAILED, "Call rtGetDeviceCount to get device failed.");
  121. }
  122. vector<int32_t>().swap(device_id_);
  123. for (int32_t i = 0; i < count; ++i) {
  124. device_id_.push_back(i);
  125. }
  126. }
  127. }
  128. Json &features = prof_conf[kFeatures];
  129. if (ParseFeaturesFromAclCfg(features) != SUCCESS) {
  130. GELOGE(FAILED, "Parse feature from acl cfg failed.");
  131. return FAILED;
  132. }
  133. is_load_profiling_ = true;
  134. is_execute_profiling_ = true;
  135. } catch (...) {
  136. GELOGE(FAILED, "Json conf is not invalid !");
  137. return ge::PARAM_INVALID;
  138. }
  139. #endif
  140. return ge::SUCCESS;
  141. }
  142. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ge::Status ProfilingManager::ParseFeaturesFromAclCfg(
  143. const Json &features) {
  144. #ifdef DAVINCI_SUPPORT_PROFILING
  145. try {
  146. for (size_t i = 0; i < features.size(); ++i) {
  147. const Json &feature = features[i];
  148. if ((feature.find(kName) == feature.end()) || feature[kName].is_null()) {
  149. continue;
  150. }
  151. const std::string &name = feature[kName];
  152. if (name == "op_trace") {
  153. const Json &conf = feature[kConf];
  154. const Json &events = conf[0][kEvents];
  155. const std::string &ai_core_events = events[0][kAiCoreEvents];
  156. GELOGI("Op trace config from acl ai_core_events:%s", ai_core_events.c_str());
  157. is_op_trace_ = true;
  158. ProfMgrConf prof_mgr_conf;
  159. int result = ProfMgrGetConf(ai_core_events, &prof_mgr_conf);
  160. if (result != 0) {
  161. GELOGE(FAILED, "ProfMgrGetConf failed.");
  162. return FAILED;
  163. }
  164. op_trace_conf_ = prof_mgr_conf.conf;
  165. op_trace_iter_num_ = static_cast<int32_t>(op_trace_conf_.size());
  166. GELOGI("Op trace profiling iter num %d,", op_trace_iter_num_);
  167. } else if (name == "task_trace") {
  168. is_op_trace_ = false;
  169. if (feature.find(kConf) != feature.end()) {
  170. const Json &conf = feature[kConf];
  171. std::stringstream task_trace_conf;
  172. task_trace_conf << conf;
  173. task_trace_conf_ = task_trace_conf.str();
  174. }
  175. GELOGI("Task trace config from acl");
  176. } else if (name == "system_trace") {
  177. is_op_trace_ = false;
  178. const Json &conf = feature[kConf];
  179. std::stringstream system_trace_conf;
  180. system_trace_conf << conf;
  181. system_trace_conf_ = system_trace_conf.str();
  182. GELOGI("System trace config from acl");
  183. }
  184. profiling_opts_.push_back(name);
  185. }
  186. } catch (...) {
  187. GELOGE(ge::PARAM_INVALID, "Json conf feature is not invalid !");
  188. return ge::PARAM_INVALID;
  189. }
  190. #endif
  191. return ge::SUCCESS;
  192. }
  193. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ge::Status ProfilingManager::InitFromOptions(const Options &options) {
  194. #ifdef DAVINCI_SUPPORT_PROFILING
  195. // enable profiling support two ways: env and front end
  196. const char *profiling_mode = std::getenv("PROFILING_MODE");
  197. const char *prof_options = std::getenv("PROFILING_OPTIONS");
  198. if ((profiling_mode == nullptr) || (strcmp("true", profiling_mode) != 0) || (prof_options == nullptr)) {
  199. is_load_profiling_ = false;
  200. is_execute_profiling_ = false;
  201. } else {
  202. std::string prof_options_str = std::string(prof_options);
  203. profiling_opts_ = StringUtils::Split(prof_options_str, ':');
  204. is_load_profiling_ = true;
  205. is_execute_profiling_ = true;
  206. GELOGI("The profiling in env is %s, %s", profiling_mode, prof_options);
  207. }
  208. if (!is_load_profiling_) {
  209. const std::string enable_profiling = "1";
  210. if (options.profiling_mode != enable_profiling || options.profiling_options.empty()) {
  211. is_load_profiling_ = false;
  212. is_execute_profiling_ = false;
  213. return SUCCESS;
  214. } else {
  215. profiling_opts_ = StringUtils::Split(options.profiling_options, ':');
  216. is_load_profiling_ = true;
  217. is_execute_profiling_ = true;
  218. GELOGI("The profiling in options is %s, %s", options.profiling_mode.c_str(), options.profiling_options.c_str());
  219. }
  220. }
  221. // features:'training_trace', 'task_trace' or 'op_trace' etc
  222. if (!profiling_opts_.empty()) {
  223. if (profiling_opts_[0] == "op_trace") {
  224. is_op_trace_ = true;
  225. // op trace get conf
  226. ProfMgrConf prof_mgr_conf;
  227. int result = ProfMgrGetConf("", &prof_mgr_conf);
  228. if (result != 0) {
  229. GELOGE(FAILED, "ProfMgrGetConf failed.");
  230. return FAILED;
  231. }
  232. op_trace_conf_ = prof_mgr_conf.conf;
  233. op_trace_iter_num_ = static_cast<int32_t>(op_trace_conf_.size());
  234. GELOGI("op trace profiling iter num %d,", op_trace_iter_num_);
  235. } else {
  236. is_op_trace_ = false;
  237. op_trace_iter_num_ = 1;
  238. }
  239. }
  240. #endif
  241. return ge::SUCCESS;
  242. }
  243. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ge::Status ProfilingManager::StartProfiling(int32_t iter_num,
  244. int32_t device_id) {
  245. #ifdef DAVINCI_SUPPORT_PROFILING
  246. if (!profiling_opts_.empty()) {
  247. GELOGI("Start profiling index is %d", iter_num);
  248. // current one docker only use one device
  249. Json p_device;
  250. try {
  251. // profiling need physical_device_id
  252. p_device[kDeviceID] = std::to_string(device_id);
  253. p_device[kJobID] = job_id_;
  254. p_device[kTraceID] = std::to_string(GetContext().TraceId());
  255. if (!prof_dir_.empty()) {
  256. p_device[kProfDir] = prof_dir_;
  257. GELOGI("Prof dir: %s.", prof_dir_.c_str());
  258. }
  259. Json features;
  260. if (is_op_trace_) {
  261. Json f;
  262. f[kName] = "op_trace";
  263. Json conf;
  264. if (op_trace_conf_.size() <= static_cast<size_t>(iter_num)) {
  265. GELOGE(FAILED, "Op trace iter num is invalid!");
  266. return FAILED;
  267. }
  268. Json events;
  269. events[0] = nlohmann::json::parse(op_trace_conf_[iter_num]);
  270. conf[0][kEvents] = events;
  271. f[kConf] = conf;
  272. features[0] = f;
  273. if (iter_num == 0) {
  274. is_load_ = true;
  275. }
  276. } else {
  277. for (std::vector<std::string>::size_type i = 0; i < profiling_opts_.size(); i++) {
  278. Json f;
  279. if (profiling_opts_[i] == "system_trace") {
  280. f[kConf] = nlohmann::json::parse(system_trace_conf_);
  281. } else if (profiling_opts_[i] == "task_trace") {
  282. if (!task_trace_conf_.empty()) {
  283. f[kConf] = nlohmann::json::parse(task_trace_conf_);
  284. }
  285. }
  286. f[kName] = profiling_opts_[i];
  287. features[i] = f;
  288. }
  289. is_load_ = true;
  290. }
  291. p_device[kFeatures] = features;
  292. // only one device, but sProfMgrStartUp API require for device list
  293. Json devices;
  294. devices[0] = p_device;
  295. Json start_cfg;
  296. start_cfg[kStartCfg] = devices;
  297. // convert json to string
  298. std::stringstream ss;
  299. ss << start_cfg;
  300. send_profiling_config_ = ss.str();
  301. GELOGI("Profiling config %s\n", send_profiling_config_.c_str());
  302. } catch (...) {
  303. GELOGE(FAILED, "Op trace json conf is not invalid !");
  304. return FAILED;
  305. }
  306. // runtime startup for profiling
  307. uint64_t module = GetProfilingModule();
  308. int32_t device_num = 1;
  309. uint32_t device_id_rt = static_cast<uint32_t>(device_id);
  310. GE_CHK_RT_RET(rtProfilerStart(module, device_num, &device_id_rt));
  311. // call profiling startup API
  312. ProfMgrCfg prof_cfg = {send_profiling_config_};
  313. void *prof_handle = ProfMgrStartUp(&prof_cfg);
  314. if (prof_handle == nullptr) {
  315. GELOGW("ProfMgrStartUp failed on device %d ", device_id);
  316. return FAILED;
  317. }
  318. GELOGD("StartProfiling, prof_handle: %p", prof_handle);
  319. prof_handle_vec_.push_back(prof_handle);
  320. }
  321. #endif
  322. return SUCCESS;
  323. }
  324. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::StopProfiling() {
  325. #ifdef DAVINCI_SUPPORT_PROFILING
  326. Msprof::Engine::Reporter *reporter = PluginImpl::GetPluginReporter();
  327. if (reporter != nullptr) {
  328. int ret = reporter->Flush();
  329. GELOGI("Report data end, ret is %d", ret);
  330. }
  331. uint64_t module = GetProfilingModule();
  332. int32_t device_num = static_cast<int32_t>(device_id_.size());
  333. auto device_id_ptr = std::unique_ptr<uint32_t[]>(new (std::nothrow) uint32_t[device_num]);
  334. if (device_id_ptr == nullptr) {
  335. GELOGE(FAILED, "Stop profiling: device id ptr is null.");
  336. return;
  337. }
  338. for (int32_t i = 0; i < device_num; i++) {
  339. device_id_ptr[i] = static_cast<uint32_t>(device_id_[i]);
  340. }
  341. rtError_t rt_ret = rtProfilerStop(module, device_num, device_id_ptr.get());
  342. if (rt_ret != RT_ERROR_NONE) {
  343. GELOGW("Call rtProfilerStop failed, ret:%d", rt_ret);
  344. }
  345. for (size_t i = 0; i < prof_handle_vec_.size(); ++i) {
  346. int result = ProfMgrStop(prof_handle_vec_[i]);
  347. if (result != 0) {
  348. GELOGW("ProfMgr stop return fail:%d, handle:%p", result, prof_handle_vec_[i]);
  349. }
  350. }
  351. vector<void *>().swap(prof_handle_vec_);
  352. is_load_ = false;
  353. recv_profiling_config_ = "";
  354. GELOGI("Stop Profiling success.");
  355. #endif
  356. }
  357. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::ProfilingTaskDescInfo(
  358. const std::vector<TaskDescInfo> &task_desc_info, const int32_t &device_id) {
  359. #ifdef DAVINCI_SUPPORT_PROFILING
  360. Msprof::Engine::Reporter *reporter = PluginImpl::GetPluginReporter();
  361. if (reporter == nullptr) {
  362. GELOGI("Profiling report is nullptr!");
  363. return;
  364. }
  365. std::string data;
  366. for (const auto &task : task_desc_info) {
  367. std::string model_name = task.model_name;
  368. std::string op_name = task.op_name;
  369. uint32_t block_dim = task.block_dim;
  370. uint32_t task_id = task.task_id;
  371. uint32_t stream_id = task.stream_id;
  372. data = model_name.append(" ")
  373. .append(op_name).append(" ")
  374. .append(std::to_string(block_dim).append(" ")
  375. .append(std::to_string(task_id)).append(" ")
  376. .append(std::to_string(stream_id)).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. if (!is_acl_api_mode_) {
  508. auto ret = std::find(device_id_.begin(), device_id_.end(), logic_device_id);
  509. if (ret == device_id_.end()) {
  510. GELOGE(FAILED, "get valid phy_device_id failed, profiling report failed.");
  511. return;
  512. }
  513. }
  514. GELOGI("start ProfilingTaskDescInfo.");
  515. ProfilingTaskDescInfo(task_desc_info, logic_device_id);
  516. GELOGI("start ProfilingGraphDescInfo.");
  517. ProfilingGraphDescInfo(compute_graph_desc_info, logic_device_id);
  518. GELOGI("Report profiling data for GE end.");
  519. #endif
  520. }
  521. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::SetProfilingConfig(
  522. const std::string &profiling_cfg) {
  523. recv_profiling_config_ = profiling_cfg;
  524. }
  525. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY uint64_t ProfilingManager::GetProfilingModule() {
  526. uint64_t module = PROF_MODEL_EXECUTE_MASK |
  527. PROF_RUNTIME_API_MASK |
  528. PROF_RUNTIME_TRACE_MASK |
  529. PROF_SCHEDULE_TIMELINE_MASK |
  530. PROF_SCHEDULE_TRACE_MASK |
  531. PROF_TASK_TIME_MASK |
  532. PROF_SUBTASK_TIME_MASK |
  533. PROF_AICPU_TRACE_MASK |
  534. PROF_AICORE_METRICS_MASK |
  535. PROF_AIVECTORCORE_METRICS_MASK |
  536. 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,
  644. int32_t &device_num, 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 ProfilingManager::ProfStartProfiling(
  677. 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_ptr = std::unique_ptr<uint32_t[]>(new (std::nothrow) uint32_t[device_num]);
  687. if (device_id_ptr == nullptr) {
  688. GELOGE(FAILED, "Prof start: device id ptr is null.");
  689. return FAILED;
  690. }
  691. for (int32_t i = 0; i < device_num; i++) {
  692. device_id_ptr[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_ptr.get());
  696. if (rt_ret != RT_ERROR_NONE) {
  697. GELOGE(FAILED, "Runtime profiler config proc failed.");
  698. return FAILED;
  699. }
  700. if ((module & PROF_MODEL_EXECUTE_MASK) == PROF_MODEL_EXECUTE_MASK) {
  701. for (int32_t i = 0; i < device_num; i++) {
  702. if (std::find(device_id_.begin(), device_id_.end(), device_list[i]) == device_id_.end()) {
  703. device_id_.push_back(device_list[i]);
  704. }
  705. }
  706. GELOGI("Prof start: ge execute model start profiling.");
  707. }
  708. if ((module & PROF_MODEL_LOAD_MASK) == PROF_MODEL_LOAD_MASK) {
  709. GELOGW("Prof start: load model module is invalid.");
  710. }
  711. UpdateDeviceIdModuleMap(kProfStart, module, device_list);
  712. GELOGI("Prof start profiling success.");
  713. #endif
  714. return SUCCESS;
  715. }
  716. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfStopProfiling(uint64_t module,
  717. const std::map<std::string, std::string> &config_para) {
  718. #ifdef DAVINCI_SUPPORT_PROFILING
  719. std::lock_guard<std::mutex> lock(mutex_);
  720. int32_t device_num = 0;
  721. vector<int32_t> device_list;
  722. if (ProfParseParam(config_para, device_num, device_list) != SUCCESS) {
  723. GELOGE(FAILED, "Prof stop parse param failed.");
  724. return FAILED;
  725. }
  726. auto device_id_ptr = std::unique_ptr<uint32_t[]>(new (std::nothrow) uint32_t[device_num]);
  727. if (device_id_ptr == nullptr) {
  728. GELOGE(FAILED, "Prof stop: device id ptr is null.");
  729. return FAILED;
  730. }
  731. for (int32_t i = 0; i < device_num; i++) {
  732. device_id_ptr[i] = static_cast<uint32_t>(device_list[i]);
  733. }
  734. GELOGI("Prof stop: runtime config param: 0x%llx, device num: %d", module, device_num);
  735. rtError_t rt_ret = rtProfilerStop(module, device_num, device_id_ptr.get());
  736. if (rt_ret != RT_ERROR_NONE) {
  737. GELOGE(FAILED, "Prof stop: runtime profiler config proc failed.");
  738. return FAILED;
  739. }
  740. uint64_t execute_model_mask = module & PROF_MODEL_EXECUTE_MASK;
  741. if (execute_model_mask == PROF_MODEL_EXECUTE_MASK) {
  742. for (int32_t i = 0; i < device_num; i++) {
  743. auto iter = std::find(device_id_.begin(), device_id_.end(), device_list[i]);
  744. if (iter != device_id_.end()) {
  745. device_id_.erase(iter);
  746. }
  747. }
  748. GELOGI("Prof stop: ge execute model stop profiling.");
  749. }
  750. if ((module & PROF_MODEL_LOAD_MASK) == PROF_MODEL_LOAD_MASK) {
  751. GELOGW("Prof stop: load model module is invalid.");
  752. }
  753. UpdateDeviceIdModuleMap(kProfStop, module, device_list);
  754. GELOGI("Prof stop profiling success.");
  755. #endif
  756. return SUCCESS;
  757. }
  758. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::UpdateDeviceIdModuleMap(string prof_type,
  759. uint64_t module, const vector<int32_t> &device_list) {
  760. #ifdef DAVINCI_SUPPORT_PROFILING
  761. if (prof_type == kProfStart) {
  762. for (uint32_t i = 0; i < device_list.size(); i++) {
  763. auto iter = device_id_module_map_.find(device_list[i]);
  764. if (iter != device_id_module_map_.end()) {
  765. uint64_t prof_on_module = device_id_module_map_[device_list[i]];
  766. // save all profiling on module of device
  767. device_id_module_map_[device_list[i]] = prof_on_module | module;
  768. } else {
  769. device_id_module_map_[device_list[i]] = module;
  770. }
  771. }
  772. } else if (prof_type == kProfStop) {
  773. for (uint32_t i = 0; i < device_list.size(); i++) {
  774. auto iter = device_id_module_map_.find(device_list[i]);
  775. if (iter != device_id_module_map_.end()) {
  776. uint64_t prof_on_module = device_id_module_map_[device_list[i]];
  777. uint64_t prof_off_module = prof_on_module & module;
  778. uint64_t prof_on_left_module = prof_on_module & (~prof_off_module);
  779. // stop profiling on module of device
  780. device_id_module_map_[device_list[i]] = prof_on_left_module;
  781. }
  782. }
  783. } else {
  784. GELOGI("No need to update device_id module map.");
  785. }
  786. #endif
  787. }
  788. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY bool ProfilingManager::ProfilingModelExecuteOn() const {
  789. int32_t logic_device_id = 0;
  790. rtError_t rt_ret = rtGetDevice(&logic_device_id);
  791. if (rt_ret != RT_ERROR_NONE) {
  792. GELOGE(rt_ret, "Runtime get logic_device_id failed, current logic_device_id:%d", logic_device_id);
  793. }
  794. GELOGI("Current logic_device_id:%d", logic_device_id);
  795. bool execute_model_prof_on = false;
  796. auto iter = std::find(device_id_.begin(), device_id_.end(), logic_device_id);
  797. if (iter != device_id_.end()) {
  798. execute_model_prof_on = true;
  799. }
  800. GELOGI("Flag is_execute_profiling: %d, execute_model_prof_on: %d", is_execute_profiling_, execute_model_prof_on);
  801. return is_execute_profiling_ || execute_model_prof_on;
  802. }
  803. /**
  804. * @brief Profiling PluginImpl
  805. */
  806. // PluginImpl static variable init
  807. Msprof::Engine::Reporter *PluginImpl::reporter_ = nullptr;
  808. PluginImpl::PluginImpl(const std::string &module) : module_(module) { GELOGI("Create PluginImpl\n"); }
  809. int PluginImpl::Init(const Msprof::Engine::Reporter *reporter) {
  810. GELOGI("PluginImpl init");
  811. reporter_ = const_cast<Msprof::Engine::Reporter *>(reporter);
  812. return 0;
  813. }
  814. int PluginImpl::UnInit() {
  815. GELOGI("PluginImpl Uninit");
  816. reporter_ = nullptr;
  817. return 0;
  818. }
  819. Msprof::Engine::PluginIntf *ProfilingEngineImpl::CreatePlugin() {
  820. GELOGI(" Create Plugin");
  821. return new (std::nothrow) PluginImpl(GE_PROFILING_MODULE);
  822. }
  823. int ProfilingEngineImpl::ReleasePlugin(Msprof::Engine::PluginIntf *plugin) {
  824. if (plugin != nullptr) {
  825. delete plugin;
  826. plugin = nullptr;
  827. }
  828. return 0;
  829. }
  830. } // namespace ge

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