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.

ascend_profiling.cc 6.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**
  2. * Copyright 2021 Huawei Technologies Co., Ltd
  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. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #include "profiler/device/ascend/ascend_profiling.h"
  16. #include <map>
  17. #include <string>
  18. #include "pybind_api/api_register.h"
  19. #include "utils/log_adapter.h"
  20. #include "utils/utils.h"
  21. #include "profiler/device/ascend/memory_profiling.h"
  22. #include "runtime/device/ascend/profiling/profiling_manager.h"
  23. #include <nlohmann/json.hpp>
  24. using mindspore::device::ascend::ProfilingManager;
  25. using mindspore::profiler::ascend::MemoryProfiling;
  26. namespace mindspore {
  27. namespace profiler {
  28. namespace ascend {
  29. std::map<std::string, aclprofAicoreMetrics> kAicMetrics{
  30. {"ArithmeticUtilization", ACL_AICORE_ARITHMETIC_UTILIZATION},
  31. {"PipeUtilization", ACL_AICORE_PIPE_UTILIZATION},
  32. {"Memory", ACL_AICORE_MEMORY_BANDWIDTH},
  33. {"MemoryLO", ACL_AICORE_L0B_AND_WIDTH},
  34. {"ResourceConflictRatio", ACL_AICORE_RESOURCE_CONFLICT_RATIO},
  35. };
  36. std::shared_ptr<AscendProfiler> AscendProfiler::ascend_profiler_ = std::make_shared<AscendProfiler>();
  37. std::shared_ptr<AscendProfiler> &AscendProfiler::GetInstance() { return ascend_profiler_; }
  38. void AscendProfiler::StepProfilingEnable(const bool enable_flag) {
  39. MS_LOG(INFO) << "Start profiling";
  40. enable_flag_ = enable_flag;
  41. }
  42. void AscendProfiler::InitProfiling(const std::string &profiling_path, uint32_t device_id,
  43. const std::string &profiling_options) {
  44. MS_LOG(INFO) << "Begin to init profiling and call aclprofInit function.";
  45. profiling_options_ = profiling_options;
  46. profile_data_path_ = profiling_path;
  47. device_id_ = device_id;
  48. (void)ProfilingManager::GetInstance().InitProfiling(profiling_path, device_id);
  49. MemoryProfiling::GetInstance().SetMemoryProfilingInitialize(profiling_options_);
  50. aclError aclRet = aclprofInit(profile_data_path_.c_str(), profile_data_path_.length());
  51. if (aclRet != ACL_SUCCESS) {
  52. MS_LOG(EXCEPTION) << "Failed to call aclprofInit function.";
  53. }
  54. }
  55. uint64_t AscendProfiler::GetOptionsMask() const {
  56. uint64_t mask = ACL_PROF_ACL_API | ACL_PROF_AICORE_METRICS;
  57. nlohmann::json options_json;
  58. try {
  59. options_json = nlohmann::json::parse(profiling_options_);
  60. } catch (const std::exception &err) {
  61. MS_LOG(ERROR) << "Failed to parse profiling options.";
  62. return ACL_AICORE_NONE;
  63. }
  64. if (options_json["task_trace"] == "on") {
  65. mask |= ACL_PROF_TASK_TIME;
  66. }
  67. if (options_json["aicpu"] == "on") {
  68. mask |= ACL_PROF_AICPU;
  69. }
  70. return mask;
  71. }
  72. aclprofAicoreMetrics AscendProfiler::GetAicMetrics() const {
  73. nlohmann::json options_json;
  74. try {
  75. options_json = nlohmann::json::parse(profiling_options_);
  76. } catch (const std::exception &err) {
  77. MS_LOG(ERROR) << "Failed to parse profiling options.";
  78. return ACL_AICORE_NONE;
  79. }
  80. auto result = std::find_if(kAicMetrics.begin(), kAicMetrics.end(), [&options_json](const auto &metric) {
  81. return metric.first == options_json["aic_metrics"];
  82. });
  83. if (result == kAicMetrics.end()) {
  84. return ACL_AICORE_NONE;
  85. }
  86. return result->second;
  87. }
  88. void AscendProfiler::Start() {
  89. uint32_t device_list[1] = {device_id_};
  90. uint32_t device_num = 1;
  91. uint64_t mask = GetOptionsMask();
  92. aclprofAicoreMetrics aic_metrics = GetAicMetrics();
  93. acl_config_ = aclprofCreateConfig(device_list, device_num, aic_metrics, nullptr, GetOptionsMask());
  94. if (acl_config_ == nullptr) {
  95. MS_LOG(EXCEPTION) << "Failed to call aclprofCreateConfig function.";
  96. }
  97. aclError aclRet = aclprofStart(acl_config_);
  98. if (aclRet != ACL_SUCCESS) {
  99. MS_LOG(EXCEPTION) << "Failed to call aclprofStart function.";
  100. }
  101. MS_LOG(INFO) << "Start profiling, options mask is " << mask << " aic_metrics is " << aic_metrics;
  102. MemoryProfiling::GetInstance().StartMemoryProfiling();
  103. StepProfilingEnable(true);
  104. }
  105. void AscendProfiler::Stop() {
  106. MS_LOG(INFO) << "Begin to stop profiling.";
  107. if (acl_config_ == nullptr) {
  108. MS_LOG(EXCEPTION)
  109. << "Failed to stop profiling because of null acl config.Please make sure call Profiler.Start function "
  110. "before call Profiler.Stop function.";
  111. }
  112. aclError aclRet = aclprofStop(acl_config_);
  113. if (aclRet != ACL_SUCCESS) {
  114. MS_LOG(EXCEPTION) << "Failed to call aclprofStop function.";
  115. }
  116. aclRet = aclprofDestroyConfig(acl_config_);
  117. if (aclRet != ACL_SUCCESS) {
  118. MS_LOG(EXCEPTION) << "Failed to call aclprofDestroyConfig function.";
  119. }
  120. MemoryProfiling::GetInstance().StopMemoryProfiling();
  121. StepProfilingEnable(false);
  122. }
  123. void AscendProfiler::Finalize() const {
  124. MS_LOG(INFO) << "Begin to finalize profiling";
  125. aclError aclRet = aclprofFinalize();
  126. if (aclRet != ACL_SUCCESS) {
  127. MS_LOG(EXCEPTION) << "Failed to call aclprofDestroyConfig function.";
  128. }
  129. }
  130. REGISTER_PYBIND_DEFINE(AscendProfiler_, ([](const py::module *m) {
  131. (void)py::class_<AscendProfiler, std::shared_ptr<AscendProfiler>>(*m, "AscendProfiler")
  132. .def_static("get_instance", &AscendProfiler::GetInstance, "AscendProfiler get_instance.")
  133. .def("init", &AscendProfiler::InitProfiling, py::arg("profiling_path"), py::arg("device_id"),
  134. py::arg("profiling_options"), "init")
  135. .def("start", &AscendProfiler::Start, "start")
  136. .def("stop", &AscendProfiler::Stop, "stop")
  137. .def("finalize", &AscendProfiler::Finalize, "finalize");
  138. }));
  139. } // namespace ascend
  140. } // namespace profiler
  141. } // namespace mindspore