1 /** 2 * Copyright 2023 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_EXTENDRT_GRAPH_COMPILER_COMPILE_RESULT_BUILDER_H_ 18 #define MINDSPORE_LITE_EXTENDRT_GRAPH_COMPILER_COMPILE_RESULT_BUILDER_H_ 19 #include <string> 20 #include <memory> 21 #include <set> 22 #include <unordered_map> 23 #include <utility> 24 #include <vector> 25 #include "src/extendrt/graph_compiler/compile_result.h" 26 #include "src/extendrt/graph_compiler/compile_option.h" 27 #include "src/infer/tensor.h" 28 #include "abstract/abstract_value.h" 29 #include "ir/anf.h" 30 #include "include/api/status.h" 31 32 namespace mindspore { 33 namespace lite { 34 class CompileResultBuilder { 35 public: CompileResultBuilder(CompileOptionPtr option)36 explicit CompileResultBuilder(CompileOptionPtr option) : compile_option_(std::move(option)) { 37 MS_EXCEPTION_IF_NULL(compile_option_); 38 } 39 ~CompileResultBuilder() = default; 40 CompileResultPtr Build(const GraphSegmentPtr &graph_segment, const AnfNodePtrList &inputs, 41 const AnfNodePtrList &outputs); 42 CompileResultPtr Build(const FuncGraphPtr &func_graph); 43 44 private: 45 // build 46 StatusCode BuildInputs(const AnfNodePtrList &inputs); 47 StatusCode BuildNodes(const GraphSegmentPtr &graph_segment); 48 StatusCode BuildNodes(const std::vector<AnfNodePtr> &nodes); 49 StatusCode BuildNodes(const FuncGraphPtr &func_graph); 50 StatusCode BuildOutputs(const AnfNodePtrList &outputs); 51 StatusCode OptimizeGraph(); 52 // methods about node 53 StatusCode CreateAndAppendNode(const CNodePtr &cnode); 54 StatusCode AppendInputCNodeToInputs(const CNodePtr &cnode, const CompileNodePtr &compile_node); 55 StatusCode AppendInputParameterToInputs(const ParameterPtr ¶m_node, const CompileNodePtr &compile_node); 56 StatusCode AppendInputValueNodeToInputs(const ValueNodePtr &value_node, const CompileNodePtr &compile_node); 57 // methods about tensor 58 StatusCode BuildNodeOutputTensor(const CNodePtr &cnode, const CompileNodePtr &compile_node); 59 // methods about optimize 60 StatusCode RemoveSeqGetItemNode(); 61 StatusCode RemoveMakeSeqNode(); 62 StatusCode RemoveDependNode(); 63 // Replace `dst_tensor` with `src_tensor`. 64 void ReplaceTensor(InferTensor *dst_tensor, const InferTensor *src_tensor); 65 66 private: 67 CompileResultPtr graph_ = nullptr; 68 CompileOptionPtr compile_option_{nullptr}; 69 std::set<std::string> input_names_{}; 70 }; 71 } // namespace lite 72 } // namespace mindspore 73 74 #endif 75