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

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