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.

info.h 8.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /**
  2. * Copyright 2019 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 MINDSPORE_CCSRC_IR_INFO_H_
  17. #define MINDSPORE_CCSRC_IR_INFO_H_
  18. #include <iostream>
  19. #include <string>
  20. #include <memory>
  21. #include <stack>
  22. #include <utility>
  23. #include <vector>
  24. #include "pybind11/pybind11.h"
  25. #include "ir/base.h"
  26. #include "debug/trace_info.h"
  27. namespace mindspore {
  28. namespace py = pybind11;
  29. // namespace to support intermediate representation definition
  30. enum SourceLineTip { kSourceLineTipDiscard = 0, kSourceLineTipNextLine = 1, kSourceLineTipInLine = 2 };
  31. // Location class record the location in source code.
  32. class Location {
  33. public:
  34. Location(const std::string &file_name, int line, int column, int line_end, int column_end)
  35. : file_name_(file_name), line_(line), column_(column), line_end_(line_end), column_end_(column_end) {}
  36. Location(const Location &loc)
  37. : file_name_(loc.file_name_),
  38. line_(loc.line_),
  39. column_(loc.column_),
  40. line_end_(loc.line_end_),
  41. column_end_(loc.column_end_) {}
  42. std::string ToString(SourceLineTip tip = kSourceLineTipNextLine);
  43. std::string file_name() { return file_name_; }
  44. int line() const { return line_; }
  45. void set_line(int line) { line_ = line; }
  46. int line_end() const { return line_end_; }
  47. void set_line_end(int line) { line_end_ = line; }
  48. int column() const { return column_; }
  49. void set_column(int column) { column_ = column; }
  50. int column_end() const { return column_end_; }
  51. void set_column_end(int column) { column_end_ = column; }
  52. ~Location() = default;
  53. private:
  54. std::string file_name_;
  55. int line_;
  56. int column_;
  57. int line_end_;
  58. int column_end_;
  59. };
  60. class TraceContext;
  61. using TraceContextPtr = std::shared_ptr<TraceContext>;
  62. class FuncGraph;
  63. using FuncGraphPtr = std::shared_ptr<FuncGraph>;
  64. using FuncGraphWeakPtr = std::weak_ptr<FuncGraph>;
  65. class AnfNode;
  66. using AnfNodeWeakPtr = std::weak_ptr<AnfNode>;
  67. class TraceManager {
  68. public:
  69. TraceManager() = default;
  70. ~TraceManager() = default;
  71. static TraceContextPtr CurrentContextInfo();
  72. static void DebugTrace(const std::string &func_name, const LocationPtr &location);
  73. static void DebugTrace(const LocationPtr &location);
  74. static void DebugTrace(const TraceInfoPtr &trace_info);
  75. // debug trace with a cloned trace info with debug_info
  76. static void DebugTrace(const DebugInfoPtr &debug_info, const TraceInfoPtr &trace_info);
  77. static void EndTrace();
  78. static std::stack<TraceContextPtr> trace_context_stack_;
  79. };
  80. class TraceGuard {
  81. public:
  82. explicit TraceGuard(const std::string func_name, const LocationPtr &location) {
  83. TraceManager::DebugTrace(func_name, location);
  84. }
  85. explicit TraceGuard(const LocationPtr &location) { TraceManager::DebugTrace(location); }
  86. ~TraceGuard() { TraceManager::EndTrace(); }
  87. };
  88. class TraceContext {
  89. public:
  90. LocationPtr location_;
  91. TraceInfoPtr trace_info_;
  92. std::string func_name_;
  93. protected:
  94. void ProcessAttributeFromContext();
  95. public:
  96. ~TraceContext() = default;
  97. explicit TraceContext(const LocationPtr &loc) {
  98. ProcessAttributeFromContext();
  99. location_ = loc;
  100. }
  101. explicit TraceContext(const std::string &func_name) {
  102. ProcessAttributeFromContext();
  103. func_name_ = func_name;
  104. }
  105. explicit TraceContext(const TraceInfoPtr &trace_info) {
  106. ProcessAttributeFromContext();
  107. trace_info_ = trace_info;
  108. }
  109. void set_location(const LocationPtr &loc) { location_ = loc; }
  110. LocationPtr location() { return location_; }
  111. void set_trace_info(const TraceInfoPtr &trace_info) { trace_info_ = trace_info; }
  112. TraceInfoPtr trace_info() { return trace_info_; }
  113. void set_func_name(const std::string &func_name) { func_name_ = func_name; }
  114. std::string func_name() { return func_name_; }
  115. };
  116. class DebugInfo : public Base {
  117. public:
  118. DebugInfo();
  119. explicit DebugInfo(const std::string &name);
  120. explicit DebugInfo(const LocationPtr &loc);
  121. ~DebugInfo() override = default;
  122. MS_DECLARE_PARENT(DebugInfo, Base);
  123. int64_t debug_id();
  124. int64_t unique_id() const { return unique_id_; }
  125. int64_t unique_id_through_copy() const;
  126. std::string get_id() { return std::to_string(debug_id()); }
  127. void set_trace_info(const TraceInfoPtr &trace_info) { trace_info_ = trace_info; }
  128. TraceInfoPtr trace_info() { return trace_info_; }
  129. void set_location(const LocationPtr &loc) { location_ = loc; }
  130. virtual LocationPtr location() { return location_; }
  131. std::string name() { return name_; }
  132. void set_name(const std::string &name) { name_ = name; }
  133. virtual std::string debug_name();
  134. virtual std::string get_python_func_belonged() { return ""; }
  135. protected:
  136. template <typename Derived>
  137. std::shared_ptr<Derived> shared_from_base() {
  138. return std::static_pointer_cast<Derived>(shared_from_this());
  139. }
  140. private:
  141. void InitValueFromContext() {
  142. if (TraceManager::CurrentContextInfo() != nullptr) {
  143. auto context_info = TraceManager::CurrentContextInfo();
  144. trace_info_ = context_info->trace_info();
  145. location_ = context_info->location();
  146. }
  147. }
  148. static int64_t gen_unique_id() {
  149. static int64_t cur_unique_id = 0;
  150. return cur_unique_id++;
  151. }
  152. protected:
  153. int64_t unique_id_;
  154. int64_t debug_id_;
  155. TraceInfoPtr trace_info_;
  156. LocationPtr location_;
  157. std::string name_;
  158. };
  159. class NodeDebugInfo : public DebugInfo {
  160. public:
  161. NodeDebugInfo() {
  162. if (TraceManager::CurrentContextInfo() != nullptr) {
  163. auto context_info = TraceManager::CurrentContextInfo();
  164. py_func_belonged_ = context_info->func_name();
  165. }
  166. }
  167. explicit NodeDebugInfo(const std::string &name) : DebugInfo(name) {
  168. if (TraceManager::CurrentContextInfo() != nullptr) {
  169. auto context_info = TraceManager::CurrentContextInfo();
  170. py_func_belonged_ = context_info->func_name();
  171. }
  172. }
  173. ~NodeDebugInfo() override = default;
  174. std::string debug_name() override;
  175. void set_node(const std::shared_ptr<AnfNode> &node) { node_ = AnfNodeWeakPtr(node); }
  176. std::shared_ptr<AnfNode> get_node() const { return node_.lock(); }
  177. void set_py_func_belonged(const std::string &name) { py_func_belonged_ = name; }
  178. std::string get_python_func_belonged() override { return py_func_belonged_; }
  179. AnfNodeWeakPtr node_;
  180. std::string py_func_belonged_;
  181. };
  182. using NodeDebugInfoPtr = std::shared_ptr<NodeDebugInfo>;
  183. class GraphDebugInfo : public DebugInfo {
  184. public:
  185. GraphDebugInfo() {
  186. if (TraceManager::CurrentContextInfo() != nullptr) {
  187. auto context_info = TraceManager::CurrentContextInfo();
  188. py_func_name_ = context_info->func_name();
  189. deco_loc_ = nullptr;
  190. }
  191. }
  192. explicit GraphDebugInfo(const std::string &name) : DebugInfo(name) {
  193. if (TraceManager::CurrentContextInfo() != nullptr) {
  194. auto context_info = TraceManager::CurrentContextInfo();
  195. py_func_name_ = context_info->func_name();
  196. deco_loc_ = nullptr;
  197. }
  198. }
  199. ~GraphDebugInfo() override = default;
  200. std::string debug_name() override;
  201. LocationPtr location() override;
  202. LocationPtr deco_location() { return deco_loc_; }
  203. void set_graph(const FuncGraphPtr &func_graph) { func_graph_ = FuncGraphWeakPtr(func_graph); }
  204. FuncGraphPtr get_graph() const { return func_graph_.lock(); }
  205. void set_full_name(const std::string &name) { full_name_ = name; }
  206. std::string get_full_name() { return full_name_; }
  207. void set_deco_location(const LocationPtr &deco_list_loc);
  208. std::string get_python_func_belonged() override { return py_func_name_; }
  209. FuncGraphWeakPtr func_graph_;
  210. LocationPtr deco_loc_;
  211. std::string py_func_name_;
  212. std::string full_name_;
  213. };
  214. using GraphDebugInfoPtr = std::shared_ptr<GraphDebugInfo>;
  215. } // namespace mindspore
  216. #endif // MINDSPORE_CCSRC_IR_INFO_H_