1 /** 2 * Copyright 2019-2022 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_LITE_SRC_EXTENDRT_DELEGATE_GRAPH_EXECUTOR_LITERT_FUNC_GRAPH_REUSE_MANAGER_H_ 17 #define MINDSPORE_LITE_SRC_EXTENDRT_DELEGATE_GRAPH_EXECUTOR_LITERT_FUNC_GRAPH_REUSE_MANAGER_H_ 18 19 #include <vector> 20 #include <string> 21 #include <memory> 22 #include <utility> 23 #include <unordered_map> 24 #include <map> 25 #include "mindspore/core/base/base.h" 26 #include "include/api/status.h" 27 #include "src/common/helper/infer_helpers.h" 28 #include "src/extendrt/session/lite_graph_executor.h" 29 namespace mindspore { 30 struct ModelBufPair { 31 void *buf = nullptr; 32 size_t buf_size = 0; 33 }; 34 class FuncGraphReuseManager { 35 public: 36 static FuncGraphReuseManager *GetInstance(); 37 ~FuncGraphReuseManager(); 38 39 FuncGraphPtr GetSharedFuncGraph(std::map<std::string, std::map<std::string, std::string>> config_info); 40 Status StoreFuncGraph(FuncGraphPtr func_graph, std::map<std::string, std::map<std::string, std::string>> config_info); 41 42 std::pair<void *, std::shared_ptr<mindspore::infer::helper::InferHelpers>> GetFbModelBuf( 43 size_t *data_size, bool *is_shared_fb_buf, std::map<std::string, std::map<std::string, std::string>> config_info); 44 Status StoreFbModelBuf(void *model_buf, size_t data_size, 45 std::shared_ptr<mindspore::infer::helper::InferHelpers> helper, 46 std::map<std::string, std::map<std::string, std::string>> config_info); 47 48 Status GetInOut(std::map<std::string, std::map<std::string, std::string>> config_info, 49 std::vector<tensor::TensorPtr> *in_tensor, std::vector<tensor::TensorPtr> *out_tensor, 50 std::vector<std::string> *in_name, std::vector<std::string> *out_name); 51 Status StoreInOut(std::map<std::string, std::map<std::string, std::string>> config_info, 52 std::vector<tensor::TensorPtr> in_tensor, std::vector<tensor::TensorPtr> out_tensor, 53 std::vector<std::string> in_name, std::vector<std::string> out_name); 54 55 void ReleaseSharedFuncGraph(std::map<std::string, std::map<std::string, std::string>> config_info); 56 57 private: 58 FuncGraphReuseManager() = default; 59 60 private: 61 // runner id <=> function graph ptr 62 // the cached funcgraph is cleared when the model impl is destructed 63 std::unordered_map<std::string, FuncGraphPtr> all_func_graphs_; 64 std::unordered_map<std::string, ModelBufPair> all_fb_model_buf_; 65 std::unordered_map<std::string, std::shared_ptr<mindspore::infer::helper::InferHelpers>> all_infer_helpers_; 66 std::unordered_map<std::string, std::vector<tensor::TensorPtr>> all_in_tensors_; 67 std::unordered_map<std::string, std::vector<tensor::TensorPtr>> all_out_tensors_; 68 std::unordered_map<std::string, std::vector<std::string>> all_in_names_; 69 std::unordered_map<std::string, std::vector<std::string>> all_out_names_; 70 }; 71 } // namespace mindspore 72 #endif // MINDSPORE_LITE_SRC_EXTENDRT_DELEGATE_GRAPH_EXECUTOR_LITERT_FUNC_GRAPH_REUSE_MANAGER_H_ 73