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.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. static GlobalProfilingWrapper *GetInstance() {
  54. static GlobalProfilingWrapper global_prof_wrapper;
  55. return &global_prof_wrapper;
  56. }
  57. static void OnGlobalProfilingSwitch(void *ins, uint64_t enable_flags);
  58. void Init(uint64_t enable_flags);
  59. void Free() {
  60. global_profiler_.reset(nullptr);
  61. SetEnableFlags(0UL);
  62. }
  63. GlobalProfiler *GetGlobalProfiler() const {
  64. return global_profiler_.get();
  65. }
  66. void SetEnableFlags(uint64_t enable_flags) {
  67. enable_flags_ = enable_flags;
  68. }
  69. uint64_t GetRecordCount() {
  70. if (global_profiler_ == nullptr) {
  71. return 0UL;
  72. }
  73. return global_profiler_->GetCount();
  74. }
  75. uint64_t GetEnableFlags() const {
  76. return enable_flags_;
  77. }
  78. bool IsEnable(ProfilingType profiling_type) const {
  79. return enable_flags_ & BuiltInSubscriberUtil::EnableBit<ProfilingType>(profiling_type);
  80. }
  81. void DumpAndFree(std::ostream &out_stream) {
  82. Dump(out_stream);
  83. Free();
  84. }
  85. void Dump(std::ostream &out_stream) {
  86. if (global_profiler_ != nullptr) {
  87. global_profiler_->Dump(out_stream, idx_to_str_);
  88. }
  89. }
  90. void Record(uint64_t name_idx, uint64_t type_idx, ExecutorEvent event,
  91. std::chrono::time_point<std::chrono::system_clock> timestamp) {
  92. if (global_profiler_ != nullptr) {
  93. global_profiler_->Record(name_idx, type_idx, event, timestamp);
  94. }
  95. }
  96. uint64_t RegisterString(const char *name) {
  97. const std::lock_guard<std::mutex> lk(register_mutex_);
  98. std::string str_name = name;
  99. const auto iter = std::find(idx_to_str_.begin(), idx_to_str_.end(), str_name);
  100. if (iter == idx_to_str_.end()) {
  101. idx_to_str_[str_idx_] = str_name;
  102. ++str_idx_;
  103. if (str_idx_ >= idx_to_str_.size()) {
  104. idx_to_str_.resize(idx_to_str_.size() * kDouble);
  105. }
  106. return str_idx_ - 1UL;
  107. } else {
  108. return iter - idx_to_str_.begin();
  109. }
  110. }
  111. private:
  112. GlobalProfilingWrapper();
  113. private:
  114. std::unique_ptr<GlobalProfiler> global_profiler_{nullptr};
  115. uint64_t enable_flags_{0UL};
  116. uint64_t str_idx_{0UL};
  117. std::vector<std::string> idx_to_str_;
  118. std::mutex register_mutex_;
  119. };
  120. } // namespace gert
  121. #endif

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