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

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