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 AnfNodePtress or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_PARALLEL_OP_CONCATENATE_H_ 17 #define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_PARALLEL_OP_CONCATENATE_H_ 18 19 #include <map> 20 #include <unordered_set> 21 #include <unordered_map> 22 #include <memory> 23 #include <string> 24 #include <vector> 25 #include "include/backend/optimizer/pass.h" 26 #include "ir/func_graph.h" 27 #include "backend/common/graph_kernel/graph_kernel_helper.h" 28 #include "backend/common/graph_kernel/core/parallel_op_combine.h" 29 30 namespace mindspore::graphkernel { 31 struct ConcatenatePlan { 32 int concat_in_idx{0}; 33 int split_out_idx{0}; 34 ShapeVector in_shape; 35 ShapeVector out_shape; 36 }; 37 38 class ParallelOpConcatenater : public ParallelOpCombiner { 39 public: 40 explicit ParallelOpConcatenater(const std::string &op_name, uint64_t min_num_branches, const std::string &layout); 41 virtual ~ParallelOpConcatenater() = default; 42 43 protected: 44 virtual bool IsSupportedOp(const AnfNodePtr n) = 0; 45 virtual bool CanOpsBeCombined(const AnfNodePtr a, const AnfNodePtr b) = 0; 46 virtual AnfNodePtr MakeCombinedOp(const Group &branches) = 0; 47 bool IsArgCompatible(const AnfNodePtr a, const AnfNodePtr b); 48 AnfNodePtr MakeCombinedAnfNodePtrFromFollowingOps(const AnfNodePtr &data, const Group &branches, size_t depth) final; 49 void UpdateGroupOutput(const AnfNodePtr &data, const Group &branches, size_t depth) final; 50 51 std::map<size_t, AnfNodePtr> ConcatUniqueInputs(std::map<size_t, AnfNodePtrList> unique_inputs, size_t concat_idx); 52 ConcatenatePlan GetElemWiseFollowingPlan(const Group &branches, size_t depth); 53 AnfNodePtrList ReloadInputs(const Group &branches, size_t depth, AnfNodePtr shared_input); 54 std::vector<ConcatenatePlan> plans_; 55 }; 56 } // namespace mindspore::graphkernel 57 #endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_PARALLEL_OP_CONCATENATE_H_ 58