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.

debugger.h 11 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /**
  2. * Copyright 2020-2021 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_DEBUG_DEBUGGER_DEBUGGER_H_
  17. #define MINDSPORE_CCSRC_DEBUG_DEBUGGER_DEBUGGER_H_
  18. #include <list>
  19. #include <memory>
  20. #include <string>
  21. #include <utility>
  22. #include <vector>
  23. #include <map>
  24. #include "backend/session/kernel_graph.h"
  25. #include "debug/debugger/grpc_client.h"
  26. #include "debug/debug_services.h"
  27. #include "common/trans.h"
  28. #ifdef ENABLE_D
  29. #include "debug/dump_data_builder.h"
  30. #endif
  31. using debugger::Chunk;
  32. using debugger::DataType;
  33. using debugger::EventReply;
  34. using debugger::GraphProto;
  35. using debugger::ModelProto;
  36. using debugger::Statistics;
  37. using debugger::TensorProto;
  38. using debugger::WatchCondition;
  39. using debugger::WatchCondition_Parameter;
  40. using debugger::WatchNode;
  41. using debugger::WatchpointHit;
  42. template <class T>
  43. using ProtoVector = google::protobuf::RepeatedPtrField<T>;
  44. namespace mindspore {
  45. // different types of command received by debugger
  46. // need to keep sync with client-side proto and server-side proto
  47. enum class DebuggerCommand {
  48. kExitCMD = 2,
  49. kRunCMD = 3,
  50. kSetCMD = 4,
  51. kViewCMD = 5,
  52. kVersionMatchedCMD = 6,
  53. kUnknownCMD = -1
  54. };
  55. class Debugger : public std::enable_shared_from_this<Debugger> {
  56. public:
  57. static std::shared_ptr<Debugger> GetInstance() {
  58. std::lock_guard<std::mutex> i_lock(instance_lock_);
  59. if (debugger_ == nullptr) {
  60. debugger_ = std::shared_ptr<Debugger>(new (std::nothrow) Debugger());
  61. }
  62. return debugger_;
  63. }
  64. // deconstructor
  65. ~Debugger() = default;
  66. // init
  67. // only save device_id
  68. void Init(const uint32_t device_id, const std::string device_target);
  69. // reset debugger
  70. void Reset();
  71. void PreExecuteGraphDebugger(const std::vector<KernelGraphPtr> &graphs);
  72. // enable debugger
  73. // send graph and wait for command
  74. // do nothing if graph is set already
  75. void PreExecute(const KernelGraphPtr &graph_ptr);
  76. void SetCurrentAndPrevRootGraph(uint32_t root_graph_id);
  77. void StoreRunGraphIdList(uint32_t graph_id);
  78. // analyze tensors and wait for command
  79. // don't need a graph_ptr because it is saved during pre_execute
  80. void PostExecute();
  81. bool DumpDataEnabledIteration() const;
  82. static uint32_t GetRankID();
  83. void Dump(const KernelGraphPtr &kernel_graph) const;
  84. void DumpSingleNode(const CNodePtr &node, uint32_t graph_id);
  85. void DumpSetup(const KernelGraphPtr &kernel_graph) const;
  86. void DumpInGraphCompiler(const KernelGraphPtr &kernel_graph);
  87. void PostExecuteGraphDebugger();
  88. bool ReadNodeDataRequired(const CNodePtr &kernel) const;
  89. void PostExecuteNode(const CNodePtr &kernel, bool last_kernel);
  90. bool DumpTensorToFile(const std::string &tensor_name, bool trans_flag, const std::string &filepath,
  91. const std::string &host_fmt, const std::vector<int64_t> &host_shape, TypeId host_type,
  92. TypeId device_type, const std::string &addr_format, size_t slot) const;
  93. bool LoadNewTensor(const std::shared_ptr<TensorData> &tensor, bool keep_prev);
  94. std::shared_ptr<TensorData> GetTensor(const std::string &tensor_name) const;
  95. bool debugger_enabled() const;
  96. bool partial_memory() const;
  97. void SetEnableHeartbeat(bool enabled);
  98. void SetCurNode(const std::string &cur_name);
  99. std::string run_level() const;
  100. // check if any feature that uses the debugger backend is enabled
  101. bool DebuggerBackendEnabled() const;
  102. void SetTrainingDone(bool training_done);
  103. // returns true if reply received and mindspore version matched with mindinsight version
  104. // version_check should be true if you want the function to do backend compatibility check with Mindinsight
  105. bool SendMetadata(bool version_check);
  106. bool CheckSendMetadata();
  107. void LoadParametersAndConst();
  108. void LoadParametersAndConst(const KernelGraphPtr &graph);
  109. void UpdateStepNum(const session::KernelGraph *graph);
  110. void UpdateStepNumGPU();
  111. void ClearCurrentData();
  112. void LoadGraphOutputs();
  113. void CheckDatasetSinkMode(const KernelGraphPtr &graph_ptr);
  114. void LoadGraphs(const KernelGraphPtr &graph_ptr);
  115. uint32_t GetFirstRunGraphId() const;
  116. uint32_t GetCurrentRootGraphId() const { return cur_root_graph_id_; }
  117. uint32_t GetPrevRootGraphId() const { return prev_root_graph_id_; }
  118. void SetGraphPtr(const KernelGraphPtr &graph_ptr) { graph_ptr_ = graph_ptr; }
  119. const KernelGraphPtr GetGraphPtr() const { return graph_ptr_; }
  120. const std::list<KernelGraphPtr> GetGraphPtrList() const { return graph_ptr_list_; }
  121. bool TensorExistsInCurrent(const std::string &tensor_name);
  122. // check if dump using debugger backend is enabled
  123. bool CheckDebuggerDumpEnabled() const;
  124. // check if debugger is enabled
  125. bool CheckDebuggerEnabled() const;
  126. #ifdef ENABLE_D
  127. std::shared_ptr<DumpDataBuilder> LoadDumpDataBuilder(const std::string &node_name);
  128. void ClearDumpDataBuilder(const std::string &node_name);
  129. #endif
  130. private:
  131. // private constructor for singleton
  132. Debugger();
  133. // enable debugger
  134. // instantiate class members
  135. // read env variable for grpc client
  136. void EnableDebugger();
  137. void CheckDebuggerEnabledParam() const;
  138. bool CheckDebuggerPartialMemoryEnabled() const;
  139. // check and save graph pointer
  140. void CheckGraphPtr(const KernelGraphPtr &graph_ptr);
  141. // check if the graph is a dataset graph
  142. void CheckDatasetGraph();
  143. // serialize graph and get proto
  144. GraphProto GetGraphProto(const KernelGraphPtr &graph_ptr) const;
  145. // send heartbeat message to UI once per 30 second by default
  146. void SendHeartbeat(int32_t period);
  147. // send graph and enter command wait loop
  148. void SendGraphAndSuspend(const GraphProto &graph_proto);
  149. void SendMultiGraphsAndSuspend(const std::list<GraphProto> &graph_proto_list);
  150. // send multi_graphs and clear the graph_proto_list_
  151. void SendMultiGraphsAndClear(const KernelGraphPtr &graph_ptr);
  152. // wait for command and process command
  153. // send command request and process reply in a loop
  154. // break if RunCMD
  155. void CommandLoop();
  156. // Process the RunCMD
  157. void ProcessRunCMD(const EventReply &reply);
  158. // Process the KSetCMD
  159. void ProcessKSetCMD(const EventReply &reply);
  160. // Process the KViewCMD
  161. void ProcessKViewCMD(const EventReply &reply);
  162. // ViewCMD base level
  163. void ViewBaseLevel(const EventReply &reply);
  164. // ViewCMD statistics level
  165. void ViewStatLevel(const EventReply &reply);
  166. // ViewCMD value level
  167. void ViewValueLevel(const EventReply &reply);
  168. // set what nodes and conditions to watch
  169. void SetWatchpoint(const ProtoVector<WatchNode> &nodes, const WatchCondition &condition, const int32_t id,
  170. const ProtoVector<WatchCondition_Parameter> &parameters);
  171. // remove watchpoint with id
  172. void RemoveWatchpoint(const int32_t id);
  173. // load tensor for view command
  174. std::list<TensorProto> LoadTensors(const ProtoVector<TensorProto> &tensors) const;
  175. // load tensor base for view command
  176. std::list<TensorBase> LoadTensorsBase(const ProtoVector<TensorProto> &tensors) const;
  177. // load tensor statistics for view command
  178. std::list<TensorSummary> LoadTensorsStat(const ProtoVector<TensorProto> &tensors) const;
  179. // terminate training process
  180. void Exit(bool exit_success = false);
  181. // analyze tensors and check watchpoint conditions
  182. // return names of tensors and what condition they hit
  183. std::list<WatchpointHit> CheckWatchpoints(const std::string &watchnode = std::string(),
  184. const CNodePtr &kernel = nullptr, bool recheck = false);
  185. // send watchpoints that hit
  186. void SendWatchpoints(const std::list<WatchpointHit> &points);
  187. // Check if the port is valid
  188. bool CheckPort(const std::string &port) const;
  189. // Check if the IP is valid
  190. bool CheckIp(const std::string &host) const;
  191. void LoadSingleAnfnode(const AnfNodePtr &anf_node, const size_t output_index, uint32_t root_graph_id);
  192. // class members
  193. std::unique_ptr<GrpcClient> grpc_client_;
  194. std::unique_ptr<DebugServices> debug_services_;
  195. std::unique_ptr<std::thread> heartbeat_thread_;
  196. KernelGraphPtr graph_ptr_;
  197. uint32_t device_id_;
  198. std::string device_target_;
  199. int32_t num_step_;
  200. bool debugger_enabled_;
  201. bool suspended_at_last_kernel_;
  202. std::string run_level_;
  203. std::string node_name_;
  204. std::string cur_name_;
  205. bool training_done_;
  206. bool send_metadata_done_;
  207. bool received_new_graph_;
  208. bool is_dataset_graph_;
  209. bool partial_memory_;
  210. std::mutex access_lock_;
  211. uint32_t cur_root_graph_id_ = UINT32_MAX;
  212. uint32_t prev_root_graph_id_ = UINT32_MAX;
  213. // flag to keep track of the very first suspension of debugger
  214. bool initial_suspend_;
  215. bool enable_heartbeat_;
  216. std::list<GraphProto> graph_proto_list_;
  217. std::list<KernelGraphPtr> graph_ptr_list_;
  218. // The vector of graph pointers that have been run in the current step.
  219. std::vector<KernelGraphPtr> graph_ptr_step_vec_;
  220. #ifdef ENABLE_D
  221. // to construct kernel data for async dump, key is the dump path to the node
  222. std::map<std::string, std::shared_ptr<DumpDataBuilder>> dump_data_construct_map_;
  223. #endif
  224. // singleton
  225. static std::mutex instance_lock_;
  226. static std::shared_ptr<Debugger> debugger_;
  227. uint32_t not_dataset_graph_sum_;
  228. std::list<uint32_t> rungraph_id_list_;
  229. std::string version_;
  230. };
  231. using DebuggerPtr = std::shared_ptr<Debugger>;
  232. // get debugger ModelProto
  233. ModelProto GetDebuggerFuncGraphProto(const FuncGraphPtr &func_graph);
  234. // for getting proto DataType from Type of Tensor
  235. DataType GetDebuggerNumberDataType(const TypePtr &type);
  236. // process reply and command type
  237. DebuggerCommand GetCommand(const EventReply &reply);
  238. // parse other data out of EventReply
  239. ProtoVector<WatchCondition_Parameter> GetParameters(const EventReply &reply);
  240. ProtoVector<WatchNode> GetWatchnodes(const EventReply &reply);
  241. std::string GetNodeName(const EventReply &reply);
  242. std::string GetRunLevel(const EventReply &reply);
  243. WatchCondition GetWatchcondition(const EventReply &reply);
  244. int32_t GetWatchpointID(const EventReply &reply);
  245. bool GetWatchpointDelete(const EventReply &reply);
  246. ProtoVector<TensorProto> GetTensors(const EventReply &reply);
  247. bool GetMiVersionMatched(const EventReply &reply);
  248. // get the full name of a tensor, which is the name used in TensorLoader
  249. std::string GetTensorFullName(const TensorProto &tensor);
  250. } // namespace mindspore
  251. #endif // MINDSPORE_CCSRC_DEBUG_DEBUGGER_DEBUGGER_H_