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

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