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

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

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