1 /** 2 * Copyright 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_LITE_MINDRT_H_ 18 #define MINDSPORE_LITE_SRC_RUNTIME_LITE_MINDRT_H_ 19 #include <vector> 20 #include <memory> 21 #include <string> 22 #include <unordered_map> 23 #include <set> 24 #include <utility> 25 #include "actor/op_actor.h" 26 #include "src/executor/kernel_exec.h" 27 #include "actor/actor.h" 28 #include "async/uuid_base.h" 29 #include "async/future.h" 30 #include "src/executor/sub_graph_kernel.h" 31 #include "src/litert/cpu_info.h" 32 #include "src/tensorlist.h" 33 34 namespace mindspore::lite { 35 36 typedef enum { GRAPH, OP_BY_OP } MindRTMode; 37 class LiteOpActor : public OpActor<lite::Tensor> { 38 public: LiteOpActor(kernel::KernelExec * kernel,lite::InnerContext * ctx)39 explicit LiteOpActor(kernel::KernelExec *kernel, lite::InnerContext *ctx) 40 : OpActor<lite::Tensor>(kernel->name()), kernel_(kernel), ctx_(ctx) { 41 inputs_data_.resize(kernel_->in_tensors().size()); 42 #if defined(ENABLE_ARM) && defined(ENABLE_FP16) 43 CpuInfo cpu_info; 44 support_fp16_ = cpu_info.ArmIsSupportFp16(); 45 #endif 46 } ~LiteOpActor()47 ~LiteOpActor() override { 48 delete call_node_; 49 delete partial_node_; 50 } 51 void RunOpData(OpData<lite::Tensor> *input_data, OpContext<lite::Tensor> *context = nullptr) override; 52 virtual int CompileArrow(const std::unordered_map<void *, std::set<std::pair<AID, size_t>>> &receivers_map); RunKernel(const KernelCallBack & before,const KernelCallBack & after)53 int RunKernel(const KernelCallBack &before, const KernelCallBack &after) { 54 auto ret = kernel_->Execute(before, after); 55 if (RET_OK != ret) { 56 MS_LOG(ERROR) << "run kernel failed, name: " << kernel_->name(); 57 return ret; 58 } 59 return ret; 60 } set_isolate_input_map(std::unordered_map<Tensor *,Tensor * > * input_map)61 void set_isolate_input_map(std::unordered_map<Tensor *, Tensor *> *input_map) { 62 this->isolate_input_map_ = input_map; 63 } 64 virtual int PreInit(std::vector<std::shared_ptr<LiteOpActor>> *actors, 65 std::unordered_map<Tensor *, Tensor *> *input_map); 66 virtual int PostInit(); 67 int ResizeGraphInput(const std::vector<mindspore::lite::Tensor *> &inputs, const std::vector<std::vector<int>> &dims); 68 69 public: 70 void AddResultIndex(size_t index, size_t tensor_index); GetKernel()71 const kernel::KernelExec *GetKernel() const { return kernel_; } 72 // call this function after CompileArrow GetPartialKernels()73 virtual std::set<kernel::KernelExec *> GetPartialKernels() const { 74 if (partial_node_ == nullptr) { 75 return {}; 76 } 77 std::set<kernel::KernelExec *> ret{partial_node_}; 78 return ret; 79 } 80 81 protected: 82 virtual bool NeedResize(); 83 virtual int SetInputShape(); 84 virtual int InitInputData(); 85 virtual int AssignInputData(); 86 void SetOutputData(const OpContext<Tensor> *context); 87 virtual void AsyncOutput(OpContext<Tensor> *context); 88 89 int CompileArrowThroughOutputTensors( 90 const std::unordered_map<void *, std::set<std::pair<AID, size_t>>> &receivers_map); 91 int IsolateInputData(std::vector<std::shared_ptr<LiteOpActor>> *actors, 92 std::unordered_map<Tensor *, Tensor *> *input_map); 93 virtual int PrepareOutputData(); 94 virtual int UpdateActorOutput(); 95 96 kernel::KernelExec *kernel_; 97 std::vector<size_t> results_index_{}; 98 std::vector<size_t> results_tensor_index_{}; 99 std::vector<OpDataPtr<Tensor>> outputs_data_{}; 100 std::vector<Tensor *> inputs_data_{}; 101 std::unordered_map<Tensor *, Tensor *> *isolate_input_map_ = nullptr; /* real obj in session */ 102 lite::InnerContext *ctx_ = nullptr; 103 104 kernel::KernelExec *partial_node_ = nullptr; 105 kernel::KernelExec *call_node_ = nullptr; 106 bool support_fp16_ = false; 107 108 private: 109 int CreateCommonArrow(const std::unordered_map<void *, std::set<std::pair<AID, size_t>>> &receivers_map, 110 const std::set<void *> &receiver_tensors, const size_t &output_index, 111 std::unordered_map<AID, std::set<size_t>> *receiver_index_set); 112 int CreateEmptyArrow(const size_t &output_index); 113 bool ArrowHasCompiled(const AID &actor_name, size_t to_index, 114 const std::unordered_map<AID, std::set<size_t>> &receiver_index_set); 115 void MarkArrowAsCompiled(const AID *actor_name, size_t to_index, 116 std::unordered_map<AID, std::set<size_t>> *receiver_index_set); 117 }; 118 119 int MindrtInit(); 120 void MindrtTerminate(const std::vector<std::shared_ptr<LiteOpActor>> &, 121 const std::shared_ptr<ActorMgr> &actor_mgr = nullptr); 122 static std::atomic_int64_t actor_count = 0; 123 124 std::vector<std::shared_ptr<LiteOpActor>> CreateOpActor(const std::vector<kernel::KernelExec *> &kernels, 125 lite::InnerContext *ctx, const std::shared_ptr<ActorMgr> &mgr); 126 } // namespace mindspore::lite 127 #endif // MINDSPORE_LITE_SRC_RUNTIME_LITE_MINDRT_H_ 128