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.0 kB

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