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

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. void MoveTensorCurrentToPrev(std::string tensor_name) {
  34. auto handle = tensor_list_map.extract(tensor_name);
  35. if (!handle.empty()) {
  36. MS_LOG(INFO) << "Moving " << tensor_name << " from current map to previous map";
  37. prev_tensor_list_map.insert(std::move(handle));
  38. }
  39. }
  40. void SwapCurrentPrev() { tensor_list_map.swap(prev_tensor_list_map); }
  41. bool TensorExistsInCurrent(std::string tensor_name) {
  42. return tensor_list_map.find(tensor_name) != tensor_list_map.end();
  43. }
  44. // only parameters will return true
  45. bool PrevTensorExistsInCurrent(std::string tensor_name) { return TensorExistsInCurrent(tensor_name + ":prev"); }
  46. void MoveParametersCurrentToPrev() {
  47. MS_LOG(INFO) << "Moving parameters from current map to previous map";
  48. auto iter = tensor_list_map.begin();
  49. while (iter != tensor_list_map.end()) {
  50. auto key = iter->first;
  51. if (PrevTensorExistsInCurrent(key)) {
  52. // :prev tensor only exists for parameter. Move it to prev
  53. ++iter;
  54. MoveTensorCurrentToPrev(key);
  55. } else {
  56. ++iter;
  57. }
  58. }
  59. }
  60. bool IsPrevTensor(std::string tensor_name) {
  61. const std::string suffix = ":prev";
  62. if (tensor_name.length() <= suffix.length()) return false;
  63. return std::equal(suffix.rbegin(), suffix.rend(), tensor_name.rbegin());
  64. }
  65. bool LoadNewTensor(std::shared_ptr<TensorData> tensor, bool keep_prev) {
  66. std::lock_guard<std::mutex> lg(lock_);
  67. if (keep_prev) {
  68. // add prev step tensor into current step map with ":prev" suffix
  69. auto handle = prev_tensor_list_map.extract(tensor->GetName());
  70. if (!handle.empty()) {
  71. handle.key() = tensor->GetName() + ":prev";
  72. tensor_list_map.insert(std::move(handle));
  73. }
  74. }
  75. tensor_list_map[tensor->GetName()] = tensor; // use [] instead of insert to ensure latest value
  76. auto node_name = tensor->GetName();
  77. node_name = node_name.substr(0, node_name.find_first_of(":"));
  78. node_tensor_map.insert({node_name, tensor});
  79. return true;
  80. }
  81. std::vector<std::shared_ptr<TensorData>> GetTensor() {
  82. std::vector<std::shared_ptr<TensorData>> tensor_list;
  83. for (auto &it : tensor_list_map) {
  84. if (!IsPrevTensor(it.first)) tensor_list.push_back(it.second);
  85. }
  86. return tensor_list;
  87. }
  88. std::shared_ptr<TensorData> GetTensor(const std::string &tensor_name) {
  89. auto iter = tensor_list_map.find(tensor_name);
  90. if (iter != tensor_list_map.end()) return iter->second;
  91. return nullptr;
  92. }
  93. uint32_t GetIterNum() { return iter_num; }
  94. std::map<std::string, std::shared_ptr<TensorData>> GetTensorMap() { return tensor_list_map; }
  95. std::shared_ptr<TensorData> GetPrevTensor(const std::string &tensor_name) {
  96. if (tensor_list_map.find(tensor_name + ":prev") != tensor_list_map.end()) {
  97. return tensor_list_map[tensor_name + ":prev"];
  98. }
  99. return nullptr;
  100. }
  101. std::vector<std::shared_ptr<TensorData>> GetNodeTensorMap(std::string node_name) {
  102. std::vector<std::shared_ptr<TensorData>> tensors;
  103. for (auto itr = node_tensor_map.begin(); itr != node_tensor_map.end(); itr++) {
  104. if (itr->first == node_name) {
  105. tensors.push_back(itr->second);
  106. }
  107. }
  108. return tensors;
  109. }
  110. void SearchTensors(const std::vector<std::string> &search_list,
  111. std::vector<std::tuple<std::string, std::shared_ptr<TensorData>>> *result_list) {
  112. for (auto i : search_list) {
  113. std::map<std::string, std::shared_ptr<TensorData>>::iterator iter;
  114. iter = tensor_list_map.find(i);
  115. if (iter != tensor_list_map.end()) {
  116. result_list->push_back(std::make_tuple(i, iter->second));
  117. } else {
  118. result_list->push_back(std::make_tuple(i, nullptr));
  119. }
  120. }
  121. }
  122. void EmptyTensor() {
  123. std::lock_guard<std::mutex> lg(lock_);
  124. prev_tensor_list_map.clear();
  125. node_tensor_map.clear();
  126. tensor_list_map.swap(prev_tensor_list_map);
  127. }
  128. void EmptyPrevTensor() { prev_tensor_list_map.clear(); }
  129. void EmptyCurrentTensor() {
  130. tensor_list_map.clear();
  131. node_tensor_map.clear();
  132. }
  133. void set_iter_num(uint32_t iter_num) { this->iter_num = iter_num; }
  134. bool DumpTensorToFile(const std::string &tensor_name, bool trans_flag, const std::string &filepath,
  135. const std::string &host_fmt, const std::vector<int64_t> &host_shape, TypeId host_type,
  136. TypeId addr_type_id, const std::string &addr_format, size_t slot) const {
  137. if (filepath.empty()) {
  138. MS_LOG(ERROR) << "Dump file path is null!";
  139. return false;
  140. }
  141. std::string shape = "shape";
  142. if (host_shape.size()) {
  143. for (auto &value : host_shape) {
  144. shape = shape + '_' + std::to_string(value);
  145. }
  146. } else {
  147. shape = shape + "_0";
  148. }
  149. std::string file_extension = ".bin";
  150. std::string path = "";
  151. if (trans_flag) {
  152. path = filepath + '_' + shape + '_' + TypeIdLabel(host_type) + '_' + host_fmt + file_extension;
  153. } else {
  154. path = filepath + '_' + shape + '_' + TypeIdToType(addr_type_id)->ToString() + '_' + addr_format + file_extension;
  155. }
  156. MS_LOG(INFO) << "Dump path is " << path;
  157. std::string tensor_loader_name = tensor_name + ":" + std::to_string(slot);
  158. auto iter = tensor_list_map.find(tensor_loader_name);
  159. if (iter != tensor_list_map.end()) {
  160. std::shared_ptr<TensorData> node = iter->second;
  161. mindspore::tensor::TensorPtr out_tensor = node->GetTensor();
  162. size_t host_size = out_tensor->data().nbytes();
  163. return DumpJsonParser::DumpToFile(path, out_tensor->data_c(), host_size);
  164. }
  165. MS_LOG(INFO) << "Tensor name:" << tensor_name << " not found in tensor_list_map";
  166. return true;
  167. }
  168. private:
  169. std::map<std::string, std::shared_ptr<TensorData>> tensor_list_map;
  170. std::multimap<std::string, std::shared_ptr<TensorData>> node_tensor_map;
  171. std::map<std::string, std::shared_ptr<TensorData>> prev_tensor_list_map;
  172. uint32_t iter_num;
  173. std::mutex lock_;
  174. };
  175. } // namespace mindspore
  176. #endif // MINDSPORE_CCSRC_DEBUG_TENSOR_LOAD_H_