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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /**
  2. * Copyright 2020-2022 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 "utils/ms_device_shape_transfer.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 SetAscendKernelByKernelFlag(bool value) { ascend_kernel_by_kernel_ = value; }
  78. void StoreRunGraphIdList(uint32_t graph_id);
  79. // analyze tensors and wait for command
  80. // don't need a graph_ptr because it is saved during pre_execute
  81. void PostExecute();
  82. bool DumpDataEnabledIteration() const;
  83. static uint32_t GetRankID();
  84. void Dump(const KernelGraphPtr &kernel_graph) const;
  85. void DumpConstantDataAscend(const KernelGraphPtr &graph);
  86. void DumpSingleNode(const CNodePtr &node, uint32_t graph_id);
  87. void DumpInGraphCompiler(const KernelGraphPtr &kernel_graph);
  88. void PostExecuteGraphDebugger();
  89. bool ReadNodeDataRequired(const CNodePtr &kernel) const;
  90. void PostExecuteNode(const CNodePtr &kernel, bool last_kernel);
  91. bool DumpTensorToFile(const std::string &tensor_name, bool trans_flag, const std::string &filepath,
  92. const std::string &host_fmt, const std::vector<int64_t> &host_shape, TypeId host_type,
  93. TypeId device_type, const std::string &addr_format, size_t slot) const;
  94. bool LoadNewTensor(const std::shared_ptr<TensorData> &tensor, bool keep_prev);
  95. std::shared_ptr<TensorData> GetTensor(const std::string &tensor_name) const;
  96. bool debugger_enabled() const;
  97. bool partial_memory() const;
  98. void SetEnableHeartbeat(bool enabled);
  99. void SetCurNode(const std::string &cur_name);
  100. std::string run_level() const;
  101. // check if any feature that uses the debugger backend is enabled
  102. bool DebuggerBackendEnabled() const;
  103. void SetTrainingDone(bool training_done);
  104. // returns true if reply received and mindspore version matched with mindInsight version
  105. // version_check should be true if you want the function to do backend compatibility check with MindInsight
  106. bool SendMetadata(bool version_check);
  107. bool CheckSendMetadata();
  108. void LoadParametersAndConst();
  109. void LoadParametersAndConst(const KernelGraphPtr &graph);
  110. void UpdateStepNum(const session::KernelGraph *graph);
  111. void UpdateStepNumGPU();
  112. void ClearCurrentData();
  113. void LoadGraphOutputs();
  114. void LoadNodeOutputs(const CNodePtr &node, uint32_t exec_order, uint32_t root_graph_id);
  115. void CheckDatasetSinkMode(const KernelGraphPtr &graph_ptr);
  116. void LoadGraphs(const KernelGraphPtr &graph_ptr);
  117. uint32_t GetFirstRunGraphId() const;
  118. uint32_t GetCurrentRootGraphId() const { return cur_root_graph_id_; }
  119. uint32_t GetPrevRootGraphId() const { return prev_root_graph_id_; }
  120. void SetGraphPtr(const KernelGraphPtr &graph_ptr) { graph_ptr_ = graph_ptr; }
  121. const KernelGraphPtr GetGraphPtr() const { return graph_ptr_; }
  122. const std::list<KernelGraphPtr> GetGraphPtrList() const { return graph_ptr_list_; }
  123. bool TensorExistsInCurrent(const std::string &tensor_name);
  124. // check if dump using debugger backend is enabled
  125. bool CheckDebuggerDumpEnabled() const;
  126. // check if debugger is enabled
  127. bool CheckDebuggerEnabled() const;
  128. std::map<uint32_t, int32_t> GetGraphIterMap() { return graph_iter_num_map_; }
  129. void UpdateGraphIterMap(uint32_t graph_id, int32_t iter_num);
  130. #ifdef ENABLE_D
  131. std::shared_ptr<DumpDataBuilder> LoadDumpDataBuilder(const std::string &node_name);
  132. void ClearDumpDataBuilder(const std::string &node_name);
  133. #endif
  134. private:
  135. // private constructor for singleton
  136. Debugger();
  137. // enable debugger
  138. // instantiate class members
  139. // read env variable for grpc client
  140. void EnableDebugger();
  141. void CheckDebuggerEnabledParam() const;
  142. bool CheckDebuggerPartialMemoryEnabled() const;
  143. // check and save graph pointer
  144. void CheckGraphPtr(const KernelGraphPtr &graph_ptr);
  145. // check if the graph is a dataset graph
  146. void CheckDatasetGraph();
  147. // serialize graph and get proto
  148. GraphProto GetGraphProto(const KernelGraphPtr &graph_ptr) const;
  149. // send heartbeat message to UI once per 30 second by default
  150. void SendHeartbeat(int32_t period);
  151. // send graph and enter command wait loop
  152. void SendGraphAndSuspend(const GraphProto &graph_proto);
  153. void SendMultiGraphsAndSuspend(const std::list<GraphProto> &graph_proto_list);
  154. // send multi_graphs and clear the graph_proto_list_
  155. void SendMultiGraphsAndClear(const KernelGraphPtr &graph_ptr);
  156. // wait for command and process command
  157. // send command request and process reply in a loop
  158. // break if RunCMD
  159. void CommandLoop();
  160. // Process the RunCMD
  161. void ProcessRunCMD(const EventReply &reply);
  162. // Process the KSetCMD
  163. void ProcessKSetCMD(const EventReply &reply);
  164. // Process the KViewCMD
  165. void ProcessKViewCMD(const EventReply &reply);
  166. // ViewCMD base level
  167. void ViewBaseLevel(const EventReply &reply);
  168. // ViewCMD statistics level
  169. void ViewStatLevel(const EventReply &reply);
  170. // ViewCMD value level
  171. void ViewValueLevel(const EventReply &reply);
  172. // set what nodes and conditions to watch
  173. void SetWatchpoint(const ProtoVector<WatchNode> &nodes, const WatchCondition &condition, const int32_t id,
  174. const ProtoVector<WatchCondition_Parameter> &parameters);
  175. // remove watchpoint with id
  176. void RemoveWatchpoint(const int32_t id);
  177. // load tensor for view command
  178. std::list<TensorProto> LoadTensors(const ProtoVector<TensorProto> &tensors) const;
  179. // load tensor base for view command
  180. std::list<TensorBase> LoadTensorsBase(const ProtoVector<TensorProto> &tensors) const;
  181. // load tensor statistics for view command
  182. std::list<TensorSummary> LoadTensorsStat(const ProtoVector<TensorProto> &tensors) const;
  183. // terminate training process
  184. void Exit(bool exit_success = false);
  185. // analyze tensors and check watchpoint conditions
  186. // return names of tensors and what condition they hit
  187. std::list<WatchpointHit> CheckWatchpoints(const std::string &watchnode = std::string(),
  188. const CNodePtr &kernel = nullptr, bool recheck = false);
  189. // send watchpoints that hit
  190. void SendWatchpoints(const std::list<WatchpointHit> &points);
  191. // Check if the port is valid
  192. bool CheckPort(const std::string &port) const;
  193. // Check if the IP is valid
  194. bool CheckIp(const std::string &host) const;
  195. void LoadSingleAnfnode(const AnfNodePtr &anf_node, const size_t output_index, uint32_t root_graph_id);
  196. // class members
  197. std::unique_ptr<GrpcClient> grpc_client_;
  198. std::unique_ptr<DebugServices> debug_services_;
  199. std::unique_ptr<std::thread> heartbeat_thread_;
  200. KernelGraphPtr graph_ptr_;
  201. uint32_t device_id_;
  202. std::string device_target_;
  203. int32_t num_step_;
  204. bool debugger_enabled_;
  205. bool suspended_at_last_kernel_;
  206. std::string run_level_;
  207. std::string node_name_;
  208. std::string cur_name_;
  209. bool training_done_;
  210. bool send_metadata_done_;
  211. bool received_new_graph_;
  212. bool is_dataset_graph_;
  213. bool partial_memory_;
  214. std::mutex access_lock_;
  215. uint32_t cur_root_graph_id_ = UINT32_MAX;
  216. uint32_t prev_root_graph_id_ = UINT32_MAX;
  217. // flag to keep track of the very first suspension of debugger
  218. bool initial_suspend_;
  219. bool enable_heartbeat_;
  220. std::list<GraphProto> graph_proto_list_;
  221. std::list<KernelGraphPtr> graph_ptr_list_;
  222. // The vector of graph pointers that have been run in the current step.
  223. std::vector<KernelGraphPtr> graph_ptr_step_vec_;
  224. // map to store iter num in each epoch when dataset_sink_mode is true
  225. std::map<uint32_t, int32_t> graph_iter_num_map_;
  226. #ifdef ENABLE_D
  227. // to construct kernel data for async dump, key is the dump path to the node
  228. std::map<std::string, std::shared_ptr<DumpDataBuilder>> dump_data_construct_map_;
  229. #endif
  230. // singleton
  231. static std::mutex instance_lock_;
  232. static std::shared_ptr<Debugger> debugger_;
  233. uint32_t not_dataset_graph_sum_;
  234. std::list<uint32_t> rungraph_id_list_;
  235. bool ascend_kernel_by_kernel_;
  236. std::string version_;
  237. };
  238. using DebuggerPtr = std::shared_ptr<Debugger>;
  239. // get debugger ModelProto
  240. ModelProto GetDebuggerFuncGraphProto(const FuncGraphPtr &func_graph);
  241. // for getting proto DataType from Type of Tensor
  242. DataType GetDebuggerNumberDataType(const TypePtr &type);
  243. // process reply and command type
  244. DebuggerCommand GetCommand(const EventReply &reply);
  245. // parse other data out of EventReply
  246. ProtoVector<WatchCondition_Parameter> GetParameters(const EventReply &reply);
  247. ProtoVector<WatchNode> GetWatchnodes(const EventReply &reply);
  248. std::string GetNodeName(const EventReply &reply);
  249. std::string GetRunLevel(const EventReply &reply);
  250. WatchCondition GetWatchcondition(const EventReply &reply);
  251. int32_t GetWatchpointID(const EventReply &reply);
  252. bool GetWatchpointDelete(const EventReply &reply);
  253. ProtoVector<TensorProto> GetTensors(const EventReply &reply);
  254. bool GetMiVersionMatched(const EventReply &reply);
  255. // get the full name of a tensor, which is the name used in TensorLoader
  256. std::string GetTensorFullName(const TensorProto &tensor);
  257. } // namespace mindspore
  258. #endif // MINDSPORE_CCSRC_DEBUG_DEBUGGER_DEBUGGER_H_