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.

lic_manager.cc 5.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. * 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. #include "runtime/device/ascend/lic_manager.h"
  17. #include <regex>
  18. #include "utils/ms_context.h"
  19. #include "runtime/dev.h"
  20. #include "opt_info/opt_info.h"
  21. namespace mindspore {
  22. namespace {
  23. constexpr auto kFeKey = "opt_module.fe";
  24. constexpr auto kOpTuneKey = "opt_module.op_tune";
  25. constexpr auto kPassKey = "opt_module.pass";
  26. constexpr auto kRlTuneKey = "opt_module.rl_tune";
  27. constexpr auto kAllOpen = "ALL";
  28. static const std::map<std::string, OptPassEnum> kPassCodeMap = {
  29. {std::to_string(3), OptPassEnum::MatmulBiasaddFusion},
  30. {std::to_string(9), OptPassEnum::TransposeReshapeFusion},
  31. {std::to_string(15), OptPassEnum::BnupdateEltwiseEltwiseFusionPass},
  32. {std::to_string(16), OptPassEnum::BnupdateEltwiseFusionPass},
  33. {std::to_string(17), OptPassEnum::Conv2DBackpropEltwiseFusionPass},
  34. {std::to_string(18), OptPassEnum::ConvBnReduceFusionPass},
  35. {std::to_string(26), OptPassEnum::ReshapeTransposeFusion},
  36. {std::to_string(27), OptPassEnum::SquareSumFusion},
  37. {std::to_string(30), OptPassEnum::MatmulEltwiseFusionPass},
  38. {std::to_string(33), OptPassEnum::BatchMatmulFusedMulAddFusionPass},
  39. {std::to_string(34), OptPassEnum::EltwiseFusionPass},
  40. {std::to_string(36), OptPassEnum::MultiOutputFusionPass},
  41. {std::to_string(37), OptPassEnum::MulAddFusion},
  42. {std::to_string(39), OptPassEnum::ClipByNormNoDivSquareSumFusion},
  43. {std::to_string(42), OptPassEnum::MulAddNPass},
  44. {std::to_string(43), OptPassEnum::Resnet50DbnDwFusionPass},
  45. {std::to_string(45), OptPassEnum::MatmulConfusiontransposeUbFusion},
  46. {std::to_string(47), OptPassEnum::TbeBatchMatmulElementWiseFusionPass},
  47. };
  48. inline std::vector<std::string> SplitStrByRegex(const std::string &str, const std::string &regex) {
  49. std::regex split(regex);
  50. return std::vector<std::string>(std::sregex_token_iterator(str.begin(), str.end(), split, -1),
  51. std::sregex_token_iterator());
  52. }
  53. static std::string GetSocVersion() {
  54. constexpr int kSocVersionLen = 50;
  55. char soc_version[kSocVersionLen] = {0};
  56. auto ret = rtGetSocVersion(soc_version, kSocVersionLen);
  57. if (ret != RT_ERROR_NONE) {
  58. MS_LOG(WARNING) << "rtGetSocVersion failed, ret = " << ret;
  59. return "Ascend910";
  60. }
  61. return soc_version;
  62. }
  63. } // namespace
  64. LicManager::LicManager() { ParseSwitch(); }
  65. LicManager &LicManager::GetInstance() {
  66. static LicManager lic_manager{};
  67. return lic_manager;
  68. }
  69. bool LicManager::GetPassSwitch(OptPassEnum pass) const {
  70. auto iter = pass_switch_.find(pass);
  71. if (iter == pass_switch_.end()) {
  72. return true;
  73. }
  74. return iter->second;
  75. }
  76. void LicManager::ParseSwitch() {
  77. std::map<std::string, std::string> opt_info_map;
  78. auto ret = gelc::GetOptInfo(0, GetSocVersion(), opt_info_map);
  79. if (ret != 0) {
  80. MS_LOG(WARNING) << "GetOptInfo failed.";
  81. return;
  82. }
  83. ParseFeSwitch(opt_info_map);
  84. ParseOpTuneSwitch(opt_info_map);
  85. ParsePassSwitch(opt_info_map);
  86. ParseRlSwitch(opt_info_map);
  87. }
  88. void LicManager::ParseFeSwitch(const std::map<std::string, std::string> &options_map) {
  89. // no fe switch, open all
  90. auto options_iter = options_map.find(kFeKey);
  91. if (options_iter == options_map.end()) {
  92. return;
  93. }
  94. // "All" in options means all open, do nothing.
  95. const auto &options_str = options_iter->second;
  96. if (options_str.find(kAllOpen) != std::string::npos) {
  97. return;
  98. }
  99. // close all first
  100. for (auto iter = kPassCodeMap.begin(); iter != kPassCodeMap.end(); ++iter) {
  101. auto pass = iter->second;
  102. pass_switch_.emplace(pass, false);
  103. }
  104. // then open passes in options
  105. auto fe_pass = SplitStrByRegex(options_str, ":");
  106. for (auto &pass_code : fe_pass) {
  107. auto iter = kPassCodeMap.find(pass_code);
  108. if (iter != kPassCodeMap.end()) {
  109. pass_switch_[iter->second] = true;
  110. }
  111. }
  112. }
  113. void LicManager::ParseOpTuneSwitch(const std::map<std::string, std::string> &options_map) {
  114. auto options_iter = options_map.find(kOpTuneKey);
  115. if (options_iter == options_map.end()) {
  116. op_tune_switch_ = "null";
  117. return;
  118. }
  119. const auto &op_tune_str = options_iter->second;
  120. if (op_tune_str.empty()) {
  121. op_tune_switch_ = "off";
  122. op_tune_list_.clear();
  123. } else {
  124. op_tune_switch_ = "on";
  125. op_tune_list_ = op_tune_str;
  126. }
  127. }
  128. void LicManager::ParsePassSwitch(const std::map<std::string, std::string> &options_map) {
  129. auto options_iter = options_map.find(kPassKey);
  130. if (options_iter == options_map.end()) {
  131. pass_list_ = "invalid";
  132. return;
  133. }
  134. const auto &pass_str = options_iter->second;
  135. if (!pass_str.empty()) {
  136. pass_list_ = pass_str;
  137. }
  138. }
  139. void LicManager::ParseRlSwitch(const std::map<std::string, std::string> &options_map) {
  140. auto options_iter = options_map.find(kRlTuneKey);
  141. if (options_iter == options_map.end()) {
  142. rl_tune_switch_ = "null";
  143. return;
  144. }
  145. const auto &rl_tune_str = options_iter->second;
  146. if (rl_tune_str.empty()) {
  147. rl_tune_switch_ = "off";
  148. rl_tune_list_.clear();
  149. } else {
  150. rl_tune_switch_ = "on";
  151. rl_tune_list_ = rl_tune_str;
  152. }
  153. }
  154. } // namespace mindspore