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

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

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