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

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