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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. namespace mindspore {
  27. class TensorLoader {
  28. public:
  29. TensorLoader() : iter_num(-1) {}
  30. ~TensorLoader() { EmptyTensor(); }
  31. bool LoadNewTensor(std::shared_ptr<TensorData> tensor, bool keep_prev) {
  32. std::lock_guard<std::mutex> lg(lock_);
  33. if (keep_prev) {
  34. // add prev step tensor into current step map with ":prev" suffix
  35. auto handle = prev_tensor_list_map.extract(tensor->GetName());
  36. if (!handle.empty()) {
  37. handle.key() = tensor->GetName() + ":prev";
  38. tensor_list_map.insert(std::move(handle));
  39. }
  40. }
  41. tensor_list.push_back(tensor);
  42. tensor_list_map.insert({tensor->GetName(), tensor});
  43. return true;
  44. }
  45. std::vector<std::shared_ptr<TensorData>> GetTensor() { return tensor_list; }
  46. uint32_t GetIterNum() { return iter_num; }
  47. std::map<std::string, std::shared_ptr<TensorData>> GetTensorMap() { return tensor_list_map; }
  48. void SearchTensors(const std::vector<std::string> &search_list,
  49. std::vector<std::tuple<std::string, std::shared_ptr<TensorData>>> *result_list) {
  50. for (auto i : search_list) {
  51. std::map<std::string, std::shared_ptr<TensorData>>::iterator iter;
  52. iter = tensor_list_map.find(i);
  53. if (iter != tensor_list_map.end()) {
  54. result_list->push_back(std::make_tuple(i, iter->second));
  55. } else {
  56. result_list->push_back(std::make_tuple(i, nullptr));
  57. }
  58. }
  59. }
  60. void EmptyTensor() {
  61. std::lock_guard<std::mutex> lg(lock_);
  62. prev_tensor_list_map.clear();
  63. tensor_list_map.swap(prev_tensor_list_map);
  64. tensor_list.clear();
  65. }
  66. void EmptyPrevTensor() { prev_tensor_list_map.clear(); }
  67. void set_iter_num(uint32_t iter_num) { this->iter_num = iter_num; }
  68. private:
  69. std::vector<std::shared_ptr<TensorData>> tensor_list;
  70. std::map<std::string, std::shared_ptr<TensorData>> tensor_list_map;
  71. std::map<std::string, std::shared_ptr<TensorData>> prev_tensor_list_map;
  72. uint32_t iter_num;
  73. std::mutex lock_;
  74. };
  75. } // namespace mindspore
  76. #endif // MINDSPORE_CCSRC_DEBUG_TENSOR_LOAD_H_