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.

single_op.cc 13 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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/single_op.h"
  17. #include "common/fmk_types.h"
  18. #include "common/ge_types.h"
  19. #include "common/math/math_util.h"
  20. #include "common/profiling/profiling_manager.h"
  21. #include "framework/common/debug/ge_log.h"
  22. #include "framework/common/util.h"
  23. #include "graph/load/new_model_manager/model_utils.h"
  24. #include "runtime/mem.h"
  25. #include "single_op/single_op_manager.h"
  26. #include "graph/load/new_model_manager/model_manager.h"
  27. namespace ge {
  28. namespace {
  29. const size_t kDataMemAlignSize = 32;
  30. size_t GetAlignedSize(size_t size) {
  31. size_t aligned_size = (size + 2 * kDataMemAlignSize - 1) / kDataMemAlignSize * kDataMemAlignSize;
  32. return aligned_size;
  33. }
  34. Status ProfilingTaskInfo(OpTask *op_task) {
  35. if (!ProfilingManager::Instance().ProfilingModelExecuteOn()) {
  36. return SUCCESS;
  37. }
  38. string model_name;
  39. string op_name;
  40. uint32_t model_id;
  41. uint32_t block_dim;
  42. if (op_task->GetProfilingArgs(model_name, op_name, model_id, block_dim) != SUCCESS) {
  43. GELOGE(ACL_ERROR_GE_PARAM_INVALID, "Get profiling data of task failed");
  44. return ACL_ERROR_GE_PARAM_INVALID;
  45. }
  46. GELOGD("ProfilingReport of op[%s] model[%s] start.", op_name.c_str(), model_name.c_str());
  47. std::vector<TaskDescInfo> task_desc_info;
  48. uint32_t task_id = 0;
  49. uint32_t stream_id = 0;
  50. if (rtGetTaskIdAndStreamID(&task_id, &stream_id) != RT_ERROR_NONE) {
  51. GELOGE(ACL_ERROR_GE_PARAM_INVALID, "Get task_id and stream_id failed.");
  52. return ACL_ERROR_GE_PARAM_INVALID;
  53. }
  54. TaskDescInfo tmp_task_desc_info;
  55. tmp_task_desc_info.model_name = model_name;
  56. tmp_task_desc_info.op_name = op_name;
  57. tmp_task_desc_info.block_dim = block_dim;
  58. tmp_task_desc_info.task_id = task_id;
  59. tmp_task_desc_info.stream_id = stream_id;
  60. GELOGD("GetTaskDescInfo of op [%s] end, task_id[%u], stream_id[%u]", op_name.c_str(), task_id, stream_id);
  61. task_desc_info.emplace_back(tmp_task_desc_info);
  62. std::vector<ComputeGraphDescInfo> compute_graph_info;
  63. auto &profiling_manager = ProfilingManager::Instance();
  64. profiling_manager.ReportProfilingData(model_id, task_desc_info, compute_graph_info,
  65. !profiling_manager.IsAclApiMode());
  66. return SUCCESS;
  67. }
  68. } // namespace
  69. SingleOp::SingleOp(std::mutex *stream_mutex, rtStream_t stream) : stream_mutex_(stream_mutex), stream_(stream) {
  70. }
  71. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY SingleOp::~SingleOp() {
  72. for (auto task : tasks_) {
  73. delete task;
  74. task = nullptr;
  75. }
  76. }
  77. Status SingleOp::ValidateArgs(const std::vector<DataBuffer> &inputs, const std::vector<DataBuffer> &outputs) {
  78. auto num_inputs = inputs.size();
  79. if (num_inputs != input_sizes_.size()) {
  80. GELOGE(ACL_ERROR_GE_PARAM_INVALID, "Input num mismatch. model expect %zu, but given %zu", input_addr_list_.size(),
  81. inputs.size());
  82. return ACL_ERROR_GE_PARAM_INVALID;
  83. }
  84. for (size_t i = 0; i < num_inputs; ++i) {
  85. // preventing from read out of bound
  86. size_t aligned_size = GetAlignedSize(inputs[i].length);
  87. GELOGI("Input [%zu], aligned_size:%zu, inputs.length:%lu, input_sizes_:%zu",
  88. i, aligned_size, inputs[i].length, input_sizes_[i]);
  89. if (aligned_size < input_sizes_[i]) {
  90. GELOGE(ACL_ERROR_GE_PARAM_INVALID, "Input size mismatch. index = %zu, model expect %zu,"
  91. " but given %zu(after align)", i, input_sizes_[i], aligned_size);
  92. return ACL_ERROR_GE_PARAM_INVALID;
  93. }
  94. }
  95. auto num_outputs = outputs.size();
  96. if (num_outputs != output_sizes_.size()) {
  97. GELOGE(ACL_ERROR_GE_PARAM_INVALID, "output num mismatch. model expect %zu, but given %zu", output_sizes_.size(), outputs.size());
  98. return ACL_ERROR_GE_PARAM_INVALID;
  99. }
  100. for (size_t i = 0; i < num_outputs; ++i) {
  101. // preventing from write out of bound
  102. size_t aligned_size = GetAlignedSize(outputs[i].length);
  103. GELOGI("Output [%zu], aligned_size:%zu, outputs.length:%lu, output_sizes_:%zu",
  104. i, aligned_size, outputs[i].length, output_sizes_[i]);
  105. if (aligned_size < output_sizes_[i]) {
  106. GELOGE(ACL_ERROR_GE_PARAM_INVALID, "Output size mismatch. index = %zu, model expect %zu,"
  107. "but given %zu(after align)", i, output_sizes_[i], aligned_size);
  108. return ACL_ERROR_GE_PARAM_INVALID;
  109. }
  110. }
  111. return SUCCESS;
  112. }
  113. Status SingleOp::GetArgs(const std::vector<DataBuffer> &inputs, const std::vector<DataBuffer> &outputs) {
  114. size_t arg_index = 0;
  115. for (auto &input : inputs) {
  116. args_[arg_index++] = reinterpret_cast<uintptr_t>(input.data);
  117. }
  118. for (auto &output : outputs) {
  119. args_[arg_index++] = reinterpret_cast<uintptr_t>(output.data);
  120. }
  121. return SUCCESS;
  122. }
  123. Status SingleOp::UpdateArgs(const std::vector<DataBuffer> &inputs, const std::vector<DataBuffer> &outputs) {
  124. Status ret = GetArgs(inputs, outputs);
  125. if (ret != SUCCESS) {
  126. return ret;
  127. }
  128. // update tbe task args
  129. size_t num_args = arg_table_.size();
  130. for (size_t i = 0; i < num_args; ++i) {
  131. std::vector<uintptr_t *> &ptr_to_arg_in_tasks = arg_table_[i];
  132. if (ptr_to_arg_in_tasks.empty()) {
  133. GELOGW("found NO arg address to update for arg[%lu]", i);
  134. continue;
  135. }
  136. for (uintptr_t *arg_addr : ptr_to_arg_in_tasks) {
  137. *arg_addr = args_[i];
  138. }
  139. }
  140. // update aicpu_TF or aicpu_CC args
  141. for (auto &task : tasks_) {
  142. size_t io_addr_num = args_.size();
  143. if (task->GetOpTaskType() == OP_TASK_AICPU) {
  144. GELOGD("Update aicpu_TF task args");
  145. task->SetIoAddrsForDump(args_);
  146. auto *dst_io_addr = const_cast<uintptr_t *>(reinterpret_cast<const uintptr_t *>(task->GetIOAddr()));
  147. GE_CHECK_NOTNULL(dst_io_addr);
  148. auto rt_ret = rtMemcpyAsync(dst_io_addr,
  149. sizeof(uint64_t) * args_.size(),
  150. &args_[0],
  151. sizeof(uint64_t) * args_.size(),
  152. RT_MEMCPY_HOST_TO_DEVICE_EX,
  153. stream_);
  154. if (rt_ret != RT_ERROR_NONE) {
  155. GELOGE(rt_ret, "rtMemcpyAsync addresses failed, ret = %d", rt_ret);
  156. return rt_ret;
  157. }
  158. } else if (task->GetOpTaskType() == OP_TASK_AICPUCC) {
  159. GELOGD("Update aicpu_CC task args");
  160. const uintptr_t *task_io_addr = reinterpret_cast<const uintptr_t *>(task->GetIOAddr());
  161. GE_CHECK_NOTNULL(task_io_addr);
  162. auto io_addr = reinterpret_cast<uint64_t *>(const_cast<uintptr_t *>(task_io_addr));
  163. for (size_t i = 0; i < io_addr_num; ++i) {
  164. io_addr[i] = static_cast<uintptr_t>(args_[i]);
  165. }
  166. } else {
  167. GELOGW("Only TF_kernel aicpu and aicpu_CC are supported, but got %u", task->GetOpTaskType());
  168. continue;
  169. }
  170. }
  171. return SUCCESS;
  172. }
  173. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status SingleOp::ExecuteAsync(const std::vector<DataBuffer> &inputs,
  174. const std::vector<DataBuffer> &outputs) {
  175. Status ret = ValidateArgs(inputs, outputs);
  176. if (ret != SUCCESS) {
  177. return ret;
  178. }
  179. std::lock_guard<std::mutex> lk(*stream_mutex_);
  180. ret = UpdateArgs(inputs, outputs);
  181. if (ret != SUCCESS) {
  182. return ret;
  183. }
  184. for (auto &task : tasks_) {
  185. ret = task->LaunchKernel(stream_);
  186. if (ret != SUCCESS) {
  187. return ret;
  188. }
  189. GE_CHK_STATUS_RET_NOLOG(ProfilingTaskInfo(task));
  190. }
  191. return ret;
  192. }
  193. void SingleOp::SetStream(rtStream_t stream) {
  194. stream_ = stream;
  195. }
  196. DynamicSingleOp::DynamicSingleOp(uintptr_t resource_id, std::mutex *stream_mutex, rtStream_t stream)
  197. : resource_id_(resource_id), stream_mutex_(stream_mutex), stream_(stream) {
  198. }
  199. DynamicSingleOp::~DynamicSingleOp() {
  200. }
  201. Status DynamicSingleOp::ValidateParams(const vector<GeTensorDesc> &input_desc,
  202. const std::vector<DataBuffer> &inputs,
  203. std::vector<GeTensorDesc> &output_desc,
  204. std::vector<DataBuffer> &outputs) const {
  205. if (inputs.size() != input_desc.size()) {
  206. GELOGE(ACL_ERROR_GE_PARAM_INVALID,
  207. "Input number mismatches input desc number. Input num = %zu, input desc num = %zu",
  208. inputs.size(),
  209. input_desc.size());
  210. return ACL_ERROR_GE_PARAM_INVALID;
  211. }
  212. if (outputs.size() != output_desc.size()) {
  213. GELOGE(ACL_ERROR_GE_PARAM_INVALID,
  214. "Output number mismatches output desc number. Output num = %zu, output desc num = %zu",
  215. outputs.size(),
  216. output_desc.size());
  217. return ACL_ERROR_GE_PARAM_INVALID;
  218. }
  219. if (input_desc.size() != num_inputs_) {
  220. GELOGE(ACL_ERROR_GE_PARAM_INVALID, "Input number mismatches. expect %zu, but given %zu", num_inputs_, input_desc.size());
  221. return ACL_ERROR_GE_PARAM_INVALID;
  222. }
  223. if (output_desc.size() != num_outputs_) {
  224. GELOGE(ACL_ERROR_GE_PARAM_INVALID, "Output number mismatches. expect %zu, but given %zu", num_outputs_, output_desc.size());
  225. return ACL_ERROR_GE_PARAM_INVALID;
  226. }
  227. return SUCCESS;
  228. }
  229. Status DynamicSingleOp::AllocateWorkspaces(const std::vector<int64_t> &workspace_sizes,
  230. std::vector<void *> &workspaces) {
  231. static const std::string kPurpose("malloc workspace memory for dynamic op.");
  232. if (workspace_sizes.empty()) {
  233. GELOGD("No need to allocate workspace.");
  234. return SUCCESS;
  235. }
  236. int64_t total_size = 0;
  237. std::vector<int64_t> ws_offsets;
  238. for (auto ws_size : workspace_sizes) {
  239. // alignment and padding should be done in OpParaCalculate
  240. GE_CHK_STATUS_RET_NOLOG(CheckInt64AddOverflow(total_size, ws_size));
  241. ws_offsets.emplace_back(total_size);
  242. total_size += ws_size;
  243. }
  244. GELOGD("Total workspace size is %ld", total_size);
  245. StreamResource *stream_resource = SingleOpManager::GetInstance().GetResource(resource_id_, stream_);
  246. GE_CHECK_NOTNULL(stream_resource);
  247. auto ws_base = stream_resource->MallocMemory(kPurpose, static_cast<size_t>(total_size));
  248. if (ws_base == nullptr) {
  249. GELOGE(ACL_ERROR_GE_MEMORY_ALLOCATION, "Failed to allocate memory of size: %ld", total_size);
  250. return ACL_ERROR_GE_MEMORY_ALLOCATION;
  251. }
  252. GELOGD("Done allocating workspace memory successfully.");
  253. for (auto ws_offset : ws_offsets) {
  254. workspaces.emplace_back(ws_base + ws_offset);
  255. }
  256. return SUCCESS;
  257. }
  258. Status DynamicSingleOp::ExecuteTbeTask(const vector<GeTensorDesc> &input_desc,
  259. const vector<void *> &inputs,
  260. vector<GeTensorDesc> &output_desc,
  261. vector<void *> &outputs) {
  262. GE_CHK_STATUS_RET_NOLOG(op_task_->UpdateRunInfo(input_desc, output_desc));
  263. std::vector<void *> workspace_buffers;
  264. GE_CHK_STATUS_RET_NOLOG(AllocateWorkspaces(op_task_->GetWorkspaceSizes(), workspace_buffers));
  265. return op_task_->LaunchKernel(inputs, outputs, workspace_buffers, stream_);
  266. }
  267. Status DynamicSingleOp::ExecuteAsync(const vector<GeTensorDesc> &input_desc,
  268. const vector<DataBuffer> &input_buffers,
  269. vector<GeTensorDesc> &output_desc,
  270. vector<DataBuffer> &output_buffers) {
  271. GE_CHECK_NOTNULL(op_task_);
  272. GE_CHK_STATUS_RET_NOLOG(ValidateParams(input_desc, input_buffers, output_desc, output_buffers));
  273. std::lock_guard<std::mutex> lk(*stream_mutex_);
  274. std::vector<void *> inputs;
  275. std::vector<void *> outputs;
  276. for (auto &buffer : input_buffers) {
  277. inputs.emplace_back(buffer.data);
  278. }
  279. for (auto &buffer : output_buffers) {
  280. outputs.emplace_back(buffer.data);
  281. }
  282. if (op_task_->GetOpTaskType() == OP_TASK_TBE) {
  283. auto ret = ExecuteTbeTask(input_desc, inputs, output_desc, outputs);
  284. if (ret == SUCCESS) {
  285. GE_CHK_STATUS_RET_NOLOG(ProfilingTaskInfo(op_task_.get()));
  286. }
  287. return ret;
  288. } else if (op_task_->GetOpTaskType() == OP_TASK_AICPU || op_task_->GetOpTaskType() == OP_TASK_AICPUCC) {
  289. auto aicpu_ret = op_task_->LaunchKernel(input_desc, input_buffers, output_desc, output_buffers, stream_);
  290. if (aicpu_ret == SUCCESS) {
  291. GE_CHK_STATUS_RET_NOLOG(ProfilingTaskInfo(op_task_.get()));
  292. }
  293. return aicpu_ret;
  294. } else {
  295. GELOGE(ACL_ERROR_GE_OP_TASK_TYPE_INVALID,
  296. "Only TBE_Task, AI_CPU_Task and AI_CPUCC_Task are supported, but got %u",
  297. op_task_->GetOpTaskType());
  298. return ACL_ERROR_GE_OP_TASK_TYPE_INVALID;
  299. }
  300. }
  301. } // namespace ge

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