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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 SUCCESS;
  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. if (is_json_file_create_) {
  153. GELOGD("analyzer file has been created!No necessary to create again!");
  154. return SUCCESS;
  155. }
  156. GELOGD("start to create analyzer file!");
  157. // Check whether the manifest exists, if not, create it.
  158. string real_path = RealPath(kFilePath.c_str());
  159. if (real_path.empty()) {
  160. GELOGE(FAILED, "File path is invalid.");
  161. return FAILED;
  162. }
  163. std::lock_guard<std::mutex> lg(file_mutex_);
  164. json_file_name_ = real_path + "/" + kAnalyzeFile;
  165. GELOGD("Created analyzer file:[%s]", json_file_name_.c_str());
  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() {
  180. GELOGD("start to save analyze file!");
  181. std::lock_guard<std::mutex> lg(file_mutex_);
  182. json_file_.open(json_file_name_, std::ios::out);
  183. if (!json_file_.is_open()) {
  184. GELOGE(FAILED, "analyzer file does not exist[%s]", json_file_name_.c_str());
  185. return PARAM_INVALID;
  186. }
  187. std::lock_guard<std::recursive_mutex> lk(mutex_);
  188. for (auto &ele : graph_infos_) {
  189. for (auto &ele2 : ele.second) {
  190. json jsn;
  191. GraphInfoToJson(jsn, *(ele2.second));
  192. json_file_ << jsn.dump(kJsonDumpLevel) << std::endl;
  193. }
  194. }
  195. json_file_.close();
  196. return SUCCESS;
  197. }
  198. ge::Status Analyzer::DoAnalyze(DataInfo &data_info) {
  199. GELOGD("start to do analyzer!");
  200. auto pnode = data_info.node_ptr;
  201. GE_CHECK_NOTNULL(pnode);
  202. auto desc = pnode->GetOpDesc();
  203. GE_CHECK_NOTNULL(desc);
  204. // buff analyze data
  205. std::lock_guard<std::recursive_mutex> lg(mutex_);
  206. auto graph_info = GetJsonObject(data_info.session_id, data_info.graph_id);
  207. GE_CHECK_NOTNULL(graph_info);
  208. auto status = SaveOpInfo(desc, data_info, graph_info);
  209. if (status != SUCCESS) {
  210. GELOGE(status, "save op info failed!");
  211. return FAILED;
  212. }
  213. // create json file
  214. status = CreateAnalyzerFile();
  215. if (status != SUCCESS) {
  216. GELOGE(status, "create analyzer file failed!");
  217. return status;
  218. }
  219. // save data to file
  220. return SaveAnalyzerDataToFile();
  221. }
  222. ge::Status Analyzer::SaveOpInfo(ge::OpDescPtr desc, DataInfo &data_info,
  223. std::shared_ptr<analyzer::GraphInfo> graph_info) {
  224. auto iter = errors_map.find(data_info.analyze_type);
  225. if (iter == errors_map.end()) {
  226. return PARAM_INVALID;
  227. }
  228. OpInfo op_info;
  229. op_info.error_type = iter->second;
  230. op_info.op_name = desc->GetName();
  231. op_info.op_type = desc->GetType();
  232. op_info.reason = data_info.reason;
  233. for (const auto &ptr : desc->GetAllInputsDescPtr()) {
  234. TensorInfo tensor_info;
  235. tensor_info.shape = ptr->GetShape().GetDims();
  236. tensor_info.d_type = ge::TypeUtils::DataTypeToSerialString(ptr->GetDataType());
  237. tensor_info.layout = ge::TypeUtils::FormatToSerialString(ptr->GetFormat());
  238. op_info.input_info.emplace_back(tensor_info);
  239. }
  240. for (const auto &ptr : desc->GetAllOutputsDescPtr()) {
  241. TensorInfo tensor_info;
  242. tensor_info.shape = ptr->GetShape().GetDims();
  243. tensor_info.d_type = ge::TypeUtils::DataTypeToSerialString(ptr->GetDataType());
  244. tensor_info.layout = ge::TypeUtils::FormatToSerialString(ptr->GetFormat());
  245. op_info.output_info.emplace_back(tensor_info);
  246. }
  247. graph_info->op_info.emplace_back(op_info);
  248. return SUCCESS;
  249. }
  250. void Analyzer::TensorInfoToJson(json &j, const TensorInfo &tensor_info) {
  251. j[kShape] = tensor_info.shape;
  252. j[kDataType] = tensor_info.d_type;
  253. j[kLayout] = tensor_info.layout;
  254. }
  255. void Analyzer::OpInfoToJson(json &j, const OpInfo &op_info) {
  256. j[kErrorType] = op_info.error_type;
  257. j[kOpName] = op_info.op_name;
  258. j[kOpType] = op_info.op_type;
  259. j[kReason] = op_info.reason;
  260. for (size_t i = 0; i < op_info.input_info.size(); i++) {
  261. json json_tensor_info;
  262. TensorInfoToJson(json_tensor_info, op_info.input_info.at(i));
  263. j[kInput + std::to_string(i)] = json_tensor_info;
  264. }
  265. for (size_t i = 0; i < op_info.output_info.size(); i++) {
  266. json json_tensor_info;
  267. TensorInfoToJson(json_tensor_info, op_info.output_info.at(i));
  268. j[kOutput + std::to_string(i)] = json_tensor_info;
  269. }
  270. }
  271. void Analyzer::GraphInfoToJson(json &j, const GraphInfo &graph_info) {
  272. GELOGD("start to buff graph info!");
  273. j[kSessionId] = graph_info.session_id;
  274. j[kGraphId] = graph_info.graph_id;
  275. std::vector<json> json_op_infos;
  276. for (size_t i = 0; i < graph_info.op_info.size(); i++) {
  277. json json_op_info;
  278. OpInfoToJson(json_op_info, graph_info.op_info.at(i));
  279. json_op_infos.emplace_back(json_op_info);
  280. }
  281. j[kOp] = json_op_infos;
  282. }
  283. } // namespace ge

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