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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 "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);
  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. private:
  66. // private constructor for singleton
  67. Debugger();
  68. // enable debugger
  69. // instantiate class members
  70. // read env variable for grpc client
  71. void EnableDebugger();
  72. // check and save graph pointer
  73. void CheckGraphPtr(const KernelGraphPtr &graph_ptr);
  74. // check if the graph is a dataset graph
  75. void CheckDatasetGraph();
  76. // serialize graph and get proto
  77. GraphProto GetGraphProto() const;
  78. // send graph and enter command wait loop
  79. void SendGraphAndSuspend(const GraphProto &graph_proto);
  80. // wait for command and process command
  81. // send command request and process reply in a loop
  82. // break if RunCMD
  83. void CommandLoop();
  84. // set what nodes and conditions to watch
  85. void SetWatchpoint(const ProtoVector<WatchNode> &nodes, const WatchCondition &condition, const int32_t id);
  86. // remove watchpoint with id
  87. void RemoveWatchpoint(const int32_t id);
  88. // load tensor for view command
  89. std::list<TensorProto> LoadTensors(const ProtoVector<TensorProto> &tensors) const;
  90. // terminate training process
  91. void Exit();
  92. // analyze tensors and check watchpoint conditions
  93. // return names of tensors and what condition they hit
  94. std::list<WatchpointHit> CheckWatchpoints() const;
  95. // send watchpoints that hit and enter command wait loop
  96. void SendWatchpointsAndSuspend(const std::list<WatchpointHit> &points);
  97. // class members
  98. std::unique_ptr<GrpcClient> grpc_client_;
  99. std::unique_ptr<DebugServices> debug_services_;
  100. KernelGraphPtr graph_ptr_;
  101. uint32_t device_id_;
  102. int32_t num_step_;
  103. bool debugger_enabled_;
  104. bool is_dataset_graph_;
  105. std::mutex access_lock_;
  106. // singleton
  107. static std::mutex instance_lock_;
  108. static std::shared_ptr<Debugger> debugger_;
  109. };
  110. using DebuggerPtr = std::shared_ptr<Debugger>;
  111. // get debugger ModelProto
  112. std::string GetDebuggerFuncGraphProtoString(const FuncGraphPtr &func_graph);
  113. ModelProto GetDebuggerFuncGraphProto(const FuncGraphPtr &func_graph);
  114. // for getting proto DataType from Type of Tensor
  115. DataType GetDebuggerNumberDataType(const TypePtr &type);
  116. // process reply and command type
  117. DebuggerCommand GetCommand(const EventReply &reply);
  118. // parse other data out of EventReply
  119. ProtoVector<WatchNode> GetWatchnodes(const EventReply &reply);
  120. WatchCondition GetWatchcondition(const EventReply &reply);
  121. int32_t GetWatchpointID(const EventReply &reply);
  122. bool GetWatchpointDelete(const EventReply &reply);
  123. ProtoVector<TensorProto> GetTensors(const EventReply &reply);
  124. // get the full name of a tensor, which is the name used in TensorLoader
  125. std::string GetTensorFullName(const TensorProto &tensor);
  126. } // namespace mindspore
  127. #endif // MINDSPORE_CCSRC_DEBUG_DEBUGGER_DEBUGGER_H_