• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021-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_EXTENDRT_MINDIR_LOADER_MINDIR_MODEL_MINDIR_MODEL_H_
18 #define MINDSPORE_LITE_SRC_EXTENDRT_MINDIR_LOADER_MINDIR_MODEL_MINDIR_MODEL_H_
19 
20 #include <vector>
21 #include <string>
22 
23 #include "extendrt/mindir_loader/abstract_base_model.h"
24 #include "src/litert/schema_tensor_wrapper.h"
25 #include "proto/mind_ir.pb.h"
26 
27 namespace mindspore::infer::mindir {
28 class TensorProtoWrap {
29  public:
TensorProtoWrap(std::string name,const mind_ir::TensorProto & tensor_proto)30   TensorProtoWrap(std::string name, const mind_ir::TensorProto &tensor_proto)
31       : name_(name), tensor_proto_(tensor_proto) {}
32   ~TensorProtoWrap() = default;
33 
tensor_proto()34   const mind_ir::TensorProto &tensor_proto() { return tensor_proto_; }
35 
name()36   std::string name() { return name_; }
37 
38  private:
39   std::string name_;
40   mind_ir::TensorProto tensor_proto_;
41 };
42 
43 class MindirModel : public AbstractBaseModel {
44  public:
MindirModel()45   MindirModel() {}
~MindirModel()46   ~MindirModel() { Destroy(); }
47 
48   bool ModelVerify() const override;
49   int ConvertTensors(std::vector<mindspore::lite::Tensor *> *lite_tensors) override;
50   std::string GetModelPath() const override;
51   virtual mindspore::kernel::KernelExec *FindBackendKernel(const std::vector<mindspore::lite::Tensor *> &in_tensors,
52                                                            const std::vector<mindspore::lite::Tensor *> &out_tensors,
53                                                            const LiteGraph::Node *node, lite::InnerContext *context,
54                                                            TypeId prefer_data_type);
55 
56   void Free() override;
57   void Destroy() override;
58 
SetModelPath(const std::string & model_path)59   void SetModelPath(const std::string &model_path) { this->model_path_ = model_path; }
60 
61  private:
62   mindspore::lite::Tensor *ConvertTensor(TensorProtoWrap mindir_tensor);
63   int LoadTensorData(mindspore::lite::Tensor *lite_tensor, const mind_ir::TensorProto &mindir_tensor);
64   int CheckTensorValid(lite::Tensor *dst_tensor);
65   mindspore::kernel::KernelExec *FindLiteKernel(const std::vector<mindspore::lite::Tensor *> &in_tensors,
66                                                 const std::vector<mindspore::lite::Tensor *> &out_tensors,
67                                                 const LiteGraph::Node *node, lite::InnerContext *context,
68                                                 TypeId prefer_data_type);
69 
70  public:
71   std::vector<TensorProtoWrap> all_mindir_tensors_;
72   mind_ir::ModelProto mindir_model_proto_;
73 
74  private:
75   std::string model_path_;
76   bool select_lite_kernel_ = true;
77 };
78 }  // namespace mindspore::infer::mindir
79 
80 #endif  // MINDSPORE_LITE_SRC_EXTENDRT_MINDIR_LOADER_MINDIR_MODEL_MINDIR_MODEL_H_
81