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.

properties_manager.cc 12 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /**
  2. * Copyright 2019-2020 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 "common/properties_manager.h"
  17. #include <climits>
  18. #include <cstdio>
  19. #include <fstream>
  20. #include "common/ge/ge_util.h"
  21. #include "common/util.h"
  22. #include "framework/common/debug/ge_log.h"
  23. #include "framework/common/debug/log.h"
  24. #include "framework/common/ge_types.h"
  25. #include "framework/common/types.h"
  26. #include "graph/debug/ge_attr_define.h"
  27. #include "graph/ge_context.h"
  28. #include "graph/utils/attr_utils.h"
  29. namespace ge {
  30. namespace {
  31. const string kEnableFlag = "1";
  32. const uint32_t kAicoreOverflow = (0x1 << 0);
  33. const uint32_t kAtomicOverflow = (0x1 << 1);
  34. const uint32_t kAllOverflow = (kAicoreOverflow | kAtomicOverflow);
  35. } // namespace
  36. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY DumpProperties::DumpProperties(const DumpProperties &other) {
  37. CopyFrom(other);
  38. }
  39. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY DumpProperties &DumpProperties::operator=(
  40. const DumpProperties &other) {
  41. CopyFrom(other);
  42. return *this;
  43. }
  44. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void DumpProperties::InitByOptions() {
  45. enable_dump_.clear();
  46. enable_dump_debug_.clear();
  47. dump_path_.clear();
  48. dump_step_.clear();
  49. dump_mode_.clear();
  50. is_op_debug_ = false;
  51. op_debug_mode_ = 0;
  52. string enable_dump;
  53. (void)GetContext().GetOption(OPTION_EXEC_ENABLE_DUMP, enable_dump);
  54. enable_dump_ = enable_dump;
  55. string enable_dump_debug;
  56. (void)GetContext().GetOption(OPTION_EXEC_ENABLE_DUMP_DEBUG, enable_dump_debug);
  57. enable_dump_debug_ = enable_dump_debug;
  58. if ((enable_dump_ == kEnableFlag) || (enable_dump_debug_ == kEnableFlag)) {
  59. string dump_path;
  60. if (GetContext().GetOption(OPTION_EXEC_DUMP_PATH, dump_path) == GRAPH_SUCCESS) {
  61. if (!dump_path.empty() && dump_path[dump_path.size() - 1] != '/') {
  62. dump_path = dump_path + "/";
  63. }
  64. dump_path = dump_path + CurrentTimeInStr() + "/";
  65. GELOGI("Get dump path %s successfully", dump_path.c_str());
  66. SetDumpPath(dump_path);
  67. } else {
  68. GELOGW("DUMP_PATH is not set");
  69. }
  70. }
  71. if (enable_dump_ == kEnableFlag) {
  72. string dump_step;
  73. if (GetContext().GetOption(OPTION_EXEC_DUMP_STEP, dump_step) == GRAPH_SUCCESS) {
  74. GELOGD("Get dump step %s successfully", dump_step.c_str());
  75. SetDumpStep(dump_step);
  76. }
  77. string dump_mode;
  78. if (GetContext().GetOption(OPTION_EXEC_DUMP_MODE, dump_mode) == GRAPH_SUCCESS) {
  79. GELOGD("Get dump mode %s successfully", dump_mode.c_str());
  80. SetDumpMode(dump_mode);
  81. }
  82. AddPropertyValue(DUMP_ALL_MODEL, {});
  83. }
  84. SetDumpDebugOptions();
  85. }
  86. // The following is the new dump scenario of the fusion operator
  87. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void DumpProperties::AddPropertyValue(
  88. const std::string &model, const std::set<std::string> &layers) {
  89. for (const std::string &layer : layers) {
  90. GELOGI("This model %s config to dump layer %s", model.c_str(), layer.c_str());
  91. }
  92. model_dump_properties_map_[model] = layers;
  93. }
  94. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void DumpProperties::DeletePropertyValue(const std::string &model) {
  95. auto iter = model_dump_properties_map_.find(model);
  96. if (iter != model_dump_properties_map_.end()) {
  97. model_dump_properties_map_.erase(iter);
  98. }
  99. }
  100. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY std::set<std::string> DumpProperties::GetAllDumpModel() const {
  101. std::set<std::string> model_list;
  102. for (auto &iter : model_dump_properties_map_) {
  103. model_list.insert(iter.first);
  104. }
  105. return model_list;
  106. }
  107. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY std::set<std::string> DumpProperties::GetPropertyValue(
  108. const std::string &model) const {
  109. auto iter = model_dump_properties_map_.find(model);
  110. if (iter != model_dump_properties_map_.end()) {
  111. return iter->second;
  112. }
  113. return {};
  114. }
  115. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY bool DumpProperties::IsLayerNeedDump(
  116. const std::string &model, const std::string &om_name, const std::string &op_name) const {
  117. // if dump all
  118. if (model_dump_properties_map_.find(DUMP_ALL_MODEL) != model_dump_properties_map_.end()) {
  119. return true;
  120. }
  121. // if this model need dump
  122. auto om_name_iter = model_dump_properties_map_.find(om_name);
  123. auto model_name_iter = model_dump_properties_map_.find(model);
  124. if (om_name_iter != model_dump_properties_map_.end() || model_name_iter != model_dump_properties_map_.end()) {
  125. // if no dump layer info, dump all layer in this model
  126. auto model_iter = om_name_iter != model_dump_properties_map_.end() ? om_name_iter : model_name_iter;
  127. if (model_iter->second.empty()) {
  128. return true;
  129. }
  130. return model_iter->second.find(op_name) != model_iter->second.end();
  131. }
  132. GELOGD("Model %s is not seated to be dump.", model.c_str());
  133. return false;
  134. }
  135. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void DumpProperties::SetDumpPath(const std::string &path) {
  136. dump_path_ = path;
  137. }
  138. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY std::string DumpProperties::GetDumpPath() const { return dump_path_; }
  139. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void DumpProperties::SetDumpStep(const std::string &step) {
  140. dump_step_ = step;
  141. }
  142. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY std::string DumpProperties::GetDumpStep() const { return dump_step_; }
  143. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void DumpProperties::SetDumpMode(const std::string &mode) {
  144. dump_mode_ = mode;
  145. }
  146. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY std::string DumpProperties::GetDumpMode() const { return dump_mode_; }
  147. void DumpProperties::CopyFrom(const DumpProperties &other) {
  148. if (&other != this) {
  149. enable_dump_ = other.enable_dump_;
  150. enable_dump_debug_ = other.enable_dump_debug_;
  151. dump_path_ = other.dump_path_;
  152. dump_step_ = other.dump_step_;
  153. dump_mode_ = other.dump_mode_;
  154. model_dump_properties_map_ = other.model_dump_properties_map_;
  155. is_op_debug_ = other.is_op_debug_;
  156. op_debug_mode_ = other.op_debug_mode_;
  157. }
  158. }
  159. void DumpProperties::SetDumpDebugOptions() {
  160. if (enable_dump_debug_ == kEnableFlag) {
  161. string dump_debug_mode;
  162. if (GetContext().GetOption(OPTION_EXEC_DUMP_DEBUG_MODE, dump_debug_mode) == GRAPH_SUCCESS) {
  163. GELOGD("Get dump debug mode %s successfully", dump_debug_mode.c_str());
  164. } else {
  165. GELOGW("Dump debug mode is not set.");
  166. return;
  167. }
  168. if (dump_debug_mode == OP_DEBUG_AICORE) {
  169. GELOGD("ge.exec.dumpDebugMode=aicore_overflow, op debug is open.");
  170. is_op_debug_ = true;
  171. op_debug_mode_ = kAicoreOverflow;
  172. } else if (dump_debug_mode == OP_DEBUG_ATOMIC) {
  173. GELOGD("ge.exec.dumpDebugMode=atomic_overflow, op debug is open.");
  174. is_op_debug_ = true;
  175. op_debug_mode_ = kAtomicOverflow;
  176. } else if (dump_debug_mode == OP_DEBUG_ALL) {
  177. GELOGD("ge.exec.dumpDebugMode=all, op debug is open.");
  178. is_op_debug_ = true;
  179. op_debug_mode_ = kAllOverflow;
  180. } else {
  181. GELOGW("ge.exec.dumpDebugMode is invalid.");
  182. }
  183. } else {
  184. GELOGI("ge.exec.enableDumpDebug is false or is not set.");
  185. }
  186. }
  187. PropertiesManager::PropertiesManager() : is_inited_(false), delimiter("=") {}
  188. PropertiesManager::~PropertiesManager() {}
  189. // singleton
  190. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY PropertiesManager &PropertiesManager::Instance() {
  191. static PropertiesManager instance;
  192. return instance;
  193. }
  194. // Initialize property configuration
  195. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY bool PropertiesManager::Init(const std::string &file_path) {
  196. std::lock_guard<std::mutex> lock(mutex_);
  197. if (is_inited_) {
  198. GELOGW("Already inited, will be initialized again");
  199. properties_map_.clear();
  200. is_inited_ = false;
  201. return is_inited_;
  202. }
  203. if (!LoadFileContent(file_path)) {
  204. return false;
  205. }
  206. is_inited_ = true;
  207. return is_inited_;
  208. }
  209. // Load file contents
  210. bool PropertiesManager::LoadFileContent(const std::string &file_path) {
  211. // Normalize the path
  212. string resolved_file_path = RealPath(file_path.c_str());
  213. if (resolved_file_path.empty()) {
  214. DOMI_LOGE("Invalid input file path [%s], make sure that the file path is correct.", file_path.c_str());
  215. return false;
  216. }
  217. std::ifstream fs(resolved_file_path, std::ifstream::in);
  218. if (!fs.is_open()) {
  219. GELOGE(PARAM_INVALID, "Open %s failed.", file_path.c_str());
  220. return false;
  221. }
  222. std::string line;
  223. while (getline(fs, line)) { // line not with \n
  224. if (!ParseLine(line)) {
  225. GELOGE(PARAM_INVALID, "Parse line failed. content is [%s].", line.c_str());
  226. fs.close();
  227. return false;
  228. }
  229. }
  230. fs.close(); // close the file
  231. GELOGI("LoadFileContent success.");
  232. return true;
  233. }
  234. // Parsing the command line
  235. bool PropertiesManager::ParseLine(const std::string &line) {
  236. std::string temp = Trim(line);
  237. // Comment or newline returns true directly
  238. if (temp.find_first_of('#') == 0 || *(temp.c_str()) == '\n') {
  239. return true;
  240. }
  241. if (!temp.empty()) {
  242. std::string::size_type pos = temp.find_first_of(delimiter);
  243. if (pos == std::string::npos) {
  244. GELOGE(PARAM_INVALID, "Incorrect line [%s], it must include [%s].Perhaps you use illegal chinese symbol",
  245. line.c_str(), delimiter.c_str());
  246. return false;
  247. }
  248. std::string map_key = Trim(temp.substr(0, pos));
  249. std::string value = Trim(temp.substr(pos + 1));
  250. if (map_key.empty() || value.empty()) {
  251. GELOGE(PARAM_INVALID, "Map_key or value empty. %s", line.c_str());
  252. return false;
  253. }
  254. properties_map_[map_key] = value;
  255. }
  256. return true;
  257. }
  258. // Remove the space and tab before and after the string
  259. std::string PropertiesManager::Trim(const std::string &str) {
  260. if (str.empty()) {
  261. return str;
  262. }
  263. std::string::size_type start = str.find_first_not_of(" \t\r\n");
  264. if (start == std::string::npos) {
  265. return str;
  266. }
  267. std::string::size_type end = str.find_last_not_of(" \t\r\n") + 1;
  268. return str.substr(start, end);
  269. }
  270. // Get property value, if not found, return ""
  271. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY std::string PropertiesManager::GetPropertyValue(
  272. const std::string &map_key) {
  273. std::lock_guard<std::mutex> lock(mutex_);
  274. auto iter = properties_map_.find(map_key);
  275. if (properties_map_.end() != iter) {
  276. return iter->second;
  277. }
  278. return "";
  279. }
  280. // Set property value
  281. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void PropertiesManager::SetPropertyValue(const std::string &map_key,
  282. const std::string &value) {
  283. std::lock_guard<std::mutex> lock(mutex_);
  284. properties_map_[map_key] = value;
  285. }
  286. // return properties_map_
  287. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY std::map<std::string, std::string>
  288. PropertiesManager::GetPropertyMap() {
  289. std::lock_guard<std::mutex> lock(mutex_);
  290. return properties_map_;
  291. }
  292. // Set separator
  293. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void PropertiesManager::SetPropertyDelimiter(const std::string &de) {
  294. std::lock_guard<std::mutex> lock(mutex_);
  295. delimiter = de;
  296. }
  297. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY DumpProperties &PropertiesManager::GetDumpProperties(
  298. uint64_t session_id) {
  299. std::lock_guard<std::mutex> lock(mutex_);
  300. // If session_id is not found in dump_properties_map_, operator[] will insert one.
  301. return dump_properties_map_[session_id];
  302. }
  303. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void PropertiesManager::RemoveDumpProperties(uint64_t session_id) {
  304. std::lock_guard<std::mutex> lock(mutex_);
  305. auto iter = dump_properties_map_.find(session_id);
  306. if (iter != dump_properties_map_.end()) {
  307. dump_properties_map_.erase(iter);
  308. }
  309. }
  310. } // namespace ge

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