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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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. index_id_(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. if (!is_load_profiling_ && subscribe_count_ == 0) {
  306. GELOGD("Profiling is not turned on, no need to profile step info.");
  307. return SUCCESS;
  308. }
  309. GELOGD("Profiling Step Info TraceTask execute async start, index_id = %lu, model_id = %lu, tag_id = %u",
  310. index_id, model_id, tag_id);
  311. rtError_t rt_ret = rtProfilerTraceEx(index_id, model_id, tag_id, stream);
  312. if (rt_ret != RT_ERROR_NONE) {
  313. GELOGE(RT_FAILED, "[Call][rtProfilerTraceEx]Failed, ret 0x%X", rt_ret);
  314. REPORT_CALL_ERROR("E19999", "Call rtProfilerTraceEx failed, ret 0x%X", rt_ret);
  315. return RT_ERROR_TO_GE_STATUS(rt_ret);
  316. }
  317. GELOGD("Profiling Step Info TraceTask execute async success, index_id = %lu, model_id = %lu, tag_id = %u",
  318. index_id, model_id, tag_id);
  319. mmTimespec timespec = mmGetTickCount();
  320. // 1000 ^ 3 converts second to nanosecond
  321. int64_t time = timespec.tv_sec * 1000 * 1000 * 1000 + timespec.tv_nsec;
  322. uint32_t task_id = 0;
  323. uint32_t stream_id = 0;
  324. rt_ret = rtGetTaskIdAndStreamID(&task_id, &stream_id);
  325. if (rt_ret != RT_ERROR_NONE) {
  326. GELOGE(RT_FAILED, "[Get][RtsInfo]Task_id and stream_id failed, ret 0x%X", rt_ret);
  327. REPORT_CALL_ERROR("E19999", "Get task_id and stream_id failed, ret 0x%X", rt_ret);
  328. return RT_ERROR_TO_GE_STATUS(rt_ret);
  329. }
  330. GELOGD("Get profiling args, task_id[%u], stream_id[%u]", task_id, stream_id);
  331. Json step_info;
  332. step_info[kIndexId] = index_id;
  333. step_info[kModelId] = model_id;
  334. step_info[kTimeStamp] = time;
  335. step_info[kTagId] = tag_id;
  336. step_info[kTaskId] = task_id;
  337. step_info[kStreamId] = stream_id;
  338. step_info[kThreadId] = mmGetTid();
  339. std::string reported_data;
  340. try {
  341. reported_data = step_info.dump(kInteval, ' ', false, Json::error_handler_t::ignore);
  342. } catch (std::exception &e) {
  343. GELOGE(FAILED, "Failed to convert JSON to string, reason: %s.", e.what());
  344. GELOGE(FAILED, "[Convert][ReportedData]Failed to convert from json to string, reason: %s",
  345. e.what());
  346. REPORT_CALL_ERROR("E19999", "Failed to convert reported data from json to string, reason: %s",
  347. e.what());
  348. } catch (...) {
  349. GELOGE(FAILED, "[Convert][ReportedData]Failed to convert from json to string");
  350. REPORT_CALL_ERROR("E19999", "Failed to convert reported data from json to string");
  351. }
  352. reported_data.append(",")
  353. .append("\n");
  354. ReportData(device_id, reported_data, "step_info");
  355. #endif
  356. return SUCCESS;
  357. }
  358. void ProfilingManager::ReportData(const int32_t &device_id, const string &data, const string &tag_name) {
  359. #ifdef DAVINCI_SUPPORT_PROFILING
  360. ReporterData reporter_data{};
  361. int ret = -1;
  362. int32_t cb_ret = -1;
  363. size_t report_max_len = reporter_max_len_;
  364. size_t index = data.size() / report_max_len;
  365. if (index >= 1) {
  366. reporter_data.deviceId = device_id;
  367. ret = memcpy_s(reporter_data.tag, MSPROF_ENGINE_MAX_TAG_LEN + 1, tag_name.c_str(), tag_name.size());
  368. GE_IF_BOOL_EXEC(ret != EOK, GELOGE(ret, "Report data tag [%s] memcpy error!", tag_name.c_str()); return;);
  369. std::lock_guard<std::mutex> lock(mutex_report_);
  370. for (size_t i = 0; i < index; ++i) {
  371. reporter_data.data = (unsigned char *)data.c_str() + report_max_len * i;
  372. reporter_data.dataLen = report_max_len;
  373. cb_ret = CallMsprofReport(reporter_data);
  374. GE_IF_BOOL_EXEC(cb_ret != 0, GELOGE(cb_ret, "Reporter data [%s] failed, ret:%d", tag_name.c_str(), cb_ret);
  375. return;);
  376. }
  377. reporter_data.dataLen = data.size() - report_max_len * index;
  378. if (reporter_data.dataLen != 0) {
  379. reporter_data.data = (unsigned char *)data.c_str() + report_max_len * index;
  380. cb_ret = CallMsprofReport(reporter_data);
  381. GE_IF_BOOL_EXEC(cb_ret != 0, GELOGE(cb_ret, "Reporter data [%s] failed, ret:%d", tag_name.c_str(), cb_ret);
  382. return;);
  383. }
  384. } else {
  385. reporter_data.deviceId = device_id;
  386. reporter_data.data = (unsigned char *)data.c_str();
  387. reporter_data.dataLen = data.size();
  388. ret = memcpy_s(reporter_data.tag, MSPROF_ENGINE_MAX_TAG_LEN + 1, tag_name.c_str(), tag_name.size());
  389. GE_IF_BOOL_EXEC(ret != EOK, GELOGE(ret, "Report data tag [%s] memcpy error!", tag_name.c_str()); return;);
  390. std::lock_guard<std::mutex> lock(mutex_report_);
  391. cb_ret = CallMsprofReport(reporter_data);
  392. GE_IF_BOOL_EXEC(cb_ret != 0, GELOGE(cb_ret, "Reporter data [%s] failed, ret:%d", tag_name.c_str(), cb_ret);
  393. return;);
  394. }
  395. #endif
  396. }
  397. void ProfilingManager::ReportProfilingData(uint32_t model_id, const std::vector<TaskDescInfo> &task_desc_info) {
  398. #ifdef DAVINCI_SUPPORT_PROFILING
  399. int32_t logic_device_id = 0;
  400. rtError_t rt_ret = rtGetDevice(&logic_device_id);
  401. if (rt_ret != RT_ERROR_NONE) {
  402. GELOGE(rt_ret, "[Get][LogicDeviceId]Failed, ret 0x%X", rt_ret);
  403. REPORT_CALL_ERROR("E19999", "Get logic device id failed, ret 0x%X", rt_ret);
  404. return;
  405. }
  406. GELOGD("current logic_device_id:%d", logic_device_id);
  407. GELOGD("start ProfilingTaskDescInfo.");
  408. ProfilingTaskDescInfo(model_id, task_desc_info, logic_device_id);
  409. GELOGD("Report profiling data for GE end.");
  410. #endif
  411. }
  412. uint64_t ProfilingManager::GetProfilingModule() {
  413. uint64_t module = PROF_MODEL_EXECUTE_MASK |
  414. PROF_RUNTIME_API_MASK |
  415. PROF_RUNTIME_TRACE_MASK |
  416. PROF_SCHEDULE_TIMELINE_MASK |
  417. PROF_SCHEDULE_TRACE_MASK |
  418. PROF_TASK_TIME_MASK |
  419. PROF_SUBTASK_TIME_MASK |
  420. PROF_AICPU_TRACE_MASK |
  421. PROF_AICORE_METRICS_MASK |
  422. PROF_AIVECTORCORE_METRICS_MASK |
  423. PROF_MODEL_LOAD_MASK;
  424. return module;
  425. }
  426. void ProfilingManager::UpdateSubscribeDeviceModuleMap(std::string prof_type, uint32_t device_id, uint64_t module) {
  427. #ifdef DAVINCI_SUPPORT_PROFILING
  428. if (prof_type == kProfModelSubscribe) {
  429. if (subs_dev_module_.find(device_id) != subs_dev_module_.end()) {
  430. subs_dev_module_[device_id].subscribe_count++;
  431. } else {
  432. DeviceSubsInfo dev_info;
  433. dev_info.module = module;
  434. dev_info.subscribe_count = 1;
  435. subs_dev_module_[device_id] = dev_info;
  436. }
  437. } else if (prof_type == kProfModelUnsubscribe) {
  438. auto iter = subs_dev_module_.find(device_id);
  439. if (iter != subs_dev_module_.end()) {
  440. if (iter->second.subscribe_count > 0) {
  441. iter->second.subscribe_count--;
  442. }
  443. if (iter->second.subscribe_count == 0) {
  444. subs_dev_module_.erase(iter);
  445. }
  446. }
  447. } else {
  448. GELOGI("No need to update device_id module map.");
  449. }
  450. #endif
  451. }
  452. Status ProfilingManager::ProfModelSubscribe(uint64_t module, void *model) {
  453. #ifdef DAVINCI_SUPPORT_PROFILING
  454. std::lock_guard<std::mutex> lock(mutex_);
  455. uint64_t model_load_mask = module & PROF_MODEL_LOAD_MASK;
  456. if ((subscribe_count_ == 0) && (model_load_mask == PROF_MODEL_LOAD_MASK)) {
  457. // register framework to profiling
  458. // register Framework to profiling
  459. int32_t cb_ret = PluginInit();
  460. if (cb_ret != 0) {
  461. GELOGE(cb_ret, "[Init][ProfilingPlugin]Failed, ret %d", cb_ret);
  462. REPORT_CALL_ERROR("E19999", "Init profiling plugin failed, ret %d", cb_ret);
  463. return cb_ret;
  464. }
  465. GELOGI("Prof subscribe: model load profiling on.");
  466. }
  467. subscribe_count_++;
  468. auto davinci_model = static_cast<DavinciModel *>(model);
  469. int32_t device_num = 1;
  470. uint32_t device[1];
  471. device[0] = davinci_model->GetDeviceId();
  472. rtError_t rt_ret = rtProfilerStart(module, device_num, device);
  473. if (rt_ret != RT_ERROR_NONE) {
  474. GELOGE(FAILED, "[Start][Profiler]Malloc buffer failed, ret 0x%X", rt_ret);
  475. REPORT_CALL_ERROR("E19999", "Malloc buffer failed when start profiling, ret 0x%X", rt_ret);
  476. return FAILED;
  477. }
  478. UpdateSubscribeDeviceModuleMap(kProfModelSubscribe, device[0], module);
  479. // Report profiling data
  480. Status p_ret = davinci_model->ReportProfilingData();
  481. if (p_ret != SUCCESS) {
  482. GELOGE(p_ret, "[Report][ProfilingData]Failed, ret %u", p_ret);
  483. REPORT_CALL_ERROR("E19999", "Report profiling data failed, ret %u", p_ret);
  484. return p_ret;
  485. }
  486. #endif
  487. return SUCCESS;
  488. }
  489. Status ProfilingManager::ProfModelUnsubscribe(void *model) {
  490. #ifdef DAVINCI_SUPPORT_PROFILING
  491. std::lock_guard<std::mutex> lock(mutex_);
  492. if (subscribe_count_ == 0) {
  493. GELOGW("The profiler has not been subscribed, you do not need to cannel the subscription.");
  494. return SUCCESS;
  495. }
  496. auto davinci_model = static_cast<DavinciModel *>(model);
  497. int32_t dev_num = 1;
  498. uint32_t device[1];
  499. device[0] = davinci_model->GetDeviceId();
  500. auto iter = subs_dev_module_.find(device[0]);
  501. if (iter != subs_dev_module_.end()) {
  502. if (subs_dev_module_[device[0]].subscribe_count == 1) {
  503. // The same device_id, only stop at last time
  504. rtError_t rt_ret = rtProfilerStop(subs_dev_module_[device[0]].module, dev_num, device);
  505. if (rt_ret != RT_ERROR_NONE) {
  506. GELOGE(FAILED, "[Stop][Profiler]Malloc buffer Failed, ret %d", rt_ret);
  507. REPORT_CALL_ERROR("E19999", "Malloc buffer failed when stop profiling, ret %d", rt_ret);
  508. return FAILED;
  509. }
  510. }
  511. UpdateSubscribeDeviceModuleMap(kProfModelUnsubscribe, device[0], subs_dev_module_[device[0]].module);
  512. } else {
  513. GELOGE(FAILED, "[Cancel][DeviceId]The device_id %u has not been subscribed, "
  514. "do not need to cancel", device[0]);
  515. REPORT_CALL_ERROR("E19999", "The device_id %u has not been subscribed, do not need to cancel",
  516. device[0]);
  517. return FAILED;
  518. }
  519. subscribe_count_--;
  520. if (subscribe_count_ == 0) {
  521. // profiling plugin uninit at last subscription
  522. PluginUnInit();
  523. }
  524. #endif
  525. return SUCCESS;
  526. }
  527. Status ProfilingManager::ProfInit(uint64_t module) {
  528. #ifdef DAVINCI_SUPPORT_PROFILING
  529. std::lock_guard<std::mutex> lock(mutex_);
  530. uint64_t model_load_mask = module & PROF_MODEL_LOAD_MASK;
  531. if (model_load_mask == PROF_MODEL_LOAD_MASK) {
  532. // register Framework to profiling
  533. int32_t cb_ret = PluginInit();
  534. if (cb_ret != 0) {
  535. GELOGE(cb_ret, "[Init][ProfilingPlugin]Failed, ret %d", cb_ret);
  536. REPORT_CALL_ERROR("E19999", "Init profiling plugin failed, ret %d", cb_ret);
  537. return cb_ret;
  538. }
  539. int32_t device_num = -1;
  540. rtError_t rt_ret = rtProfilerStart(model_load_mask, device_num, nullptr);
  541. if (rt_ret != RT_ERROR_NONE) {
  542. GELOGE(FAILED, "[Start][Profiler]Malloc buffer failed, ret 0x%X", rt_ret);
  543. REPORT_CALL_ERROR("E19999", "Malloc buffer failed when start profiling, ret 0x%X", rt_ret);
  544. return FAILED;
  545. }
  546. is_load_profiling_ = true;
  547. GELOGI("Prof init: model load profiling on.");
  548. }
  549. uint64_t training_trace_mask = module & PROF_TRAINING_TRACE_MASK;
  550. if (training_trace_mask == PROF_TRAINING_TRACE_MASK) {
  551. is_training_trace_ = true;
  552. }
  553. GELOGI("Prof init success.");
  554. #endif
  555. return SUCCESS;
  556. }
  557. Status ProfilingManager::ProfFinalize() {
  558. #ifdef DAVINCI_SUPPORT_PROFILING
  559. std::lock_guard<std::mutex> lock(mutex_);
  560. is_load_profiling_ = false;
  561. is_training_trace_ = false;
  562. is_execute_profiling_ = false;
  563. index_id_ = UINT64_MAX;
  564. // profiling plugin uninit
  565. PluginUnInit();
  566. CleanSubscribeInfo();
  567. int32_t dev_num = -1;
  568. rtError_t rt_ret = rtProfilerStop(PROF_MODEL_LOAD_MASK, dev_num, nullptr);
  569. if (rt_ret != RT_ERROR_NONE) {
  570. GELOGE(FAILED, "[Stop][Profiler]Malloc buffer failed, ret 0x%X", rt_ret);
  571. REPORT_CALL_ERROR("E19999", "Malloc buffer failed when stop profiling, ret 0x%X", rt_ret);
  572. return FAILED;
  573. }
  574. for (auto device_id_module : device_id_module_map_) {
  575. if (device_id_module.second != 0) {
  576. uint32_t device_id = static_cast<uint32_t>(device_id_module.first);
  577. GELOGI("Prof finalize: device_id: %u, module: 0x%lx.", device_id, device_id_module.second);
  578. rt_ret = rtProfilerStop(device_id_module.second, 1, &device_id);
  579. if (rt_ret != RT_ERROR_NONE) {
  580. GELOGE(FAILED, "[Stop][Profiler]Failed, device_id %d, ret 0x%X", device_id, rt_ret);
  581. REPORT_CALL_ERROR("E19999", "Stop runtime profiler failed, device_id %d, ret 0x%X",
  582. device_id,rt_ret);
  583. return FAILED;
  584. }
  585. }
  586. }
  587. device_id_module_map_.clear();
  588. device_id_.clear();
  589. device_id_map_.clear();
  590. model_id_map_.clear();
  591. GELOGI("Prof finalize success.");
  592. #endif
  593. return SUCCESS;
  594. }
  595. Status ProfilingManager::ProfParseDeviceId(const std::map<std::string, std::string> &config_para,
  596. vector<int32_t> &device_list) {
  597. #ifdef DAVINCI_SUPPORT_PROFILING
  598. auto iter = config_para.find(kConfigDevIdList);
  599. if (iter != config_para.end()) {
  600. std::string device_id_list = iter->second;
  601. std::string temp;
  602. vector<std::string> decvice_id;
  603. for (uint32_t i = 0; i < device_id_list.size(); i++) {
  604. if (isdigit(device_id_list[i])) {
  605. temp.append(1, device_id_list[i]);
  606. } else {
  607. if (!temp.empty()) {
  608. decvice_id.emplace_back(temp);
  609. }
  610. temp.clear();
  611. }
  612. }
  613. if (!temp.empty()) {
  614. decvice_id.emplace_back(temp);
  615. }
  616. for (uint32_t i = 0; i < decvice_id.size(); i++) {
  617. try {
  618. int32_t dev_id = std::stoi(decvice_id[i]);
  619. device_list.push_back(dev_id);
  620. } catch (std::invalid_argument &) {
  621. GELOGE(FAILED, "[Parse][DeviceId]Failed, it is invalid, %s", decvice_id[i].c_str());
  622. REPORT_CALL_ERROR("E19999", "Parse device id %s failed, it is invalid",
  623. decvice_id[i].c_str());
  624. return FAILED;
  625. } catch (std::out_of_range &) {
  626. GELOGE(FAILED, "[Parse][DeviceId]Failed, it is out of range, %s", decvice_id[i].c_str());
  627. REPORT_CALL_ERROR("E19999", "Parse device id %s failed, it is out of range",
  628. decvice_id[i].c_str());
  629. return FAILED;
  630. } catch (...) {
  631. GELOGE(FAILED, "[Parse][DeviceId]Faield, it cannot change to int, %s",
  632. decvice_id[i].c_str());
  633. REPORT_CALL_ERROR("E19999", "Parse device id %s failed, it cannot change to int",
  634. decvice_id[i].c_str());
  635. return FAILED;
  636. }
  637. }
  638. } else {
  639. GELOGE(FAILED, "[Parse][DeviceId]Config para not contain device id list");
  640. REPORT_CALL_ERROR("E19999", "Parse device id failed, config para not contain device id list");
  641. return FAILED;
  642. }
  643. #endif
  644. return SUCCESS;
  645. }
  646. Status ProfilingManager::ProfParseParam(const std::map<std::string, std::string> &config_para, int32_t &device_num,
  647. vector<int32_t> &device_list) {
  648. #ifdef DAVINCI_SUPPORT_PROFILING
  649. // device num
  650. auto iter = config_para.find(kConfigNumsdev);
  651. if (iter != config_para.end()) {
  652. try {
  653. device_num = std::stoi(iter->second);
  654. } catch (std::invalid_argument &) {
  655. GELOGE(FAILED, "[Parse][Param]Failed, device num %s is invalid", iter->second.c_str());
  656. REPORT_CALL_ERROR("E19999", "Parse param failed, device num %s is invalid",
  657. iter->second.c_str());
  658. return FAILED;
  659. } catch (std::out_of_range &) {
  660. GELOGE(FAILED, "[Parse][Param]Failed, device num %s cannot change to int",
  661. iter->second.c_str());
  662. REPORT_CALL_ERROR("E19999", "Parse param failed, device num %s cannot change to int",
  663. iter->second.c_str());
  664. return FAILED;
  665. } catch (...) {
  666. GELOGE(FAILED, "[Parse][Param]Failed, device num %s cannot change to int",
  667. iter->second.c_str());
  668. REPORT_CALL_ERROR("E19999", "Parse param failed, device num %s cannot change to int",
  669. iter->second.c_str());
  670. return FAILED;
  671. }
  672. } else {
  673. GELOGE(FAILED, "[Parse][Param]Config para not contain device num %s", iter->second.c_str());
  674. REPORT_CALL_ERROR("E19999", "Parse param failed, config para not contain device num %s",
  675. iter->second.c_str());
  676. return FAILED;
  677. }
  678. // device id
  679. if (ProfParseDeviceId(config_para, device_list) != SUCCESS) {
  680. GELOGE(FAILED, "[Parse][DeviceId]Failed");
  681. REPORT_CALL_ERROR("E19999", "Parse device id failed");
  682. return FAILED;
  683. }
  684. if (device_num == 0 || device_num > kMaxDeviceNum || device_num != static_cast<int32_t>(device_list.size())) {
  685. GELOGE(FAILED, "[Parse][Param]Failed, config para device num %d not equal to "
  686. "device list size %zu", device_num, device_list.size());
  687. REPORT_INNER_ERROR("E19999", "[Parse][Param]Failed, config para device num %d "
  688. "not equal to device list size %zu", device_num, device_list.size());
  689. return FAILED;
  690. }
  691. #endif
  692. return SUCCESS;
  693. }
  694. Status ProfilingManager::ProfStartProfiling(uint64_t module, const std::map<std::string, std::string> &config_para) {
  695. #ifdef DAVINCI_SUPPORT_PROFILING
  696. std::lock_guard<std::mutex> lock(mutex_);
  697. uint64_t training_trace_mask = module & PROF_TRAINING_TRACE_MASK;
  698. if (training_trace_mask == PROF_TRAINING_TRACE_MASK) {
  699. is_training_trace_ = true;
  700. }
  701. int32_t device_num = 0;
  702. vector<int32_t> device_list;
  703. if (ProfParseParam(config_para, device_num, device_list) != SUCCESS) {
  704. GELOGE(FAILED, "[Parse][Param]Prof start parse param failed, device num %d, "
  705. "device list size %zu", device_num, device_list.size());
  706. REPORT_CALL_ERROR("E19999", "Prof start parse param failed, device num %d, "
  707. "device list size %zu", device_num, device_list.size());
  708. return FAILED;
  709. }
  710. auto device_id_ptr = std::unique_ptr<uint32_t[]>(new (std::nothrow) uint32_t[device_num]);
  711. if (device_id_ptr == nullptr) {
  712. GELOGE(FAILED, "[Start][Profiling]Malloc buffer failed when start profiling, device num %d",
  713. device_num);
  714. REPORT_CALL_ERROR("E19999", "Malloc buffer failed when start profiling, device num %d",
  715. device_num);
  716. return FAILED;
  717. }
  718. for (int32_t i = 0; i < device_num; i++) {
  719. device_id_ptr[i] = static_cast<uint32_t>(device_list[i]);
  720. }
  721. GELOGI("Runtime config param: 0x%lx, device num: %d.", module, device_num);
  722. rtError_t rt_ret = rtProfilerStart(module, device_num, device_id_ptr.get());
  723. if (rt_ret != RT_ERROR_NONE) {
  724. GELOGE(FAILED, "[Start][Profiler]Runtime profiler config proc failed, config param 0x%lx, "
  725. "device num %d, ret 0x%X", module, device_num, rt_ret);
  726. REPORT_CALL_ERROR("E19999", "Runtime profiler config proc failed, config param 0x%lx, "
  727. "device num %d, ret 0x%X", module, device_num, rt_ret);
  728. return FAILED;
  729. }
  730. if ((module & PROF_MODEL_EXECUTE_MASK) == PROF_MODEL_EXECUTE_MASK) {
  731. for (int32_t i = 0; i < device_num; i++) {
  732. if (std::find(device_id_.begin(), device_id_.end(), device_list[i]) == device_id_.end()) {
  733. device_id_.push_back(device_list[i]);
  734. }
  735. }
  736. GELOGI("Prof start: ge execute model start profiling.");
  737. }
  738. if ((module & PROF_MODEL_LOAD_MASK) == PROF_MODEL_LOAD_MASK) {
  739. GELOGW("Prof start: load model module is invalid.");
  740. }
  741. UpdateDeviceIdModuleMap(kProfStart, module, device_list);
  742. GELOGI("Prof start profiling success.");
  743. #endif
  744. return SUCCESS;
  745. }
  746. Status ProfilingManager::ProfStopProfiling(uint64_t module, const std::map<std::string, std::string> &config_para) {
  747. #ifdef DAVINCI_SUPPORT_PROFILING
  748. std::lock_guard<std::mutex> lock(mutex_);
  749. int32_t device_num = 0;
  750. vector<int32_t> device_list;
  751. if (ProfParseParam(config_para, device_num, device_list) != SUCCESS) {
  752. GELOGE(FAILED, "[Stop][Profiling]Prof stop parse param failed, device num %d, "
  753. "device list size %zu", device_num, device_list.size());
  754. REPORT_CALL_ERROR("E19999", "Prof stop parse param failed, device num %d, device list size %zu",
  755. device_num, device_list.size());
  756. return FAILED;
  757. }
  758. auto device_id_ptr = std::unique_ptr<uint32_t[]>(new (std::nothrow) uint32_t[device_num]);
  759. if (device_id_ptr == nullptr) {
  760. GELOGE(FAILED, "[Stop][Profiling]Malloc buffer failed when stop profiling, device num %d",
  761. device_num);
  762. REPORT_CALL_ERROR("E19999", "Malloc buffer failed when stop profiling, device num %d",
  763. device_num);
  764. return FAILED;
  765. }
  766. for (int32_t i = 0; i < device_num; i++) {
  767. device_id_ptr[i] = static_cast<uint32_t>(device_list[i]);
  768. }
  769. GELOGI("Prof stop: runtime config param: 0x%lx, device num: %d", module, device_num);
  770. rtError_t rt_ret = rtProfilerStop(module, device_num, device_id_ptr.get());
  771. if (rt_ret != RT_ERROR_NONE) {
  772. GELOGE(FAILED, "[Stop][Profiler]Runtime profiler config proc failed, config param 0x%lx, "
  773. "device num: %d, ret 0x%X", module, device_num, rt_ret);
  774. REPORT_CALL_ERROR("E19999", "Runtime profiler config proc failed, config param 0x%lx, "
  775. "device num %d, ret 0x%X", module, device_num, rt_ret);
  776. return FAILED;
  777. }
  778. uint64_t execute_model_mask = module & PROF_MODEL_EXECUTE_MASK;
  779. if (execute_model_mask == PROF_MODEL_EXECUTE_MASK) {
  780. for (int32_t i = 0; i < device_num; i++) {
  781. auto iter = std::find(device_id_.begin(), device_id_.end(), device_list[i]);
  782. if (iter != device_id_.end()) {
  783. device_id_.erase(iter);
  784. }
  785. }
  786. GELOGI("Prof stop: ge execute model stop profiling.");
  787. }
  788. if ((module & PROF_MODEL_LOAD_MASK) == PROF_MODEL_LOAD_MASK) {
  789. GELOGW("Prof stop: load model module is invalid.");
  790. }
  791. UpdateDeviceIdModuleMap(kProfStop, module, device_list);
  792. GELOGI("Prof stop profiling success.");
  793. #endif
  794. return SUCCESS;
  795. }
  796. void ProfilingManager::UpdateDeviceIdModuleMap(string prof_type, uint64_t module, const vector<int32_t> &device_list) {
  797. #ifdef DAVINCI_SUPPORT_PROFILING
  798. if (prof_type == kProfStart) {
  799. for (uint32_t i = 0; i < device_list.size(); i++) {
  800. auto iter = device_id_module_map_.find(device_list[i]);
  801. if (iter != device_id_module_map_.end()) {
  802. uint64_t prof_on_module = device_id_module_map_[device_list[i]];
  803. // save all profiling on module of device
  804. device_id_module_map_[device_list[i]] = prof_on_module | module;
  805. } else {
  806. device_id_module_map_[device_list[i]] = module;
  807. }
  808. }
  809. } else if (prof_type == kProfStop) {
  810. for (uint32_t i = 0; i < device_list.size(); i++) {
  811. auto iter = device_id_module_map_.find(device_list[i]);
  812. if (iter != device_id_module_map_.end()) {
  813. uint64_t prof_on_module = device_id_module_map_[device_list[i]];
  814. uint64_t prof_off_module = prof_on_module & module;
  815. uint64_t prof_on_left_module = prof_on_module & (~prof_off_module);
  816. // stop profiling on module of device
  817. device_id_module_map_[device_list[i]] = prof_on_left_module;
  818. }
  819. }
  820. } else {
  821. GELOGI("No need to update device_id module map.");
  822. }
  823. #endif
  824. }
  825. bool ProfilingManager::ProfilingModelExecuteOn() const {
  826. int32_t logic_device_id = 0;
  827. rtError_t rt_ret = rtGetDevice(&logic_device_id);
  828. if (rt_ret != RT_ERROR_NONE) {
  829. GELOGE(rt_ret, "[Get][LogicDeviceId]Failed, ret 0x%X", rt_ret);
  830. REPORT_CALL_ERROR("E19999", "Get logic device id failed, ret 0x%X", rt_ret);
  831. }
  832. GELOGI("Current logic_device_id:%d", logic_device_id);
  833. bool execute_model_prof_on = false;
  834. auto iter = std::find(device_id_.begin(), device_id_.end(), logic_device_id);
  835. if (iter != device_id_.end()) {
  836. execute_model_prof_on = true;
  837. }
  838. GELOGI("Flag is_execute_profiling: %d, execute_model_prof_on: %d", is_execute_profiling_, execute_model_prof_on);
  839. return execute_model_prof_on;
  840. }
  841. Status ProfilingManager::PluginInit() {
  842. if (prof_cb_.msprofReporterCallback == nullptr) {
  843. GELOGE(ge::PARAM_INVALID, "[Check][Param]MsprofReporterCallback callback is nullptr");
  844. REPORT_INNER_ERROR("E19999", "MsprofReporterCallback callback is nullptr");
  845. return ge::PARAM_INVALID;
  846. }
  847. int32_t cb_ret = prof_cb_.msprofReporterCallback(
  848. static_cast<uint32_t>(MsprofReporterModuleId::MSPROF_MODULE_FRAMEWORK),
  849. static_cast<uint32_t>(MsprofReporterCallbackType::MSPROF_REPORTER_INIT),
  850. nullptr, 0);
  851. if (cb_ret != MSPROF_ERROR_NONE) {
  852. REPORT_CALL_ERROR("E19999", "Profiling reporter init failed, ret 0x%X", cb_ret);
  853. GELOGE(INTERNAL_ERROR, "[Init][ProfilingReporter]Failed, ret 0x%X", cb_ret);
  854. return INTERNAL_ERROR;
  855. }
  856. cb_ret = prof_cb_.msprofReporterCallback(
  857. static_cast<uint32_t>(MsprofReporterModuleId::MSPROF_MODULE_FRAMEWORK),
  858. static_cast<uint32_t>(MsprofReporterCallbackType::MSPROF_REPORTER_DATA_MAX_LEN),
  859. &reporter_max_len_, sizeof(uint32_t));
  860. if (cb_ret != MSPROF_ERROR_NONE) {
  861. REPORT_CALL_ERROR("E19999", "Get profiling reporter data max len failed, ret 0x%X", cb_ret);
  862. GELOGE(INTERNAL_ERROR, "[Get][ProfilingDataMaxLen]Failed, ret 0x%X", cb_ret);
  863. return INTERNAL_ERROR;
  864. }
  865. return SUCCESS;
  866. }
  867. void ProfilingManager::PluginUnInit() const {
  868. #ifdef DAVINCI_SUPPORT_PROFILING
  869. if (prof_cb_.msprofReporterCallback == nullptr) {
  870. GELOGE(ge::PARAM_INVALID, "[Check][Param]MsprofReporterCallback callback is nullptr");
  871. REPORT_INNER_ERROR("E19999", "MsprofReporterCallback callback is nullptr");
  872. return;
  873. }
  874. int32_t cb_ret = prof_cb_.msprofReporterCallback(
  875. static_cast<uint32_t>(MsprofReporterModuleId::MSPROF_MODULE_FRAMEWORK),
  876. static_cast<uint32_t>(MsprofReporterCallbackType::MSPROF_REPORTER_UNINIT),
  877. nullptr, 0);
  878. if (cb_ret != 0) {
  879. GELOGW("profiling plugin uninit failed, ret:%d", cb_ret);
  880. }
  881. #endif
  882. }
  883. Status ProfilingManager::CallMsprofReport(ReporterData &reporter_data) const {
  884. if (prof_cb_.msprofReporterCallback == nullptr) {
  885. GELOGE(ge::PARAM_INVALID, "[Check][Param]MsprofReporterCallback callback is nullptr");
  886. REPORT_INNER_ERROR("E19999", "MsprofReporterCallback callback is nullptr");
  887. return ge::PARAM_INVALID;
  888. }
  889. return prof_cb_.msprofReporterCallback(
  890. static_cast<uint32_t>(MsprofReporterModuleId::MSPROF_MODULE_FRAMEWORK),
  891. static_cast<uint32_t>(MsprofReporterCallbackType::MSPROF_REPORTER_REPORT),
  892. static_cast<void *>(&reporter_data), sizeof(ReporterData));
  893. }
  894. void ProfilingManager::GetOpInputInfo(const OpDescPtr &op, TaskDescInfo &task_desc_info) const {
  895. std::vector<Format> input_format;
  896. std::vector<std::vector<int64_t>> input_shape;
  897. std::vector<DataType> input_data_type;
  898. for (size_t i = 0; i < op->GetAllInputsSize(); ++i) {
  899. GeTensorDescPtr input_tensor_desc = op->MutableInputDesc(i);
  900. if (input_tensor_desc == nullptr) {
  901. continue;
  902. }
  903. input_format.emplace_back(input_tensor_desc->GetFormat());
  904. input_shape.emplace_back(input_tensor_desc->GetShape().GetDims());
  905. input_data_type.emplace_back(input_tensor_desc->GetDataType());
  906. }
  907. std::vector<Format> format_default = { FORMAT_NULL };
  908. std::vector<std::vector<int64_t>> shape_default = { {0} };
  909. std::vector<DataType> data_type_default = { DT_UNDEFINED };
  910. task_desc_info.input_format = input_format.empty() ? format_default : input_format;
  911. task_desc_info.input_shape = input_shape.empty() ? shape_default : input_shape;
  912. task_desc_info.input_data_type = input_data_type.empty() ? data_type_default : input_data_type;
  913. }
  914. void ProfilingManager::GetOpOutputInfo(const OpDescPtr &op, TaskDescInfo &task_desc_info) const {
  915. std::vector<Format> output_format;
  916. std::vector<std::vector<int64_t>> output_shape;
  917. std::vector<DataType> output_data_type;
  918. for (size_t j = 0; j < op->GetOutputsSize(); ++j) {
  919. GeTensorDescPtr output_tensor_desc = op->MutableOutputDesc(j);
  920. if (output_tensor_desc == nullptr) {
  921. continue;
  922. }
  923. output_format.emplace_back(output_tensor_desc->GetFormat());
  924. output_shape.emplace_back(output_tensor_desc->GetShape().GetDims());
  925. output_data_type.emplace_back(output_tensor_desc->GetDataType());
  926. }
  927. std::vector<Format> format_default = { FORMAT_NULL };
  928. std::vector<std::vector<int64_t>> shape_default = { {0} };
  929. std::vector<DataType> data_type_default = { DT_UNDEFINED };
  930. task_desc_info.output_format = output_format.empty() ? format_default : output_format;
  931. task_desc_info.output_shape = output_shape.empty() ? shape_default : output_shape;
  932. task_desc_info.output_data_type = output_data_type.empty() ? data_type_default : output_data_type;
  933. }
  934. void ProfilingManager::GetOpInputOutputInfo(const OpDescPtr &op, TaskDescInfo &task_desc_info) const {
  935. GetOpInputInfo(op, task_desc_info);
  936. GetOpOutputInfo(op, task_desc_info);
  937. }
  938. void ProfilingManager::GetFpBpPoint(std::string &fp_point, std::string &bp_point) {
  939. // Env or options mode, fp_point_/bp_point_ have initiliazed on profiling init
  940. if (!fp_point_.empty() && !bp_point_.empty()) {
  941. fp_point = fp_point_;
  942. bp_point = bp_point_;
  943. GELOGI("Bp Fp have been initialized in env or options. bp_point: %s, fp_point: %s", bp_point.c_str(),
  944. fp_point.c_str());
  945. return;
  946. }
  947. // ProfApi mode and training trace is set
  948. // Parse options first
  949. char env_profiling_options[MSPROF_OPTIONS_DEF_LEN_MAX] = {0x00};
  950. bool is_profiling_valid = false;
  951. std::string profiling_options;
  952. if (ge::GetContext().GetOption(OPTION_EXEC_PROFILING_OPTIONS, profiling_options) == SUCCESS &&
  953. !profiling_options.empty()) {
  954. is_profiling_valid = true;
  955. } else {
  956. INT32 ret = mmGetEnv("PROFILING_OPTIONS", env_profiling_options, MSPROF_OPTIONS_DEF_LEN_MAX);
  957. if (ret != EN_OK) {
  958. GELOGI("PROFILING_OPTIONS env is not exist.");
  959. return;
  960. }
  961. GELOGI("Parse env PROFILING_OPTIONS:%s.", env_profiling_options);
  962. profiling_options = env_profiling_options;
  963. is_profiling_valid = true;
  964. }
  965. if (is_profiling_valid) {
  966. try {
  967. Json prof_options = Json::parse(profiling_options);
  968. if (prof_options.contains(kFpPoint)) {
  969. fp_point_ = prof_options[kFpPoint];
  970. }
  971. if (prof_options.contains(kBpPoint)) {
  972. bp_point_ = prof_options[kBpPoint];
  973. }
  974. fp_point = fp_point_;
  975. bp_point = bp_point_;
  976. if (!fp_point_.empty() && !bp_point_.empty()) {
  977. GELOGI("Training trace bp fp is set, bp_point:%s, fp_point:%s.", bp_point_.c_str(), fp_point_.c_str());
  978. }
  979. } catch (...) {
  980. GELOGW("Json prof options is invalid.");
  981. return;
  982. }
  983. }
  984. return;
  985. }
  986. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::GetDeviceIdFromGraph(
  987. uint32_t graph_id, uint32_t &device_id) {
  988. auto iter = device_id_map_.find(graph_id);
  989. if (iter != device_id_map_.end()) {
  990. device_id = iter->second;
  991. return SUCCESS;
  992. }
  993. REPORT_CALL_ERROR("E19999", "graph_id:%u does not exist!", graph_id);
  994. GELOGE(PARAM_INVALID, "[Check][GraphId]graph_id:%u does not exist!", graph_id);
  995. return FAILED;
  996. }
  997. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::SetSubscribeInfo(
  998. uint64_t prof_switch, uint32_t model_id, bool is_subscribe) {
  999. subscribe_info_.is_subscribe = is_subscribe;
  1000. subscribe_info_.prof_switch = prof_switch;
  1001. subscribe_info_.graph_id = model_id;
  1002. }
  1003. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void ProfilingManager::CleanSubscribeInfo() {
  1004. subscribe_info_.is_subscribe = false;
  1005. subscribe_info_.prof_switch = 0;
  1006. subscribe_info_.graph_id = 0;
  1007. }
  1008. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ProfilingManager::GetModelIdFromGraph(
  1009. uint32_t graph_id, uint32_t &model_id) {
  1010. auto iter = model_id_map_.find(graph_id);
  1011. if (iter != model_id_map_.end()) {
  1012. model_id = iter->second;
  1013. return SUCCESS;
  1014. }
  1015. REPORT_CALL_ERROR("E19999", "graph_id:%u does not exist!", graph_id);
  1016. GELOGE(PARAM_INVALID, "[Check][GraphId]graph_id:%u does not exist!", graph_id);
  1017. return FAILED;
  1018. }
  1019. } // namespace ge

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