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 13 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
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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, const bool step_end, const bool recheck) {
  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. // no elements to analyze
  69. if (tensor_ptr->DataSize() == 0) continue;
  70. int tensor_dtype = tensor_ptr->data_type_c();
  71. std::vector<watchpoint_t> watchpoints_to_check;
  72. std::string qualified_tensor_name;
  73. for (auto w_table_item : watchpoint_table) {
  74. auto wp = std::get<1>(w_table_item);
  75. // check ONLY init conditions on intial suspended state.
  76. // skip other conditions on intial suspended state
  77. if (init_dbg_suspend && (wp.condition.type != INIT)) continue;
  78. // skip init condition if not init suspend
  79. if ((wp.condition.type == INIT) && !init_dbg_suspend) continue;
  80. // check change conditions only on step end.
  81. if (wp.change_condition() && !step_end) continue;
  82. // if recheck, ignore the cache results and reanalyze everything.
  83. // if not a recheck, check only unanalyzed tensors
  84. if (!recheck && wp_id_cache[tensor_name].count(wp.id)) continue;
  85. std::string found = wp.FindQualifiedTensorName(tensor_name_no_slot);
  86. if (!found.empty()) {
  87. qualified_tensor_name = found;
  88. watchpoints_to_check.push_back(w_table_item.second);
  89. }
  90. }
  91. // no wp set on current tensor
  92. if (watchpoints_to_check.empty()) continue;
  93. uint32_t num_elements = tensor_ptr->DataSize();
  94. void *previous_tensor_ptr = tensor_loader_->GetPrevTensor(tensor_name)
  95. ? tensor_loader_->GetPrevTensor(tensor_name)->GetTensor()->data_c()
  96. : nullptr;
  97. std::unique_ptr<ITensorSummary> base_summary_ptr;
  98. if (!(watchpoints_to_check.size() == 1 && watchpoints_to_check[0].condition.type == IS_OVERFLOW)) {
  99. switch (tensor_dtype) {
  100. case kNumberTypeUInt8: {
  101. base_summary_ptr =
  102. std::make_unique<TensorSummary<uint8_t>>(tensor_ptr->data_c(), previous_tensor_ptr, num_elements);
  103. break;
  104. }
  105. case kNumberTypeInt8: {
  106. base_summary_ptr =
  107. std::make_unique<TensorSummary<int8_t>>(tensor_ptr->data_c(), previous_tensor_ptr, num_elements);
  108. break;
  109. }
  110. case kNumberTypeUInt16: {
  111. base_summary_ptr =
  112. std::make_unique<TensorSummary<uint16_t>>(tensor_ptr->data_c(), previous_tensor_ptr, num_elements);
  113. break;
  114. }
  115. case kNumberTypeInt16: {
  116. base_summary_ptr =
  117. std::make_unique<TensorSummary<int16_t>>(tensor_ptr->data_c(), previous_tensor_ptr, num_elements);
  118. break;
  119. }
  120. case kNumberTypeUInt32: {
  121. base_summary_ptr =
  122. std::make_unique<TensorSummary<uint32_t>>(tensor_ptr->data_c(), previous_tensor_ptr, num_elements);
  123. break;
  124. }
  125. case kNumberTypeInt32:
  126. case kNumberTypeInt: {
  127. base_summary_ptr =
  128. std::make_unique<TensorSummary<int32_t>>(tensor_ptr->data_c(), previous_tensor_ptr, num_elements);
  129. break;
  130. }
  131. case kNumberTypeUInt64: {
  132. base_summary_ptr =
  133. std::make_unique<TensorSummary<uint64_t>>(tensor_ptr->data_c(), previous_tensor_ptr, num_elements);
  134. break;
  135. }
  136. case kNumberTypeInt64: {
  137. base_summary_ptr =
  138. std::make_unique<TensorSummary<int64_t>>(tensor_ptr->data_c(), previous_tensor_ptr, num_elements);
  139. break;
  140. }
  141. case kNumberTypeFloat16: {
  142. base_summary_ptr =
  143. std::make_unique<TensorSummary<float16>>(tensor_ptr->data_c(), previous_tensor_ptr, num_elements);
  144. break;
  145. }
  146. case kNumberTypeFloat32:
  147. case kNumberTypeFloat: {
  148. base_summary_ptr =
  149. std::make_unique<TensorSummary<float>>(tensor_ptr->data_c(), previous_tensor_ptr, num_elements);
  150. break;
  151. }
  152. case kNumberTypeFloat64: {
  153. base_summary_ptr =
  154. std::make_unique<TensorSummary<double>>(tensor_ptr->data_c(), previous_tensor_ptr, num_elements);
  155. break;
  156. }
  157. case kNumberTypeBool: {
  158. base_summary_ptr =
  159. std::make_unique<TensorSummary<bool>>(tensor_ptr->data_c(), previous_tensor_ptr, num_elements);
  160. break;
  161. }
  162. default:
  163. MS_LOG(INFO) << "Unsupported tensor type";
  164. continue;
  165. }
  166. base_summary_ptr->SummarizeTensor(watchpoints_to_check);
  167. }
  168. for (auto &wp : watchpoints_to_check) {
  169. bool is_hit = false;
  170. int error_code = 0;
  171. std::vector<parameter_t> parameter_list = {};
  172. if (wp.condition.type == IS_OVERFLOW) {
  173. is_hit = (std::find(op_overflows.begin(), op_overflows.end(), tensor_name_no_slot) != op_overflows.end());
  174. } else if (base_summary_ptr != nullptr) {
  175. auto item = base_summary_ptr->IsWatchpointHit(wp);
  176. is_hit = std::get<0>(item);
  177. error_code = std::get<1>(item);
  178. parameter_list = std::get<2>(item);
  179. }
  180. // add analyzed tensor to cache
  181. if (!recheck) {
  182. wp_id_cache[tensor_name].insert(wp.id);
  183. }
  184. if (is_hit || error_code) {
  185. name->push_back(qualified_tensor_name);
  186. slot->push_back(tensor_slot);
  187. condition->push_back(wp.condition.type);
  188. watchpoint_id->push_back(wp.id);
  189. parameters->push_back(parameter_list);
  190. error_codes->push_back(error_code);
  191. }
  192. }
  193. }
  194. }
  195. void DebugServices::ReadNodesTensors(std::vector<std::string> name, std::vector<std::string> *ret_name,
  196. std::vector<char *> *data_ptr, std::vector<ssize_t> *data_size,
  197. std::vector<TypePtr> *dtype, std::vector<std::vector<int64_t>> *shape) {
  198. std::vector<std::tuple<std::string, std::shared_ptr<TensorData>>> result_list;
  199. tensor_loader_->SearchTensors(name, &result_list);
  200. for (auto result : result_list) {
  201. if (!std::get<1>(result)) {
  202. continue;
  203. }
  204. ret_name->push_back(std::get<0>(result));
  205. data_ptr->push_back(reinterpret_cast<char *>(std::get<1>(result)->GetTensor()->data_c()));
  206. data_size->push_back(std::get<1>(result)->GetTensor()->data().nbytes());
  207. dtype->push_back(std::get<1>(result)->GetTensor()->Dtype());
  208. shape->push_back(std::get<1>(result)->GetTensor()->shape());
  209. }
  210. }
  211. bool DebugServices::IsWatchPoint(const std::string &kernel_name, const CNodePtr &kernel) const {
  212. bool ret = false;
  213. for (auto w_table_item : watchpoint_table) {
  214. auto check_node_list = std::get<1>(w_table_item).check_node_list;
  215. for (auto check_node : check_node_list) {
  216. std::string w_name = std::get<0>(check_node);
  217. bool w_type = std::get<1>(check_node);
  218. if ((w_type == true &&
  219. ((kernel_name.find(w_name) != string::npos && kernel_name.rfind(w_name, 0) == 0) || w_name == "*")) ||
  220. (w_type == false && (kernel_name == w_name || IsWatchPointNodeInput(w_name, kernel)))) {
  221. ret = true;
  222. return ret;
  223. }
  224. }
  225. }
  226. return ret;
  227. }
  228. bool DebugServices::IsWatchPointNodeInput(const std::string &w_name, const CNodePtr &kernel) const {
  229. if (kernel) {
  230. auto input_size = AnfAlgo::GetInputTensorNum(kernel);
  231. for (size_t j = 0; j < input_size; ++j) {
  232. auto input_kernel = kernel->input(j + 1);
  233. std::string input_kernel_name = input_kernel->fullname_with_scope();
  234. auto found = w_name.find_last_of('/');
  235. if (found != std::string::npos && w_name.substr(found + 1) == input_kernel_name) return true;
  236. }
  237. return false;
  238. } else {
  239. return false;
  240. }
  241. }
  242. void DebugServices::EmptyTensor() { tensor_loader_->EmptyTensor(); }
  243. std::vector<std::shared_ptr<TensorData>> DebugServices::GetTensor() const { return tensor_loader_->GetTensor(); }
  244. std::vector<std::shared_ptr<TensorData>> DebugServices::GetNodeTensorMap(const std::string &node_name) const {
  245. return tensor_loader_->GetNodeTensorMap(node_name);
  246. }
  247. uint32_t DebugServices::GetTensorLoaderIterNum() const { return tensor_loader_->GetIterNum(); }
  248. void DebugServices::SetTensorLoaderIterNum(uint32_t iter_num) { tensor_loader_->set_iter_num(iter_num); }
  249. void DebugServices::EmptyPrevTensor() { tensor_loader_->EmptyPrevTensor(); }
  250. void DebugServices::EmptyCurrentTensor() { tensor_loader_->EmptyCurrentTensor(); }
  251. bool DebugServices::DumpTensorToFile(const std::string &tensor_name, bool trans_flag, const std::string &filepath,
  252. const std::string &host_fmt, const std::vector<int64_t> &host_shape,
  253. TypeId host_type, TypeId addr_type_id, const std::string &addr_format,
  254. size_t slot) const {
  255. return tensor_loader_->DumpTensorToFile(tensor_name, trans_flag, filepath, host_fmt, host_shape, host_type,
  256. addr_type_id, addr_format, slot);
  257. }
  258. bool DebugServices::LoadNewTensor(const std::shared_ptr<TensorData> &tensor, bool keep_prev) {
  259. return tensor_loader_->LoadNewTensor(tensor, keep_prev);
  260. }
  261. std::unordered_map<unsigned int, DebugServices::watchpoint_t> DebugServices::GetWatchpointTable() {
  262. return watchpoint_table;
  263. }
  264. void DebugServices::ResetLoadedTensors() {
  265. wp_id_cache.clear();
  266. MS_LOG(INFO) << "Resetting loaded tensors";
  267. tensor_loader_->MoveParametersCurrentToPrev();
  268. tensor_loader_->EmptyCurrentTensor();
  269. // will move parameters from previous to current map
  270. tensor_loader_->SwapCurrentPrev();
  271. }
  272. std::vector<std::shared_ptr<TensorData>> DebugServices::GetNodeTensor(const CNodePtr &kernel) {
  273. MS_EXCEPTION_IF_NULL(kernel);
  274. std::vector<std::shared_ptr<TensorData>> result;
  275. auto output_size = AnfAlgo::GetOutputTensorNum(kernel);
  276. auto kernel_name = kernel->fullname_with_scope();
  277. for (size_t j = 0; j < output_size; ++j) {
  278. auto tensor_name_with_slot = kernel_name + ":" + std::to_string(j);
  279. auto tensor = tensor_loader_->GetTensor(tensor_name_with_slot);
  280. if (tensor) result.push_back(tensor);
  281. }
  282. return result;
  283. }
  284. bool DebugServices::TensorExistsInCurrent(std::string tensor_name) {
  285. return tensor_loader_->TensorExistsInCurrent(tensor_name);
  286. }
  287. void DebugServices::MoveTensorCurrentToPrev(std::string tensor_name) {
  288. tensor_loader_->MoveTensorCurrentToPrev(tensor_name);
  289. }
  290. } // namespace mindspore