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.

hybrid_model_executor.cc 4.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 "hybrid_model_executor.h"
  17. #include "graph/ge_context.h"
  18. #include "graph/runtime_inference_context.h"
  19. namespace ge {
  20. namespace hybrid {
  21. namespace {
  22. const int kIntBase = 10;
  23. const char *const kEnvProfilingLevel = "HYBRID_PROFILING_LEVEL";
  24. } // namespace
  25. HybridModelExecutor::HybridModelExecutor(HybridModel *model, uint32_t device_id, rtStream_t stream)
  26. : model_(model), device_id_(device_id), stream_(stream) {}
  27. HybridModelExecutor::~HybridModelExecutor() {
  28. if (context_.rt_gen_context != nullptr) {
  29. (void)rtCtxDestroy(context_.rt_gen_context);
  30. }
  31. }
  32. Status HybridModelExecutor::Init() {
  33. GELOGD("Start to init HybridGraphEngine.");
  34. GE_CHK_STATUS_RET_NOLOG(InitExecutionContext());
  35. GELOGD("HybridGraphEngine initialized successfully.");
  36. return SUCCESS;
  37. }
  38. Status HybridModelExecutor::Execute(HybridModelExecutor::ExecuteArgs &args) {
  39. GELOGD("Start to execute model.");
  40. auto root_graph_item = model_->GetRootGraphItem();
  41. GE_CHECK_NOTNULL(root_graph_item);
  42. SubgraphExecutor executor(model_->GetRootGraphItem(), &context_);
  43. auto ret = ExecuteGraphInternal(executor, args);
  44. Cleanup();
  45. RECORD_MODEL_EXECUTION_EVENT(&context_, "[Cleanup] End");
  46. GE_CHK_STATUS_RET(ret, "Failed to execute model");
  47. GELOGD("Model executed successfully.");
  48. if (context_.profiler != nullptr) {
  49. context_.profiler->Dump(std::cout);
  50. context_.profiler->Reset();
  51. }
  52. context_.iteration += 1;
  53. return SUCCESS;
  54. }
  55. Status HybridModelExecutor::ExecuteGraphInternal(SubgraphExecutor &executor, HybridModelExecutor::ExecuteArgs &args) {
  56. RECORD_MODEL_EXECUTION_EVENT(&context_, "[InitContext] Start");
  57. GE_CHK_STATUS_RET_NOLOG(ResetExecutionContext(context_));
  58. RECORD_MODEL_EXECUTION_EVENT(&context_, "[InitContext] End");
  59. GE_CHK_STATUS_RET(executor.ExecuteAsync(args.inputs, args.input_desc), "Failed to execute partitioned call.");
  60. RECORD_MODEL_EXECUTION_EVENT(&context_, "[ExecuteAsync] End");
  61. GE_CHK_STATUS_RET(executor.Synchronize(), "Failed to sync root graph.");
  62. RECORD_MODEL_EXECUTION_EVENT(&context_, "[Synchronize] End");
  63. GE_CHK_STATUS_RET(executor.GetOutputs(args.outputs, args.output_desc), "Failed to get outputs");
  64. RECORD_MODEL_EXECUTION_EVENT(&context_, "[GetOutput] End");
  65. return SUCCESS;
  66. }
  67. Status HybridModelExecutor::Cleanup() {
  68. GELOGD("Start to cleanup.");
  69. context_.callback_manager->Destroy();
  70. RuntimeInferenceContext::DestroyContext(to_string(context_.session_id));
  71. GELOGD("Cleanup successfully.");
  72. return SUCCESS;
  73. }
  74. Status HybridModelExecutor::InitExecutionContext() {
  75. GE_CHK_RT_RET(rtCtxGetCurrent(&context_.rt_context));
  76. GE_CHK_RT_RET(rtCtxCreate(&context_.rt_gen_context, RT_CTX_GEN_MODE, 0));
  77. GE_CHK_RT_RET(rtCtxSetCurrent(context_.rt_context));
  78. context_.stream = stream_;
  79. context_.model = model_;
  80. context_.session_id = ::ge::GetContext().SessionId();
  81. GELOGD("session id from model = %lu, from context = %lu", model_->GetSessionId(), context_.session_id);
  82. context_.allocator = NpuMemoryAllocator::GetAllocator(device_id_);
  83. GE_CHECK_NOTNULL(context_.allocator);
  84. context_.callback_manager = std::unique_ptr<CallbackManager>(new (std::nothrow) CallbackManager(stream_));
  85. GE_CHECK_NOTNULL(context_.callback_manager);
  86. context_.dump_properties = PropertiesManager::Instance().GetDumpProperties(context_.session_id);
  87. const char *profiling_level = std::getenv(kEnvProfilingLevel);
  88. if (profiling_level != nullptr) {
  89. context_.profiling_level = std::strtol(profiling_level, nullptr, kIntBase);
  90. GELOGD("Got profiling level = %d", context_.profiling_level);
  91. if (context_.profiling_level > 0) {
  92. context_.profiler.reset(new (std::nothrow) HybridProfiler());
  93. GE_CHECK_NOTNULL(context_.profiler);
  94. }
  95. }
  96. if (IsLogEnable(GE_MODULE_NAME, DLOG_DEBUG)) {
  97. context_.trace_enabled = true;
  98. }
  99. return SUCCESS;
  100. }
  101. Status HybridModelExecutor::ResetExecutionContext(GraphExecutionContext &context) {
  102. GE_CHK_STATUS_RET_NOLOG(context.callback_manager->Init());
  103. string ctx_id = std::to_string(context.session_id);
  104. RuntimeInferenceContext::DestroyContext(ctx_id);
  105. GE_CHK_GRAPH_STATUS_RET(RuntimeInferenceContext::CreateContext(ctx_id), "Failed to Destroy RuntimeInferenceContext");
  106. return SUCCESS;
  107. }
  108. } // namespace hybrid
  109. } // namespace ge

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