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

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