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.

global_profiling.h 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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_SUBSCRIBER_GLOBAL_PROFILING_H_
  17. #define AIR_CXX_INC_FRAMEWORK_RUNTIME_SUBSCRIBER_GLOBAL_PROFILING_H_
  18. #include <algorithm>
  19. #include <memory>
  20. #include <unordered_map>
  21. #include "built_in_subscriber_definitions.h"
  22. #include "common/debug/ge_log.h"
  23. #include "framework/common/ge_visibility.h"
  24. #include "runtime/subscriber/executor_subscriber_c.h"
  25. namespace gert {
  26. struct ProfilingData {
  27. uint64_t name_idx;
  28. uint64_t type_idx;
  29. ExecutorEvent event;
  30. std::chrono::time_point<std::chrono::system_clock> timestamp;
  31. };
  32. class GlobalProfiler {
  33. public:
  34. GlobalProfiler() = default;
  35. void Record(uint64_t name_idx, uint64_t type_idx, ExecutorEvent event,
  36. std::chrono::time_point<std::chrono::system_clock> timestamp) {
  37. auto index = count_++;
  38. if (index >= kProfilingDataCap) {
  39. return;
  40. }
  41. records_[index] = {name_idx, type_idx, event, timestamp};
  42. }
  43. void Dump(std::ostream &out_stream, std::vector<std::string> &idx_to_str) const;
  44. size_t GetCount() const {
  45. return count_;
  46. }
  47. private:
  48. std::atomic<size_t> count_{0UL};
  49. ProfilingData records_[kProfilingDataCap];
  50. };
  51. class VISIBILITY_EXPORT GlobalProfilingWrapper {
  52. public:
  53. GlobalProfilingWrapper(const GlobalProfilingWrapper &) = delete;
  54. GlobalProfilingWrapper(GlobalProfilingWrapper &&) = delete;
  55. GlobalProfilingWrapper &operator=(const GlobalProfilingWrapper &) = delete;
  56. GlobalProfilingWrapper &operator=(GlobalProfilingWrapper &&) = delete;
  57. static GlobalProfilingWrapper *GetInstance() {
  58. static GlobalProfilingWrapper global_prof_wrapper;
  59. return &global_prof_wrapper;
  60. }
  61. static void OnGlobalProfilingSwitch(void *ins, uint64_t enable_flags);
  62. void Init(uint64_t enable_flags);
  63. void Free() {
  64. global_profiler_.reset(nullptr);
  65. SetEnableFlags(0UL);
  66. }
  67. GlobalProfiler *GetGlobalProfiler() const {
  68. return global_profiler_.get();
  69. }
  70. void SetEnableFlags(uint64_t enable_flags) {
  71. enable_flags_ = enable_flags;
  72. }
  73. uint64_t GetRecordCount() {
  74. if (global_profiler_ == nullptr) {
  75. return 0UL;
  76. }
  77. return global_profiler_->GetCount();
  78. }
  79. uint64_t GetEnableFlags() const {
  80. return enable_flags_;
  81. }
  82. bool IsEnable(ProfilingType profiling_type) const {
  83. return enable_flags_ & BuiltInSubscriberUtil::EnableBit<ProfilingType>(profiling_type);
  84. }
  85. void DumpAndFree(std::ostream &out_stream) {
  86. Dump(out_stream);
  87. Free();
  88. }
  89. void Dump(std::ostream &out_stream) {
  90. if (global_profiler_ != nullptr) {
  91. global_profiler_->Dump(out_stream, idx_to_str_);
  92. }
  93. }
  94. void Record(uint64_t name_idx, uint64_t type_idx, ExecutorEvent event,
  95. std::chrono::time_point<std::chrono::system_clock> timestamp) {
  96. if (global_profiler_ != nullptr) {
  97. global_profiler_->Record(name_idx, type_idx, event, timestamp);
  98. }
  99. }
  100. uint64_t RegisterString(const char *name) {
  101. const std::lock_guard<std::mutex> lk(register_mutex_);
  102. std::string str_name = name;
  103. const auto iter = std::find(idx_to_str_.begin(), idx_to_str_.end(), str_name);
  104. if (iter == idx_to_str_.end()) {
  105. idx_to_str_[str_idx_] = str_name;
  106. ++str_idx_;
  107. if (str_idx_ >= idx_to_str_.size()) {
  108. idx_to_str_.resize(idx_to_str_.size() * kDouble);
  109. }
  110. return str_idx_ - 1UL;
  111. } else {
  112. return iter - idx_to_str_.begin();
  113. }
  114. }
  115. private:
  116. GlobalProfilingWrapper();
  117. private:
  118. std::unique_ptr<GlobalProfiler> global_profiler_{nullptr};
  119. uint64_t enable_flags_{0UL};
  120. uint64_t str_idx_{0UL};
  121. std::vector<std::string> idx_to_str_;
  122. std::mutex register_mutex_;
  123. };
  124. } // namespace gert
  125. #endif

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