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.5 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 LoadParameters();
  82. private:
  83. // private constructor for singleton
  84. Debugger();
  85. // enable debugger
  86. // instantiate class members
  87. // read env variable for grpc client
  88. void EnableDebugger();
  89. // check if dump using debugger backend is enabled
  90. bool CheckDebuggerDumpEnabled();
  91. // check if debugger enabled
  92. bool CheckDebuggerEnabled();
  93. bool CheckDebuggerPartialMemoryEnabled();
  94. // check and save graph pointer
  95. void CheckGraphPtr(const KernelGraphPtr &graph_ptr);
  96. // check if the graph is a dataset graph
  97. void CheckDatasetGraph();
  98. // serialize graph and get proto
  99. GraphProto GetGraphProto() const;
  100. // send graph and enter command wait loop
  101. void SendGraphAndSuspend(const GraphProto &graph_proto);
  102. // wait for command and process command
  103. // send command request and process reply in a loop
  104. // break if RunCMD
  105. void CommandLoop();
  106. // set what nodes and conditions to watch
  107. void SetWatchpoint(const ProtoVector<WatchNode> &nodes, const WatchCondition &condition, const int32_t id);
  108. // remove watchpoint with id
  109. void RemoveWatchpoint(const int32_t id);
  110. // load tensor for view command
  111. std::list<TensorProto> LoadTensors(const ProtoVector<TensorProto> &tensors) const;
  112. // terminate training process
  113. void Exit();
  114. // analyze tensors and check watchpoint conditions
  115. // return names of tensors and what condition they hit
  116. std::list<WatchpointHit> CheckWatchpoints(const std::string &watchnode = std::string());
  117. // send watchpoints that hit and enter command wait loop
  118. void SendWatchpointsAndSuspend(const std::list<WatchpointHit> &points);
  119. // Find if any operation overflow happened and return their names
  120. std::vector<std::string> CheckOpOverflow();
  121. // Check if the port is valid
  122. bool CheckPort(const char *port);
  123. // class members
  124. std::unique_ptr<GrpcClient> grpc_client_;
  125. std::unique_ptr<DebugServices> debug_services_;
  126. KernelGraphPtr graph_ptr_;
  127. uint32_t device_id_;
  128. std::string device_target_;
  129. int32_t num_step_;
  130. bool debugger_enabled_;
  131. std::string run_level_;
  132. std::string node_name_;
  133. std::string cur_name_;
  134. bool training_done_;
  135. bool is_dataset_graph_;
  136. bool partial_memory_;
  137. std::mutex access_lock_;
  138. std::map<std::pair<uint32_t, uint32_t>, std::string> stream_task_to_opname_;
  139. double last_overflow_bin_;
  140. std::string overflow_bin_path_;
  141. // singleton
  142. static std::mutex instance_lock_;
  143. static std::shared_ptr<Debugger> debugger_;
  144. };
  145. using DebuggerPtr = std::shared_ptr<Debugger>;
  146. // get debugger ModelProto
  147. std::string GetDebuggerFuncGraphProtoString(const FuncGraphPtr &func_graph);
  148. ModelProto GetDebuggerFuncGraphProto(const FuncGraphPtr &func_graph);
  149. // for getting proto DataType from Type of Tensor
  150. DataType GetDebuggerNumberDataType(const TypePtr &type);
  151. // process reply and command type
  152. DebuggerCommand GetCommand(const EventReply &reply);
  153. // parse other data out of EventReply
  154. ProtoVector<WatchNode> GetWatchnodes(const EventReply &reply);
  155. std::string GetNodeName(const EventReply &reply);
  156. std::string GetRunLevel(const EventReply &reply);
  157. WatchCondition GetWatchcondition(const EventReply &reply);
  158. int32_t GetWatchpointID(const EventReply &reply);
  159. bool GetWatchpointDelete(const EventReply &reply);
  160. ProtoVector<TensorProto> GetTensors(const EventReply &reply);
  161. // get the full name of a tensor, which is the name used in TensorLoader
  162. std::string GetTensorFullName(const TensorProto &tensor);
  163. uint64_t BytestoInt64(const std::vector<char> &buffer);
  164. } // namespace mindspore
  165. #endif // MINDSPORE_CCSRC_DEBUG_DEBUGGER_DEBUGGER_H_