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.

model_v2_executor.h 6.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
  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. #ifndef AIR_CXX_RUNTIME_V2_CORE_MODEL_V_2_EXECUTOR_H_
  17. #define AIR_CXX_RUNTIME_V2_CORE_MODEL_V_2_EXECUTOR_H_
  18. #include <memory>
  19. #include "graph/compute_graph.h"
  20. #include "graph/ge_error_codes.h"
  21. #include "model_desc.h"
  22. #include "runtime/stream.h"
  23. #include "exe_graph/runtime/tensor.h"
  24. #include "common/ge_visibility.h"
  25. #include "exe_graph_resource_guard.h"
  26. #include "exe_graph_executor.h"
  27. #include "subscriber/executor_subscribers_scheduler.h"
  28. #include "common/ge_types.h"
  29. #include "mem_allocator.h"
  30. namespace gert {
  31. enum class ExecutorState { kInit, kLoaded };
  32. enum SubExeGraphType { kInitExeGraph, kMainExeGraph, kDeInitExeGraph, kSubExeGraphTypeEnd };
  33. inline const ge::char_t *GetSubExeGraphTypeStr(const SubExeGraphType type) {
  34. constexpr const ge::char_t *kSubExeGraphTypeStrs[kSubExeGraphTypeEnd] = {"Init", "Main", "DeInit"};
  35. return kSubExeGraphTypeStrs[type];
  36. }
  37. enum class ExecuteArgIndex { kExternalAllocator = -2, kStream, kEnd };
  38. struct ModelExecuteArg {
  39. rtStream_t stream;
  40. ExternalAllocators *external_allocator;
  41. ModelExecuteArg() : stream(nullptr), external_allocator(nullptr) {}
  42. ModelExecuteArg(const rtStream_t stream_, ExternalAllocators *const external_allocator_ = nullptr)
  43. : stream(stream_), external_allocator(external_allocator_) {}
  44. };
  45. static_assert(std::is_standard_layout<ModelExecuteArg>::value, "The class ModelExecuteArg must be a POD");
  46. class VISIBILITY_EXPORT ModelV2Executor {
  47. public:
  48. static std::unique_ptr<ModelV2Executor> Create(const ge::ComputeGraphPtr &root_graph, const ge::ModelData &model_data,
  49. const std::shared_ptr<ge::GeRootModel> &root_model);
  50. static std::unique_ptr<ModelV2Executor> Create(const ge::ComputeGraphPtr &root_graph);
  51. ge::graphStatus Load();
  52. /**
  53. * 加载模型,本接口需要在模型执行前被调用。加载流程会完成模型的初始化、将重要数据拷贝到NPU等整个模型生命周期内仅需要执行一次的行为。
  54. * @param arg
  55. * 模型的执行参数,需要注意的是,此处传入的执行参数应该与Execute接口传入的执行参数具有相同的stream和allocator,
  56. * 否则在load完成后,外部需要调用流同步以保证不出现时序问题
  57. * @return 成功时返回`ge::GRAPH_SUCCESS`
  58. */
  59. ge::graphStatus Load(const ModelExecuteArg &arg);
  60. /**
  61. * 异步执行模型,本接口将模型异步下发到NPU执行,本接口返回不代表模型执行完成,用户需要手动调用流同步等待模型执行完成。
  62. * 调用本接口前,请确保已经调用`Load`接口
  63. *
  64. * 用户可以通过多种方式指定输出Tensor,其行为分别为:
  65. *
  66. * * 调用本接口前,用户自行申请了足量空间的输出内存,并通过输出Tensor传入:执行完成后,输出内容被写入到用户申请的输出Tensor。
  67. * 若用户申请的输出Tensor不够长,那么本接口返回失败。
  68. * * 用户生成了输出Tensor,但是没有申请输出内存,将不包含输出内存的Tensor传入:本接口内部主动申请输出内存,并将输出内存传出。
  69. * 若用户没有在arg中指定Allocator,那么本接口输出的内存生命周期与本Executor一致;
  70. * 如果用户在arg中传入了Allocator,那么输出内存将使用用户传入的Allocator申请
  71. *
  72. * 注意:
  73. *
  74. * 1. 本接口不支持并发调用
  75. * 2.
  76. * 如果外部指定了Allocator,那么建议Allocator应该与stream绑定,如果出现同一个allocator,匹配不同的stream多次调用Execute接口时,
  77. * 需要满足两个条件:不可以并发调用,在切换stream执行中间,需要对上一条stream做流同步
  78. * 3.
  79. * 若外部指定了Allocator,在模型执行完成前,不可以将Allocator中的内存归还给操作系统(即使这块内存已经由执行器归还给Allocator)
  80. *
  81. * @param arg 执行参数
  82. * @param inputs 网络的输入tensor,从调用本接口开始,到流同步等待本模型执行结束之前,用户需要保证传入的Tensor有效
  83. * @param input_num 输入tensor的数量
  84. * @param outputs 网络的输出tensor
  85. * @param output_num 输出tensor的数量
  86. * @return 成功时返回`ge::GRAPH_SUCCESS`
  87. */
  88. ge::graphStatus Execute(const ModelExecuteArg &arg, Tensor **inputs, size_t input_num, Tensor **outputs,
  89. size_t output_num);
  90. ge::graphStatus ExecuteSync(Tensor **inputs, size_t input_num, Tensor **outputs, size_t output_num);
  91. ge::graphStatus UnLoad();
  92. const ModelDesc &GetModelDesc() const;
  93. void SetModelDesc(ModelDesc *model_desc);
  94. ExeGraphExecutor *GetExeGraphExecutor(const SubExeGraphType type) {
  95. if (type >= kSubExeGraphTypeEnd) {
  96. return nullptr;
  97. }
  98. return &graphs_[static_cast<size_t>(type)];
  99. }
  100. ExecutorSubscribersScheduler &GetSubscribers();
  101. const ExecutorSubscribersScheduler &GetSubscribers() const;
  102. ModelV2Executor(const ModelV2Executor &) = delete;
  103. ModelV2Executor(ModelV2Executor &&) = delete;
  104. ModelV2Executor &operator=(const ModelV2Executor &) = delete;
  105. ModelV2Executor &operator=(ModelV2Executor &&) = delete;
  106. private:
  107. friend class ModelV2ExecutorBuilder;
  108. friend class ModelV2ExecutorTestHelper;
  109. ModelV2Executor();
  110. private:
  111. ResourceGuard resource_guard_;
  112. std::array<ExeGraphExecutor, kSubExeGraphTypeEnd> graphs_;
  113. ModelDesc *model_desc_ = nullptr;
  114. rtStream_t default_stream_ = nullptr;
  115. ExecutorSubscribersScheduler subscribers_;
  116. ExecutorState state_ = ExecutorState::kInit;
  117. };
  118. } // namespace gert
  119. #endif // AIR_CXX_RUNTIME_V2_CORE_MODEL_V_2_EXECUTOR_H_

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