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 10 kB

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