• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 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 
17 #ifndef MINDSPORE_LITE_SRC_RUNTIME_PACK_WEIGHT_MANAGER_H_
18 #define MINDSPORE_LITE_SRC_RUNTIME_PACK_WEIGHT_MANAGER_H_
19 #include <memory>
20 #include <vector>
21 #include <unordered_map>
22 #include <map>
23 #include <string>
24 #include "include/model.h"
25 #include "include/errorcode.h"
26 #include "src/tensor.h"
27 #ifdef SHARING_MODEL_WEIGHT
28 #include "src/litert/pack_weight.h"
29 #endif
30 namespace mindspore::lite {
31 class PackWeightManager {
32  public:
33   static PackWeightManager *GetInstance();
34   ~PackWeightManager() = default;
35   STATUS InitPackWeightManager(const char *model_buf, size_t model_size, std::string *model_id, std::string *runner_id,
36                                const std::map<std::string, std::map<std::string, std::string>> *config_info);
37   char *GetSharedModelBuf(const char *model_buf, std::string model_id,
38                           const std::map<std::string, std::map<std::string, std::string>> *config_info,
39                           bool *is_shared);
40   STATUS StoreOriginTensorData(Model *model, std::vector<Tensor *> *all_tensors);
41   void *GetPackData(const void *tensor_data, const size_t size, bool *is_packed);
42   void Free(void *tensor_data);
43   bool IsCopyTensor(int op_type);
44   void *ReplaceFp16Data(void *origin_fp16_data, size_t size, bool *replace);
45   void FreePackWeight(std::string runner_id, std::string model_id);
46   std::string GenModelID();
47 
48  private:
49   void *MallocData(size_t size);
50   void FreeData(void *tensor_data);
51   PackWeightManager() = default;
52   bool is_parallel_ = false;
53 #ifdef SHARING_MODEL_WEIGHT
54   std::shared_ptr<PackWeight> pack_weight_ = nullptr;
55 #endif
56   std::mutex manager_mutex_;
57   std::vector<std::string> model_ids_;
58   size_t model_id_ = 1;
59 };
60 }  // namespace mindspore::lite
61 #endif  // MINDSPORE_LITE_SRC_RUNTIME_PACK_WEIGHT_MANAGER_H_
62