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 3.8 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 "gert_api.h"
  30. #include "mem_allocator.h"
  31. namespace gert {
  32. enum class ExecutorState { kInit, kLoaded };
  33. enum SubExeGraphType { kInitExeGraph, kMainExeGraph, kDeInitExeGraph, kSubExeGraphTypeEnd };
  34. inline const char *GetSubExeGraphTypeStr(SubExeGraphType type) {
  35. constexpr const char *kSubExeGraphTypeStrs[kSubExeGraphTypeEnd] = {"Init", "Main", "DeInit"};
  36. return kSubExeGraphTypeStrs[type];
  37. }
  38. enum class ExecuteArgIndex { kExternalAllocator = -2, kStream = -1, kEnd };
  39. struct ModelExecuteArg {
  40. rtStream_t stream;
  41. ExternalAllocators *external_allocator;
  42. ModelExecuteArg() : stream(nullptr), external_allocator(nullptr) {}
  43. ModelExecuteArg(rtStream_t stream_, ExternalAllocators *external_allocator_ = nullptr)
  44. : stream(stream_), external_allocator(external_allocator_) {}
  45. };
  46. static_assert(std::is_standard_layout<ModelExecuteArg>::value, "The class ModelExecuteArg must be a POD");
  47. class VISIBILITY_EXPORT ModelV2Executor {
  48. public:
  49. static std::unique_ptr<ModelV2Executor> Create(const ge::ComputeGraphPtr &root_graph, const ge::ModelData &model_data,
  50. const std::shared_ptr<ge::GeRootModel> &root_model);
  51. static std::unique_ptr<ModelV2Executor> Create(const ge::ComputeGraphPtr &root_graph);
  52. ge::graphStatus Load();
  53. ge::graphStatus Execute(const ModelExecuteArg &arg, Tensor **inputs, size_t input_num, Tensor **outputs,
  54. size_t output_num);
  55. ge::graphStatus ExecuteSync(Tensor **inputs, size_t input_num, Tensor **outputs, size_t output_num);
  56. ge::graphStatus UnLoad();
  57. const ModelDesc &GetModelDesc() const;
  58. void SetModelDesc(ModelDesc *model_desc);
  59. ExeGraphExecutor *GetExeGraphExecutor(SubExeGraphType type) {
  60. if (type >= kSubExeGraphTypeEnd) {
  61. return nullptr;
  62. }
  63. return &graphs_[static_cast<size_t>(type)];
  64. }
  65. ExecutorSubscribersScheduler &GetSubscribers();
  66. const ExecutorSubscribersScheduler &GetSubscribers() const;
  67. ModelV2Executor(const ModelV2Executor &) = delete;
  68. ModelV2Executor(ModelV2Executor &&) = delete;
  69. ModelV2Executor &operator=(const ModelV2Executor &) = delete;
  70. ModelV2Executor &operator=(ModelV2Executor &&) = delete;
  71. private:
  72. friend class ModelV2ExecutorBuilder;
  73. friend class ModelV2ExecutorTestHelper;
  74. ModelV2Executor();
  75. private:
  76. std::array<ExeGraphExecutor, kSubExeGraphTypeEnd> graphs_;
  77. ResourceGuard resource_guard_;
  78. ModelDesc *model_desc_ = nullptr;
  79. rtStream_t default_stream_ = nullptr;
  80. ExecutorSubscribersScheduler subscribers_;
  81. ExecutorState state_ = ExecutorState::kInit;
  82. };
  83. } // namespace gert
  84. #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两部分组成,详细的架构图如下所示