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.6 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. PropertiesManager::PropertiesManager() : is_inited_(false), delimiter("=") {}
  31. PropertiesManager::~PropertiesManager() {}
  32. // singleton
  33. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY PropertiesManager &PropertiesManager::Instance() {
  34. static PropertiesManager instance;
  35. return instance;
  36. }
  37. // Initialize property configuration
  38. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY 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 %s failed.", file_path.c_str());
  63. return false;
  64. }
  65. std::string line;
  66. while (getline(fs, line)) { // line not with \n
  67. if (!ParseLine(line)) {
  68. GELOGE(PARAM_INVALID, "Parse line failed. content is [%s].", line.c_str());
  69. fs.close();
  70. return false;
  71. }
  72. }
  73. fs.close(); // close the file
  74. GELOGI("LoadFileContent success.");
  75. return true;
  76. }
  77. // Parsing the command line
  78. bool PropertiesManager::ParseLine(const std::string &line) {
  79. std::string temp = Trim(line);
  80. // Comment or newline returns true directly
  81. if (temp.find_first_of('#') == 0 || *(temp.c_str()) == '\n') {
  82. return true;
  83. }
  84. if (!temp.empty()) {
  85. std::string::size_type pos = temp.find_first_of(delimiter);
  86. if (pos == std::string::npos) {
  87. GELOGE(PARAM_INVALID, "Incorrect line [%s], it must include [%s].Perhaps you use illegal chinese symbol",
  88. line.c_str(), delimiter.c_str());
  89. return false;
  90. }
  91. std::string map_key = Trim(temp.substr(0, pos));
  92. std::string value = Trim(temp.substr(pos + 1));
  93. if (map_key.empty() || value.empty()) {
  94. GELOGE(PARAM_INVALID, "Map_key or value empty. %s", line.c_str());
  95. return false;
  96. }
  97. properties_map_[map_key] = value;
  98. }
  99. return true;
  100. }
  101. // Remove the space and tab before and after the string
  102. std::string PropertiesManager::Trim(const std::string &str) {
  103. if (str.empty()) {
  104. return str;
  105. }
  106. std::string::size_type start = str.find_first_not_of(" \t\r\n");
  107. if (start == std::string::npos) {
  108. return str;
  109. }
  110. std::string::size_type end = str.find_last_not_of(" \t\r\n") + 1;
  111. return str.substr(start, end);
  112. }
  113. // Get property value, if not found, return ""
  114. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY std::string PropertiesManager::GetPropertyValue(
  115. const std::string &map_key) {
  116. std::lock_guard<std::mutex> lock(mutex_);
  117. auto iter = properties_map_.find(map_key);
  118. if (properties_map_.end() != iter) {
  119. return iter->second;
  120. }
  121. return "";
  122. }
  123. // Set property value
  124. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void PropertiesManager::SetPropertyValue(const std::string &map_key,
  125. const std::string &value) {
  126. std::lock_guard<std::mutex> lock(mutex_);
  127. properties_map_[map_key] = value;
  128. }
  129. // return properties_map_
  130. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY std::map<std::string, std::string>
  131. PropertiesManager::GetPropertyMap() {
  132. std::lock_guard<std::mutex> lock(mutex_);
  133. return properties_map_;
  134. }
  135. // Set separator
  136. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void PropertiesManager::SetPropertyDelimiter(const std::string &de) {
  137. std::lock_guard<std::mutex> lock(mutex_);
  138. delimiter = de;
  139. }
  140. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY DumpProperties &PropertiesManager::GetDumpProperties(
  141. uint64_t session_id) {
  142. std::lock_guard<std::mutex> lock(mutex_);
  143. // If session_id is not found in dump_properties_map_, operator[] will insert one.
  144. return dump_properties_map_[session_id];
  145. }
  146. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void PropertiesManager::RemoveDumpProperties(uint64_t session_id) {
  147. std::lock_guard<std::mutex> lock(mutex_);
  148. auto iter = dump_properties_map_.find(session_id);
  149. if (iter != dump_properties_map_.end()) {
  150. dump_properties_map_.erase(iter);
  151. }
  152. }
  153. } // namespace ge

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