You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

profiling_manager.cc 44 kB

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

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