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

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