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.

executor_subscribers_scheduler.h 5.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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_INC_FRAMEWORK_RUNTIME_EXECUTOR_SUBSCRIBERS_SCHEDULER_H_
  17. #define AIR_CXX_INC_FRAMEWORK_RUNTIME_EXECUTOR_SUBSCRIBERS_SCHEDULER_H_
  18. #include <vector>
  19. #include <array>
  20. #include "executor_subscriber_guarder.h"
  21. #include "built_in_subscriber_definitions.h"
  22. #include "global_profiling.h"
  23. #include "framework/common/ge_visibility.h"
  24. namespace gert {
  25. class ModelV2Executor;
  26. class VISIBILITY_EXPORT ExecutorSubscribersScheduler {
  27. public:
  28. static void OnExecuteEvent(ExecutorSubscribersScheduler *ins, ExecutorEvent event, const void *node,
  29. KernelStatus result);
  30. ExecutorSubscribersScheduler()
  31. : executor_(nullptr),
  32. enabled_(false),
  33. built_in_subscribers_ptr_(),
  34. subscribers_(),
  35. subscriber_wrapper_({reinterpret_cast<::SubscriberFunc>(ExecutorSubscribersScheduler::OnExecuteEvent), this}) {}
  36. #ifdef ONLY_COMPILE_OPEN_SRC
  37. ~ExecutorSubscribersScheduler();
  38. #endif
  39. void Init(ModelV2Executor *executor);
  40. ExecutorSubscribersScheduler(const ExecutorSubscribersScheduler &) = delete;
  41. ExecutorSubscribersScheduler &operator=(const ExecutorSubscribersScheduler &) = delete;
  42. ExecutorSubscriber &GetSubscriber() {
  43. if (subscribers_.size() == 1UL) {
  44. return subscribers_[0].GetSubscriber();
  45. } else {
  46. return subscriber_wrapper_;
  47. }
  48. }
  49. ModelV2Executor *GetModelV2Executor() {
  50. return executor_;
  51. }
  52. const ModelV2Executor *GetModelV2Executor() const {
  53. return executor_;
  54. }
  55. /**
  56. * 设置订阅者,订阅者需要实现一个static方法,原型为:
  57. * ```c++
  58. * static void OnExecuteEvent(T *void_arg, ExecutorEvent event, const void *node, KernelStatus result);
  59. * ```
  60. *
  61. * 默认情况下,subscribers处于disable状态,在添加首个subscriber时,自动将状态切换到enable状态。
  62. *
  63. * @tparam T 订阅者类型
  64. * @tparam Args 订阅者初始化参数类型
  65. * @param args 订阅者初始化参数
  66. * @return 添加的subscriber指针,注意subscriber所有权归`ExecutorSubscribersScheduler`所有,外部使用者不可以释放此指针
  67. */
  68. template <typename T, typename... Args>
  69. T *AddSubscriber(Args... args) {
  70. auto ins = new (std::nothrow) T(args...);
  71. if (ins == nullptr) {
  72. return nullptr;
  73. }
  74. constexpr size_t kInitSubscriberSize = 1UL;
  75. // profiler exists when ess init
  76. if (subscribers_.size() == kInitSubscriberSize) {
  77. enabled_ = true;
  78. }
  79. subscribers_.emplace_back(reinterpret_cast<::SubscriberFunc>(T::OnExecuteEvent), ins, ObjectDeleter<T>);
  80. return ins;
  81. }
  82. /**
  83. * 添加一个内置的subscriber
  84. * 内置subscriber较少,当前没有使用注册机制,后续如果需要扩展,那么可以考虑通过注册机制自动注册。
  85. * 为了易用性,在本类提供了获取内置subscriber的指针的接口。而自注册的subscriber将丢失此能力。
  86. * @param subscriber_type
  87. */
  88. void AddBuiltIn(BuiltInSubscriberType subscriber_type, uint64_t enable_flag);
  89. void RemoveSubscriber(void *subscriber_ptr) {
  90. for (auto iter = subscribers_.begin(); iter != subscribers_.end(); ++iter) {
  91. if (iter->GetSubscriber().arg == subscriber_ptr) {
  92. subscribers_.erase(iter);
  93. break;
  94. }
  95. }
  96. for (auto &built_in_subscriber : built_in_subscribers_ptr_) {
  97. if (built_in_subscriber == subscriber_ptr) {
  98. built_in_subscriber = nullptr;
  99. }
  100. }
  101. if (subscribers_.empty()) {
  102. enabled_ = false;
  103. }
  104. }
  105. template <typename T>
  106. inline T *MutableBuiltInSubscriber(const BuiltInSubscriberType type) {
  107. return static_cast<T *>(built_in_subscribers_ptr_[static_cast<size_t>(type)]);
  108. }
  109. template <typename T>
  110. inline const T *GetBuiltInSubscriber(const BuiltInSubscriberType type) {
  111. return static_cast<T *>(built_in_subscribers_ptr_[static_cast<size_t>(type)]);
  112. }
  113. bool IsEnable() const {
  114. return enabled_ || GlobalProfilingWrapper::GetInstance()->GetEnableFlags();
  115. }
  116. void SetEnable(bool enable_flag) {
  117. enabled_ = enable_flag;
  118. }
  119. void Clear() {
  120. subscribers_.clear();
  121. for (auto &built_in_subscriber : built_in_subscribers_ptr_) {
  122. built_in_subscriber = nullptr;
  123. }
  124. enabled_ = false;
  125. }
  126. size_t GetSize() const {
  127. return subscribers_.size();
  128. }
  129. private:
  130. ModelV2Executor *executor_{nullptr};
  131. bool enabled_{false};
  132. std::array<void *, static_cast<size_t>(BuiltInSubscriberType::kNum)> built_in_subscribers_ptr_;
  133. std::vector<ExecutorSubscriberGuarder> subscribers_;
  134. ExecutorSubscriber subscriber_wrapper_;
  135. };
  136. } // namespace gert
  137. #endif // AIR_CXX_INC_FRAMEWORK_RUNTIME_EXECUTOR_SUBSCRIBERS_SCHEDULER_H_

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