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