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 #ifndef MINDSPORE_LITE_EXTENDRT_DELEGATE_GRAPH_EXECUTOR_H_ 17 #define MINDSPORE_LITE_EXTENDRT_DELEGATE_GRAPH_EXECUTOR_H_ 18 #include <vector> 19 #include <memory> 20 #include "include/api/delegate_api.h" 21 #include "ir/func_graph.h" 22 #include "ir/anf.h" 23 #include "runtime/hardware/device_context.h" 24 #include "tools/common/func_graph_subgraph.h" 25 #include "kernel/kernel.h" 26 namespace mindspore { 27 // Graph sink delegate, the whole FuncGraph as a node to execute. 28 class GraphSinkDelegate : public IDelegate<FuncGraph, CNode, kernel::KernelMod> { 29 public: GraphSinkDelegate(const std::vector<mindspore::MSTensor> & inputs,const std::vector<mindspore::MSTensor> & outputs)30 GraphSinkDelegate(const std::vector<mindspore::MSTensor> &inputs, const std::vector<mindspore::MSTensor> &outputs) 31 : IDelegate<FuncGraph, CNode, kernel::KernelMod>(inputs, outputs) {} 32 virtual ~GraphSinkDelegate() = default; 33 void ReplaceNodes(const std::shared_ptr<FuncGraph> &graph) override; 34 35 bool IsDelegateNode(const std::shared_ptr<CNode> &node) override; 36 37 protected: 38 FuncGraphPtr sink_graph_; 39 }; 40 41 // wrap graph executor as delegate 42 class GraphExecutorDelegate : public GraphSinkDelegate { 43 public: GraphExecutorDelegate(const std::vector<mindspore::MSTensor> & inputs,const std::vector<mindspore::MSTensor> & outputs,std::shared_ptr<device::GraphExecutor> executor)44 explicit GraphExecutorDelegate(const std::vector<mindspore::MSTensor> &inputs, 45 const std::vector<mindspore::MSTensor> &outputs, 46 std::shared_ptr<device::GraphExecutor> executor) 47 : GraphSinkDelegate(inputs, outputs), executor_(executor) {} 48 virtual ~GraphExecutorDelegate() = default; 49 std::shared_ptr<kernel::KernelMod> CreateKernel(const std::shared_ptr<CNode> &node) override; 50 51 private: 52 const std::shared_ptr<device::GraphExecutor> executor_; 53 }; 54 } // namespace mindspore 55 #endif 56