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 5.0 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
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * Copyright 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 "framework/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. PropertiesManager::PropertiesManager() : is_inited_(false), delimiter("=") {}
  31. PropertiesManager::~PropertiesManager() {}
  32. // singleton
  33. PropertiesManager &PropertiesManager::Instance() {
  34. static PropertiesManager instance;
  35. return instance;
  36. }
  37. // Initialize property configuration
  38. bool PropertiesManager::Init(const std::string &file_path) {
  39. std::lock_guard<std::mutex> lock(mutex_);
  40. if (is_inited_) {
  41. GELOGW("Already inited, will be initialized again");
  42. properties_map_.clear();
  43. is_inited_ = false;
  44. return is_inited_;
  45. }
  46. if (!LoadFileContent(file_path)) {
  47. return false;
  48. }
  49. is_inited_ = true;
  50. return is_inited_;
  51. }
  52. // Load file contents
  53. bool PropertiesManager::LoadFileContent(const std::string &file_path) {
  54. // Normalize the path
  55. string resolved_file_path = RealPath(file_path.c_str());
  56. if (resolved_file_path.empty()) {
  57. DOMI_LOGE("Invalid input file path [%s], make sure that the file path is correct.", file_path.c_str());
  58. return false;
  59. }
  60. std::ifstream fs(resolved_file_path, std::ifstream::in);
  61. if (!fs.is_open()) {
  62. GELOGE(PARAM_INVALID, "[Open][File]Failed, file path %s invalid", file_path.c_str());
  63. REPORT_CALL_ERROR("E19999", "Open file failed, path %s invalid", file_path.c_str());
  64. return false;
  65. }
  66. std::string line;
  67. while (getline(fs, line)) { // line not with \n
  68. if (!ParseLine(line)) {
  69. GELOGE(PARAM_INVALID, "[Parse][Line]Failed, content is %s", line.c_str());
  70. REPORT_CALL_ERROR("E19999", "Parse line failed, content is %s", line.c_str());
  71. fs.close();
  72. return false;
  73. }
  74. }
  75. fs.close(); // close the file
  76. GELOGI("LoadFileContent success.");
  77. return true;
  78. }
  79. // Parsing the command line
  80. bool PropertiesManager::ParseLine(const std::string &line) {
  81. std::string temp = Trim(line);
  82. // Comment or newline returns true directly
  83. if (temp.find_first_of('#') == 0 || *(temp.c_str()) == '\n') {
  84. return true;
  85. }
  86. if (!temp.empty()) {
  87. std::string::size_type pos = temp.find_first_of(delimiter);
  88. if (pos == std::string::npos) {
  89. GELOGE(PARAM_INVALID, "[Check][Param]Incorrect line %s, it must include %s",
  90. line.c_str(), delimiter.c_str());
  91. REPORT_CALL_ERROR("E19999", "Incorrect line %s, it must include %s",
  92. line.c_str(), delimiter.c_str());
  93. return false;
  94. }
  95. std::string map_key = Trim(temp.substr(0, pos));
  96. std::string value = Trim(temp.substr(pos + 1));
  97. if (map_key.empty() || value.empty()) {
  98. GELOGE(PARAM_INVALID, "[Check][Param]Map_key or value empty, line %s", line.c_str());
  99. REPORT_CALL_ERROR("E19999", "Map_key or value empty, line %s", line.c_str());
  100. return false;
  101. }
  102. properties_map_[map_key] = value;
  103. }
  104. return true;
  105. }
  106. // Remove the space and tab before and after the string
  107. std::string PropertiesManager::Trim(const std::string &str) {
  108. if (str.empty()) {
  109. return str;
  110. }
  111. std::string::size_type start = str.find_first_not_of(" \t\r\n");
  112. if (start == std::string::npos) {
  113. return str;
  114. }
  115. std::string::size_type end = str.find_last_not_of(" \t\r\n") + 1;
  116. return str.substr(start, end);
  117. }
  118. // Get property value, if not found, return ""
  119. std::string PropertiesManager::GetPropertyValue(const std::string &map_key) {
  120. std::lock_guard<std::mutex> lock(mutex_);
  121. auto iter = properties_map_.find(map_key);
  122. if (properties_map_.end() != iter) {
  123. return iter->second;
  124. }
  125. return "";
  126. }
  127. // Set property value
  128. void PropertiesManager::SetPropertyValue(const std::string &map_key, const std::string &value) {
  129. std::lock_guard<std::mutex> lock(mutex_);
  130. properties_map_[map_key] = value;
  131. }
  132. // return properties_map_
  133. std::map<std::string, std::string> PropertiesManager::GetPropertyMap() {
  134. std::lock_guard<std::mutex> lock(mutex_);
  135. return properties_map_;
  136. }
  137. // Set separator
  138. void PropertiesManager::SetPropertyDelimiter(const std::string &de) {
  139. std::lock_guard<std::mutex> lock(mutex_);
  140. delimiter = de;
  141. }
  142. } // namespace ge

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