• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020-2021 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_DELEGATE_NPU_NPU_MANAGER_H_
18 #define MINDSPORE_LITE_SRC_RUNTIME_DELEGATE_NPU_NPU_MANAGER_H_
19 #include <string>
20 #include <memory>
21 #include <utility>
22 #include <vector>
23 #include <unordered_map>
24 #include <set>
25 #include "include/hiai_ir_build.h"
26 #include "schema/model_generated.h"
27 #include "include/errorcode.h"
28 #include "include/HiAiModelManagerService.h"
29 
30 using mindspore::lite::RET_ERROR;
31 using mindspore::lite::RET_OK;
32 
33 namespace mindspore {
34 
35 struct SubGraphModel {
36  public:
SubGraphModelSubGraphModel37   SubGraphModel(int index, std::string model_name, std::shared_ptr<domi::ModelBufferData> model_buffer_data)
38       : index_(index), model_name_(std::move(model_name)), model_buffer_data_(model_buffer_data) {}
39 
40   bool is_freed_ = false;
41   bool is_loaded_ = false;
42   int index_;
43   std::string model_name_;
44   std::shared_ptr<domi::ModelBufferData> model_buffer_data_;
45   std::shared_ptr<hiai::AiModelMngerClient> client_ = nullptr;
46   std::shared_ptr<hiai::AiModelDescription> desc_ = nullptr;
47 };
48 
49 class NPUManager {
50  public:
NPUManager(int frequency)51   explicit NPUManager(int frequency) : frequency_(frequency) {}
52 
~NPUManager()53   ~NPUManager() { Reset(); }
54 
55   bool IsSupportNPU();
56 
57   // provide to subgraph to add model.
58   int AddModel(std::shared_ptr<domi::ModelBufferData> model_buffer_data, const std::string &model_name, int frequency);
59 
60   // scheduler to load om model.
61   int LoadOMModel();
62 
63   // provide to executor.
64   std::shared_ptr<hiai::AiModelMngerClient> GetClient(const std::string &model_name);
65 
SubGraphIndex()66   int SubGraphIndex() const { return subgraph_index_; }
67 
68   void Reset();
69 
70   int LoadModel(const std::shared_ptr<hiai::AiModelMngerClient> &client,
71                 std::vector<std::shared_ptr<hiai::AiModelDescription>> desc_list);
72 
GetFrequency()73   int GetFrequency() { return frequency_; }
74 
75  private:
76   bool IsKirinChip();
77 
78   bool CheckEMUIVersion();
79 
80   bool CheckDDKVersion();
81 
82   int CompareVersion(const std::string &version1, const std::string &version2);
83 
84   std::shared_ptr<hiai::AiModelMngerClient> CreateAiModelMngerClient();
85 
86  private:
87   int subgraph_index_ = 0;
88   bool is_check_version_ = false;
89   bool is_support_ = false;
90   std::unordered_map<std::string, std::shared_ptr<SubGraphModel>> models_;
91   std::vector<std::shared_ptr<hiai::AiModelMngerClient>> clients_;
92   int frequency_ = 0;
93 };
94 
95 }  // namespace mindspore
96 #endif  // MINDSPORE_LITE_SRC_RUNTIME_DELEGATE_NPU_NPU_MANAGER_H_
97