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

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