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.

analyzer.cc 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 "analyzer.h"
  17. #include <cstdlib>
  18. #include <cstdio>
  19. #include <iostream>
  20. #include "framework/common/debug/ge_log.h"
  21. #include "framework/common/util.h"
  22. #include "graph/utils/graph_utils.h"
  23. #include "graph/utils/node_utils.h"
  24. #include "graph/utils/type_utils.h"
  25. namespace ge {
  26. using json = nlohmann::json;
  27. using Status = ge::Status;
  28. using ComputeGraph = ge::ComputeGraph;
  29. using namespace analyzer;
  30. namespace {
  31. constexpr int kFileAuthority = 0640;
  32. constexpr int kJsonDumpLevel = 4;
  33. const std::string kFilePath = "./";
  34. const std::string kAnalyzeFile = "ge_check_op.json";
  35. const std::string kUnknownShape = "unknownshape";
  36. const std::string kUnsupport = "unsupport";
  37. const std::string kSessionId = "session_id";
  38. const std::string kGraphId = "graph_id";
  39. const std::string kOpInfo = "op_info";
  40. const std::string kErrorType = "error_type";
  41. const std::string kOpName = "name";
  42. const std::string kOpType = "type";
  43. const std::string kReason = "reason";
  44. const std::string kInput = "input";
  45. const std::string kOutput = "output";
  46. const std::string kShape = "shape";
  47. const std::string kDataType = "data_type";
  48. const std::string kLayout = "layout";
  49. const std::string kResult = "result";
  50. const std::string kOp = "op";
  51. std::map<analyzer::AnalyzeType, std::string> errors_map{{PARSER, "paser_error"},
  52. {INFER_SHAPE, "infer_shape_error"},
  53. {CHECKSUPPORT, "check_support_error"},
  54. {GRAPH_OPTIMIZE, "graph_optimize_error"},
  55. {GRAPH_PARTION, "graph_partion_error"},
  56. {GRAPH_BUILDER, "graph_builder_error"}};
  57. } // namespace
  58. Analyzer *Analyzer::GetInstance() {
  59. static Analyzer instance;
  60. return &instance;
  61. }
  62. Status Analyzer::BuildJsonObject(uint64_t session_id, uint64_t graph_id) {
  63. GELOGD("Start to build map. SessionId:%lu GraphId:%lu", session_id, graph_id);
  64. std::lock_guard<std::recursive_mutex> lg(mutex_);
  65. auto iter = graph_infos_.find(session_id);
  66. if (iter == graph_infos_.end()) {
  67. auto p = new (std::nothrow) GraphInfo();
  68. GE_CHECK_NOTNULL(p);
  69. std::shared_ptr<GraphInfo> graph_info(p);
  70. std::map<uint64_t, std::shared_ptr<GraphInfo>> graph_map;
  71. graph_map[graph_id] = graph_info;
  72. graph_info->session_id = session_id;
  73. graph_info->graph_id = graph_id;
  74. graph_infos_.insert({session_id, graph_map});
  75. } else {
  76. auto iter1 = (iter->second).find(graph_id);
  77. if (iter1 == (iter->second).end()) {
  78. auto p = new (std::nothrow) GraphInfo();
  79. GE_CHECK_NOTNULL(p);
  80. std::shared_ptr<GraphInfo> graph_info(p);
  81. graph_info->session_id = session_id;
  82. graph_info->graph_id = graph_id;
  83. (iter->second).insert({graph_id, graph_info});
  84. } else {
  85. GELOGI("session_id:%lu graph_id:%lu already existed json object", session_id, graph_id);
  86. }
  87. }
  88. return SUCCESS;
  89. }
  90. ge::Status Analyzer::Initialize() {
  91. ClearHistoryFile();
  92. return CreateAnalyzerFile();
  93. }
  94. void Analyzer::Finalize() {
  95. GELOGD("Analyzer start to finalize!");
  96. std::lock_guard<std::recursive_mutex> lg(mutex_);
  97. for (auto &session_resource : graph_infos_) {
  98. session_resource.second.clear();
  99. }
  100. graph_infos_.clear();
  101. std::lock_guard<std::mutex> lk(file_mutex_);
  102. if (json_file_.is_open()) {
  103. json_file_.close();
  104. }
  105. }
  106. void Analyzer::DestroySessionJsonObject(uint64_t session_id) {
  107. std::lock_guard<std::recursive_mutex> lg(mutex_);
  108. auto iter = graph_infos_.find(session_id);
  109. if (iter == graph_infos_.end()) {
  110. GELOGW("can not find the stored object by session_id[%lu].Do nothing", session_id);
  111. } else {
  112. graph_infos_.erase(iter);
  113. }
  114. }
  115. void Analyzer::DestroyGraphJsonObject(uint64_t session_id, uint64_t graph_id) {
  116. std::lock_guard<std::recursive_mutex> lg(mutex_);
  117. auto iter = graph_infos_.find(session_id);
  118. if (iter == graph_infos_.end()) {
  119. GELOGW("can not find the stored object by session_id[%lu].Do nothing", session_id);
  120. } else {
  121. auto iter1 = (iter->second).find(graph_id);
  122. if (iter1 == (iter->second).end()) {
  123. GELOGW("can not find the graph json object by session_id[%lu] and graph_id[%lu].Do nothing", session_id,
  124. graph_id);
  125. }
  126. (iter->second).erase(iter1);
  127. }
  128. }
  129. std::shared_ptr<GraphInfo> Analyzer::GetJsonObject(uint64_t session_id, uint64_t graph_id) {
  130. std::lock_guard<std::recursive_mutex> lg(mutex_);
  131. auto iter = graph_infos_.find(session_id);
  132. if (iter == graph_infos_.end()) {
  133. GELOGE(PARAM_INVALID, "session_id:%lu does not exist!", session_id);
  134. return nullptr;
  135. } else {
  136. auto iter1 = (iter->second).find(graph_id);
  137. if (iter1 == (iter->second).end()) {
  138. GELOGE(PARAM_INVALID, "graph_id:%lu does not exist!", graph_id);
  139. return nullptr;
  140. }
  141. GELOGI("GetJsonObject Success!session_id:%lu graph_id:%lu", session_id, graph_id);
  142. return iter1->second;
  143. }
  144. }
  145. void Analyzer::ClearHistoryFile() {
  146. GELOGD("Analyzer start to clear history file!");
  147. // Remove history files
  148. int res = remove(json_file_name_.c_str());
  149. GELOGD("remove file %s, result:%d", json_file_name_.c_str(), res);
  150. }
  151. ge::Status Analyzer::CreateAnalyzerFile() {
  152. GELOGD("start to create analyzer file!");
  153. // Check whether the manifest exists, if not, create it.
  154. string real_path = RealPath(kFilePath.c_str());
  155. if (real_path.empty()) {
  156. GELOGE(FAILED, "File path is invalid.");
  157. return FAILED;
  158. }
  159. string file = real_path + "/" + kAnalyzeFile;
  160. GELOGD("Created analyzer file:[%s]", file.c_str());
  161. int fd = open(file.c_str(), O_WRONLY | O_CREAT | O_TRUNC, kFileAuthority);
  162. if (fd < 0) {
  163. GELOGE(INTERNAL_ERROR, "Fail to open the file: %s.", file.c_str());
  164. return INTERNAL_ERROR;
  165. }
  166. if (close(fd) != 0) {
  167. GELOGE(INTERNAL_ERROR, "Fail to close the file: %s.", file.c_str());
  168. return INTERNAL_ERROR;
  169. }
  170. json_file_name_ = file;
  171. GELOGD("success to create analyzer file[%s]!", json_file_name_.c_str());
  172. return SUCCESS;
  173. }
  174. ge::Status Analyzer::SaveAnalyzerDataToFile() {
  175. GELOGD("start to save analyze file!");
  176. std::lock_guard<std::mutex> lg(file_mutex_);
  177. json_file_.open(json_file_name_, std::ios::out);
  178. if (!json_file_.is_open()) {
  179. GELOGE(FAILED, "analyzer file does not exist[%s]", json_file_name_.c_str());
  180. return PARAM_INVALID;
  181. }
  182. std::lock_guard<std::recursive_mutex> lk(mutex_);
  183. for (auto &ele : graph_infos_) {
  184. for (auto &ele2 : ele.second) {
  185. json jsn;
  186. GraphInfoToJson(jsn, *(ele2.second));
  187. json_file_ << jsn.dump(kJsonDumpLevel) << std::endl;
  188. }
  189. }
  190. json_file_.close();
  191. return SUCCESS;
  192. }
  193. ge::Status Analyzer::DoAnalyze(DataInfo &data_info) {
  194. GELOGD("start to do analyzer!");
  195. auto pnode = data_info.node_ptr;
  196. GE_CHECK_NOTNULL(pnode);
  197. auto desc = pnode->GetOpDesc();
  198. GE_CHECK_NOTNULL(desc);
  199. // buff analyze data
  200. std::lock_guard<std::recursive_mutex> lg(mutex_);
  201. auto graph_info = GetJsonObject(data_info.session_id, data_info.graph_id);
  202. GE_CHECK_NOTNULL(graph_info);
  203. auto status = SaveOpInfo(desc, data_info, graph_info);
  204. if (status != SUCCESS) {
  205. GELOGE(status, "save op info failed!");
  206. return FAILED;
  207. }
  208. // save data to file
  209. return SaveAnalyzerDataToFile();
  210. }
  211. ge::Status Analyzer::SaveOpInfo(ge::OpDescPtr desc, DataInfo &data_info,
  212. std::shared_ptr<analyzer::GraphInfo> graph_info) {
  213. auto iter = errors_map.find(data_info.analyze_type);
  214. if (iter == errors_map.end()) {
  215. return PARAM_INVALID;
  216. }
  217. OpInfo op_info;
  218. op_info.error_type = iter->second;
  219. op_info.op_name = desc->GetName();
  220. op_info.op_type = desc->GetType();
  221. op_info.reason = data_info.reason;
  222. for (const auto &ptr : desc->GetAllInputsDescPtr()) {
  223. TensorInfo tensor_info;
  224. tensor_info.shape = ptr->GetShape().GetDims();
  225. tensor_info.d_type = ge::TypeUtils::DataTypeToSerialString(ptr->GetDataType());
  226. tensor_info.layout = ge::TypeUtils::FormatToSerialString(ptr->GetFormat());
  227. op_info.input_info.emplace_back(tensor_info);
  228. }
  229. for (const auto &ptr : desc->GetAllOutputsDescPtr()) {
  230. TensorInfo tensor_info;
  231. tensor_info.shape = ptr->GetShape().GetDims();
  232. tensor_info.d_type = ge::TypeUtils::DataTypeToSerialString(ptr->GetDataType());
  233. tensor_info.layout = ge::TypeUtils::FormatToSerialString(ptr->GetFormat());
  234. op_info.output_info.emplace_back(tensor_info);
  235. }
  236. graph_info->op_info.emplace_back(op_info);
  237. return SUCCESS;
  238. }
  239. void Analyzer::TensorInfoToJson(json &j, const TensorInfo &tensor_info) {
  240. j[kShape] = tensor_info.shape;
  241. j[kDataType] = tensor_info.d_type;
  242. j[kLayout] = tensor_info.layout;
  243. }
  244. void Analyzer::OpInfoToJson(json &j, const OpInfo &op_info) {
  245. j[kErrorType] = op_info.error_type;
  246. j[kOpName] = op_info.op_name;
  247. j[kOpType] = op_info.op_type;
  248. j[kReason] = op_info.reason;
  249. for (size_t i = 0; i < op_info.input_info.size(); i++) {
  250. json json_tensor_info;
  251. TensorInfoToJson(json_tensor_info, op_info.input_info.at(i));
  252. j[kInput + std::to_string(i)] = json_tensor_info;
  253. }
  254. for (size_t i = 0; i < op_info.output_info.size(); i++) {
  255. json json_tensor_info;
  256. TensorInfoToJson(json_tensor_info, op_info.output_info.at(i));
  257. j[kOutput + std::to_string(i)] = json_tensor_info;
  258. }
  259. }
  260. void Analyzer::GraphInfoToJson(json &j, const GraphInfo &graph_info) {
  261. GELOGD("start to buff graph info!");
  262. j[kSessionId] = graph_info.session_id;
  263. j[kGraphId] = graph_info.graph_id;
  264. std::vector<json> json_op_infos;
  265. for (size_t i = 0; i < graph_info.op_info.size(); i++) {
  266. json json_op_info;
  267. OpInfoToJson(json_op_info, graph_info.op_info.at(i));
  268. json_op_infos.emplace_back(json_op_info);
  269. }
  270. j[kOp] = json_op_infos;
  271. }
  272. } // namespace ge

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