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

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