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

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

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