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.2 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. #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,
  60. ge::NodePtr node, std::string error_info) {
  61. session_id = sess;
  62. graph_id = graph;
  63. analyze_type = type;
  64. node_ptr = node;
  65. reason = error_info;
  66. }
  67. uint64_t session_id;
  68. uint64_t graph_id;
  69. AnalyzeType analyze_type;
  70. ge::NodePtr node_ptr{nullptr};
  71. std::string reason;
  72. };
  73. }
  74. class Analyzer {
  75. public:
  76. /**
  77. * @ingroup ge
  78. * @brief: get analyzer instance.
  79. * @param [in]: None
  80. * @return: Analyzer instance ptr
  81. */
  82. static Analyzer *GetInstance();
  83. /**
  84. * @ingroup ge
  85. * @brief: check whether env var ENABLE_NETWORK_ANALYSIS_DEBUG is enabled.
  86. * When enable env, it will keep adaptor sink geop graph even though fail.
  87. * @param [in]: None
  88. * @return: true: enable env false : disable env
  89. */
  90. bool IsEnableNetAnalyzeDebug() { return std::getenv("ENABLE_NETWORK_ANALYSIS_DEBUG") != nullptr; }
  91. /**
  92. * @ingroup ge
  93. * @brief: build buff object by sess id and graph id .
  94. * @param [in]: session id & graph id
  95. * @return: 0: success other: failed
  96. */
  97. ge::Status BuildJsonObject(uint64_t session_id, uint64_t graph_id);
  98. /**
  99. * @ingroup ge
  100. * @brief: get buff object by sess id and graph id .
  101. * @param [in]: session id & graph id
  102. * @return: nullptr if failed
  103. */
  104. std::shared_ptr<analyzer::GraphInfo> GetJsonObject(uint64_t session_id, uint64_t graph_id);
  105. /**
  106. * @ingroup ge
  107. * @brief: analyzer globle init method.
  108. * @param [in]: None
  109. * @return: None
  110. */
  111. ge::Status Initialize();
  112. /**
  113. * @ingroup ge
  114. * @brief: DeConstruct method. Release all used resource of analyzer.
  115. * @param [in]: None
  116. * @return: None
  117. */
  118. void Finalize();
  119. /**
  120. * @ingroup ge
  121. * @brief: DeConstruct method. Only release resource about session id.
  122. * @param [in]: None
  123. * @return: None
  124. */
  125. void DestroySessionJsonObject(uint64_t session_id);
  126. /**
  127. * @ingroup ge
  128. * @brief: DeConstruct method. Only release resource about session id and graph id.
  129. * @param [in]: None
  130. * @return: None
  131. */
  132. void DestroyGraphJsonObject(uint64_t session_id, uint64_t graph_id);
  133. /**
  134. * @ingroup ge
  135. * @brief: main process method. Buff analyzed data and output to json file
  136. * @param [in]: DataInfo Object
  137. * @return: 0: SUCCESS other: FAILED
  138. */
  139. ge::Status DoAnalyze(analyzer::DataInfo &data_info);
  140. /**
  141. * @ingroup ge
  142. * @brief: Buff analyzed data and output to json file
  143. * @param [in]: session id , graph id
  144. * @return: 0: SUCCESS other: FAILED
  145. */
  146. ge::Status SaveAnalyzerDataToFile(uint64_t session_id, uint64_t graph_id);
  147. Analyzer(const Analyzer &) = delete;
  148. Analyzer& operator=(const Analyzer&) = delete;
  149. Analyzer(Analyzer &&) = delete;
  150. Analyzer& operator=(Analyzer &&) = delete;
  151. private:
  152. void TensorInfoToJson(nlohmann::json& j, const analyzer::TensorInfo &tensor_info);
  153. void OpInfoToJson(nlohmann::json& j, const analyzer::OpInfo &op_info);
  154. void GraphInfoToJson(nlohmann::json& j, const analyzer::GraphInfo &graph_info);
  155. ge::Status SaveOpInfo(ge::OpDescPtr desc, analyzer::DataInfo &data_info,
  156. std::shared_ptr<analyzer::GraphInfo> graph_info);
  157. void ClearHistoryFile();
  158. ge::Status CreateAnalyzerFile();
  159. explicit Analyzer() {};
  160. ~Analyzer() = default;
  161. private:
  162. std::map<uint64_t, std::map<uint64_t, std::shared_ptr<analyzer::GraphInfo>>> graph_infos_;
  163. std::recursive_mutex mutex_; // protect graph_infos_
  164. std::mutex file_mutex_; // protect json_file_
  165. std::ofstream json_file_;
  166. std::string json_file_name_;
  167. std::atomic_bool is_json_file_create_{false};
  168. };
  169. } // namespace ge
  170. #endif // DOMI_ANALYZER_ANANLYZER_H_

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