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 34 kB

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

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