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 9.6 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 "single_op/task/build_task_utils.h"
  27. #include "graph/load/new_model_manager/model_manager.h"
  28. namespace ge {
  29. namespace {
  30. const size_t kDataMemAlignSize = 32;
  31. const size_t kDataMemAlignUnit = 2;
  32. size_t GetAlignedSize(size_t size) {
  33. size_t aligned_size = (size + kDataMemAlignUnit * kDataMemAlignSize - 1) / kDataMemAlignSize * kDataMemAlignSize;
  34. return aligned_size;
  35. }
  36. Status ProfilingTaskInfo(OpTask *op_task) {
  37. if (!ProfilingManager::Instance().ProfilingModelExecuteOn()) {
  38. return SUCCESS;
  39. }
  40. string model_name;
  41. string op_name;
  42. uint32_t model_id;
  43. uint32_t block_dim;
  44. if (op_task->GetProfilingArgs(model_name, op_name, model_id, block_dim) != SUCCESS) {
  45. GELOGE(ACL_ERROR_GE_PARAM_INVALID, "Get profiling data of task failed");
  46. return ACL_ERROR_GE_PARAM_INVALID;
  47. }
  48. GELOGD("ProfilingReport of op[%s] model[%s] start.", op_name.c_str(), model_name.c_str());
  49. std::vector<TaskDescInfo> task_desc_info;
  50. uint32_t task_id = 0;
  51. uint32_t stream_id = 0;
  52. if (rtGetTaskIdAndStreamID(&task_id, &stream_id) != RT_ERROR_NONE) {
  53. GELOGE(ACL_ERROR_GE_PARAM_INVALID, "Get task_id and stream_id failed.");
  54. return ACL_ERROR_GE_PARAM_INVALID;
  55. }
  56. TaskDescInfo tmp_task_desc_info;
  57. tmp_task_desc_info.model_name = model_name;
  58. tmp_task_desc_info.op_name = op_name;
  59. tmp_task_desc_info.block_dim = block_dim;
  60. tmp_task_desc_info.task_id = task_id;
  61. tmp_task_desc_info.stream_id = stream_id;
  62. GELOGD("GetTaskDescInfo of op [%s] end, task_id[%u], stream_id[%u]", op_name.c_str(), task_id, stream_id);
  63. task_desc_info.emplace_back(tmp_task_desc_info);
  64. std::vector<ComputeGraphDescInfo> compute_graph_info;
  65. auto &profiling_manager = ProfilingManager::Instance();
  66. profiling_manager.ReportProfilingData(model_id, task_desc_info, compute_graph_info,
  67. !profiling_manager.IsAclApiMode());
  68. return SUCCESS;
  69. }
  70. } // namespace
  71. SingleOp::SingleOp(StreamResource *stream_resource, std::mutex *stream_mutex, rtStream_t stream)
  72. : stream_resource_(stream_resource), stream_mutex_(stream_mutex), stream_(stream) {
  73. }
  74. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY SingleOp::~SingleOp() {
  75. for (auto task : tasks_) {
  76. delete task;
  77. task = nullptr;
  78. }
  79. }
  80. Status SingleOp::ValidateArgs(const std::vector<DataBuffer> &inputs, const std::vector<DataBuffer> &outputs) {
  81. auto num_inputs = inputs.size();
  82. if (num_inputs != input_sizes_.size()) {
  83. GELOGE(ACL_ERROR_GE_PARAM_INVALID, "Input num mismatch. model expect %zu, but given %zu", input_addr_list_.size(),
  84. inputs.size());
  85. return ACL_ERROR_GE_PARAM_INVALID;
  86. }
  87. for (size_t i = 0; i < num_inputs; ++i) {
  88. // preventing from read out of bound
  89. size_t aligned_size = GetAlignedSize(inputs[i].length);
  90. GELOGI("Input [%zu], aligned_size:%zu, inputs.length:%lu, input_sizes_:%zu",
  91. i, aligned_size, inputs[i].length, input_sizes_[i]);
  92. if (aligned_size < input_sizes_[i]) {
  93. GELOGE(ACL_ERROR_GE_PARAM_INVALID, "Input size mismatch. index = %zu, model expect %zu,"
  94. " but given %zu(after align)", i, input_sizes_[i], aligned_size);
  95. return ACL_ERROR_GE_PARAM_INVALID;
  96. }
  97. }
  98. auto num_outputs = outputs.size();
  99. if (num_outputs != output_sizes_.size()) {
  100. GELOGE(ACL_ERROR_GE_PARAM_INVALID, "output num mismatch. model expect %zu, but given %zu",
  101. output_sizes_.size(), outputs.size());
  102. return ACL_ERROR_GE_PARAM_INVALID;
  103. }
  104. for (size_t i = 0; i < num_outputs; ++i) {
  105. // preventing from write out of bound
  106. size_t aligned_size = GetAlignedSize(outputs[i].length);
  107. GELOGI("Output [%zu], aligned_size:%zu, outputs.length:%lu, output_sizes_:%zu",
  108. i, aligned_size, outputs[i].length, output_sizes_[i]);
  109. if (aligned_size < output_sizes_[i]) {
  110. GELOGE(ACL_ERROR_GE_PARAM_INVALID, "Output size mismatch. index = %zu, model expect %zu,"
  111. "but given %zu(after align)", i, output_sizes_[i], aligned_size);
  112. return ACL_ERROR_GE_PARAM_INVALID;
  113. }
  114. }
  115. return SUCCESS;
  116. }
  117. Status SingleOp::GetArgs(const std::vector<DataBuffer> &inputs, const std::vector<DataBuffer> &outputs) {
  118. size_t arg_index = 0;
  119. for (auto &input : inputs) {
  120. args_[arg_index++] = reinterpret_cast<uintptr_t>(input.data);
  121. }
  122. for (auto &output : outputs) {
  123. args_[arg_index++] = reinterpret_cast<uintptr_t>(output.data);
  124. }
  125. return SUCCESS;
  126. }
  127. Status SingleOp::UpdateArgs(const std::vector<DataBuffer> &inputs, const std::vector<DataBuffer> &outputs) {
  128. Status ret = GetArgs(inputs, outputs);
  129. if (ret != SUCCESS) {
  130. return ret;
  131. }
  132. // update tbe task args
  133. size_t num_args = arg_table_.size();
  134. for (size_t i = 0; i < num_args; ++i) {
  135. std::vector<uintptr_t *> &ptr_to_arg_in_tasks = arg_table_[i];
  136. if (ptr_to_arg_in_tasks.empty()) {
  137. GELOGW("found NO arg address to update for arg[%lu]", i);
  138. continue;
  139. }
  140. for (uintptr_t *arg_addr : ptr_to_arg_in_tasks) {
  141. *arg_addr = args_[i];
  142. }
  143. }
  144. return SUCCESS;
  145. }
  146. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status SingleOp::ExecuteAsync(const std::vector<DataBuffer> &inputs,
  147. const std::vector<DataBuffer> &outputs) {
  148. Status ret = ValidateArgs(inputs, outputs);
  149. if (ret != SUCCESS) {
  150. return ret;
  151. }
  152. GE_CHECK_NOTNULL(stream_resource_);
  153. std::lock_guard<std::mutex> lk(*stream_mutex_);
  154. auto current_mem_base = stream_resource_->GetMemoryBase();
  155. if (running_param_->mem_base != current_mem_base) {
  156. running_param_->mem_base = const_cast<uint8_t *>(current_mem_base);
  157. GELOGD("Memory base changed, new memory base = %p", current_mem_base);
  158. for (auto &task : tasks_) {
  159. auto new_address = BuildTaskUtils::GetAddresses(task->GetOpdesc(), *running_param_);
  160. GE_CHK_STATUS_RET(task->UpdateArgTable(*running_param_),
  161. "[%s] Failed to update arg table",
  162. task->GetOpdesc()->GetName().c_str());
  163. }
  164. }
  165. ret = UpdateArgs(inputs, outputs);
  166. if (ret != SUCCESS) {
  167. return ret;
  168. }
  169. for (auto &task : tasks_) {
  170. ret = task->LaunchKernel(stream_);
  171. if (ret != SUCCESS) {
  172. return ret;
  173. }
  174. GE_CHK_STATUS_RET_NOLOG(ProfilingTaskInfo(task));
  175. }
  176. return ret;
  177. }
  178. void SingleOp::SetStream(rtStream_t stream) {
  179. stream_ = stream;
  180. }
  181. DynamicSingleOp::DynamicSingleOp(uintptr_t resource_id, std::mutex *stream_mutex, rtStream_t stream)
  182. : resource_id_(resource_id), stream_mutex_(stream_mutex), stream_(stream) {
  183. }
  184. Status DynamicSingleOp::ValidateParams(const vector<GeTensorDesc> &input_desc,
  185. const std::vector<DataBuffer> &inputs,
  186. std::vector<GeTensorDesc> &output_desc,
  187. std::vector<DataBuffer> &outputs) const {
  188. if (inputs.size() != input_desc.size()) {
  189. GELOGE(ACL_ERROR_GE_PARAM_INVALID,
  190. "Input number mismatches input desc number. Input num = %zu, input desc num = %zu",
  191. inputs.size(),
  192. input_desc.size());
  193. return ACL_ERROR_GE_PARAM_INVALID;
  194. }
  195. if (outputs.size() != output_desc.size()) {
  196. GELOGE(ACL_ERROR_GE_PARAM_INVALID,
  197. "Output number mismatches output desc number. Output num = %zu, output desc num = %zu",
  198. outputs.size(),
  199. output_desc.size());
  200. return ACL_ERROR_GE_PARAM_INVALID;
  201. }
  202. if (input_desc.size() != num_inputs_) {
  203. GELOGE(ACL_ERROR_GE_PARAM_INVALID,
  204. "Input number mismatches. expect %zu, but given %zu",
  205. num_inputs_,
  206. input_desc.size());
  207. return ACL_ERROR_GE_PARAM_INVALID;
  208. }
  209. if (output_desc.size() != num_outputs_) {
  210. GELOGE(ACL_ERROR_GE_PARAM_INVALID,
  211. "Output number mismatches. expect %zu, but given %zu",
  212. num_outputs_,
  213. output_desc.size());
  214. return ACL_ERROR_GE_PARAM_INVALID;
  215. }
  216. return SUCCESS;
  217. }
  218. Status DynamicSingleOp::ExecuteAsync(const vector<GeTensorDesc> &input_desc,
  219. const vector<DataBuffer> &input_buffers,
  220. vector<GeTensorDesc> &output_desc,
  221. vector<DataBuffer> &output_buffers) {
  222. GE_CHECK_NOTNULL(op_task_);
  223. GE_CHK_STATUS_RET_NOLOG(ValidateParams(input_desc, input_buffers, output_desc, output_buffers));
  224. std::lock_guard<std::mutex> lk(*stream_mutex_);
  225. GE_CHK_STATUS_RET_NOLOG(op_task_->LaunchKernel(input_desc, input_buffers, output_desc, output_buffers, stream_));
  226. GE_CHK_STATUS_RET_NOLOG(ProfilingTaskInfo(op_task_.get()));
  227. return SUCCESS;
  228. }
  229. } // namespace ge

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