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

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

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