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.

debug_services.cc 11 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /**
  2. * Copyright 2019-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. #include <algorithm>
  17. #include <map>
  18. #include "backend/session/anf_runtime_algorithm.h"
  19. #include "debug/debug_services.h"
  20. #include "debug/debugger/tensor_summary.h"
  21. namespace mindspore {
  22. DebugServices::DebugServices() {
  23. tensor_loader_ = new TensorLoader();
  24. uint32_t iter_num = -1;
  25. tensor_loader_->set_iter_num(iter_num);
  26. }
  27. DebugServices::DebugServices(const DebugServices &other) {
  28. tensor_loader_ = other.tensor_loader_;
  29. watchpoint_table = other.watchpoint_table;
  30. }
  31. DebugServices &DebugServices::operator=(const DebugServices &other) {
  32. if (this != &other) {
  33. tensor_loader_ = other.tensor_loader_;
  34. watchpoint_table = other.watchpoint_table;
  35. }
  36. return *this;
  37. }
  38. DebugServices::~DebugServices() { delete tensor_loader_; }
  39. void DebugServices::AddWatchpoint(unsigned int id, unsigned int watch_condition, float parameter,
  40. const std::vector<std::tuple<std::string, bool>> &check_node_list,
  41. const std::vector<parameter_t> &parameter_list) {
  42. std::lock_guard<std::mutex> lg(lock_);
  43. watchpoint_t watchpoint_item;
  44. watchpoint_item.id = id;
  45. watchpoint_item.condition.type = static_cast<CONDITION_TYPE>(watch_condition);
  46. watchpoint_item.condition.parameter = parameter;
  47. watchpoint_item.check_node_list = check_node_list;
  48. watchpoint_item.parameter_list = parameter_list;
  49. watchpoint_table[id] = watchpoint_item;
  50. }
  51. void DebugServices::RemoveWatchpoint(unsigned int id) {
  52. std::lock_guard<std::mutex> lg(lock_);
  53. watchpoint_table.erase(id);
  54. }
  55. void DebugServices::CheckWatchpoints(std::vector<std::string> *name, std::vector<std::string> *slot,
  56. std::vector<int> *condition, std::vector<unsigned int> *watchpoint_id,
  57. std::vector<std::vector<parameter_t>> *parameters,
  58. std::vector<int32_t> *error_codes, const std::vector<std::string> &op_overflows,
  59. const std::vector<std::shared_ptr<TensorData>> &tensor_list,
  60. const bool init_dbg_suspend) {
  61. std::lock_guard<std::mutex> lg(lock_);
  62. if (watchpoint_table.empty()) return;
  63. for (const auto &tensor : tensor_list) {
  64. const auto tensor_name = tensor->GetName();
  65. const auto tensor_name_no_slot = tensor_name.substr(0, tensor_name.find_first_of(':'));
  66. const auto tensor_slot = std::to_string(tensor->GetSlot());
  67. mindspore::tensor::TensorPtr tensor_ptr = tensor->GetTensor();
  68. int tensor_dtype = tensor_ptr->data_type_c();
  69. std::vector<watchpoint_t> watchpoints_to_check;
  70. std::string qualified_tensor_name;
  71. for (auto w_table_item : watchpoint_table) {
  72. auto wp = std::get<1>(w_table_item);
  73. if (wp.condition.type == INIT && !init_dbg_suspend) continue;
  74. if (wp.condition.type != IS_OVERFLOW && tensor_dtype == kNumberTypeBool) continue;
  75. std::string found = wp.FindQualifiedTensorName(tensor_name_no_slot);
  76. if (!found.empty()) {
  77. qualified_tensor_name = found;
  78. watchpoints_to_check.push_back(w_table_item.second);
  79. }
  80. }
  81. // no wp set on current tensor
  82. if (watchpoints_to_check.empty()) continue;
  83. uint32_t num_elements = tensor_ptr->DataSize();
  84. void *previous_tensor_ptr = tensor_loader_->GetPrevTensor(tensor_name)
  85. ? tensor_loader_->GetPrevTensor(tensor_name)->GetTensor()->data_c()
  86. : nullptr;
  87. std::unique_ptr<ITensorSummary> base_summary_ptr;
  88. if (!(watchpoints_to_check.size() == 1 && watchpoints_to_check[0].condition.type == IS_OVERFLOW)) {
  89. switch (tensor_dtype) {
  90. case kNumberTypeUInt8: {
  91. base_summary_ptr =
  92. std::make_unique<TensorSummary<uint8_t>>(tensor_ptr->data_c(), previous_tensor_ptr, num_elements);
  93. break;
  94. }
  95. case kNumberTypeInt8: {
  96. base_summary_ptr =
  97. std::make_unique<TensorSummary<int8_t>>(tensor_ptr->data_c(), previous_tensor_ptr, num_elements);
  98. break;
  99. }
  100. case kNumberTypeUInt16: {
  101. base_summary_ptr =
  102. std::make_unique<TensorSummary<uint16_t>>(tensor_ptr->data_c(), previous_tensor_ptr, num_elements);
  103. break;
  104. }
  105. case kNumberTypeInt16: {
  106. base_summary_ptr =
  107. std::make_unique<TensorSummary<int16_t>>(tensor_ptr->data_c(), previous_tensor_ptr, num_elements);
  108. break;
  109. }
  110. case kNumberTypeUInt32: {
  111. base_summary_ptr =
  112. std::make_unique<TensorSummary<uint32_t>>(tensor_ptr->data_c(), previous_tensor_ptr, num_elements);
  113. break;
  114. }
  115. case kNumberTypeInt32:
  116. case kNumberTypeInt: {
  117. base_summary_ptr =
  118. std::make_unique<TensorSummary<int32_t>>(tensor_ptr->data_c(), previous_tensor_ptr, num_elements);
  119. break;
  120. }
  121. case kNumberTypeUInt64: {
  122. base_summary_ptr =
  123. std::make_unique<TensorSummary<uint64_t>>(tensor_ptr->data_c(), previous_tensor_ptr, num_elements);
  124. break;
  125. }
  126. case kNumberTypeInt64: {
  127. base_summary_ptr =
  128. std::make_unique<TensorSummary<int64_t>>(tensor_ptr->data_c(), previous_tensor_ptr, num_elements);
  129. break;
  130. }
  131. case kNumberTypeFloat16: {
  132. base_summary_ptr =
  133. std::make_unique<TensorSummary<float16>>(tensor_ptr->data_c(), previous_tensor_ptr, num_elements);
  134. break;
  135. }
  136. case kNumberTypeFloat32:
  137. case kNumberTypeFloat: {
  138. base_summary_ptr =
  139. std::make_unique<TensorSummary<float>>(tensor_ptr->data_c(), previous_tensor_ptr, num_elements);
  140. break;
  141. }
  142. case kNumberTypeFloat64: {
  143. base_summary_ptr =
  144. std::make_unique<TensorSummary<double>>(tensor_ptr->data_c(), previous_tensor_ptr, num_elements);
  145. break;
  146. }
  147. default:
  148. MS_LOG(INFO) << "Unsupported tensor type";
  149. break;
  150. }
  151. base_summary_ptr->SummarizeTensor(watchpoints_to_check);
  152. }
  153. for (auto &wp : watchpoints_to_check) {
  154. bool is_hit = false;
  155. int error_code = 0;
  156. std::vector<parameter_t> parameter_list = {};
  157. if (wp.condition.type == IS_OVERFLOW) {
  158. is_hit = (std::find(op_overflows.begin(), op_overflows.end(), tensor_name_no_slot) != op_overflows.end());
  159. } else {
  160. auto item = base_summary_ptr->IsWatchpointHit(wp);
  161. is_hit = std::get<0>(item);
  162. error_code = std::get<1>(item);
  163. parameter_list = std::get<2>(item);
  164. }
  165. if (is_hit || error_code) {
  166. name->push_back(qualified_tensor_name);
  167. slot->push_back(tensor_slot);
  168. condition->push_back(wp.condition.type);
  169. watchpoint_id->push_back(wp.id);
  170. parameters->push_back(parameter_list);
  171. error_codes->push_back(error_code);
  172. }
  173. }
  174. }
  175. }
  176. void DebugServices::ReadNodesTensors(std::vector<std::string> name, std::vector<std::string> *ret_name,
  177. std::vector<char *> *data_ptr, std::vector<unsigned int> *data_size,
  178. std::vector<TypePtr> *dtype, std::vector<std::vector<int64_t>> *shape) {
  179. std::vector<std::tuple<std::string, std::shared_ptr<TensorData>>> result_list;
  180. tensor_loader_->SearchTensors(name, &result_list);
  181. for (auto result : result_list) {
  182. if (!std::get<1>(result)) {
  183. continue;
  184. }
  185. ret_name->push_back(std::get<0>(result));
  186. data_ptr->push_back(reinterpret_cast<char *>(std::get<1>(result)->GetTensor()->data_c()));
  187. data_size->push_back(std::get<1>(result)->GetTensor()->data().nbytes());
  188. dtype->push_back(std::get<1>(result)->GetTensor()->Dtype());
  189. shape->push_back(std::get<1>(result)->GetTensor()->shape());
  190. }
  191. }
  192. bool DebugServices::IsWatchPoint(std::string kernel_name, const CNodePtr &kernel) {
  193. bool ret = false;
  194. for (auto w_table_item : watchpoint_table) {
  195. auto check_node_list = std::get<1>(w_table_item).check_node_list;
  196. for (auto check_node : check_node_list) {
  197. std::string w_name = std::get<0>(check_node);
  198. bool w_type = std::get<1>(check_node);
  199. if ((w_type == true &&
  200. ((kernel_name.find(w_name) != string::npos && kernel_name.rfind(w_name, 0) == 0) || w_name == "*")) ||
  201. (w_type == false && (kernel_name == w_name || IsWatchPointNodeInput(w_name, kernel)))) {
  202. ret = true;
  203. return ret;
  204. }
  205. }
  206. }
  207. return ret;
  208. }
  209. bool DebugServices::IsWatchPointNodeInput(std::string w_name, const CNodePtr &kernel) {
  210. if (kernel) {
  211. auto input_size = AnfAlgo::GetInputTensorNum(kernel);
  212. for (size_t j = 0; j < input_size; ++j) {
  213. auto input_kernel = kernel->input(j + 1);
  214. std::string input_kernel_name = input_kernel->fullname_with_scope();
  215. auto found = w_name.find_last_of('/');
  216. if (found != std::string::npos && w_name.substr(found + 1) == input_kernel_name) return true;
  217. }
  218. return false;
  219. } else {
  220. return false;
  221. }
  222. }
  223. void DebugServices::AddWeightsBiasInputs(std::vector<std::shared_ptr<TensorData>> *tensor_list,
  224. const CNodePtr &kernel) {
  225. if (kernel) {
  226. auto input_size = AnfAlgo::GetInputTensorNum(kernel);
  227. for (size_t j = 0; j < input_size; ++j) {
  228. auto input_kernel = kernel->input(j + 1);
  229. std::string input_kernel_name = input_kernel->fullname_with_scope();
  230. auto found_dot = input_kernel_name.find_last_of('.');
  231. if (found_dot != std::string::npos &&
  232. (input_kernel_name.substr(found_dot + 1) == "weight" || input_kernel_name.substr(found_dot + 1) == "bias")) {
  233. std::string locate_tensor = input_kernel_name + ":0";
  234. std::map<std::string, std::shared_ptr<TensorData>> tensor_map = tensor_loader_->GetTensorMap();
  235. std::map<std::string, std::shared_ptr<TensorData>>::iterator iter;
  236. iter = tensor_map.find(locate_tensor);
  237. if (iter != tensor_map.end()) {
  238. tensor_list->push_back(iter->second);
  239. }
  240. }
  241. }
  242. }
  243. }
  244. TensorLoader *DebugServices::tensor_loader() const { return tensor_loader_; }
  245. std::unordered_map<unsigned int, DebugServices::watchpoint_t> DebugServices::GetWatchpointTable() {
  246. return watchpoint_table;
  247. }
  248. } // namespace mindspore