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 32 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "common/profiling/profiling_manager.h"
  17. #include "framework/common/debug/ge_log.h"
  18. #include "framework/common/debug/log.h"
  19. #include "framework/common/string_util.h"
  20. #include "graph/ge_context.h"
  21. #include "runtime/base.h"
  22. #include "graph/load/new_model_manager/davinci_model.h"
  23. #include "opskernel_manager/ops_kernel_builder_manager.h"
  24. namespace {
  25. const char *const kTrainingTrace = "training_trace";
  26. const char *const kFpPoint = "fp_point";
  27. const char *const kBpPoint = "bp_point";
  28. const size_t kReportMaxLen = 2048;
  29. const int32_t kMaxDeviceNum = 256;
  30. const std::string kConfigNumsdev = "devNums";
  31. const std::string kConfigDevIdList = "devIdList";
  32. const std::string kProfStart = "prof_start";
  33. const std::string kProfStop = "prof_stop";
  34. const std::string kProfModelSubscribe = "prof_model_subscribe";
  35. const std::string kProfModelUnsubscribe = "prof_model_cancel_subscribe";
  36. } // namespace
  37. namespace ge {
  38. ProfilingManager::ProfilingManager() : is_load_profiling_(false),
  39. is_execute_profiling_(false),
  40. is_training_trace_(false),
  41. subscribe_count_(0) {
  42. prof_cb_.msprofCtrlCallback = nullptr;
  43. prof_cb_.msprofReporterCallback = nullptr;
  44. }
  45. ProfilingManager::~ProfilingManager() {}
  46. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ProfilingManager &ProfilingManager::Instance() {
  47. static ProfilingManager profiling_manager;
  48. return profiling_manager;
  49. }
  50. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ge::Status ProfilingManager::Init(const Options &options) {
  51. #ifdef DAVINCI_SUPPORT_PROFILING
  52. vector<int32_t>().swap(device_id_);
  53. subscribe_count_ = 0;
  54. GELOGI("ProfilingManager::Init job_id:%s", options.job_id.c_str());
  55. struct MsprofGeOptions prof_conf = { 0 };
  56. Status ret = InitFromOptions(options, prof_conf);
  57. if (ret != SUCCESS) {
  58. GELOGE(ret, "Failed to init profiling.");
  59. return ret;
  60. }
  61. if (is_execute_profiling_) {
  62. if (prof_cb_.msprofCtrlCallback == nullptr) {
  63. GELOGE(ge::PARAM_INVALID, "MsprofCtrlCallback callback is nullptr.");
  64. return ge::PARAM_INVALID;
  65. }
  66. int32_t cb_ret = prof_cb_.msprofCtrlCallback(
  67. static_cast<uint32_t>(MsprofCtrlCallbackType::MSPROF_CTRL_INIT_GE_OPTIONS),
  68. static_cast<void *>(&prof_conf), sizeof(MsprofGeOptions));
  69. if (cb_ret != 0) {
  70. GELOGE(FAILED, "Call msprofCtrlCallback failed, type:%u, return:%d",
  71. static_cast<uint32_t>(MsprofCtrlCallbackType::MSPROF_CTRL_INIT_GE_OPTIONS), cb_ret);
  72. return FAILED;
  73. }
  74. GELOGI("Profiling init success");
  75. } else {
  76. GELOGI("The profiling is off, skip the initialization");
  77. }
  78. #endif
  79. return SUCCESS;
  80. }
  81. ge::Status ProfilingManager::InitFromOptions(const Options &options, MsprofGeOptions &prof_conf) {
  82. #ifdef DAVINCI_SUPPORT_PROFILING
  83. // enable profiling by env
  84. char env_profiling_mode[MMPA_MAX_PATH] = { 0x00 };
  85. is_load_profiling_ = false; // Change in ProfInit
  86. is_execute_profiling_ = false;
  87. if (options.profiling_mode == "1" && !options.profiling_options.empty()) {
  88. // enable profiling by ge option
  89. if (memcpy_s(prof_conf.options, MSPROF_OPTIONS_DEF_LEN_MAX, options.profiling_options.c_str(),
  90. sizeof(options.profiling_options.c_str())) != EOK) {
  91. GELOGE(INTERNAL_ERROR, "copy profiling_options failed.");
  92. return INTERNAL_ERROR;
  93. }
  94. is_execute_profiling_ = true;
  95. GELOGI("The profiling in options is %s, %s", options.profiling_mode.c_str(), prof_conf.options);
  96. } else {
  97. (void)mmGetEnv("PROFILING_MODE", env_profiling_mode, MMPA_MAX_PATH);
  98. (void)mmGetEnv("PROFILING_OPTIONS", prof_conf.options, MSPROF_OPTIONS_DEF_LEN_MAX);
  99. // The env is invalid
  100. if ((env_profiling_mode == nullptr) || (strcmp("true", env_profiling_mode) != 0)
  101. || (strcmp(prof_conf.options, "\0") == 0)) {
  102. return SUCCESS;
  103. }
  104. // enable profiling by env
  105. is_execute_profiling_ = true;
  106. GELOGI("The profiling in env is %s, %s", env_profiling_mode, prof_conf.options);
  107. }
  108. if (!is_execute_profiling_) {
  109. return SUCCESS;
  110. }
  111. // Parse json str for bp fp
  112. Status ret = ParseOptions(prof_conf.options);
  113. if (ret != ge::SUCCESS) {
  114. GELOGE(ge::PARAM_INVALID, "Parse training trace param failed.");
  115. return ge::PARAM_INVALID;
  116. }
  117. if (memcpy_s(prof_conf.jobId, sizeof(prof_conf.jobId), options.job_id.c_str(),
  118. sizeof(options.job_id.c_str())) != EOK) {
  119. GELOGE(INTERNAL_ERROR, "copy job_id failed.");
  120. return INTERNAL_ERROR;
  121. }
  122. // get traceId from options
  123. prof_conf.traceId = GetContext().TraceId();
  124. #endif
  125. return ge::SUCCESS;
  126. }
  127. ge::Status ProfilingManager::ParseOptions(const std::string &options) {
  128. if (options.empty()) {
  129. GELOGE(ge::PARAM_INVALID, "Profiling options is empty.")
  130. return ge::PARAM_INVALID;
  131. }
  132. try {
  133. Json prof_options = Json::parse(options);
  134. const std::string training_trace = prof_options[kTrainingTrace];
  135. if (training_trace.empty()) {
  136. GELOGI("Training trace will not take effect.");
  137. return ge::SUCCESS;
  138. }
  139. GELOGI("GE profiling training trace:%s", training_trace.c_str());
  140. if (training_trace != "on") {
  141. GELOGE(ge::PARAM_INVALID, "Training trace param:%s is invalid.", training_trace.c_str());
  142. return ge::PARAM_INVALID;
  143. }
  144. fp_point_ = prof_options[kFpPoint];
  145. bp_point_ = prof_options[kBpPoint];
  146. if (!fp_point_.empty() && !bp_point_.empty()) {
  147. GELOGI("Training trace bp fp is set, bp_point:%s, fp_point:%s.", bp_point_.c_str(), fp_point_.c_str());
  148. }
  149. } catch (...) {
  150. GELOGE(FAILED, "Json prof_conf options is invalid.");
  151. return ge::PARAM_INVALID;
  152. }
  153. return ge::SUCCESS;
  154. }
  155. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::StopProfiling() {
  156. #ifdef DAVINCI_SUPPORT_PROFILING
  157. uint64_t module = GetProfilingModule();
  158. // The following if case will not be executed in normal case, inc case of ProfStopProfiling is abnormal
  159. int32_t device_num = static_cast<int32_t>(device_id_.size());
  160. if (device_num != 0) {
  161. auto device_id_ptr = std::unique_ptr<uint32_t[]>(new (std::nothrow) uint32_t[device_num]);
  162. if (device_id_ptr == nullptr) {
  163. GELOGE(FAILED, "Stop profiling: device id ptr is null.");
  164. return;
  165. }
  166. for (int32_t i = 0; i < device_num; i++) {
  167. device_id_ptr[i] = static_cast<uint32_t>(device_id_[i]);
  168. }
  169. rtError_t rt_ret = rtProfilerStop(module, device_num, device_id_ptr.get());
  170. if (rt_ret != RT_ERROR_NONE) {
  171. GELOGW("Call rtProfilerStop failed, ret:%d", rt_ret);
  172. }
  173. }
  174. // stop profiling
  175. if (prof_cb_.msprofCtrlCallback == nullptr) {
  176. GELOGE(ge::PARAM_INVALID, "MsprofCtrlCallback callback is nullptr.");
  177. return;
  178. }
  179. int32_t cb_ret = prof_cb_.msprofCtrlCallback(static_cast<uint32_t>(MsprofCtrlCallbackType::MSPROF_CTRL_FINALIZE),
  180. nullptr, 0);
  181. if (cb_ret != 0) {
  182. GELOGW("call msprofCtrlCallback failed, type:%u, return:%d",
  183. static_cast<uint32_t>(MsprofCtrlCallbackType::MSPROF_CTRL_FINALIZE), cb_ret);
  184. return;
  185. }
  186. GELOGI("Stop Profiling success.");
  187. #endif
  188. }
  189. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::ProfilingTaskDescInfo(
  190. uint32_t model_id, const std::vector<TaskDescInfo> &task_desc_info, const int32_t &device_id) {
  191. #ifdef DAVINCI_SUPPORT_PROFILING
  192. std::string data;
  193. for (const auto &task : task_desc_info) {
  194. std::string model_name = task.model_name;
  195. std::string op_name = task.op_name;
  196. uint32_t block_dim = task.block_dim;
  197. uint32_t task_id = task.task_id;
  198. uint32_t stream_id = task.stream_id;
  199. data = model_name.append(" ")
  200. .append(op_name).append(" ")
  201. .append(std::to_string(block_dim).append(" ")
  202. .append(std::to_string(task_id)).append(" ")
  203. .append(std::to_string(stream_id)).append(" ")
  204. .append(std::to_string(model_id)).append("\n"));
  205. ReporterData reporter_data{};
  206. reporter_data.deviceId = device_id;
  207. reporter_data.data = (unsigned char *)data.c_str();
  208. reporter_data.dataLen = data.size();
  209. int ret = memcpy_s(reporter_data.tag, MSPROF_ENGINE_MAX_TAG_LEN + 1, "task_desc_info", sizeof("task_desc_info"));
  210. if (ret != EOK) {
  211. GELOGE(ret, "Report data tag of task_desc_info memcpy error!");
  212. return;
  213. }
  214. int32_t cb_ret = CallMsprofReport(reporter_data);
  215. if (cb_ret != 0) {
  216. GELOGE(cb_ret, "Reporter data of task_desc_info failed, ret:%d", cb_ret);
  217. return;
  218. }
  219. }
  220. data.clear();
  221. #endif
  222. }
  223. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::ProfilingGraphDescInfo(
  224. uint32_t model_id, const std::vector<ComputeGraphDescInfo> &compute_graph_desc_info, const int32_t &device_id) {
  225. #ifdef DAVINCI_SUPPORT_PROFILING
  226. std::string data;
  227. for (const auto &graph : compute_graph_desc_info) {
  228. data.append("model_name:")
  229. .append(graph.model_name)
  230. .append(" op_name:")
  231. .append(graph.op_name)
  232. .append(" op_type:")
  233. .append(graph.op_type);
  234. for (size_t i = 0; i < graph.input_format.size(); ++i) {
  235. data.append(" input_id:")
  236. .append(std::to_string(i))
  237. .append(" input_format:")
  238. .append(std::to_string(graph.input_format.at(i)))
  239. .append(" input_data_type:")
  240. .append(std::to_string(graph.input_data_type.at(i)))
  241. .append(" input_shape:\"");
  242. size_t input_shape_len = graph.input_shape.at(i).size();
  243. if (input_shape_len == 0) {
  244. data.append("");
  245. } else if (input_shape_len == 1) {
  246. data.append(std::to_string(graph.input_shape.at(i).at(0)));
  247. } else {
  248. for (size_t j = 0; j < input_shape_len - 1; ++j) {
  249. data.append(std::to_string(graph.input_shape.at(i).at(j))).append(",");
  250. }
  251. data.append(std::to_string(graph.input_shape.at(i).at(input_shape_len - 1)));
  252. }
  253. data.append("\"");
  254. }
  255. for (size_t i = 0; i < graph.output_format.size(); ++i) {
  256. data.append(" output_id:")
  257. .append(std::to_string(i))
  258. .append(" output_format:")
  259. .append(std::to_string(graph.output_format.at(i)))
  260. .append(" output_data_type:")
  261. .append(std::to_string(graph.output_data_type.at(i)))
  262. .append(" output_shape:\"");
  263. size_t output_shape_len = graph.output_shape.at(i).size();
  264. if (output_shape_len == 0) {
  265. data.append("");
  266. } else if (output_shape_len == 1) {
  267. data.append(std::to_string(graph.output_shape.at(i).at(0)));
  268. } else {
  269. for (size_t j = 0; j < output_shape_len - 1; ++j) {
  270. data.append(std::to_string(graph.output_shape.at(i).at(j))).append(",");
  271. }
  272. data.append(std::to_string(graph.output_shape.at(i).at(output_shape_len - 1)));
  273. }
  274. data.append("\"");
  275. }
  276. data.append(" model_id:").append(std::to_string(model_id));
  277. data.append("\n");
  278. GraphDescReport(device_id, data);
  279. data.clear();
  280. }
  281. #endif
  282. }
  283. void ProfilingManager::GraphDescReport(const int32_t &device_id, const string &data) {
  284. #ifdef DAVINCI_SUPPORT_PROFILING
  285. ReporterData reporter_data{};
  286. int ret = -1;
  287. int32_t cb_ret = -1;
  288. size_t index = data.size() / kReportMaxLen;
  289. if (index >= 1) {
  290. reporter_data.deviceId = device_id;
  291. ret = memcpy_s(reporter_data.tag, MSPROF_ENGINE_MAX_TAG_LEN + 1, "graph_desc_info", sizeof("graph_desc_info"));
  292. GE_IF_BOOL_EXEC(ret != EOK, GELOGE(ret, "Report data tag of graph_desc_info memcpy error!"); return;);
  293. for (size_t i = 0; i < index; ++i) {
  294. reporter_data.data = (unsigned char *)data.c_str() + kReportMaxLen * i;
  295. reporter_data.dataLen = kReportMaxLen;
  296. cb_ret = CallMsprofReport(reporter_data);
  297. GE_IF_BOOL_EXEC(cb_ret != 0, GELOGE(cb_ret, "Reporter data of graph_desc_info failed, ret:%d", cb_ret); return;);
  298. }
  299. reporter_data.dataLen = data.size() - kReportMaxLen * index;
  300. if (reporter_data.dataLen != 0) {
  301. reporter_data.data = (unsigned char *)data.c_str() + kReportMaxLen * index;
  302. cb_ret = CallMsprofReport(reporter_data);
  303. GE_IF_BOOL_EXEC(cb_ret != 0, GELOGE(cb_ret, "Reporter data of graph_desc_info failed, ret:%d", cb_ret); return;);
  304. }
  305. } else {
  306. reporter_data.deviceId = device_id;
  307. reporter_data.data = (unsigned char *)data.c_str();
  308. reporter_data.dataLen = data.size();
  309. ret = memcpy_s(reporter_data.tag, MSPROF_ENGINE_MAX_TAG_LEN + 1, "graph_desc_info", sizeof("graph_desc_info"));
  310. GE_IF_BOOL_EXEC(ret != EOK, GELOGE(ret, "Report data tag of graph_desc_info memcpy error!"); return;);
  311. cb_ret = CallMsprofReport(reporter_data);
  312. GE_IF_BOOL_EXEC(cb_ret != 0, GELOGE(cb_ret, "Reporter data of graph_desc_info failed, ret:%d", cb_ret); return;);
  313. }
  314. #endif
  315. }
  316. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::ReportProfilingData(
  317. uint32_t model_id, const std::vector<TaskDescInfo> &task_desc_info,
  318. const std::vector<ComputeGraphDescInfo> &compute_graph_desc_info) {
  319. #ifdef DAVINCI_SUPPORT_PROFILING
  320. int32_t logic_device_id = 0;
  321. rtError_t rt_ret = rtGetDevice(&logic_device_id);
  322. if (rt_ret != RT_ERROR_NONE) {
  323. GELOGE(rt_ret, "runtime get logic_device_id failed, current logic_device_id:%d", logic_device_id);
  324. return;
  325. }
  326. GELOGD("current logic_device_id:%d", logic_device_id);
  327. GELOGD("start ProfilingTaskDescInfo.");
  328. ProfilingTaskDescInfo(model_id, task_desc_info, logic_device_id);
  329. GELOGD("start ProfilingGraphDescInfo.");
  330. ProfilingGraphDescInfo(model_id, compute_graph_desc_info, logic_device_id);
  331. GELOGD("Report profiling data for GE end.");
  332. #endif
  333. }
  334. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY uint64_t ProfilingManager::GetProfilingModule() {
  335. uint64_t module = PROF_MODEL_EXECUTE_MASK |
  336. PROF_RUNTIME_API_MASK |
  337. PROF_RUNTIME_TRACE_MASK |
  338. PROF_SCHEDULE_TIMELINE_MASK |
  339. PROF_SCHEDULE_TRACE_MASK |
  340. PROF_TASK_TIME_MASK |
  341. PROF_SUBTASK_TIME_MASK |
  342. PROF_AICPU_TRACE_MASK |
  343. PROF_AICORE_METRICS_MASK |
  344. PROF_AIVECTORCORE_METRICS_MASK |
  345. PROF_MODEL_LOAD_MASK;
  346. return module;
  347. }
  348. void ProfilingManager::UpdateSubscribeDeviceModuleMap(std::string prof_type, uint32_t device_id, uint64_t module) {
  349. #ifdef DAVINCI_SUPPORT_PROFILING
  350. if (prof_type == kProfModelSubscribe) {
  351. if (subs_dev_module_.find(device_id) != subs_dev_module_.end()) {
  352. subs_dev_module_[device_id].subscribe_count++;
  353. } else {
  354. DeviceSubsInfo dev_info;
  355. dev_info.module = module;
  356. dev_info.subscribe_count = 1;
  357. subs_dev_module_[device_id] = dev_info;
  358. }
  359. } else if (prof_type == kProfModelUnsubscribe) {
  360. auto iter = subs_dev_module_.find(device_id);
  361. if (iter != subs_dev_module_.end()) {
  362. if (iter->second.subscribe_count > 0) {
  363. iter->second.subscribe_count--;
  364. }
  365. if (iter->second.subscribe_count == 0) {
  366. subs_dev_module_.erase(iter);
  367. }
  368. }
  369. } else {
  370. GELOGI("No need to update device_id module map.");
  371. }
  372. #endif
  373. }
  374. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfModelSubscribe(
  375. uint64_t module, void *model) {
  376. #ifdef DAVINCI_SUPPORT_PROFILING
  377. std::lock_guard<std::mutex> lock(mutex_);
  378. uint64_t model_load_mask = module & PROF_MODEL_LOAD_MASK;
  379. if ((subscribe_count_ == 0) && (model_load_mask == PROF_MODEL_LOAD_MASK)) {
  380. // register framework to profiling
  381. // register Framework to profiling
  382. int32_t cb_ret = PluginInit();
  383. if (cb_ret != 0) {
  384. GELOGE(cb_ret, "profiling plugin init failed, ret:%d", cb_ret);
  385. return cb_ret;
  386. }
  387. GELOGI("Prof subscribe: model load profiling on.");
  388. }
  389. subscribe_count_++;
  390. auto davinci_model = static_cast<DavinciModel *>(model);
  391. int32_t device_num = 1;
  392. uint32_t device[1];
  393. device[0] = davinci_model->GetDeviceId();
  394. rtError_t rt_ret = rtProfilerStart(module, device_num, device);
  395. if (rt_ret != RT_ERROR_NONE) {
  396. GELOGE(FAILED, "Runtime profiler start failed.");
  397. return FAILED;
  398. }
  399. UpdateSubscribeDeviceModuleMap(kProfModelSubscribe, device[0], module);
  400. // Report profiling data
  401. Status p_ret = davinci_model->ReportProfilingData();
  402. if (p_ret != SUCCESS) {
  403. GELOGE(p_ret, "Report profiling data failed.");
  404. return p_ret;
  405. }
  406. #endif
  407. return SUCCESS;
  408. }
  409. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfModelUnsubscribe(
  410. void *model) {
  411. #ifdef DAVINCI_SUPPORT_PROFILING
  412. std::lock_guard<std::mutex> lock(mutex_);
  413. if (subscribe_count_ == 0) {
  414. GELOGW("The profiler has not been subscribed, you do not need to cannel the subscription.");
  415. return SUCCESS;
  416. }
  417. auto davinci_model = static_cast<DavinciModel *>(model);
  418. int32_t dev_num = 1;
  419. uint32_t device[1];
  420. device[0] = davinci_model->GetDeviceId();
  421. auto iter = subs_dev_module_.find(device[0]);
  422. if (iter != subs_dev_module_.end()) {
  423. if (subs_dev_module_[device[0]].subscribe_count == 1) {
  424. // The same device_id, only stop at last time
  425. rtError_t rt_ret = rtProfilerStop(subs_dev_module_[device[0]].module, dev_num, device);
  426. if (rt_ret != RT_ERROR_NONE) {
  427. GELOGE(FAILED, "Runtime profiler stop failed.");
  428. return FAILED;
  429. }
  430. }
  431. UpdateSubscribeDeviceModuleMap(kProfModelUnsubscribe, device[0], subs_dev_module_[device[0]].module);
  432. } else {
  433. GELOGE(FAILED, "The device_id:%u has not been subscribed, do not need to cancel.", device[0]);
  434. return FAILED;
  435. }
  436. subscribe_count_--;
  437. if (subscribe_count_ == 0) {
  438. // profiling plugin uninit at last subscription
  439. PluginUnInit();
  440. }
  441. #endif
  442. return SUCCESS;
  443. }
  444. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfInit(uint64_t module) {
  445. #ifdef DAVINCI_SUPPORT_PROFILING
  446. std::lock_guard<std::mutex> lock(mutex_);
  447. uint64_t model_load_mask = module & PROF_MODEL_LOAD_MASK;
  448. if (model_load_mask == PROF_MODEL_LOAD_MASK) {
  449. // register Framework to profiling
  450. int32_t cb_ret = PluginInit();
  451. if (cb_ret != 0) {
  452. GELOGE(cb_ret, "profiling plugin init failed, ret:%d", cb_ret);
  453. return cb_ret;
  454. }
  455. int32_t device_num = -1;
  456. rtError_t rt_ret = rtProfilerStart(model_load_mask, device_num, nullptr);
  457. if (rt_ret != RT_ERROR_NONE) {
  458. GELOGE(FAILED, "Runtime profiler start failed.");
  459. return FAILED;
  460. }
  461. Status hccl_ret = OpsKernelBuilderManager::Instance().ProfStart(model_load_mask);
  462. if (hccl_ret != SUCCESS) {
  463. GELOGE(FAILED, "Hccl profiler start failed.");
  464. return FAILED;
  465. }
  466. is_load_profiling_ = true;
  467. GELOGI("Prof init: model load profiling on.");
  468. }
  469. uint64_t training_trace_mask = module & PROF_TRAINING_TRACE_MASK;
  470. if (training_trace_mask == PROF_TRAINING_TRACE_MASK) {
  471. is_training_trace_ = true;
  472. }
  473. GELOGI("Prof init success.");
  474. #endif
  475. return SUCCESS;
  476. }
  477. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfFinalize() {
  478. #ifdef DAVINCI_SUPPORT_PROFILING
  479. std::lock_guard<std::mutex> lock(mutex_);
  480. is_load_profiling_ = false;
  481. is_training_trace_ = false;
  482. is_execute_profiling_ = false;
  483. // profiling plugin uninit
  484. PluginUnInit();
  485. int32_t dev_num = -1;
  486. rtError_t rt_ret = rtProfilerStop(PROF_MODEL_LOAD_MASK, dev_num, nullptr);
  487. if (rt_ret != RT_ERROR_NONE) {
  488. GELOGE(FAILED, "Runtime profiler stop failed.");
  489. return FAILED;
  490. }
  491. Status hccl_ret = OpsKernelBuilderManager::Instance().ProfStop(PROF_MODEL_LOAD_MASK);
  492. if (hccl_ret != SUCCESS) {
  493. GELOGE(FAILED, "Hccl profiler stop failed.");
  494. return FAILED;
  495. }
  496. for (auto device_id_module : device_id_module_map_) {
  497. if (device_id_module.second != 0) {
  498. uint32_t device_id = static_cast<uint32_t>(device_id_module.first);
  499. GELOGI("Prof finalize: device_id: %u, module: 0x%llx.", device_id, device_id_module.second);
  500. rt_ret = rtProfilerStop(device_id_module.second, 1, &device_id);
  501. if (rt_ret != RT_ERROR_NONE) {
  502. GELOGE(FAILED, "Runtime profiler stop failed.");
  503. return FAILED;
  504. }
  505. }
  506. }
  507. device_id_module_map_.clear();
  508. device_id_.clear();
  509. GELOGI("Prof finalize success.");
  510. #endif
  511. return SUCCESS;
  512. }
  513. Status ProfilingManager::ProfParseDeviceId(const std::map<std::string, std::string> &config_para,
  514. vector<int32_t> &device_list) {
  515. #ifdef DAVINCI_SUPPORT_PROFILING
  516. auto iter = config_para.find(kConfigDevIdList);
  517. if (iter != config_para.end()) {
  518. std::string device_id_list = iter->second;
  519. std::string temp;
  520. vector<std::string> decvice_id;
  521. for (uint32_t i = 0; i < device_id_list.size(); i++) {
  522. if (isdigit(device_id_list[i])) {
  523. temp.append(1, device_id_list[i]);
  524. } else {
  525. if (!temp.empty()) {
  526. decvice_id.emplace_back(temp);
  527. }
  528. temp.clear();
  529. }
  530. }
  531. if (!temp.empty()) {
  532. decvice_id.emplace_back(temp);
  533. }
  534. for (uint32_t i = 0; i < decvice_id.size(); i++) {
  535. try {
  536. int32_t dev_id = std::stoi(decvice_id[i]);
  537. device_list.push_back(dev_id);
  538. } catch (std::invalid_argument &) {
  539. GELOGE(FAILED, "Device id: %s is invalid.", decvice_id[i].c_str());
  540. return FAILED;
  541. } catch (std::out_of_range &) {
  542. GELOGE(FAILED, "Device id: %s is out of range.", decvice_id[i].c_str());
  543. return FAILED;
  544. } catch (...) {
  545. GELOGE(FAILED, "Device id: %s cannot change to int.", decvice_id[i].c_str());
  546. return FAILED;
  547. }
  548. }
  549. } else {
  550. GELOGE(FAILED, "Config para not contain device id list.");
  551. return FAILED;
  552. }
  553. #endif
  554. return SUCCESS;
  555. }
  556. Status ProfilingManager::ProfParseParam(const std::map<std::string, std::string> &config_para,
  557. int32_t &device_num, vector<int32_t> &device_list) {
  558. #ifdef DAVINCI_SUPPORT_PROFILING
  559. // device num
  560. auto iter = config_para.find(kConfigNumsdev);
  561. if (iter != config_para.end()) {
  562. try {
  563. device_num = std::stoi(iter->second);
  564. } catch (std::invalid_argument &) {
  565. GELOGE(FAILED, "Device nun: %s is invalid.", iter->second.c_str());
  566. return FAILED;
  567. } catch (std::out_of_range &) {
  568. GELOGE(FAILED, "Device num: %s is out of range.", iter->second.c_str());
  569. return FAILED;
  570. } catch (...) {
  571. GELOGE(FAILED, "Device num: %s cannot change to int.", iter->second.c_str());
  572. return FAILED;
  573. }
  574. } else {
  575. GELOGE(FAILED, "Config para not contain device num.");
  576. return FAILED;
  577. }
  578. // device id
  579. if (ProfParseDeviceId(config_para, device_list) != SUCCESS) {
  580. GELOGE(FAILED, "Parse config para device id failed.");
  581. return FAILED;
  582. }
  583. if (device_num == 0 || device_num > kMaxDeviceNum || device_num != static_cast<int32_t>(device_list.size())) {
  584. GELOGE(FAILED, "Config para device num: %d not equal to device list size: %d.", device_num, device_list.size());
  585. return FAILED;
  586. }
  587. #endif
  588. return SUCCESS;
  589. }
  590. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfStartProfiling(
  591. uint64_t module, const std::map<std::string, std::string> &config_para) {
  592. #ifdef DAVINCI_SUPPORT_PROFILING
  593. std::lock_guard<std::mutex> lock(mutex_);
  594. int32_t device_num = 0;
  595. vector<int32_t> device_list;
  596. if (ProfParseParam(config_para, device_num, device_list) != SUCCESS) {
  597. GELOGE(FAILED, "Prof start parse param failed.");
  598. return FAILED;
  599. }
  600. auto device_id_ptr = std::unique_ptr<uint32_t[]>(new (std::nothrow) uint32_t[device_num]);
  601. if (device_id_ptr == nullptr) {
  602. GELOGE(FAILED, "Prof start: device id ptr is null.");
  603. return FAILED;
  604. }
  605. for (int32_t i = 0; i < device_num; i++) {
  606. device_id_ptr[i] = static_cast<uint32_t>(device_list[i]);
  607. }
  608. GELOGI("Runtime config param: 0x%llx, device num: %d.", module, device_num);
  609. rtError_t rt_ret = rtProfilerStart(module, device_num, device_id_ptr.get());
  610. if (rt_ret != RT_ERROR_NONE) {
  611. GELOGE(FAILED, "Runtime profiler config proc failed.");
  612. return FAILED;
  613. }
  614. Status hccl_ret = OpsKernelBuilderManager::Instance().ProfStart(module);
  615. if (hccl_ret != SUCCESS) {
  616. GELOGE(FAILED, "Hccl profiler start failed.");
  617. return FAILED;
  618. }
  619. if ((module & PROF_MODEL_EXECUTE_MASK) == PROF_MODEL_EXECUTE_MASK) {
  620. for (int32_t i = 0; i < device_num; i++) {
  621. if (std::find(device_id_.begin(), device_id_.end(), device_list[i]) == device_id_.end()) {
  622. device_id_.push_back(device_list[i]);
  623. }
  624. }
  625. GELOGI("Prof start: ge execute model start profiling.");
  626. }
  627. if ((module & PROF_MODEL_LOAD_MASK) == PROF_MODEL_LOAD_MASK) {
  628. GELOGW("Prof start: load model module is invalid.");
  629. }
  630. UpdateDeviceIdModuleMap(kProfStart, module, device_list);
  631. GELOGI("Prof start profiling success.");
  632. #endif
  633. return SUCCESS;
  634. }
  635. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfStopProfiling(uint64_t module,
  636. const std::map<std::string, std::string> &config_para) {
  637. #ifdef DAVINCI_SUPPORT_PROFILING
  638. std::lock_guard<std::mutex> lock(mutex_);
  639. int32_t device_num = 0;
  640. vector<int32_t> device_list;
  641. if (ProfParseParam(config_para, device_num, device_list) != SUCCESS) {
  642. GELOGE(FAILED, "Prof stop parse param failed.");
  643. return FAILED;
  644. }
  645. auto device_id_ptr = std::unique_ptr<uint32_t[]>(new (std::nothrow) uint32_t[device_num]);
  646. if (device_id_ptr == nullptr) {
  647. GELOGE(FAILED, "Prof stop: device id ptr is null.");
  648. return FAILED;
  649. }
  650. for (int32_t i = 0; i < device_num; i++) {
  651. device_id_ptr[i] = static_cast<uint32_t>(device_list[i]);
  652. }
  653. GELOGI("Prof stop: runtime config param: 0x%llx, device num: %d", module, device_num);
  654. rtError_t rt_ret = rtProfilerStop(module, device_num, device_id_ptr.get());
  655. if (rt_ret != RT_ERROR_NONE) {
  656. GELOGE(FAILED, "Prof stop: runtime profiler config proc failed.");
  657. return FAILED;
  658. }
  659. Status hccl_ret = OpsKernelBuilderManager::Instance().ProfStop(module);
  660. if (hccl_ret != SUCCESS) {
  661. GELOGE(FAILED, "Hccl profiler stop failed.");
  662. return FAILED;
  663. }
  664. uint64_t execute_model_mask = module & PROF_MODEL_EXECUTE_MASK;
  665. if (execute_model_mask == PROF_MODEL_EXECUTE_MASK) {
  666. for (int32_t i = 0; i < device_num; i++) {
  667. auto iter = std::find(device_id_.begin(), device_id_.end(), device_list[i]);
  668. if (iter != device_id_.end()) {
  669. device_id_.erase(iter);
  670. }
  671. }
  672. GELOGI("Prof stop: ge execute model stop profiling.");
  673. }
  674. if ((module & PROF_MODEL_LOAD_MASK) == PROF_MODEL_LOAD_MASK) {
  675. GELOGW("Prof stop: load model module is invalid.");
  676. }
  677. UpdateDeviceIdModuleMap(kProfStop, module, device_list);
  678. GELOGI("Prof stop profiling success.");
  679. #endif
  680. return SUCCESS;
  681. }
  682. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::UpdateDeviceIdModuleMap(string prof_type,
  683. uint64_t module, const vector<int32_t> &device_list) {
  684. #ifdef DAVINCI_SUPPORT_PROFILING
  685. if (prof_type == kProfStart) {
  686. for (uint32_t i = 0; i < device_list.size(); i++) {
  687. auto iter = device_id_module_map_.find(device_list[i]);
  688. if (iter != device_id_module_map_.end()) {
  689. uint64_t prof_on_module = device_id_module_map_[device_list[i]];
  690. // save all profiling on module of device
  691. device_id_module_map_[device_list[i]] = prof_on_module | module;
  692. } else {
  693. device_id_module_map_[device_list[i]] = module;
  694. }
  695. }
  696. } else if (prof_type == kProfStop) {
  697. for (uint32_t i = 0; i < device_list.size(); i++) {
  698. auto iter = device_id_module_map_.find(device_list[i]);
  699. if (iter != device_id_module_map_.end()) {
  700. uint64_t prof_on_module = device_id_module_map_[device_list[i]];
  701. uint64_t prof_off_module = prof_on_module & module;
  702. uint64_t prof_on_left_module = prof_on_module & (~prof_off_module);
  703. // stop profiling on module of device
  704. device_id_module_map_[device_list[i]] = prof_on_left_module;
  705. }
  706. }
  707. } else {
  708. GELOGI("No need to update device_id module map.");
  709. }
  710. #endif
  711. }
  712. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY bool ProfilingManager::ProfilingModelExecuteOn() const {
  713. int32_t logic_device_id = 0;
  714. rtError_t rt_ret = rtGetDevice(&logic_device_id);
  715. if (rt_ret != RT_ERROR_NONE) {
  716. GELOGE(rt_ret, "Runtime get logic_device_id failed, current logic_device_id:%d", logic_device_id);
  717. }
  718. GELOGI("Current logic_device_id:%d", logic_device_id);
  719. bool execute_model_prof_on = false;
  720. auto iter = std::find(device_id_.begin(), device_id_.end(), logic_device_id);
  721. if (iter != device_id_.end()) {
  722. execute_model_prof_on = true;
  723. }
  724. GELOGI("Flag is_execute_profiling: %d, execute_model_prof_on: %d", is_execute_profiling_, execute_model_prof_on);
  725. return execute_model_prof_on;
  726. }
  727. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::PluginInit() const {
  728. if (prof_cb_.msprofReporterCallback == nullptr) {
  729. GELOGE(ge::PARAM_INVALID, "MsprofReporterCallback callback is nullptr.");
  730. return ge::PARAM_INVALID;
  731. }
  732. return prof_cb_.msprofReporterCallback(
  733. static_cast<uint32_t>(MsprofReporterModuleId::MSPROF_MODULE_FRAMEWORK),
  734. static_cast<uint32_t>(MsprofReporterCallbackType::MSPROF_REPORTER_INIT),
  735. nullptr, 0);
  736. }
  737. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::PluginUnInit() const {
  738. #ifdef DAVINCI_SUPPORT_PROFILING
  739. if (prof_cb_.msprofReporterCallback == nullptr) {
  740. GELOGE(ge::PARAM_INVALID, "MsprofReporterCallback callback is nullptr.");
  741. return;
  742. }
  743. int32_t cb_ret = prof_cb_.msprofReporterCallback(
  744. static_cast<uint32_t>(MsprofReporterModuleId::MSPROF_MODULE_FRAMEWORK),
  745. static_cast<uint32_t>(MsprofReporterCallbackType::MSPROF_REPORTER_UNINIT),
  746. nullptr, 0);
  747. if (cb_ret != 0) {
  748. GELOGW("profiling plugin uninit failed, ret:%d", cb_ret);
  749. }
  750. #endif
  751. }
  752. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::CallMsprofReport(
  753. ReporterData &reporter_data) const {
  754. if (prof_cb_.msprofReporterCallback == nullptr) {
  755. GELOGE(ge::PARAM_INVALID, "MsprofReporterCallback callback is nullptr.");
  756. return ge::PARAM_INVALID;
  757. }
  758. return prof_cb_.msprofReporterCallback(
  759. static_cast<uint32_t>(MsprofReporterModuleId::MSPROF_MODULE_FRAMEWORK),
  760. static_cast<uint32_t>(MsprofReporterCallbackType::MSPROF_REPORTER_REPORT),
  761. static_cast<void *>(&reporter_data), sizeof(ReporterData));
  762. }
  763. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::GetFpBpPoint(
  764. std::string &fp_point, std::string &bp_point) {
  765. // Env or options mode, fp_point_/bp_point_ have initiliazed on profiling init
  766. if (!fp_point_.empty() && !bp_point_.empty()) {
  767. fp_point = fp_point_;
  768. bp_point = bp_point_;
  769. GELOGI("Bp Fp have been initialized in env or options. bp_point: %s, fp_point: %s", bp_point.c_str(), fp_point.c_str());
  770. return;
  771. }
  772. // ProfApi mode and training trace is set
  773. try {
  774. char env_profiling_options[MSPROF_OPTIONS_DEF_LEN_MAX] = { 0x00 };
  775. INT32 ret = mmGetEnv("PROFILING_OPTIONS", env_profiling_options, MSPROF_OPTIONS_DEF_LEN_MAX);
  776. if (ret != EN_OK) {
  777. GELOGI("PROFILING_OPTIONS env is not exist.");
  778. return;
  779. }
  780. GELOGI("Parse env PROFILING_OPTIONS:%s.", env_profiling_options);
  781. Json prof_options = Json::parse(env_profiling_options);
  782. fp_point_ = prof_options[kFpPoint];
  783. bp_point_ = prof_options[kBpPoint];
  784. fp_point = fp_point_;
  785. bp_point = bp_point_;
  786. if (!fp_point_.empty() && !bp_point_.empty()) {
  787. GELOGI("Training trace bp fp is set, bp_point:%s, fp_point:%s.", bp_point_.c_str(), fp_point_.c_str());
  788. }
  789. } catch (...) {
  790. GELOGE(FAILED, "Json prof options is invalid.");
  791. return;
  792. }
  793. return;
  794. }
  795. } // namespace ge

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