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.h 5.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. #ifndef DOMI_ANALYZER_ANANLYZER_H_
  17. #define DOMI_ANALYZER_ANANLYZER_H_
  18. #include "nlohmann/json.hpp"
  19. #include <map>
  20. #include <string>
  21. #include <mutex>
  22. #include <memory>
  23. #include <fstream>
  24. #include <atomic>
  25. #include "external/ge/ge_api_types.h"
  26. #include "graph/compute_graph.h"
  27. #include "graph/node.h"
  28. namespace ge {
  29. namespace analyzer {
  30. enum AnalyzeType {
  31. PARSER = 0,
  32. INFER_SHAPE = 1,
  33. CHECKSUPPORT = 2,
  34. GRAPH_OPTIMIZE = 3,
  35. GRAPH_PARTION = 4,
  36. GRAPH_BUILDER = 5,
  37. };
  38. struct TensorInfo {
  39. vector<int64_t> shape;
  40. string d_type;
  41. string layout;
  42. };
  43. struct OpInfo {
  44. string error_type;
  45. string op_name;
  46. string op_type;
  47. std::vector<TensorInfo> input_info;
  48. std::vector<TensorInfo> output_info;
  49. string reason;
  50. };
  51. struct GraphInfo {
  52. uint64_t session_id = 0;
  53. uint64_t graph_id = 0;
  54. std::vector<OpInfo> op_info;
  55. };
  56. struct DataInfo {
  57. DataInfo() = default;
  58. ~DataInfo() = default;
  59. DataInfo(uint64_t sess, uint64_t graph, AnalyzeType type, ge::NodePtr node, std::string error_info) {
  60. session_id = sess;
  61. graph_id = graph;
  62. analyze_type = type;
  63. node_ptr = node;
  64. reason = error_info;
  65. }
  66. uint64_t session_id;
  67. uint64_t graph_id;
  68. AnalyzeType analyze_type;
  69. ge::NodePtr node_ptr{nullptr};
  70. std::string reason;
  71. };
  72. } // namespace analyzer
  73. class Analyzer {
  74. public:
  75. /**
  76. * @ingroup ge
  77. * @brief: get analyzer instance.
  78. * @param [in]: None
  79. * @return: Analyzer instance ptr
  80. */
  81. static Analyzer *GetInstance();
  82. /**
  83. * @ingroup ge
  84. * @brief: check whether env var ENABLE_NETWORK_ANALYSIS_DEBUG is enabled.
  85. * When enable env, it will keep adaptor sink geop graph even though fail.
  86. * @param [in]: None
  87. * @return: true: enable env false : disable env
  88. */
  89. bool IsEnableNetAnalyzeDebug() { return std::getenv("ENABLE_NETWORK_ANALYSIS_DEBUG") != nullptr; }
  90. /**
  91. * @ingroup ge
  92. * @brief: build buff object by sess id and graph id .
  93. * @param [in]: session id & graph id
  94. * @return: 0: success other: failed
  95. */
  96. ge::Status BuildJsonObject(uint64_t session_id, uint64_t graph_id);
  97. /**
  98. * @ingroup ge
  99. * @brief: get buff object by sess id and graph id .
  100. * @param [in]: session id & graph id
  101. * @return: nullptr if failed
  102. */
  103. std::shared_ptr<analyzer::GraphInfo> GetJsonObject(uint64_t session_id, uint64_t graph_id);
  104. /**
  105. * @ingroup ge
  106. * @brief: analyzer globle init method.
  107. * @param [in]: None
  108. * @return: None
  109. */
  110. ge::Status Initialize();
  111. /**
  112. * @ingroup ge
  113. * @brief: DeConstruct method. Release all used resource of analyzer.
  114. * @param [in]: None
  115. * @return: None
  116. */
  117. void Finalize();
  118. /**
  119. * @ingroup ge
  120. * @brief: DeConstruct method. Only release resource about session id.
  121. * @param [in]: None
  122. * @return: None
  123. */
  124. void DestroySessionJsonObject(uint64_t session_id);
  125. /**
  126. * @ingroup ge
  127. * @brief: DeConstruct method. Only release resource about session id and graph id.
  128. * @param [in]: None
  129. * @return: None
  130. */
  131. void DestroyGraphJsonObject(uint64_t session_id, uint64_t graph_id);
  132. /**
  133. * @ingroup ge
  134. * @brief: main process method. Buff analyzed data and output to json file
  135. * @param [in]: DataInfo Object
  136. * @return: 0: SUCCESS other: FAILED
  137. */
  138. ge::Status DoAnalyze(analyzer::DataInfo &data_info);
  139. Analyzer(const Analyzer &) = delete;
  140. Analyzer &operator=(const Analyzer &) = delete;
  141. Analyzer(Analyzer &&) = delete;
  142. Analyzer &operator=(Analyzer &&) = delete;
  143. private:
  144. void TensorInfoToJson(nlohmann::json &j, const analyzer::TensorInfo &tensor_info);
  145. void OpInfoToJson(nlohmann::json &j, const analyzer::OpInfo &op_info);
  146. void GraphInfoToJson(nlohmann::json &j, const analyzer::GraphInfo &graph_info);
  147. ge::Status SaveAnalyzerDataToFile();
  148. ge::Status SaveOpInfo(ge::OpDescPtr desc, analyzer::DataInfo &data_info,
  149. std::shared_ptr<analyzer::GraphInfo> graph_info);
  150. void ClearHistoryFile();
  151. ge::Status CreateAnalyzerFile();
  152. explicit Analyzer(){};
  153. ~Analyzer() = default;
  154. private:
  155. std::map<uint64_t, std::map<uint64_t, std::shared_ptr<analyzer::GraphInfo>>> graph_infos_;
  156. std::recursive_mutex mutex_; // protect graph_infos_
  157. std::mutex file_mutex_; // protect json_file_
  158. std::ofstream json_file_;
  159. std::string json_file_name_;
  160. std::atomic_bool is_json_file_create_{false};
  161. };
  162. } // namespace ge
  163. #endif // DOMI_ANALYZER_ANANLYZER_H_

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