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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 "backend/session/kernel_graph.h"
  22. #include "debug/debugger/grpc_client.h"
  23. #include "debug/debug_services.h"
  24. using debugger::DataType;
  25. using debugger::EventReply;
  26. using debugger::GraphProto;
  27. using debugger::ModelProto;
  28. using debugger::TensorProto;
  29. using debugger::WatchCondition;
  30. using debugger::WatchNode;
  31. using debugger::WatchpointHit;
  32. template <class T>
  33. using ProtoVector = google::protobuf::RepeatedPtrField<T>;
  34. namespace mindspore {
  35. // different types of command recieved by debugger
  36. // need to keep sync with client-side proto and server-side proto
  37. enum class DebuggerCommand { kExitCMD = 2, kRunCMD = 3, kSetCMD = 4, kViewCMD = 5, kUnknownCMD = -1 };
  38. class Debugger : public std::enable_shared_from_this<Debugger> {
  39. public:
  40. static std::shared_ptr<Debugger> GetInstance() {
  41. std::lock_guard<std::mutex> i_lock(instance_lock_);
  42. if (debugger_ == nullptr) {
  43. debugger_ = std::shared_ptr<Debugger>(new (std::nothrow) Debugger());
  44. }
  45. return debugger_;
  46. }
  47. // deconstructor
  48. ~Debugger() = default;
  49. // init
  50. // only save device_id
  51. void Init(const uint32_t device_id, const std::string device_target);
  52. // reset debugger
  53. void Reset();
  54. // enable debugger
  55. // send graph and wait for command
  56. // do nothing if graph is set already
  57. void PreExecute(const KernelGraphPtr &graph_ptr);
  58. // analyze tensors and wait for command
  59. // don't need a graph_ptr because it is saved during pre_execute
  60. void PostExecute();
  61. // suspend the execution after a debug_op
  62. void PostDebugOp();
  63. DebugServices *debug_services() const;
  64. bool debugger_enabled() const;
  65. bool partial_memory();
  66. private:
  67. // private constructor for singleton
  68. Debugger();
  69. // enable debugger
  70. // instantiate class members
  71. // read env variable for grpc client
  72. void EnableDebugger();
  73. // check and save graph pointer
  74. void CheckGraphPtr(const KernelGraphPtr &graph_ptr);
  75. // check if the graph is a dataset graph
  76. void CheckDatasetGraph();
  77. // serialize graph and get proto
  78. GraphProto GetGraphProto() const;
  79. // send graph and enter command wait loop
  80. void SendGraphAndSuspend(const GraphProto &graph_proto);
  81. // wait for command and process command
  82. // send command request and process reply in a loop
  83. // break if RunCMD
  84. void CommandLoop();
  85. // set what nodes and conditions to watch
  86. void SetWatchpoint(const ProtoVector<WatchNode> &nodes, const WatchCondition &condition, const int32_t id);
  87. // remove watchpoint with id
  88. void RemoveWatchpoint(const int32_t id);
  89. // load tensor for view command
  90. std::list<TensorProto> LoadTensors(const ProtoVector<TensorProto> &tensors) const;
  91. // terminate training process
  92. void Exit();
  93. // analyze tensors and check watchpoint conditions
  94. // return names of tensors and what condition they hit
  95. std::list<WatchpointHit> CheckWatchpoints() const;
  96. // send watchpoints that hit and enter command wait loop
  97. void SendWatchpointsAndSuspend(const std::list<WatchpointHit> &points);
  98. // class members
  99. std::unique_ptr<GrpcClient> grpc_client_;
  100. std::unique_ptr<DebugServices> debug_services_;
  101. KernelGraphPtr graph_ptr_;
  102. uint32_t device_id_;
  103. std::string device_target_;
  104. int32_t num_step_;
  105. bool debugger_enabled_;
  106. bool is_dataset_graph_;
  107. bool partial_memory_;
  108. std::mutex access_lock_;
  109. // singleton
  110. static std::mutex instance_lock_;
  111. static std::shared_ptr<Debugger> debugger_;
  112. };
  113. using DebuggerPtr = std::shared_ptr<Debugger>;
  114. // get debugger ModelProto
  115. std::string GetDebuggerFuncGraphProtoString(const FuncGraphPtr &func_graph);
  116. ModelProto GetDebuggerFuncGraphProto(const FuncGraphPtr &func_graph);
  117. // for getting proto DataType from Type of Tensor
  118. DataType GetDebuggerNumberDataType(const TypePtr &type);
  119. // process reply and command type
  120. DebuggerCommand GetCommand(const EventReply &reply);
  121. // parse other data out of EventReply
  122. ProtoVector<WatchNode> GetWatchnodes(const EventReply &reply);
  123. WatchCondition GetWatchcondition(const EventReply &reply);
  124. int32_t GetWatchpointID(const EventReply &reply);
  125. bool GetWatchpointDelete(const EventReply &reply);
  126. ProtoVector<TensorProto> GetTensors(const EventReply &reply);
  127. // get the full name of a tensor, which is the name used in TensorLoader
  128. std::string GetTensorFullName(const TensorProto &tensor);
  129. } // namespace mindspore
  130. #endif // MINDSPORE_CCSRC_DEBUG_DEBUGGER_DEBUGGER_H_