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.

model_cache_helper.h 5.4 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * Copyright 2019-2020 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 GE_COMMON_HELPER_MODEL_CACHE_HELPER_H_
  17. #define GE_COMMON_HELPER_MODEL_CACHE_HELPER_H_
  18. #include <nlohmann/json.hpp>
  19. #include <set>
  20. #include <string>
  21. #include "external/ge/ge_api_error_codes.h"
  22. #include "graph/compute_graph.h"
  23. #include "graph/manager/graph_var_manager.h"
  24. #include "model/ge_model.h"
  25. namespace ge {
  26. using Json = nlohmann::json;
  27. struct CacheInfo {
  28. size_t node_num;
  29. size_t edge_num;
  30. size_t graph_hash;
  31. map<std::string, size_t> nodes_hash;
  32. CacheInfo() : node_num(0), edge_num(0), graph_hash(0) {}
  33. };
  34. class ModelCacheHelper {
  35. public:
  36. ModelCacheHelper(uint64_t session_id, uint32_t graph_id, ComputeGraphPtr &compute_graph);
  37. ~ModelCacheHelper();
  38. Status SaveCacheInfoToCache () const;
  39. Status SaveVarManagerToCache(bool before_build) const;
  40. Status SaveOmModelToCache(const GeModelPtr &ge_model) const;
  41. bool IsModelCacheHit() const;
  42. Status RecoverVarManagerFromCache() const;
  43. Status LoadOmModelFromCache(GeModelPtr &ge_model) const;
  44. Status RefreshComputeGraph(const ComputeGraphPtr &compute_graph);
  45. Status ClearCache(uint32_t graph_id) const;
  46. private:
  47. Status GetComputeGraphHash(size_t &hash) const;
  48. Status GetNodesHash(map<std::string, size_t> &hash_map) const;
  49. Status GetCacheInfo(CacheInfo &cache_info) const;
  50. Status RecoverMemResource(const Json &json) const;
  51. Status RecoverAllocatedGraphId(const Json &json) const;
  52. Status RecoverChangedGraphId(const Json &json) const;
  53. Status RecoverVarAddrAndTensorDesc(const Json &json) const;
  54. Status RecoverBroadcastInfo(const Json &json) const;
  55. Status RecoverTransRoads(const Json &json) const;
  56. static Status GetNodesNeedRecompile(ComputeGraphPtr &graph, vector<NodePtr> &nodes);
  57. static Status RecompileNodes(GeModelPtr &ge_model);
  58. bool IsNodeHashSameAsCache(const map<std::string, size_t> &hash_map) const;
  59. bool IsMemResourceSameAsCache(Json &json) const;
  60. bool IsChangedGraphIdSameAsCache(Json &json) const;
  61. bool IsAllocatedGraphIdSameAsCache(Json &json) const;
  62. bool IsCurVarTensorDescSameAsCache(Json &json) const;
  63. bool IsVarAddrMgrMapSameAsCache(Json &json) const;
  64. bool IsBroadcastInfoSameAsCache(Json &json) const;
  65. bool IsTransRoadsSameAsCache(Json &json) const;
  66. bool IsVarManagerSameAsCache(Json &json) const;
  67. bool IsVarManagerParamSameAsCache(Json &json) const;
  68. Status SaveJsonToFile(const string &file_name, const Json &json) const;
  69. Status LoadJsonFromFile(const string &file_name, Json &json) const;
  70. Status GetNodesHashMapJson(Json &json) const;
  71. Status GetMemResourceMap(Json &json) const;
  72. Status GetVarAddrMgrMapJson(Json &json) const;
  73. Status GetCurVarTensorDescMapJson(Json &json) const;
  74. Status GetTransRoadsJson(Json &json) const;
  75. Status GetChangedGraphIdJson(Json &json) const;
  76. Status GetAllocatedGraphIdJson(Json &json) const;
  77. Status GetBroadcastInfoJson(Json &json) const;
  78. Status GetVarResourceJson(Json &json) const;
  79. Status GetVarManagerJson(Json &json) const;
  80. static Status TensorDescToJson(const GeTensorDesc &ge_tensor_desc, Json &json);
  81. static Status JsonToTensorDesc(const Json &json, GeTensorDesc &ge_tensor_desc);
  82. static Status ParseMemResourceFromJson(const Json &json, map<rtMemType_t, int64_t> &mem_resource);
  83. static Status ParseVarAddrMgrMapFromJson(const Json &json,
  84. std::vector<std::pair<std::string, VarAddrMgr>> &var_addr_mgr_vector,
  85. std::set<uint64_t> &var_offset_set);
  86. static Status ParseCurVarTensorDescMapFromJson(
  87. const Json &json, std::unordered_map<std::string, ge::GeTensorDesc> &cur_var_tensor_desc_map);
  88. static Status ParseTransRoadsFromJson(const Json &json,
  89. std::unordered_map<std::string, std::vector<TransNodeInfo>> &trans_roads);
  90. static Status ParseChangedGraphIdFromJson(const Json &json,
  91. std::map<std::string, uint32_t> &changed_graph_id);
  92. static Status ParseAllocatedGraphIdFromJson(const Json &json,
  93. std::map<std::string, uint32_t> &allocated_graph_id);
  94. static Status ParseBroadcastInfoFromJson(const Json &json,
  95. std::unordered_map<std::string, VarBroadCastInfo> &var_broadcast_info);
  96. static Status GetVarNameFromVarKey(const string &var_key, const GeTensorDesc &tensor_desc, string &var_name);
  97. uint64_t session_id_;
  98. uint32_t graph_id_;
  99. string cache_path_;
  100. ComputeGraphPtr compute_graph_;
  101. std::set<string> var_names_;
  102. bool is_cache_path_valid_for_output;
  103. static map<uint32_t, uint32_t> graph_id_run_times_;
  104. };
  105. using ModelCacheHelperPtr = std::shared_ptr<ModelCacheHelper>;
  106. } // namespace ge
  107. #endif // GE_COMMON_HELPER_MODEL_CACHE_HELPER_H_

图引擎模块(GE)是MindSpore的一个子模块,其代码由C++实现,位于前端模块ME和底层硬件之间,起到承接作用。图引擎模块以ME下发的图作为输入,然后进行一系列的深度图优化操作,最后输出一张可以在底层硬件上高效运行的图。GE针对昇腾AI处理器的硬件结构特点,做了特定的优化工作,以此来充分发挥出昇腾AI处理器的强大算力。在进行模型训练/推理时,GE会被自动调用而用户并不感知。GE主要由GE API和GE Core两部分组成,详细的架构图如下所示