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.

tensor_load.h 5.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * Copyright 2019 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_TENSOR_LOAD_H_
  17. #define MINDSPORE_CCSRC_DEBUG_TENSOR_LOAD_H_
  18. #include <memory>
  19. #include <vector>
  20. #include <map>
  21. #include <mutex>
  22. #include <tuple>
  23. #include <string>
  24. #include <utility>
  25. #include "debug/tensor_data.h"
  26. #include "debug/data_dump/dump_json_parser.h"
  27. #include "ir/dtype.h"
  28. namespace mindspore {
  29. class TensorLoader {
  30. public:
  31. TensorLoader() : iter_num(-1) {}
  32. ~TensorLoader() { EmptyTensor(); }
  33. bool LoadNewTensor(std::shared_ptr<TensorData> tensor, bool keep_prev) {
  34. std::lock_guard<std::mutex> lg(lock_);
  35. if (keep_prev) {
  36. // add prev step tensor into current step map with ":prev" suffix
  37. auto handle = prev_tensor_list_map.extract(tensor->GetName());
  38. if (!handle.empty()) {
  39. handle.key() = tensor->GetName() + ":prev";
  40. tensor_list_map.insert(std::move(handle));
  41. }
  42. }
  43. tensor_list.push_back(tensor);
  44. tensor_list_map[tensor->GetName()] = tensor; // use [] instead of insert to ensure latest value
  45. auto node_name = tensor->GetName();
  46. node_name = node_name.substr(0, node_name.find_first_of(":"));
  47. node_tensor_map.insert({node_name, tensor});
  48. return true;
  49. }
  50. std::vector<std::shared_ptr<TensorData>> GetTensor() { return tensor_list; }
  51. uint32_t GetIterNum() { return iter_num; }
  52. std::map<std::string, std::shared_ptr<TensorData>> GetTensorMap() { return tensor_list_map; }
  53. std::vector<std::shared_ptr<TensorData>> GetNodeTensorMap(std::string node_name) {
  54. std::vector<std::shared_ptr<TensorData>> tensors;
  55. for (auto itr = node_tensor_map.begin(); itr != node_tensor_map.end(); itr++) {
  56. if (itr->first == node_name) {
  57. tensors.push_back(itr->second);
  58. }
  59. }
  60. return tensors;
  61. }
  62. void SearchTensors(const std::vector<std::string> &search_list,
  63. std::vector<std::tuple<std::string, std::shared_ptr<TensorData>>> *result_list) {
  64. for (auto i : search_list) {
  65. std::map<std::string, std::shared_ptr<TensorData>>::iterator iter;
  66. iter = tensor_list_map.find(i);
  67. if (iter != tensor_list_map.end()) {
  68. result_list->push_back(std::make_tuple(i, iter->second));
  69. } else {
  70. result_list->push_back(std::make_tuple(i, nullptr));
  71. }
  72. }
  73. }
  74. void EmptyTensor() {
  75. std::lock_guard<std::mutex> lg(lock_);
  76. prev_tensor_list_map.clear();
  77. node_tensor_map.clear();
  78. tensor_list_map.swap(prev_tensor_list_map);
  79. tensor_list.clear();
  80. }
  81. void EmptyPrevTensor() { prev_tensor_list_map.clear(); }
  82. void EmptyCurrentTensor() {
  83. tensor_list_map.clear();
  84. tensor_list.clear();
  85. }
  86. void set_iter_num(uint32_t iter_num) { this->iter_num = iter_num; }
  87. bool DumpTensorToFile(std::string tensor_name, bool trans_flag, const std::string &filepath,
  88. const std::string &host_fmt, const std::vector<int> &host_shape, TypeId host_type,
  89. TypeId addr_type_id, std::string addr_format, size_t slot) const {
  90. if (filepath.empty()) {
  91. MS_LOG(ERROR) << "Dump file path is null!";
  92. return false;
  93. }
  94. std::string shape = "shape";
  95. if (host_shape.size()) {
  96. for (auto &value : host_shape) {
  97. shape = shape + '_' + std::to_string(value);
  98. }
  99. } else {
  100. shape = shape + "_0";
  101. }
  102. std::string file_extension = ".bin";
  103. std::string path = "";
  104. if (trans_flag) {
  105. path = filepath + '_' + shape + '_' + TypeIdLabel(host_type) + '_' + host_fmt + file_extension;
  106. } else {
  107. path = filepath + '_' + shape + '_' + TypeIdToType(addr_type_id)->ToString() + '_' + addr_format + file_extension;
  108. }
  109. MS_LOG(INFO) << "Dump path is " << path;
  110. std::string tensor_loader_name = tensor_name + ":" + std::to_string(slot);
  111. auto iter = tensor_list_map.find(tensor_loader_name);
  112. if (iter != tensor_list_map.end()) {
  113. std::shared_ptr<TensorData> node = iter->second;
  114. mindspore::tensor::TensorPtr out_tensor = node->GetTensor();
  115. size_t host_size = out_tensor->data().nbytes();
  116. return DumpJsonParser::DumpToFile(path, out_tensor->data_c(), host_size);
  117. }
  118. MS_LOG(INFO) << "Tensor name:" << tensor_name << " not found in tensor_list_map";
  119. return true;
  120. }
  121. private:
  122. std::vector<std::shared_ptr<TensorData>> tensor_list;
  123. std::map<std::string, std::shared_ptr<TensorData>> tensor_list_map;
  124. std::multimap<std::string, std::shared_ptr<TensorData>> node_tensor_map;
  125. std::map<std::string, std::shared_ptr<TensorData>> prev_tensor_list_map;
  126. uint32_t iter_num;
  127. std::mutex lock_;
  128. };
  129. } // namespace mindspore
  130. #endif // MINDSPORE_CCSRC_DEBUG_TENSOR_LOAD_H_