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

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