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

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