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
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
4 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
4 years ago
5 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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 index = data.size() / kReportMaxLen;
  273. if (index >= 1) {
  274. reporter_data.deviceId = device_id;
  275. ret = memcpy_s(reporter_data.tag, MSPROF_ENGINE_MAX_TAG_LEN + 1, tag_name.c_str(), tag_name.size());
  276. GE_IF_BOOL_EXEC(ret != EOK, GELOGE(ret, "Report data tag [%s] memcpy error!", tag_name.c_str()); return;);
  277. for (size_t i = 0; i < index; ++i) {
  278. reporter_data.data = (unsigned char *)data.c_str() + kReportMaxLen * i;
  279. reporter_data.dataLen = kReportMaxLen;
  280. cb_ret = CallMsprofReport(reporter_data);
  281. GE_IF_BOOL_EXEC(cb_ret != 0, GELOGE(cb_ret, "Reporter data [%s] failed, ret:%d", tag_name.c_str(), cb_ret);
  282. return;);
  283. }
  284. reporter_data.dataLen = data.size() - kReportMaxLen * index;
  285. if (reporter_data.dataLen != 0) {
  286. reporter_data.data = (unsigned char *)data.c_str() + kReportMaxLen * index;
  287. cb_ret = CallMsprofReport(reporter_data);
  288. GE_IF_BOOL_EXEC(cb_ret != 0, GELOGE(cb_ret, "Reporter data [%s] failed, ret:%d", tag_name.c_str(), cb_ret);
  289. return;);
  290. }
  291. } else {
  292. reporter_data.deviceId = device_id;
  293. reporter_data.data = (unsigned char *)data.c_str();
  294. reporter_data.dataLen = data.size();
  295. ret = memcpy_s(reporter_data.tag, MSPROF_ENGINE_MAX_TAG_LEN + 1, tag_name.c_str(), tag_name.size());
  296. GE_IF_BOOL_EXEC(ret != EOK, GELOGE(ret, "Report data tag [%s] memcpy error!", tag_name.c_str()); return;);
  297. cb_ret = CallMsprofReport(reporter_data);
  298. GE_IF_BOOL_EXEC(cb_ret != 0, GELOGE(cb_ret, "Reporter data [%s] failed, ret:%d", tag_name.c_str(), cb_ret);
  299. return;);
  300. }
  301. #endif
  302. }
  303. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::ReportProfilingData(
  304. uint32_t model_id, const std::vector<TaskDescInfo> &task_desc_info) {
  305. #ifdef DAVINCI_SUPPORT_PROFILING
  306. int32_t logic_device_id = 0;
  307. rtError_t rt_ret = rtGetDevice(&logic_device_id);
  308. if (rt_ret != RT_ERROR_NONE) {
  309. GELOGE(rt_ret, "runtime get logic_device_id failed, current logic_device_id:%d", logic_device_id);
  310. return;
  311. }
  312. GELOGD("current logic_device_id:%d", logic_device_id);
  313. GELOGD("start ProfilingTaskDescInfo.");
  314. ProfilingTaskDescInfo(model_id, task_desc_info, logic_device_id);
  315. GELOGD("Report profiling data for GE end.");
  316. #endif
  317. }
  318. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY uint64_t ProfilingManager::GetProfilingModule() {
  319. uint64_t module = PROF_MODEL_EXECUTE_MASK |
  320. PROF_RUNTIME_API_MASK |
  321. PROF_RUNTIME_TRACE_MASK |
  322. PROF_SCHEDULE_TIMELINE_MASK |
  323. PROF_SCHEDULE_TRACE_MASK |
  324. PROF_TASK_TIME_MASK |
  325. PROF_SUBTASK_TIME_MASK |
  326. PROF_AICPU_TRACE_MASK |
  327. PROF_AICORE_METRICS_MASK |
  328. PROF_AIVECTORCORE_METRICS_MASK |
  329. PROF_MODEL_LOAD_MASK;
  330. return module;
  331. }
  332. void ProfilingManager::UpdateSubscribeDeviceModuleMap(std::string prof_type, uint32_t device_id, uint64_t module) {
  333. #ifdef DAVINCI_SUPPORT_PROFILING
  334. if (prof_type == kProfModelSubscribe) {
  335. if (subs_dev_module_.find(device_id) != subs_dev_module_.end()) {
  336. subs_dev_module_[device_id].subscribe_count++;
  337. } else {
  338. DeviceSubsInfo dev_info;
  339. dev_info.module = module;
  340. dev_info.subscribe_count = 1;
  341. subs_dev_module_[device_id] = dev_info;
  342. }
  343. } else if (prof_type == kProfModelUnsubscribe) {
  344. auto iter = subs_dev_module_.find(device_id);
  345. if (iter != subs_dev_module_.end()) {
  346. if (iter->second.subscribe_count > 0) {
  347. iter->second.subscribe_count--;
  348. }
  349. if (iter->second.subscribe_count == 0) {
  350. subs_dev_module_.erase(iter);
  351. }
  352. }
  353. } else {
  354. GELOGI("No need to update device_id module map.");
  355. }
  356. #endif
  357. }
  358. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfModelSubscribe(
  359. uint64_t module, void *model) {
  360. #ifdef DAVINCI_SUPPORT_PROFILING
  361. std::lock_guard<std::mutex> lock(mutex_);
  362. uint64_t model_load_mask = module & PROF_MODEL_LOAD_MASK;
  363. if ((subscribe_count_ == 0) && (model_load_mask == PROF_MODEL_LOAD_MASK)) {
  364. // register framework to profiling
  365. // register Framework to profiling
  366. int32_t cb_ret = PluginInit();
  367. if (cb_ret != 0) {
  368. GELOGE(cb_ret, "profiling plugin init failed, ret:%d", cb_ret);
  369. return cb_ret;
  370. }
  371. GELOGI("Prof subscribe: model load profiling on.");
  372. }
  373. subscribe_count_++;
  374. auto davinci_model = static_cast<DavinciModel *>(model);
  375. int32_t device_num = 1;
  376. uint32_t device[1];
  377. device[0] = davinci_model->GetDeviceId();
  378. rtError_t rt_ret = rtProfilerStart(module, device_num, device);
  379. if (rt_ret != RT_ERROR_NONE) {
  380. GELOGE(FAILED, "Runtime profiler start failed.");
  381. return FAILED;
  382. }
  383. UpdateSubscribeDeviceModuleMap(kProfModelSubscribe, device[0], module);
  384. // Report profiling data
  385. Status p_ret = davinci_model->ReportProfilingData();
  386. if (p_ret != SUCCESS) {
  387. GELOGE(p_ret, "Report profiling data failed.");
  388. return p_ret;
  389. }
  390. #endif
  391. return SUCCESS;
  392. }
  393. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfModelUnsubscribe(
  394. void *model) {
  395. #ifdef DAVINCI_SUPPORT_PROFILING
  396. std::lock_guard<std::mutex> lock(mutex_);
  397. if (subscribe_count_ == 0) {
  398. GELOGW("The profiler has not been subscribed, you do not need to cannel the subscription.");
  399. return SUCCESS;
  400. }
  401. auto davinci_model = static_cast<DavinciModel *>(model);
  402. int32_t dev_num = 1;
  403. uint32_t device[1];
  404. device[0] = davinci_model->GetDeviceId();
  405. auto iter = subs_dev_module_.find(device[0]);
  406. if (iter != subs_dev_module_.end()) {
  407. if (subs_dev_module_[device[0]].subscribe_count == 1) {
  408. // The same device_id, only stop at last time
  409. rtError_t rt_ret = rtProfilerStop(subs_dev_module_[device[0]].module, dev_num, device);
  410. if (rt_ret != RT_ERROR_NONE) {
  411. GELOGE(FAILED, "Runtime profiler stop failed.");
  412. return FAILED;
  413. }
  414. }
  415. UpdateSubscribeDeviceModuleMap(kProfModelUnsubscribe, device[0], subs_dev_module_[device[0]].module);
  416. } else {
  417. GELOGE(FAILED, "The device_id:%u has not been subscribed, do not need to cancel.", device[0]);
  418. return FAILED;
  419. }
  420. subscribe_count_--;
  421. if (subscribe_count_ == 0) {
  422. // profiling plugin uninit at last subscription
  423. PluginUnInit();
  424. }
  425. #endif
  426. return SUCCESS;
  427. }
  428. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfInit(uint64_t module) {
  429. #ifdef DAVINCI_SUPPORT_PROFILING
  430. std::lock_guard<std::mutex> lock(mutex_);
  431. uint64_t model_load_mask = module & PROF_MODEL_LOAD_MASK;
  432. if (model_load_mask == PROF_MODEL_LOAD_MASK) {
  433. // register Framework to profiling
  434. int32_t cb_ret = PluginInit();
  435. if (cb_ret != 0) {
  436. GELOGE(cb_ret, "profiling plugin init failed, ret:%d", cb_ret);
  437. return cb_ret;
  438. }
  439. int32_t device_num = -1;
  440. rtError_t rt_ret = rtProfilerStart(model_load_mask, device_num, nullptr);
  441. if (rt_ret != RT_ERROR_NONE) {
  442. GELOGE(FAILED, "Runtime profiler start failed.");
  443. return FAILED;
  444. }
  445. is_load_profiling_ = true;
  446. GELOGI("Prof init: model load profiling on.");
  447. }
  448. uint64_t training_trace_mask = module & PROF_TRAINING_TRACE_MASK;
  449. if (training_trace_mask == PROF_TRAINING_TRACE_MASK) {
  450. is_training_trace_ = true;
  451. }
  452. GELOGI("Prof init success.");
  453. #endif
  454. return SUCCESS;
  455. }
  456. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfFinalize() {
  457. #ifdef DAVINCI_SUPPORT_PROFILING
  458. std::lock_guard<std::mutex> lock(mutex_);
  459. is_load_profiling_ = false;
  460. is_training_trace_ = false;
  461. is_execute_profiling_ = false;
  462. // profiling plugin uninit
  463. PluginUnInit();
  464. int32_t dev_num = -1;
  465. rtError_t rt_ret = rtProfilerStop(PROF_MODEL_LOAD_MASK, dev_num, nullptr);
  466. if (rt_ret != RT_ERROR_NONE) {
  467. GELOGE(FAILED, "Runtime profiler stop failed.");
  468. return FAILED;
  469. }
  470. for (auto device_id_module : device_id_module_map_) {
  471. if (device_id_module.second != 0) {
  472. uint32_t device_id = static_cast<uint32_t>(device_id_module.first);
  473. GELOGI("Prof finalize: device_id: %u, module: 0x%lx.", device_id, device_id_module.second);
  474. rt_ret = rtProfilerStop(device_id_module.second, 1, &device_id);
  475. if (rt_ret != RT_ERROR_NONE) {
  476. GELOGE(FAILED, "Runtime profiler stop failed.");
  477. return FAILED;
  478. }
  479. }
  480. }
  481. device_id_module_map_.clear();
  482. device_id_.clear();
  483. GELOGI("Prof finalize success.");
  484. #endif
  485. return SUCCESS;
  486. }
  487. Status ProfilingManager::ProfParseDeviceId(const std::map<std::string, std::string> &config_para,
  488. vector<int32_t> &device_list) {
  489. #ifdef DAVINCI_SUPPORT_PROFILING
  490. auto iter = config_para.find(kConfigDevIdList);
  491. if (iter != config_para.end()) {
  492. std::string device_id_list = iter->second;
  493. std::string temp;
  494. vector<std::string> decvice_id;
  495. for (uint32_t i = 0; i < device_id_list.size(); i++) {
  496. if (isdigit(device_id_list[i])) {
  497. temp.append(1, device_id_list[i]);
  498. } else {
  499. if (!temp.empty()) {
  500. decvice_id.emplace_back(temp);
  501. }
  502. temp.clear();
  503. }
  504. }
  505. if (!temp.empty()) {
  506. decvice_id.emplace_back(temp);
  507. }
  508. for (uint32_t i = 0; i < decvice_id.size(); i++) {
  509. try {
  510. int32_t dev_id = std::stoi(decvice_id[i]);
  511. device_list.push_back(dev_id);
  512. } catch (std::invalid_argument &) {
  513. GELOGE(FAILED, "Device id: %s is invalid.", decvice_id[i].c_str());
  514. return FAILED;
  515. } catch (std::out_of_range &) {
  516. GELOGE(FAILED, "Device id: %s is out of range.", decvice_id[i].c_str());
  517. return FAILED;
  518. } catch (...) {
  519. GELOGE(FAILED, "Device id: %s cannot change to int.", decvice_id[i].c_str());
  520. return FAILED;
  521. }
  522. }
  523. } else {
  524. GELOGE(FAILED, "Config para not contain device id list.");
  525. return FAILED;
  526. }
  527. #endif
  528. return SUCCESS;
  529. }
  530. Status ProfilingManager::ProfParseParam(const std::map<std::string, std::string> &config_para,
  531. int32_t &device_num, vector<int32_t> &device_list) {
  532. #ifdef DAVINCI_SUPPORT_PROFILING
  533. // device num
  534. auto iter = config_para.find(kConfigNumsdev);
  535. if (iter != config_para.end()) {
  536. try {
  537. device_num = std::stoi(iter->second);
  538. } catch (std::invalid_argument &) {
  539. GELOGE(FAILED, "Device nun: %s is invalid.", iter->second.c_str());
  540. return FAILED;
  541. } catch (std::out_of_range &) {
  542. GELOGE(FAILED, "Device num: %s is out of range.", iter->second.c_str());
  543. return FAILED;
  544. } catch (...) {
  545. GELOGE(FAILED, "Device num: %s cannot change to int.", iter->second.c_str());
  546. return FAILED;
  547. }
  548. } else {
  549. GELOGE(FAILED, "Config para not contain device num.");
  550. return FAILED;
  551. }
  552. // device id
  553. if (ProfParseDeviceId(config_para, device_list) != SUCCESS) {
  554. GELOGE(FAILED, "Parse config para device id failed.");
  555. return FAILED;
  556. }
  557. if (device_num == 0 || device_num > kMaxDeviceNum || device_num != static_cast<int32_t>(device_list.size())) {
  558. GELOGE(FAILED, "Config para device num: %d not equal to device list size: %zu.", device_num, device_list.size());
  559. return FAILED;
  560. }
  561. #endif
  562. return SUCCESS;
  563. }
  564. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfStartProfiling(
  565. uint64_t module, const std::map<std::string, std::string> &config_para) {
  566. #ifdef DAVINCI_SUPPORT_PROFILING
  567. std::lock_guard<std::mutex> lock(mutex_);
  568. uint64_t training_trace_mask = module & PROF_TRAINING_TRACE_MASK;
  569. if (training_trace_mask == PROF_TRAINING_TRACE_MASK) {
  570. is_training_trace_ = true;
  571. }
  572. int32_t device_num = 0;
  573. vector<int32_t> device_list;
  574. if (ProfParseParam(config_para, device_num, device_list) != SUCCESS) {
  575. GELOGE(FAILED, "Prof start parse param failed.");
  576. return FAILED;
  577. }
  578. auto device_id_ptr = std::unique_ptr<uint32_t[]>(new (std::nothrow) uint32_t[device_num]);
  579. if (device_id_ptr == nullptr) {
  580. GELOGE(FAILED, "Prof start: device id ptr is null.");
  581. return FAILED;
  582. }
  583. for (int32_t i = 0; i < device_num; i++) {
  584. device_id_ptr[i] = static_cast<uint32_t>(device_list[i]);
  585. }
  586. GELOGI("Runtime config param: 0x%lx, device num: %d.", module, device_num);
  587. rtError_t rt_ret = rtProfilerStart(module, device_num, device_id_ptr.get());
  588. if (rt_ret != RT_ERROR_NONE) {
  589. GELOGE(FAILED, "Runtime profiler config proc failed.");
  590. return FAILED;
  591. }
  592. if ((module & PROF_MODEL_EXECUTE_MASK) == PROF_MODEL_EXECUTE_MASK) {
  593. for (int32_t i = 0; i < device_num; i++) {
  594. if (std::find(device_id_.begin(), device_id_.end(), device_list[i]) == device_id_.end()) {
  595. device_id_.push_back(device_list[i]);
  596. }
  597. }
  598. GELOGI("Prof start: ge execute model start profiling.");
  599. }
  600. if ((module & PROF_MODEL_LOAD_MASK) == PROF_MODEL_LOAD_MASK) {
  601. GELOGW("Prof start: load model module is invalid.");
  602. }
  603. UpdateDeviceIdModuleMap(kProfStart, module, device_list);
  604. GELOGI("Prof start profiling success.");
  605. #endif
  606. return SUCCESS;
  607. }
  608. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::ProfStopProfiling(uint64_t module,
  609. const std::map<std::string, std::string> &config_para) {
  610. #ifdef DAVINCI_SUPPORT_PROFILING
  611. std::lock_guard<std::mutex> lock(mutex_);
  612. int32_t device_num = 0;
  613. vector<int32_t> device_list;
  614. if (ProfParseParam(config_para, device_num, device_list) != SUCCESS) {
  615. GELOGE(FAILED, "Prof stop parse param failed.");
  616. return FAILED;
  617. }
  618. auto device_id_ptr = std::unique_ptr<uint32_t[]>(new (std::nothrow) uint32_t[device_num]);
  619. if (device_id_ptr == nullptr) {
  620. GELOGE(FAILED, "Prof stop: device id ptr is null.");
  621. return FAILED;
  622. }
  623. for (int32_t i = 0; i < device_num; i++) {
  624. device_id_ptr[i] = static_cast<uint32_t>(device_list[i]);
  625. }
  626. GELOGI("Prof stop: runtime config param: 0x%lx, device num: %d", module, device_num);
  627. rtError_t rt_ret = rtProfilerStop(module, device_num, device_id_ptr.get());
  628. if (rt_ret != RT_ERROR_NONE) {
  629. GELOGE(FAILED, "Prof stop: runtime profiler config proc failed.");
  630. return FAILED;
  631. }
  632. uint64_t execute_model_mask = module & PROF_MODEL_EXECUTE_MASK;
  633. if (execute_model_mask == PROF_MODEL_EXECUTE_MASK) {
  634. for (int32_t i = 0; i < device_num; i++) {
  635. auto iter = std::find(device_id_.begin(), device_id_.end(), device_list[i]);
  636. if (iter != device_id_.end()) {
  637. device_id_.erase(iter);
  638. }
  639. }
  640. GELOGI("Prof stop: ge execute model stop profiling.");
  641. }
  642. if ((module & PROF_MODEL_LOAD_MASK) == PROF_MODEL_LOAD_MASK) {
  643. GELOGW("Prof stop: load model module is invalid.");
  644. }
  645. UpdateDeviceIdModuleMap(kProfStop, module, device_list);
  646. GELOGI("Prof stop profiling success.");
  647. #endif
  648. return SUCCESS;
  649. }
  650. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::UpdateDeviceIdModuleMap(string prof_type,
  651. uint64_t module, const vector<int32_t> &device_list) {
  652. #ifdef DAVINCI_SUPPORT_PROFILING
  653. if (prof_type == kProfStart) {
  654. for (uint32_t i = 0; i < device_list.size(); i++) {
  655. auto iter = device_id_module_map_.find(device_list[i]);
  656. if (iter != device_id_module_map_.end()) {
  657. uint64_t prof_on_module = device_id_module_map_[device_list[i]];
  658. // save all profiling on module of device
  659. device_id_module_map_[device_list[i]] = prof_on_module | module;
  660. } else {
  661. device_id_module_map_[device_list[i]] = module;
  662. }
  663. }
  664. } else if (prof_type == kProfStop) {
  665. for (uint32_t i = 0; i < device_list.size(); i++) {
  666. auto iter = device_id_module_map_.find(device_list[i]);
  667. if (iter != device_id_module_map_.end()) {
  668. uint64_t prof_on_module = device_id_module_map_[device_list[i]];
  669. uint64_t prof_off_module = prof_on_module & module;
  670. uint64_t prof_on_left_module = prof_on_module & (~prof_off_module);
  671. // stop profiling on module of device
  672. device_id_module_map_[device_list[i]] = prof_on_left_module;
  673. }
  674. }
  675. } else {
  676. GELOGI("No need to update device_id module map.");
  677. }
  678. #endif
  679. }
  680. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY bool ProfilingManager::ProfilingModelExecuteOn() const {
  681. int32_t logic_device_id = 0;
  682. rtError_t rt_ret = rtGetDevice(&logic_device_id);
  683. if (rt_ret != RT_ERROR_NONE) {
  684. GELOGE(rt_ret, "Runtime get logic_device_id failed, current logic_device_id:%d", logic_device_id);
  685. }
  686. GELOGI("Current logic_device_id:%d", logic_device_id);
  687. bool execute_model_prof_on = false;
  688. auto iter = std::find(device_id_.begin(), device_id_.end(), logic_device_id);
  689. if (iter != device_id_.end()) {
  690. execute_model_prof_on = true;
  691. }
  692. GELOGI("Flag is_execute_profiling: %d, execute_model_prof_on: %d", is_execute_profiling_, execute_model_prof_on);
  693. return execute_model_prof_on;
  694. }
  695. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::PluginInit() const {
  696. if (prof_cb_.msprofReporterCallback == nullptr) {
  697. GELOGE(ge::PARAM_INVALID, "MsprofReporterCallback callback is nullptr.");
  698. return ge::PARAM_INVALID;
  699. }
  700. return prof_cb_.msprofReporterCallback(
  701. static_cast<uint32_t>(MsprofReporterModuleId::MSPROF_MODULE_FRAMEWORK),
  702. static_cast<uint32_t>(MsprofReporterCallbackType::MSPROF_REPORTER_INIT),
  703. nullptr, 0);
  704. }
  705. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::PluginUnInit() const {
  706. #ifdef DAVINCI_SUPPORT_PROFILING
  707. if (prof_cb_.msprofReporterCallback == nullptr) {
  708. GELOGE(ge::PARAM_INVALID, "MsprofReporterCallback callback is nullptr.");
  709. return;
  710. }
  711. int32_t cb_ret = prof_cb_.msprofReporterCallback(
  712. static_cast<uint32_t>(MsprofReporterModuleId::MSPROF_MODULE_FRAMEWORK),
  713. static_cast<uint32_t>(MsprofReporterCallbackType::MSPROF_REPORTER_UNINIT),
  714. nullptr, 0);
  715. if (cb_ret != 0) {
  716. GELOGW("profiling plugin uninit failed, ret:%d", cb_ret);
  717. }
  718. #endif
  719. }
  720. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::CallMsprofReport(
  721. ReporterData &reporter_data) const {
  722. if (prof_cb_.msprofReporterCallback == nullptr) {
  723. GELOGE(ge::PARAM_INVALID, "MsprofReporterCallback callback is nullptr.");
  724. return ge::PARAM_INVALID;
  725. }
  726. return prof_cb_.msprofReporterCallback(
  727. static_cast<uint32_t>(MsprofReporterModuleId::MSPROF_MODULE_FRAMEWORK),
  728. static_cast<uint32_t>(MsprofReporterCallbackType::MSPROF_REPORTER_REPORT),
  729. static_cast<void *>(&reporter_data), sizeof(ReporterData));
  730. }
  731. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::GetOpInputOutputInfo(
  732. const OpDescPtr &op, TaskDescInfo &task_desc_info) const {
  733. std::vector<Format> input_format;
  734. std::vector<std::vector<int64_t>> input_shape;
  735. std::vector<DataType> input_data_type;
  736. for (size_t i = 0; i < op->GetAllInputsSize(); ++i) {
  737. GeTensorDescPtr input_tensor_desc = op->MutableInputDesc(i);
  738. if (input_tensor_desc == nullptr) {
  739. continue;
  740. }
  741. input_format.emplace_back(input_tensor_desc->GetFormat());
  742. input_shape.emplace_back(input_tensor_desc->GetShape().GetDims());
  743. input_data_type.emplace_back(input_tensor_desc->GetDataType());
  744. }
  745. std::vector<Format> output_format;
  746. std::vector<std::vector<int64_t>> output_shape;
  747. std::vector<DataType> output_data_type;
  748. for (size_t j = 0; j < op->GetOutputsSize(); ++j) {
  749. GeTensorDescPtr output_tensor_desc = op->MutableOutputDesc(j);
  750. if (output_tensor_desc == nullptr) {
  751. continue;
  752. }
  753. output_format.emplace_back(output_tensor_desc->GetFormat());
  754. output_shape.emplace_back(output_tensor_desc->GetShape().GetDims());
  755. output_data_type.emplace_back(output_tensor_desc->GetDataType());
  756. }
  757. std::vector<Format> format_default = { FORMAT_NULL };
  758. std::vector<std::vector<int64_t>> shape_default = { {0} };
  759. std::vector<DataType> data_type_default = { DT_UNDEFINED };
  760. task_desc_info.input_format = input_format.empty() ? format_default : input_format;
  761. task_desc_info.input_shape = input_shape.empty() ? shape_default : input_shape;
  762. task_desc_info.input_data_type = input_data_type.empty() ? data_type_default : input_data_type;
  763. task_desc_info.output_format = output_format.empty() ? format_default : output_format;
  764. task_desc_info.output_shape = output_shape.empty() ? shape_default : output_shape;
  765. task_desc_info.output_data_type = output_data_type.empty() ? data_type_default : output_data_type;
  766. }
  767. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::GetFpBpPoint(
  768. std::string &fp_point, std::string &bp_point) {
  769. // Env or options mode, fp_point_/bp_point_ have initiliazed on profiling init
  770. if (!fp_point_.empty() && !bp_point_.empty()) {
  771. fp_point = fp_point_;
  772. bp_point = bp_point_;
  773. GELOGI("Bp Fp have been initialized in env or options. bp_point: %s, fp_point: %s", bp_point.c_str(),
  774. fp_point.c_str());
  775. return;
  776. }
  777. // ProfApi mode and training trace is set
  778. // Parse options first
  779. char env_profiling_options[MSPROF_OPTIONS_DEF_LEN_MAX] = { 0x00 };
  780. bool is_profiling_valid = false;
  781. std::string profiling_options;
  782. if (ge::GetContext().GetOption(OPTION_EXEC_PROFILING_OPTIONS, profiling_options) == SUCCESS &&
  783. !profiling_options.empty()) {
  784. is_profiling_valid = true;
  785. } else {
  786. INT32 ret = mmGetEnv("PROFILING_OPTIONS", env_profiling_options, MSPROF_OPTIONS_DEF_LEN_MAX);
  787. if (ret != EN_OK) {
  788. GELOGI("PROFILING_OPTIONS env is not exist.");
  789. return;
  790. }
  791. GELOGI("Parse env PROFILING_OPTIONS:%s.", env_profiling_options);
  792. profiling_options = env_profiling_options;
  793. is_profiling_valid = true;
  794. }
  795. if (is_profiling_valid) {
  796. try {
  797. Json prof_options = Json::parse(profiling_options);
  798. fp_point_ = prof_options[kFpPoint];
  799. bp_point_ = prof_options[kBpPoint];
  800. fp_point = fp_point_;
  801. bp_point = bp_point_;
  802. if (!fp_point_.empty() && !bp_point_.empty()) {
  803. GELOGI("Training trace bp fp is set, bp_point:%s, fp_point:%s.", bp_point_.c_str(), fp_point_.c_str());
  804. }
  805. } catch (...) {
  806. GELOGW("Json prof options is invalid.");
  807. return;
  808. }
  809. }
  810. return;
  811. }
  812. } // namespace ge

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