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

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