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

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