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.

op_task.cc 47 kB

5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 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
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  1. /**
  2. * Copyright 2019-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 "single_op/task/op_task.h"
  17. #include <google/protobuf/extension_set.h>
  18. #include <chrono>
  19. #include <thread>
  20. #include "aicpu/common/aicpu_task_struct.h"
  21. #include "common/dump/dump_manager.h"
  22. #include "common/dump/dump_op.h"
  23. #include "common/profiling/profiling_manager.h"
  24. #include "common/formats/formats.h"
  25. #include "common/math/math_util.h"
  26. #include "framework/common/debug/log.h"
  27. #include "register/op_tiling.h"
  28. #include "runtime/rt.h"
  29. #include "single_op/task/build_task_utils.h"
  30. namespace ge {
  31. namespace {
  32. constexpr int kLaunchRetryTimes = 1000;
  33. constexpr size_t kMemcpyArgCount = 2;
  34. constexpr int kSleepTime = 10;
  35. constexpr uint64_t kReleaseFlag = 1;
  36. constexpr int kCopyNum = 2;
  37. constexpr uint64_t kInferSessionId = 0;
  38. void FreeHbm(void *var) {
  39. if (var) {
  40. (void)rtFree(var);
  41. }
  42. }
  43. } // namespace
  44. Status OpTask::OpenDump(rtStream_t stream) {
  45. if (DumpManager::GetInstance().GetDumpProperties(kInferSessionId).IsSingleOpNeedDump()) {
  46. GELOGI("Dump is open in single op, start to set dump info");
  47. std::vector<uint64_t> input_addrs;
  48. std::vector<uint64_t> output_adds;
  49. auto input_size = op_desc_->GetInputsSize();
  50. auto output_size = op_desc_->GetOutputsSize();
  51. uintptr_t *arg_base = nullptr;
  52. size_t arg_num = 0;
  53. GetIoAddr(arg_base, arg_num);
  54. if (arg_num < input_size + output_size) {
  55. GELOGE(ACL_ERROR_GE_INTERNAL_ERROR,
  56. "[Check][Size]io_addrs_for_dump_ size %zu is not equal input and output size %zu",
  57. arg_num, input_size + output_size);
  58. REPORT_INNER_ERROR("E19999", "io_addrs_for_dump_ size %zu is not equal input and output size %zu",
  59. arg_num, input_size + output_size);
  60. return ACL_ERROR_GE_INTERNAL_ERROR;
  61. }
  62. for (size_t i = 0; i < input_size; i++) {
  63. uint64_t input_addr = arg_base[i];
  64. input_addrs.emplace_back(input_addr);
  65. }
  66. for (size_t j = 0; j < output_size; j++) {
  67. uint64_t output_addr = arg_base[input_size + j];
  68. output_adds.emplace_back(output_addr);
  69. }
  70. dump_op_.SetDumpInfo(DumpManager::GetInstance().GetDumpProperties(kInferSessionId),
  71. op_desc_, input_addrs, output_adds, stream);
  72. auto status = dump_op_.LaunchDumpOp();
  73. if (status != SUCCESS) {
  74. GELOGE(status, "[Launch][DumpOp] failed in single op.");
  75. return status;
  76. }
  77. return SUCCESS;
  78. }
  79. GELOGI("Dump is not open in single op");
  80. return SUCCESS;
  81. }
  82. void TbeOpTask::SetStubFunc(const std::string &name, const void *stub_func) {
  83. this->stub_name_ = name;
  84. this->stub_func_ = stub_func;
  85. }
  86. void TbeOpTask::SetKernelArgs(std::unique_ptr<uint8_t[]> &&args, size_t arg_size, uint32_t block_dim,
  87. const OpDescPtr &op_desc) {
  88. args_ = std::move(args);
  89. arg_size_ = arg_size;
  90. block_dim_ = block_dim;
  91. op_desc_ = op_desc;
  92. }
  93. void TbeOpTask::SetKernelWithHandleArgs(std::unique_ptr<uint8_t[]> &&args, size_t arg_size, uint32_t block_dim,
  94. const OpDescPtr &op_desc,
  95. const domi::KernelDefWithHandle &kernel_def_with_handle) {
  96. SetKernelArgs(std::move(args), arg_size, block_dim, op_desc);
  97. original_kernel_key_ = kernel_def_with_handle.original_kernel_key();
  98. node_info_ = kernel_def_with_handle.node_info();
  99. }
  100. void TbeOpTask::SetSmDesc(void *sm_desc) { sm_desc_ = sm_desc; }
  101. void OpTask::SetModelArgs(std::string model_name, uint32_t model_id) {
  102. model_name_ = model_name;
  103. model_id_ = model_id;
  104. }
  105. Status OpTask::GetProfilingArgs(TaskDescInfo &task_desc_info, uint32_t &model_id) {
  106. uint32_t task_id = 0;
  107. uint32_t stream_id = 0;
  108. auto rt_ret = rtGetTaskIdAndStreamID(&task_id, &stream_id);
  109. if (rt_ret != RT_ERROR_NONE) {
  110. GELOGE(RT_FAILED, "[Get][TaskIdAndStreamID] failed, ret: 0x%X.", rt_ret);
  111. REPORT_CALL_ERROR("E19999", "rtGetTaskIdAndStreamID failed, ret: 0x%X.", rt_ret);
  112. return RT_ERROR_TO_GE_STATUS(rt_ret);
  113. }
  114. GE_CHECK_NOTNULL(op_desc_);
  115. string op_name = op_desc_->GetName();
  116. GELOGD("Get profiling args of op [%s] end, task_id[%u], stream_id[%u].", op_name.c_str(), task_id, stream_id);
  117. model_id = model_id_;
  118. task_desc_info.model_name = model_name_;
  119. task_desc_info.block_dim = block_dim_;
  120. task_desc_info.task_id = task_id;
  121. task_desc_info.stream_id = stream_id;
  122. task_desc_info.op_name = op_name;
  123. task_desc_info.op_type = op_desc_->GetType();
  124. auto &prof_mgr = ProfilingManager::Instance();
  125. prof_mgr.GetOpInputOutputInfo(op_desc_, task_desc_info);
  126. return SUCCESS;
  127. }
  128. Status OpTask::UpdateRunInfo() {
  129. return UNSUPPORTED;
  130. }
  131. Status OpTask::DoUpdateArgTable(const SingleOpModelParam &param, bool keep_workspace) {
  132. auto addresses = BuildTaskUtils::GetAddresses(op_desc_, param, keep_workspace);
  133. auto all_addresses = BuildTaskUtils::JoinAddresses(addresses);
  134. uintptr_t *arg_base = nullptr;
  135. size_t arg_num = 0;
  136. GetIoAddr(arg_base, arg_num);
  137. if (arg_num < all_addresses.size()) {
  138. GELOGE(ACL_ERROR_GE_INTERNAL_ERROR,
  139. "[Check][Size][%s] arg number mismatches, expect at least = %zu, but got = %zu.",
  140. op_desc_->GetName().c_str(), all_addresses.size(), arg_num);
  141. REPORT_INNER_ERROR("E19999", "%s arg number mismatches, expect at least = %zu, but got = %zu.",
  142. op_desc_->GetName().c_str(), all_addresses.size(), arg_num);
  143. return ACL_ERROR_GE_INTERNAL_ERROR;
  144. }
  145. for (void *addr : all_addresses) {
  146. *arg_base++ = reinterpret_cast<uintptr_t >(addr);
  147. }
  148. return SUCCESS;
  149. }
  150. Status OpTask::UpdateArgTable(const SingleOpModelParam &param) {
  151. return DoUpdateArgTable(param, true);
  152. }
  153. Status OpTask::LaunchKernel(const vector<GeTensorDesc> &input_desc,
  154. const vector<DataBuffer> &input_buffers,
  155. vector<GeTensorDesc> &output_desc,
  156. vector<DataBuffer> &output_buffers,
  157. rtStream_t stream) {
  158. return UNSUPPORTED;
  159. }
  160. const std::string &OpTask::GetTaskType() const { return kTaskTypeInvalid; }
  161. TbeOpTask::~TbeOpTask() {
  162. if (sm_desc_ != nullptr) {
  163. (void)rtMemFreeManaged(sm_desc_);
  164. }
  165. if (tiling_buffer_ != nullptr) {
  166. (void)rtFree(tiling_buffer_);
  167. }
  168. }
  169. const void *TbeOpTask::GetArgs() const { return args_.get(); }
  170. size_t TbeOpTask::GetArgSize() const { return arg_size_; }
  171. const std::string &TbeOpTask::GetStubName() const { return stub_name_; }
  172. const std::string &TbeOpTask::GetTaskType() const { return kTaskTypeAicore; }
  173. void TbeOpTask::SetHandle(void *handle) {
  174. this->handle_ = handle;
  175. }
  176. Status TbeOpTask::LaunchKernel(rtStream_t stream) {
  177. GELOGD("To invoke rtKernelLaunch. task = %s, block_dim = %u", this->stub_name_.c_str(), block_dim_);
  178. auto ret = DoLaunchKernel(stream);
  179. int retry_times = 0;
  180. while (ret != RT_ERROR_NONE && retry_times < kLaunchRetryTimes) {
  181. retry_times++;
  182. GELOGW("Retry after %d ms, retry_times: %d", kSleepTime, retry_times);
  183. std::this_thread::sleep_for(std::chrono::milliseconds(kSleepTime));
  184. ret = DoLaunchKernel(stream);
  185. }
  186. if (ret != RT_ERROR_NONE) {
  187. GELOGE(ret, "[Invoke][RtKernelLaunch] failed. ret = %d, task = %s", ret, this->stub_name_.c_str());
  188. REPORT_INNER_ERROR("E19999", "invoke rtKernelLaunch failed, ret = %d, task = %s", ret, this->stub_name_.c_str());
  189. return RT_ERROR_TO_GE_STATUS(ret);
  190. }
  191. GELOGI("[TASK_INFO] %s", this->stub_name_.c_str());
  192. return SUCCESS;
  193. }
  194. Status TbeOpTask::UpdateRunInfo() {
  195. // invoke OpParaCalculate
  196. GELOGD("Start to invoke OpParaCalculate.");
  197. optiling::utils::OpRunInfo run_info(0, true, 0);
  198. auto ret = optiling::OpParaCalculateV2(*node_, run_info);
  199. if (ret != GRAPH_SUCCESS) {
  200. GELOGE(ACL_ERROR_GE_INTERNAL_ERROR, "[Invoke][OpParaCalculate] failed, ret = %u.", ret);
  201. REPORT_INNER_ERROR("E19999", "invoke OpParaCalculate failed, ret = %u.", ret);
  202. return ACL_ERROR_GE_INTERNAL_ERROR;
  203. }
  204. block_dim_ = run_info.GetBlockDim();
  205. tiling_data_ = run_info.GetAllTilingData().str();
  206. tiling_key_ = run_info.GetTilingKey();
  207. run_info.GetAllWorkspaces(run_info_workspaces_);
  208. GELOGD("Done invoking OpParaCalculate successfully. block_dim = %u, tiling size = %zu, tiling_key = %u", block_dim_,
  209. tiling_data_.size(), tiling_key_);
  210. return SUCCESS;
  211. }
  212. Status TbeOpTask::UpdateTensorDesc(const GeTensorDesc &src_tensor, GeTensorDesc &dst_tensor) {
  213. int64_t storage_format_val = static_cast<Format>(FORMAT_RESERVED);
  214. (void)AttrUtils::GetInt(src_tensor, ge::ATTR_NAME_STORAGE_FORMAT, storage_format_val);
  215. auto storage_format = static_cast<Format>(storage_format_val);
  216. if (storage_format == FORMAT_RESERVED) {
  217. GELOGD("Storage format not set. update shape to [%s], and original shape to [%s]",
  218. src_tensor.GetShape().ToString().c_str(), src_tensor.GetOriginShape().ToString().c_str());
  219. dst_tensor.SetShape(src_tensor.GetShape());
  220. dst_tensor.SetOriginShape(src_tensor.GetOriginShape());
  221. } else {
  222. std::vector<int64_t> storage_shape;
  223. if (!AttrUtils::GetListInt(src_tensor, ge::ATTR_NAME_STORAGE_SHAPE, storage_shape)) {
  224. GELOGE(ACL_ERROR_GE_INTERNAL_ERROR, "[Get][ListInt]failed while storage_format was set.");
  225. return ACL_ERROR_GE_INTERNAL_ERROR;
  226. }
  227. GELOGD("Storage format set. update shape to [%s], and original shape to [%s]",
  228. GeShape(storage_shape).ToString().c_str(), src_tensor.GetShape().ToString().c_str());
  229. dst_tensor.SetShape(GeShape(std::move(storage_shape)));
  230. dst_tensor.SetOriginShape(src_tensor.GetShape());
  231. }
  232. return SUCCESS;
  233. }
  234. Status TbeOpTask::UpdateNodeByShape(const vector<GeTensorDesc> &input_desc, const vector<GeTensorDesc> &output_desc) {
  235. auto op_desc = node_->GetOpDesc();
  236. GE_CHECK_NOTNULL(op_desc);
  237. // Set runtime shape to node
  238. for (size_t i = 0; i < input_desc.size(); ++i) {
  239. auto tensor_desc = op_desc->MutableInputDesc(i);
  240. auto &runtime_tensor_desc = input_desc[i];
  241. GE_CHECK_NOTNULL(tensor_desc);
  242. GE_CHK_STATUS_RET(UpdateTensorDesc(runtime_tensor_desc, *tensor_desc));
  243. }
  244. for (size_t i = 0; i < output_desc.size(); ++i) {
  245. auto tensor_desc = op_desc->MutableOutputDesc(i);
  246. auto &runtime_tensor_desc = output_desc[i];
  247. GE_CHECK_NOTNULL(tensor_desc);
  248. GE_CHK_STATUS_RET(UpdateTensorDesc(runtime_tensor_desc, *tensor_desc));
  249. }
  250. return SUCCESS;
  251. }
  252. Status TbeOpTask::EnableDynamicSupport(const NodePtr &node, void *tiling_buffer, uint32_t max_tiling_size) {
  253. if (tiling_buffer != nullptr) {
  254. uintptr_t *arg_base = nullptr;
  255. size_t arg_num = 0;
  256. GetIoAddr(arg_base, arg_num);
  257. GE_CHECK_NOTNULL(node);
  258. GE_CHECK_NOTNULL(node->GetOpDesc());
  259. uint32_t inputs_num = node->GetOpDesc()->GetInputsSize();
  260. uint32_t outputs_num = node->GetOpDesc()->GetOutputsSize();
  261. uint32_t workspace_nums = node->GetOpDesc()->GetWorkspace().size();
  262. uint32_t tiling_index = inputs_num + outputs_num + workspace_nums;
  263. if (arg_num == 0 || arg_num < tiling_index) {
  264. GELOGE(ACL_ERROR_GE_INTERNAL_ERROR, "[Check][Size]Tiling index %u, arg number %zu is invalid.",
  265. tiling_index, arg_num);
  266. return ACL_ERROR_GE_INTERNAL_ERROR;
  267. }
  268. arg_base[tiling_index] = reinterpret_cast<uintptr_t>(tiling_buffer);
  269. }
  270. node_ = node;
  271. tiling_buffer_ = tiling_buffer;
  272. max_tiling_size_ = max_tiling_size;
  273. return SUCCESS;
  274. }
  275. Status TbeOpTask::AllocateWorkspaces(const vector<int64_t> &workspace_sizes) {
  276. static const std::string kPurpose("malloc workspace memory for dynamic op.");
  277. workspaces_.clear();
  278. if (workspace_sizes.empty()) {
  279. GELOGD("No need to allocate workspace.");
  280. return SUCCESS;
  281. }
  282. int64_t total_size = 0;
  283. std::vector<int64_t> ws_offsets;
  284. for (auto ws_size : workspace_sizes) {
  285. // alignment and padding should be done in OpParaCalculate
  286. if (CheckInt64AddOverflow(total_size, ws_size) != SUCCESS) {
  287. return ACL_ERROR_GE_INTERNAL_ERROR;
  288. }
  289. ws_offsets.emplace_back(total_size);
  290. total_size += ws_size;
  291. }
  292. GELOGD("Total workspace size is %ld", total_size);
  293. GE_CHECK_NOTNULL(stream_resource_);
  294. auto ws_base = stream_resource_->MallocMemory(kPurpose, static_cast<size_t>(total_size));
  295. if (ws_base == nullptr) {
  296. GELOGE(ACL_ERROR_GE_MEMORY_ALLOCATION, "[Malloc][Memory] failed, size: %ld", total_size);
  297. REPORT_INNER_ERROR("E19999", "MallocMemory failed, size: %ld", total_size);
  298. return ACL_ERROR_GE_MEMORY_ALLOCATION;
  299. }
  300. GELOGD("Done allocating workspace memory successfully.");
  301. for (auto ws_offset : ws_offsets) {
  302. workspaces_.emplace_back(ws_base + ws_offset);
  303. }
  304. return SUCCESS;
  305. }
  306. Status TbeOpTask::LaunchKernel(const vector<GeTensorDesc> &input_desc,
  307. const vector<DataBuffer> &input_buffers,
  308. vector<GeTensorDesc> &output_desc,
  309. vector<DataBuffer> &output_buffers,
  310. rtStream_t stream) {
  311. GELOGD("[%s] Start to launch kernel", node_->GetName().c_str());
  312. GE_CHK_STATUS_RET_NOLOG(UpdateNodeByShape(input_desc, output_desc));
  313. GE_CHK_STATUS_RET_NOLOG(UpdateRunInfo());
  314. GE_CHK_STATUS_RET(AllocateWorkspaces(run_info_workspaces_), "[Allocate][Workspaces] failed.");
  315. std::vector<void *> args;
  316. for (auto &buffer : input_buffers) {
  317. args.emplace_back(buffer.data);
  318. }
  319. for (auto &buffer : output_buffers) {
  320. args.emplace_back(buffer.data);
  321. }
  322. for (auto &buffer : workspaces_) {
  323. args.emplace_back(buffer);
  324. }
  325. if (tiling_buffer_ != nullptr) {
  326. GELOGD("[%s] Start to copy tiling info. size = %zu", node_->GetName().c_str(), tiling_data_.size());
  327. GE_CHK_RT_RET(rtMemcpyAsync(tiling_buffer_, max_tiling_size_, tiling_data_.data(), tiling_data_.size(),
  328. RT_MEMCPY_HOST_TO_DEVICE_EX, stream));
  329. args.emplace_back(tiling_buffer_);
  330. }
  331. GELOGD("Dst size is %zu, src size is %zu.", arg_size_, args.size() * sizeof(void *));
  332. // node with workspace: build can not get size of workspace, need to update arg_size_ when execute
  333. if (arg_size_ < (args.size() * sizeof(void *))) {
  334. size_t temp_size = args.size() * sizeof(void *);
  335. GELOGD("Need to reset size of args_ from %zu to %zu.", arg_size_, temp_size);
  336. args_.reset(new(std::nothrow) uint8_t[temp_size]());
  337. GE_CHECK_NOTNULL(args_);
  338. arg_size_ = temp_size;
  339. }
  340. if (memcpy_s(args_.get(), arg_size_, args.data(), args.size() * sizeof(void *)) != EOK) {
  341. GELOGE(ACL_ERROR_GE_MEMORY_OPERATE_FAILED, "[Update][KernelArgs] failed for [%s].", node_->GetName().c_str());
  342. REPORT_INNER_ERROR("E19999", "update kernel args failed for %s.", node_->GetName().c_str());
  343. return ACL_ERROR_GE_MEMORY_OPERATE_FAILED;
  344. }
  345. GELOGD("[%s] Start to invoke rtKernelLaunch", node_->GetName().c_str());
  346. GE_CHK_STATUS_RET(DoLaunchKernel(stream), "Failed to do launch kernel.");
  347. return SUCCESS;
  348. }
  349. Status TbeOpTask::DoLaunchKernel(rtStream_t stream) {
  350. auto *sm_desc = reinterpret_cast<rtSmDesc_t *>(sm_desc_);
  351. if (handle_ == nullptr) {
  352. GE_CHK_RT_RET(rtKernelLaunch(stub_func_, block_dim_, args_.get(), static_cast<uint32_t>(arg_size_),
  353. sm_desc, stream));
  354. } else {
  355. std::string dev_func = original_kernel_key_ + "_" + std::to_string(tiling_key_);
  356. std::string kernel_info = node_info_ + "/" + std::to_string(tiling_key_);
  357. GE_CHK_RT_RET(rtKernelLaunchWithHandle(handle_, dev_func.c_str(), block_dim_, args_.get(),
  358. static_cast<uint32_t>(arg_size_), sm_desc, stream, kernel_info.c_str()));
  359. }
  360. return SUCCESS;
  361. }
  362. void TbeOpTask::GetIoAddr(uintptr_t *&arg_base, size_t &arg_count) {
  363. arg_base = reinterpret_cast<uintptr_t *>(args_.get());
  364. arg_count = arg_size_ / sizeof(void *);
  365. if (tiling_buffer_ != nullptr) {
  366. --arg_count;
  367. }
  368. }
  369. AiCpuBaseTask::~AiCpuBaseTask() {
  370. if (ext_info_addr_dev_ != nullptr) {
  371. (void)rtFree(ext_info_addr_dev_);
  372. }
  373. FreeHbm(copy_ioaddr_dev_);
  374. FreeHbm(copy_input_release_flag_dev_);
  375. FreeHbm(copy_input_data_size_dev_);
  376. FreeHbm(copy_input_src_dev_);
  377. FreeHbm(copy_input_dst_dev_);
  378. for (auto summary : output_summary_) {
  379. FreeHbm(summary);
  380. }
  381. for (auto out_shape : out_shape_hbm_) {
  382. FreeHbm(out_shape);
  383. }
  384. }
  385. Status AiCpuBaseTask::SetExtInfoAndType(const std::string &kernel_ext_info, uint64_t kernel_id) {
  386. if (kernel_ext_info.empty()) {
  387. GELOGI("Kernel_ext_info is empty, no need copy to device.");
  388. return SUCCESS;
  389. }
  390. int32_t unknown_shape_type_val = 0;
  391. (void) AttrUtils::GetInt(op_desc_, ::ge::ATTR_NAME_UNKNOWN_SHAPE_TYPE, unknown_shape_type_val);
  392. GELOGD("Get unknown_type is %d.", unknown_shape_type_val);
  393. unknown_type_ = static_cast<UnknowShapeOpType>(unknown_shape_type_val);
  394. aicpu_ext_handle_.reset(new(std::nothrow) ::ge::hybrid::AicpuExtInfoHandler(op_desc_->GetName(),
  395. num_inputs_,
  396. num_outputs_,
  397. unknown_type_));
  398. GE_CHK_BOOL_RET_STATUS(aicpu_ext_handle_ != nullptr, ACL_ERROR_GE_MEMORY_ALLOCATION,
  399. "[Malloc][Memory] failed for aicpu_ext_handle!");
  400. Status ret = aicpu_ext_handle_->Parse(kernel_ext_info);
  401. if (ret != SUCCESS) {
  402. GELOGE(ret, "[Parse][Param:kernel_ext_info] failed, kernel_ext_info_size=%zu.", kernel_ext_info.size());
  403. REPORT_INNER_ERROR("E19999",
  404. "Parse Param:kernel_ext_info failed, kernel_ext_info_size=%zu.", kernel_ext_info.size());
  405. return ret;
  406. }
  407. GE_CHK_STATUS_RET(aicpu_ext_handle_->UpdateSessionInfo(ULLONG_MAX, kernel_id, false),
  408. "[Update][SessionInfo] failed.");
  409. GE_CHK_RT_RET(rtMalloc(&ext_info_addr_dev_, aicpu_ext_handle_->GetExtInfoLen(), RT_MEMORY_HBM));
  410. GE_CHK_RT_RET(rtMemcpy(ext_info_addr_dev_, aicpu_ext_handle_->GetExtInfoLen(),
  411. aicpu_ext_handle_->GetExtInfo(), aicpu_ext_handle_->GetExtInfoLen(),
  412. RT_MEMCPY_HOST_TO_DEVICE));
  413. return SUCCESS;
  414. }
  415. Status AiCpuBaseTask::SetInputConst() {
  416. input_is_const_.clear();
  417. const vector<bool> v_is_input_const = op_desc_->GetIsInputConst();
  418. for (size_t i = 0; i < op_desc_->GetAllInputsSize(); ++i) {
  419. const GeTensorDescPtr tensor_desc = op_desc_->MutableInputDesc(static_cast<uint32_t>(i));
  420. if (tensor_desc == nullptr) {
  421. GELOGD("SingleOp: %s, Index: %zu, has no input", op_desc_->GetName().c_str(), i);
  422. continue;
  423. }
  424. if (i < v_is_input_const.size() && v_is_input_const[i]) {
  425. GELOGD("SingleOp: %s, Index: %zu, input is const", op_desc_->GetName().c_str(), i);
  426. input_is_const_.push_back(true);
  427. continue;
  428. }
  429. input_is_const_.push_back(false);
  430. }
  431. return SUCCESS;
  432. }
  433. Status AiCpuBaseTask::UpdateExtInfo(const std::vector<GeTensorDesc> &input_desc,
  434. std::vector<GeTensorDesc> &output_desc,
  435. rtStream_t stream) {
  436. GELOGI("Update ext info begin, unknown_type=%d.", unknown_type_);
  437. GE_CHECK_NOTNULL(aicpu_ext_handle_);
  438. GE_CHK_STATUS_RET(aicpu_ext_handle_->UpdateExecuteMode(false), "[Update][ExecuteMode] failed.");
  439. if (num_inputs_ == 0 && num_outputs_ == 0) {
  440. GELOGI("No input and output, no need update ext info.");
  441. return SUCCESS;
  442. }
  443. size_t non_const_index = 0;
  444. for (size_t input_index = 0; input_index < num_inputs_; input_index++) {
  445. if (input_index < input_is_const_.size() && input_is_const_[input_index]) {
  446. // get input_desc from op_desc if const input, num_inputs_ is op_desc_ input_size
  447. auto const_input_desc = op_desc_->MutableInputDesc(static_cast<uint32_t>(input_index));
  448. GE_CHECK_NOTNULL(const_input_desc);
  449. GE_CHK_STATUS_RET(aicpu_ext_handle_->UpdateInputShapeAndType(input_index, *const_input_desc),
  450. "[Update][InputShapeAndType] failed, input_index:%zu.", input_index);
  451. continue;
  452. }
  453. GE_CHK_BOOL_RET_STATUS(non_const_index < input_desc.size(), ACL_ERROR_GE_PARAM_INVALID,
  454. "[Check][Size]Input_desc size is %zu, but get non_const_index is %zu", input_desc.size(), non_const_index);
  455. GE_CHK_STATUS_RET(aicpu_ext_handle_->UpdateInputShapeAndType(input_index, input_desc[non_const_index]),
  456. "[Update][InputShapeAndType]failed, input_index:%zu.", input_index);
  457. if (DumpManager::GetInstance().GetDumpProperties(kInferSessionId).IsSingleOpNeedDump()) {
  458. GE_CHK_STATUS_RET(op_desc_->UpdateInputDesc(input_index, input_desc[non_const_index]),
  459. "AiCpuTask Update [%zu]th input desc failed.",input_index);
  460. }
  461. non_const_index++;
  462. }
  463. if (unknown_type_ != DEPEND_COMPUTE) {
  464. for (size_t j = 0; j < num_outputs_; ++j) {
  465. GE_CHK_STATUS_RET(aicpu_ext_handle_->UpdateOutputShapeAndType(j, output_desc[j]),
  466. "[Update][OutputShapeAndType] failed, Output:%zu.", j);
  467. if (DumpManager::GetInstance().GetDumpProperties(kInferSessionId).IsSingleOpNeedDump()) {
  468. GE_CHK_STATUS_RET(op_desc_->UpdateOutputDesc(j, output_desc[j]),
  469. "AiCpuTask Update [%zu]th output desc failed.",j);
  470. }
  471. }
  472. }
  473. GE_CHK_RT_RET(rtMemcpyAsync(ext_info_addr_dev_,
  474. aicpu_ext_handle_->GetExtInfoLen(), // check size
  475. aicpu_ext_handle_->GetExtInfo(),
  476. aicpu_ext_handle_->GetExtInfoLen(),
  477. RT_MEMCPY_HOST_TO_DEVICE_EX,
  478. stream));
  479. GELOGI("Update ext info end.");
  480. return SUCCESS;
  481. }
  482. Status AiCpuBaseTask::UpdateOutputShape(vector<GeTensorDesc> &output_desc) {
  483. if (num_outputs_ == 0) {
  484. GELOGD("AiCpuBaseTask output_num is 0, no need update output shape.");
  485. return SUCCESS;
  486. }
  487. GELOGD("Start to update DEPEND_SHAPE_RANGE AiCpuBaseTask outputshape.");
  488. GE_CHK_RT_RET(rtMemcpy(aicpu_ext_handle_->GetExtInfo(), aicpu_ext_handle_->GetExtInfoLen(), ext_info_addr_dev_,
  489. aicpu_ext_handle_->GetExtInfoLen(), RT_MEMCPY_DEVICE_TO_HOST));
  490. for (size_t i = 0; i < num_outputs_; ++i) {
  491. GeShape shape;
  492. DataType data_type;
  493. aicpu_ext_handle_->GetOutputShapeAndType(i, shape, data_type);
  494. GE_CHK_STATUS_RET(UpdateShapeToOutputDesc(shape, output_desc[i]),
  495. "[Update][ShapeToOutputDesc] failed, output:%zu.", i);
  496. if (DumpManager::GetInstance().GetDumpProperties(kInferSessionId).IsSingleOpNeedDump()) {
  497. GE_CHK_STATUS_RET(op_desc_->UpdateOutputDesc(i, output_desc[i]), "[Update][OutputDesc] failed, output:%zu.", i);
  498. }
  499. }
  500. GELOGD("Update DEPEND_SHAPE_RANGE AiCpuBaseTask outputshape finished.");
  501. return SUCCESS;
  502. }
  503. Status AiCpuBaseTask::UpdateShapeToOutputDesc(const GeShape &shape_new, GeTensorDesc &output_desc) {
  504. auto shape_old = output_desc.GetShape();
  505. output_desc.SetShape(shape_new);
  506. GELOGD("Update AiCpuBaseTask shape from %s to %s", shape_old.ToString().c_str(), shape_new.ToString().c_str());
  507. auto origin_shape_old = output_desc.GetOriginShape();
  508. auto origin_format = output_desc.GetOriginFormat();
  509. auto format = output_desc.GetFormat();
  510. if (origin_format == format) {
  511. output_desc.SetOriginShape(shape_new);
  512. return SUCCESS;
  513. }
  514. std::vector<int64_t> origin_dims_new;
  515. auto trans_ret = formats::TransShape(format, shape_new.GetDims(),
  516. output_desc.GetDataType(), origin_format, origin_dims_new);
  517. GE_CHK_STATUS_RET(trans_ret,
  518. "[Trans][Shape] failed, AiCpuTask originFormat[%d] is not same as format[%d], shape=%s.",
  519. origin_format, format, shape_new.ToString().c_str());
  520. auto origin_shape_new = GeShape(origin_dims_new);
  521. output_desc.SetOriginShape(origin_shape_new);
  522. GELOGD("AiCpuTask originFormat[%d] is not same as format[%d], need update from %s ro %s.",
  523. origin_format, format, origin_shape_old.ToString().c_str(), origin_shape_new.ToString().c_str());
  524. return SUCCESS;
  525. }
  526. Status AiCpuBaseTask::UpdateIoAddr(const vector<DataBuffer> &inputs, const vector<DataBuffer> &outputs) {
  527. uintptr_t *arg_base = nullptr;
  528. size_t arg_num = 0;
  529. GetIoAddr(arg_base, arg_num);
  530. // input number and output number was check in ValidateParams
  531. size_t non_const_index = 0;
  532. for (size_t input_index = 0; input_index < num_inputs_; input_index++) {
  533. if (input_index < input_is_const_.size() && input_is_const_[input_index]) {
  534. // const input no need update addr
  535. GE_CHECK_NOTNULL(arg_base);
  536. GELOGD("AICpuTask input[%zu] addr = %lu", input_index, *arg_base);
  537. arg_base++;
  538. continue;
  539. }
  540. GE_CHK_BOOL_RET_STATUS(non_const_index < inputs.size(), ACL_ERROR_GE_PARAM_INVALID,
  541. "[Check][Size] Input size is %zu, but get non_const_index is %zu", inputs.size(), non_const_index);
  542. auto addr = inputs[non_const_index].data;
  543. GE_CHECK_NOTNULL(addr);
  544. GELOGD("AICpuTask input[%zu] addr = %p", input_index, addr);
  545. *arg_base++ = reinterpret_cast<uintptr_t>(addr);
  546. non_const_index++;
  547. }
  548. for (size_t i = 0; i < outputs.size(); ++i) {
  549. auto addr = outputs[i].data;
  550. GE_CHECK_NOTNULL(addr);
  551. GELOGD("AICpuTask output[%zu] addr = %p", i, addr);
  552. *arg_base++ = reinterpret_cast<uintptr_t>(addr);
  553. }
  554. return SUCCESS;
  555. }
  556. AiCpuTask::~AiCpuTask() {
  557. FreeHbm(args_);
  558. FreeHbm(io_addr_);
  559. FreeHbm(workspace_addr_);
  560. FreeHbm(copy_workspace_buf_);
  561. FreeHbm(copy_task_args_buf_);
  562. }
  563. Status AiCpuTask::LaunchKernel(rtStream_t stream) {
  564. GELOGD("Start to launch kernel. task = %s", this->op_type_.c_str());
  565. auto ret = rtMemcpyAsync(io_addr_,
  566. io_addr_size_,
  567. io_addr_host_.data(),
  568. io_addr_host_.size() * sizeof(void *),
  569. RT_MEMCPY_HOST_TO_DEVICE_EX,
  570. stream);
  571. if (ret != RT_ERROR_NONE) {
  572. GELOGE(ret, "[MemcpyAsync][Date] failed. ret = %d, task = %s", ret, this->op_type_.c_str());
  573. REPORT_CALL_ERROR("E19999", "rtMemcpyAsync data failed, ret = %d, task = %s", ret, this->op_type_.c_str());
  574. return RT_ERROR_TO_GE_STATUS(ret);
  575. }
  576. GELOGI("To invoke rtKernelLaunchEx. task = %s", this->op_type_.c_str());
  577. ret = rtKernelLaunchEx(args_, arg_size_, 0, stream);
  578. if (ret != RT_ERROR_NONE) {
  579. GELOGE(ret, "[Invoke][rtKernelLaunch] failed. ret = %d, task = %s", ret, this->op_type_.c_str());
  580. REPORT_CALL_ERROR("E19999", "invoke rtKernelLaunchEx failed, ret = %d, task = %s", ret, this->op_type_.c_str());
  581. return RT_ERROR_TO_GE_STATUS(ret);
  582. }
  583. GELOGI("[TASK_INFO] %lu/%s", kernel_id_, op_type_.c_str());
  584. GELOGD("Done launch kernel successfully. task = %s", this->op_type_.c_str());
  585. return SUCCESS;
  586. }
  587. Status AiCpuBaseTask::PrepareCopyInputs(vector<DataBuffer> &outputs) {
  588. std::vector<uint64_t> copy_input_release_flag;
  589. std::vector<uint64_t> copy_input_data_size;
  590. std::vector<uint64_t> copy_input_src;
  591. std::vector<uint64_t> copy_input_dst;
  592. for (size_t i = 0; i < num_outputs_; ++i) {
  593. const auto &summary = output_summary_host_[i];
  594. GELOGI("Node out[%zu] summary, shape data=0x%lx, shape data size=%lu, raw data=0x%lx, raw data size=%lu.",
  595. i, summary.shape_data_ptr, summary.shape_data_size,
  596. summary.raw_data_ptr, summary.raw_data_size);
  597. auto output = outputs[i];
  598. copy_input_release_flag.emplace_back(kReleaseFlag);
  599. if (summary.raw_data_size > 0) {
  600. copy_input_data_size.emplace_back(output.length);
  601. } else {
  602. copy_input_data_size.emplace_back(summary.raw_data_size);
  603. }
  604. copy_input_src.emplace_back(summary.raw_data_ptr);
  605. copy_input_dst.emplace_back(reinterpret_cast<uintptr_t>(output.data));
  606. const auto &shape_buffer = out_shape_hbm_[i];
  607. copy_input_release_flag.emplace_back(kReleaseFlag);
  608. copy_input_data_size.emplace_back(summary.shape_data_size);
  609. copy_input_src.emplace_back(summary.shape_data_ptr);
  610. copy_input_dst.emplace_back(reinterpret_cast<uintptr_t>(shape_buffer));
  611. }
  612. const size_t copy_input_buf_len = num_outputs_ * kCopyNum * sizeof(uint64_t);
  613. GE_CHK_RT_RET(rtMemcpy(copy_input_release_flag_dev_, copy_input_buf_len,
  614. copy_input_release_flag.data(), copy_input_buf_len, RT_MEMCPY_HOST_TO_DEVICE));
  615. GE_CHK_RT_RET(rtMemcpy(copy_input_data_size_dev_, copy_input_buf_len,
  616. copy_input_data_size.data(), copy_input_buf_len, RT_MEMCPY_HOST_TO_DEVICE));
  617. GE_CHK_RT_RET(rtMemcpy(copy_input_src_dev_, copy_input_buf_len,
  618. copy_input_src.data(), copy_input_buf_len, RT_MEMCPY_HOST_TO_DEVICE));
  619. GE_CHK_RT_RET(rtMemcpy(copy_input_dst_dev_, copy_input_buf_len,
  620. copy_input_dst.data(), copy_input_buf_len, RT_MEMCPY_HOST_TO_DEVICE));
  621. return SUCCESS;
  622. }
  623. Status AiCpuBaseTask::ReadResultSummaryAndPrepareMemory() {
  624. for (size_t i = 0; i < num_outputs_; ++i) {
  625. auto &result_summary = output_summary_host_[i];
  626. GE_CHK_RT_RET(rtMemcpy(&result_summary, sizeof(aicpu::FWKAdapter::ResultSummary),
  627. output_summary_[i], sizeof(aicpu::FWKAdapter::ResultSummary),
  628. RT_MEMCPY_DEVICE_TO_HOST));
  629. auto shape_data_size = result_summary.shape_data_size;
  630. void *shape_buffer = nullptr;
  631. if (shape_data_size > 0) {
  632. GE_CHK_RT_RET(rtMalloc(&shape_buffer, shape_data_size, RT_MEMORY_HBM));
  633. }
  634. out_shape_hbm_.emplace_back(shape_buffer);
  635. }
  636. return SUCCESS;
  637. }
  638. Status AiCpuTask::CopyDataToHbm(vector<DataBuffer> &outputs,
  639. rtStream_t stream) {
  640. GE_CHK_STATUS_RET_NOLOG(PrepareCopyInputs(outputs));
  641. GE_CHK_RT_RET(rtKernelLaunchEx(copy_task_args_buf_, sizeof(STR_FWK_OP_KERNEL),
  642. RT_KERNEL_DEFAULT, stream));
  643. GE_CHK_RT_RET(rtStreamSynchronize(stream));
  644. return SUCCESS;
  645. }
  646. Status AiCpuCCTask::CopyDataToHbm(vector<DataBuffer> &outputs,
  647. rtStream_t stream) {
  648. GE_CHK_STATUS_RET_NOLOG(PrepareCopyInputs(outputs));
  649. auto ret = rtCpuKernelLaunchWithFlag(static_cast<const void *>(memcpy_so_name_.data()),
  650. static_cast<const void *>(memcpy_kernel_name_.data()),
  651. block_dim_, memcpy_args_.get(), static_cast<uint32_t>(memcpy_arg_size_),
  652. nullptr, stream, dump_flag_);
  653. GE_CHK_RT_RET(ret);
  654. GE_CHK_RT_RET(rtStreamSynchronize(stream));
  655. return SUCCESS;
  656. }
  657. Status AiCpuBaseTask::UpdateShapeByHbmBuffer(vector<GeTensorDesc> &output_desc) {
  658. for (size_t i = 0; i < num_outputs_; ++i) {
  659. const auto &result_summary = output_summary_host_[i];
  660. std::vector<int64_t> shape_dims;
  661. if (result_summary.shape_data_size > 0) {
  662. const auto &shape_hbm = out_shape_hbm_[i];
  663. uint32_t dim_num = result_summary.shape_data_size / sizeof(int64_t);
  664. std::unique_ptr<int64_t[]> shape_addr(new (std::nothrow) int64_t[dim_num]());
  665. GE_CHECK_NOTNULL(shape_addr);
  666. GE_CHK_RT_RET(rtMemcpy(shape_addr.get(), result_summary.shape_data_size, shape_hbm,
  667. result_summary.shape_data_size, RT_MEMCPY_DEVICE_TO_HOST));
  668. for (uint32_t dim_idx = 0; dim_idx < dim_num; ++dim_idx) {
  669. shape_dims.emplace_back(shape_addr[dim_idx]);
  670. GELOGD("Node [%zu]th output dim[%u]=%ld.", i, dim_idx, shape_addr[dim_idx]);
  671. }
  672. }
  673. GE_CHK_STATUS_RET(UpdateShapeToOutputDesc(GeShape(shape_dims), output_desc[i]),
  674. "[Update][ShapeToOutputDesc] failed , output:%zu.", i);
  675. if (DumpManager::GetInstance().GetDumpProperties(kInferSessionId).IsSingleOpNeedDump()) {
  676. GE_CHK_STATUS_RET(op_desc_->UpdateOutputDesc(i, output_desc[i]), "[Update][OutputDesc] failed, output:%zu.", i);
  677. }
  678. }
  679. return SUCCESS;
  680. }
  681. Status AiCpuBaseTask::UpdateShapeAndDataByResultSummary(vector<GeTensorDesc> &output_desc,
  682. vector<DataBuffer> &outputs,
  683. rtStream_t stream) {
  684. if (num_outputs_ == 0) {
  685. GELOGI("Output num is 0, there is no need to update the output and size.");
  686. return SUCCESS;
  687. }
  688. GELOGI("Update shape and data by result summary begin.");
  689. for (auto out_shape : out_shape_hbm_) {
  690. FreeHbm(out_shape);
  691. }
  692. out_shape_hbm_.clear();
  693. GE_CHK_STATUS_RET(ReadResultSummaryAndPrepareMemory(),
  694. "[Read][ResultSummaryAndPrepareMemory] failed.");
  695. GE_CHK_STATUS_RET(CopyDataToHbm(outputs, stream),
  696. "[Copy][DataToHbm] failed.");
  697. GE_CHK_STATUS_RET(UpdateShapeByHbmBuffer(output_desc),
  698. "[Update][ShapeByHbmBuffer] failed.");
  699. for (auto out_shape : out_shape_hbm_) {
  700. FreeHbm(out_shape);
  701. }
  702. out_shape_hbm_.clear();
  703. GELOGI("Update shape and data by result summary end.");
  704. return SUCCESS;
  705. }
  706. Status AiCpuTask::InitForSummaryAndCopy() {
  707. if (unknown_type_ != DEPEND_COMPUTE || num_outputs_ == 0) {
  708. GELOGI("Unknown_type is %d, output num is %zu.", unknown_type_, num_outputs_);
  709. return SUCCESS;
  710. }
  711. output_summary_.resize(num_outputs_);
  712. constexpr auto result_summary_size = sizeof(aicpu::FWKAdapter::ResultSummary);
  713. for (size_t i = 0; i < num_outputs_; ++i) {
  714. GE_CHK_RT_RET(rtMalloc(&output_summary_[i], result_summary_size, RT_MEMORY_HBM));
  715. }
  716. output_summary_host_.resize(num_outputs_);
  717. const size_t copy_input_buf_len = num_outputs_ * kCopyNum * sizeof(uint64_t);
  718. GE_CHK_RT_RET(rtMalloc(&copy_input_release_flag_dev_, copy_input_buf_len, RT_MEMORY_HBM));
  719. GE_CHK_RT_RET(rtMalloc(&copy_input_data_size_dev_, copy_input_buf_len, RT_MEMORY_HBM));
  720. GE_CHK_RT_RET(rtMalloc(&copy_input_src_dev_, copy_input_buf_len, RT_MEMORY_HBM));
  721. GE_CHK_RT_RET(rtMalloc(&copy_input_dst_dev_, copy_input_buf_len, RT_MEMORY_HBM));
  722. GE_CHK_RT_RET(rtMalloc(&copy_task_args_buf_, sizeof(STR_FWK_OP_KERNEL), RT_MEMORY_HBM));
  723. std::vector<uint64_t> copy_io_addr;
  724. copy_io_addr.emplace_back(reinterpret_cast<uintptr_t>(copy_input_release_flag_dev_));
  725. copy_io_addr.emplace_back(reinterpret_cast<uintptr_t>(copy_input_data_size_dev_));
  726. copy_io_addr.emplace_back(reinterpret_cast<uintptr_t>(copy_input_src_dev_));
  727. copy_io_addr.emplace_back(reinterpret_cast<uintptr_t>(copy_input_dst_dev_));
  728. const auto copy_io_addr_size = sizeof(uint64_t) * copy_io_addr.size();
  729. GE_CHK_RT_RET(rtMalloc(&copy_ioaddr_dev_, copy_io_addr_size, RT_MEMORY_HBM));
  730. GE_CHK_RT_RET(rtMemcpy(copy_ioaddr_dev_, copy_io_addr_size,
  731. copy_io_addr.data(), copy_io_addr_size, RT_MEMCPY_HOST_TO_DEVICE));
  732. return SUCCESS;
  733. }
  734. Status AiCpuTask::SetMemCopyTask(const domi::KernelExDef &kernel_def) {
  735. if (kernel_def.args_size() > sizeof(STR_FWK_OP_KERNEL)) {
  736. GELOGE(ACL_ERROR_GE_PARAM_INVALID, "[Check][Size]sizeof STR_FWK_OP_KERNEL is: %lu, but args_size is: %d",
  737. sizeof(STR_FWK_OP_KERNEL), kernel_def.args_size());
  738. REPORT_INNER_ERROR("E19999", "[sizeof STR_FWK_OP_KERNEL is: %lu, but args_size is: %d",
  739. sizeof(STR_FWK_OP_KERNEL), kernel_def.args_size());
  740. return ACL_ERROR_GE_PARAM_INVALID;
  741. }
  742. GE_CHK_RT_RET(rtMalloc(&copy_workspace_buf_, kernel_def.task_info_size(), RT_MEMORY_HBM));
  743. GE_CHK_RT_RET(rtMemcpy(copy_workspace_buf_, kernel_def.task_info_size(),
  744. kernel_def.task_info().data(), kernel_def.task_info_size(), RT_MEMCPY_HOST_TO_DEVICE));
  745. STR_FWK_OP_KERNEL aicpu_task = {0};
  746. auto sec_ret = memcpy_s(&aicpu_task, sizeof(STR_FWK_OP_KERNEL),
  747. kernel_def.args().data(), kernel_def.args().size());
  748. if (sec_ret != EOK) {
  749. GELOGE(ACL_ERROR_GE_MEMORY_OPERATE_FAILED, "[Update][TaskArgs] failed, ret: %d", sec_ret);
  750. REPORT_INNER_ERROR("E19999", "update STR_FWK_OP_KERNEL args failed because memcpy_s return %d.", sec_ret);
  751. return ACL_ERROR_GE_MEMORY_OPERATE_FAILED;
  752. }
  753. aicpu_task.fwkKernelBase.fwk_kernel.inputOutputAddr = reinterpret_cast<uintptr_t>(copy_ioaddr_dev_);
  754. aicpu_task.fwkKernelBase.fwk_kernel.workspaceBaseAddr = reinterpret_cast<uintptr_t>(copy_workspace_buf_);
  755. aicpu_task.fwkKernelBase.fwk_kernel.extInfoAddr = 0;
  756. aicpu_task.fwkKernelBase.fwk_kernel.extInfoLen = 0;
  757. GE_CHK_RT_RET(rtMemcpy(copy_task_args_buf_, sizeof(STR_FWK_OP_KERNEL),
  758. &aicpu_task, sizeof(STR_FWK_OP_KERNEL), RT_MEMCPY_HOST_TO_DEVICE));
  759. return SUCCESS;
  760. }
  761. Status AiCpuTask::LaunchKernel(const std::vector<GeTensorDesc> &input_desc,
  762. const std::vector<DataBuffer> &input_buffers,
  763. std::vector<GeTensorDesc> &output_desc,
  764. std::vector<DataBuffer> &output_buffers,
  765. rtStream_t stream) {
  766. GE_CHK_STATUS_RET_NOLOG(UpdateExtInfo(input_desc, output_desc, stream));
  767. if (unknown_type_ == DEPEND_COMPUTE) {
  768. std::vector<DataBuffer> summary_buffers;
  769. for (size_t i = 0; i < num_outputs_; ++i) {
  770. summary_buffers.emplace_back(output_summary_[i], sizeof(aicpu::FWKAdapter::ResultSummary), false);
  771. }
  772. GE_CHK_STATUS_RET_NOLOG(UpdateIoAddr(input_buffers, summary_buffers));
  773. } else {
  774. GE_CHK_STATUS_RET_NOLOG(UpdateIoAddr(input_buffers, output_buffers));
  775. }
  776. GE_CHK_STATUS_RET_NOLOG(LaunchKernel(stream));
  777. if (unknown_type_ == DEPEND_SHAPE_RANGE) {
  778. GE_CHK_RT_RET(rtStreamSynchronize(stream));
  779. GE_CHK_STATUS_RET_NOLOG(UpdateOutputShape(output_desc));
  780. } else if (unknown_type_ == DEPEND_COMPUTE) {
  781. GE_CHK_RT_RET(rtStreamSynchronize(stream));
  782. GE_CHK_STATUS_RET_NOLOG(UpdateShapeAndDataByResultSummary(output_desc, output_buffers, stream));
  783. }
  784. return SUCCESS;
  785. }
  786. Status AiCpuBaseTask::UpdateArgTable(const SingleOpModelParam &param) {
  787. // aicpu do not have workspace, for now
  788. return DoUpdateArgTable(param, false);
  789. }
  790. const std::string &AiCpuBaseTask::GetTaskType() const { return kTaskTypeAicpu; }
  791. void AiCpuTask::GetIoAddr(uintptr_t *&arg_base, size_t &arg_count) {
  792. arg_base = reinterpret_cast<uintptr_t *>(io_addr_host_.data());
  793. arg_count = io_addr_host_.size();
  794. }
  795. void AiCpuCCTask::SetKernelArgs(std::unique_ptr<uint8_t[]> args, size_t arg_size) {
  796. args_ = std::move(args);
  797. arg_size_ = arg_size;
  798. // The blockdim value is defult "1" for rtCpuKernelLaunch
  799. block_dim_ = 1;
  800. }
  801. void AiCpuCCTask::SetSoName(const std::string &so_name) { so_name_ = so_name; }
  802. void AiCpuCCTask::SetkernelName(const std::string &kernel_Name) { kernel_name_ = kernel_Name; }
  803. void AiCpuCCTask::SetIoAddr(uintptr_t *io_addr) { io_addr_ = io_addr; }
  804. const void *AiCpuCCTask::GetArgs() const { return args_.get(); }
  805. size_t AiCpuCCTask::GetArgSize() const { return arg_size_; }
  806. AiCpuCCTask::~AiCpuCCTask() {
  807. }
  808. Status AiCpuCCTask::LaunchKernel(rtStream_t stream) {
  809. GELOGI("To invoke rtCpuKernelLaunch. block_dim = %u, so_name is %s, kernel_name is %s", block_dim_, so_name_.data(),
  810. kernel_name_.data());
  811. // sm_desc is nullptr, because l2 buffer does not support
  812. auto *sm_desc = reinterpret_cast<rtSmDesc_t *>(sm_desc_);
  813. auto ret = rtCpuKernelLaunchWithFlag(static_cast<const void *>(so_name_.data()),
  814. static_cast<const void *>(kernel_name_.data()),
  815. block_dim_, args_.get(), static_cast<uint32_t>(arg_size_),
  816. sm_desc, stream, dump_flag_);
  817. if (ret != RT_ERROR_NONE) {
  818. GELOGE(ret, "[Invoke][rtCpuKernelLaunchWithFlag] failed. ret = %d.", ret);
  819. REPORT_CALL_ERROR("E19999", "invoke rtCpuKernelLaunchWithFlag failed, ret:%d.", ret);
  820. return RT_ERROR_TO_GE_STATUS(ret);
  821. }
  822. GELOGI("[TASK_INFO] %lu/%s", kernel_id_, op_type_.c_str());
  823. GELOGD("Invoke rtCpuKernelLaunch succeeded");
  824. return SUCCESS;
  825. }
  826. Status AiCpuCCTask::LaunchKernel(const std::vector<GeTensorDesc> &input_desc,
  827. const std::vector<DataBuffer> &input_buffers,
  828. std::vector<GeTensorDesc> &output_desc,
  829. std::vector<DataBuffer> &output_buffers,
  830. rtStream_t stream) {
  831. GE_CHK_STATUS_RET_NOLOG(UpdateExtInfo(input_desc, output_desc, stream));
  832. GE_CHK_STATUS_RET_NOLOG(UpdateIoAddr(input_buffers, output_buffers));
  833. GE_CHK_STATUS_RET_NOLOG(LaunchKernel(stream));
  834. if (unknown_type_ == DEPEND_SHAPE_RANGE) {
  835. GE_CHK_RT_RET(rtStreamSynchronize(stream));
  836. GE_CHK_STATUS_RET_NOLOG(UpdateOutputShape(output_desc));
  837. } else if (unknown_type_ == DEPEND_COMPUTE) {
  838. GE_CHK_RT_RET(rtStreamSynchronize(stream));
  839. GE_CHK_STATUS_RET_NOLOG(UpdateShapeAndDataByResultSummary(output_desc, output_buffers, stream));
  840. }
  841. return SUCCESS;
  842. }
  843. Status AiCpuCCTask::InitForSummaryAndCopy() {
  844. if (unknown_type_ != DEPEND_COMPUTE || num_outputs_ == 0) {
  845. GELOGI("Unknown_type is %d, output num is %zu.", unknown_type_, num_outputs_);
  846. return SUCCESS;
  847. }
  848. output_summary_.resize(num_outputs_);
  849. constexpr auto result_summary_size = sizeof(aicpu::FWKAdapter::ResultSummary);
  850. for (size_t i = 0; i < num_outputs_; ++i) {
  851. GE_CHK_RT_RET(rtMalloc(&output_summary_[i], result_summary_size, RT_MEMORY_HBM));
  852. }
  853. output_summary_host_.resize(num_outputs_);
  854. const size_t copy_input_buf_len = num_outputs_ * kCopyNum * sizeof(uint64_t);
  855. GE_CHK_RT_RET(rtMalloc(&copy_input_release_flag_dev_, copy_input_buf_len, RT_MEMORY_HBM));
  856. GE_CHK_RT_RET(rtMalloc(&copy_input_data_size_dev_, copy_input_buf_len, RT_MEMORY_HBM));
  857. GE_CHK_RT_RET(rtMalloc(&copy_input_src_dev_, copy_input_buf_len, RT_MEMORY_HBM));
  858. GE_CHK_RT_RET(rtMalloc(&copy_input_dst_dev_, copy_input_buf_len, RT_MEMORY_HBM));
  859. GE_CHK_RT_RET(rtMalloc(&copy_task_args_buf_, sizeof(STR_FWK_OP_KERNEL), RT_MEMORY_HBM));
  860. std::vector<uint64_t> copy_io_addr;
  861. copy_io_addr.emplace_back(reinterpret_cast<uintptr_t>(copy_input_release_flag_dev_));
  862. copy_io_addr.emplace_back(reinterpret_cast<uintptr_t>(copy_input_data_size_dev_));
  863. copy_io_addr.emplace_back(reinterpret_cast<uintptr_t>(copy_input_src_dev_));
  864. copy_io_addr.emplace_back(reinterpret_cast<uintptr_t>(copy_input_dst_dev_));
  865. const auto copy_io_addr_size = sizeof(uint64_t) * copy_io_addr.size();
  866. GE_CHK_RT_RET(rtMalloc(&copy_ioaddr_dev_, copy_io_addr_size, RT_MEMORY_HBM));
  867. GE_CHK_RT_RET(rtMemcpy(copy_ioaddr_dev_, copy_io_addr_size,
  868. copy_io_addr.data(), copy_io_addr_size, RT_MEMCPY_HOST_TO_DEVICE));
  869. return SUCCESS;
  870. }
  871. Status AiCpuCCTask::SetMemCopyTask(const domi::KernelDef &kernel_def) {
  872. auto &memcpy_args = kernel_def.args();
  873. memcpy_args_size_ = kernel_def.args_size();
  874. memcpy_so_name_ = kernel_def.so_name();
  875. memcpy_kernel_name_ = kernel_def.kernel_name();
  876. GE_IF_BOOL_EXEC(memcpy_args.size() != memcpy_args_size_,
  877. REPORT_INNER_ERROR("E19999", "MemCopy task def args.size=%zu, but args_size=%u not equal.",
  878. memcpy_args.size(), memcpy_args_size_);
  879. GELOGE(FAILED, "[Check][Size]MemCopy task def args.size=%zu, but args_size=%u not equal.",
  880. memcpy_args.size(), memcpy_args_size_);
  881. return FAILED;);
  882. GE_IF_BOOL_EXEC(memcpy_args_size_ < sizeof(aicpu::AicpuParamHead),
  883. REPORT_INNER_ERROR("E19999",
  884. "Task def args_size=%u is less than aicpu param head len=%zu.",
  885. memcpy_args_size_, sizeof(aicpu::AicpuParamHead));
  886. GELOGE(FAILED,
  887. "[Check][Size] Task def args_size=%u is less than aicpu param head len=%zu.",
  888. memcpy_args_size_, sizeof(aicpu::AicpuParamHead));
  889. return FAILED;);
  890. memcpy_args_.reset(new(std::nothrow) uint8_t[memcpy_args_size_]());
  891. GE_IF_BOOL_EXEC(memcpy_args_ == nullptr,
  892. REPORT_INNER_ERROR("E19999", "new memory failed for Node[MemCopy], task_size[%u].",
  893. memcpy_args_size_);
  894. GELOGE(FAILED, "[Malloc][Memory] failed for Node[MemCopy], task_size[%u].",
  895. memcpy_args_size_);
  896. return FAILED;);
  897. errno_t sec_ret = memcpy_s(memcpy_args_.get(), memcpy_args_size_, memcpy_args.c_str(), memcpy_args.size());
  898. GE_IF_BOOL_EXEC(sec_ret != EOK,
  899. REPORT_INNER_ERROR("E19999",
  900. "memcpy_s argc_ failed for Node[MemCopy], ret: %d", sec_ret);
  901. GELOGE(INTERNAL_ERROR,
  902. "[Update][args] failed for Node[MemCopy], ret: %d", sec_ret);
  903. return sec_ret;);
  904. auto memcpy_param_head = reinterpret_cast<aicpu::AicpuParamHead *>(memcpy_args_.get());
  905. uint32_t memcpy_io_num = memcpy_param_head->ioAddrNum;
  906. auto memcpy_io_addr = memcpy_args_.get() + sizeof(aicpu::AicpuParamHead);
  907. // if has input and output, need copy to ioaddr
  908. int cpy_ret = memcpy_s(memcpy_io_addr, memcpy_args_size_ - sizeof(aicpu::AicpuParamHead),
  909. &copy_ioaddr_dev_, sizeof(uint64_t) * memcpy_io_num);
  910. GE_IF_BOOL_EXEC(cpy_ret != 0,
  911. REPORT_INNER_ERROR("E19999", "Node[Memcpoy] memcpy io addr to AicpuParamHead failed,"
  912. "ret=%d, args_size=%u, io nums=%u.",
  913. cpy_ret, memcpy_args_size_, memcpy_io_num);
  914. GELOGE(INTERNAL_ERROR, "[Update][io_addr]Node[MemCopy] memcpy io addr to AicpuParamHead failed,"
  915. "ret=%d, args_size=%u, io nums=%u.",
  916. cpy_ret, memcpy_args_size_, memcpy_io_num);
  917. return INTERNAL_ERROR;);
  918. GELOGD("Set memcpy task for node[MemCopy] successfully.");
  919. return SUCCESS;
  920. }
  921. void AiCpuCCTask::GetIoAddr(uintptr_t *&arg_base, size_t &arg_count) {
  922. arg_base = io_addr_;
  923. arg_count = io_addr_num_;
  924. }
  925. Status MemcpyAsyncTask::LaunchKernel(rtStream_t stream) {
  926. auto src_addr = reinterpret_cast<void *>(addresses_[0]);
  927. auto dst_addr = reinterpret_cast<void *>(addresses_[1]);
  928. kind_ = (kind_ == RT_MEMCPY_ADDR_DEVICE_TO_DEVICE) ? RT_MEMCPY_DEVICE_TO_DEVICE : kind_;
  929. GE_CHK_RT_RET(rtMemcpyAsync(dst_addr, dst_max_, src_addr, count_, kind_, stream));
  930. return SUCCESS;
  931. }
  932. void MemcpyAsyncTask::GetIoAddr(uintptr_t *&arg_base, size_t &arg_count) {
  933. arg_base = addresses_;
  934. arg_count = kMemcpyArgCount;
  935. }
  936. } // namespace ge

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