1 /** 2 * Copyright 2019 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_CCSRC_FRONTEND_PARALLEL_GRAPH_UTIL_GENERATE_GRAPH_H_ 18 #define MINDSPORE_CCSRC_FRONTEND_PARALLEL_GRAPH_UTIL_GENERATE_GRAPH_H_ 19 20 #include <map> 21 #include <memory> 22 #include <string> 23 #include <unordered_map> 24 #include <utility> 25 #include <vector> 26 27 #include "frontend/optimizer/opt.h" 28 #include "frontend/parallel/strategy.h" 29 #include "frontend/parallel/tensor_layout/tensor_redistribution.h" 30 31 namespace mindspore { 32 namespace parallel { 33 #define USING_HASH_NAME "USING_HASH_NAME" 34 // Get the operator's path where the operator has be defined 35 std::string GetOpPythonPath(const OperatorName &op_name); 36 37 // Init python operator Instance 38 ValuePtr CreatOpInstance(const OperatorAttrs &attrs, const OperatorName &op_name, const std::string &instance_name); 39 40 AnfNodePtr CreatTypeInt(int64_t value); 41 AnfNodePtr CreatInt64Imm(int64_t value); 42 AnfNodePtr CreateInt32Tensor(int64_t value); 43 AnfNodePtr ValuePtrToAnfNodePtr(const ValuePtr &value_ptr); 44 AnfNodePtr CreateTuple(const std::vector<int64_t> &tuple); 45 std::string HashInstanceName(const std::string &name); 46 47 class GenerateGraph { 48 public: GenerateGraph(std::unordered_map<std::string,ValuePtr> origin_attrs)49 explicit GenerateGraph(std::unordered_map<std::string, ValuePtr> origin_attrs) 50 : name_idx_(0), origin_attrs_(origin_attrs) {} 51 Status Init(const CNodePtr &cnode); 52 ~GenerateGraph() = default; virtual_input_node()53 AnfNodePtr virtual_input_node() { return virtual_input_node_; } 54 AnfNodePtr NewOpInst(const OperatorName &op_name, const OperatorAttrs &attrs); 55 AnfNodePtr NewOpInst(const OperatorName &op_name); 56 AnfNodePtr PushBack(const std::vector<AnfNodePtr> &inputs); 57 58 private: 59 CNodePtr cnode_; 60 FuncGraphManagerPtr manager_; 61 ScopePtr scope_; 62 FuncGraphPtr func_graph_; 63 AnfNodePtr virtual_input_node_; 64 std::string instance_name_base_; 65 int64_t name_idx_; 66 std::unordered_map<std::string, ValuePtr> origin_attrs_; 67 }; 68 } // namespace parallel 69 } // namespace mindspore 70 71 #endif // MINDSPORE_CCSRC_FRONTEND_PARALLEL_GRAPH_UTIL_GENERATE_GRAPH_H_ 72