1 /** 2 * Copyright 2020 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_OPS_INFO_UNSORTEDSEGMENTOP_INFO_H_ 18 #define MINDSPORE_CCSRC_FRONTEND_PARALLEL_OPS_INFO_UNSORTEDSEGMENTOP_INFO_H_ 19 20 #include <memory> 21 #include <string> 22 #include <unordered_map> 23 #include <vector> 24 25 #include "frontend/parallel/auto_parallel/operator_costmodel.h" 26 #include "frontend/parallel/ops_info/operator_info.h" 27 #include "frontend/parallel/strategy.h" 28 #include "ir/value.h" 29 30 namespace mindspore { 31 namespace parallel { 32 constexpr size_t UNSORTEDSEGMENTOP_INPUTS_SIZE = 2; 33 constexpr size_t UNSORTEDSEGMENTOP_OUTPUTS_SIZE = 1; 34 35 // The operator UnsortedSegment accepts three inputs: 36 // input0 : vector, the shape is x1,x2,x3,...,xr 37 // input1 : segment id, the shape is x1,x2,..,xn 38 // input2 : value, the number of the segments 39 // For Sum: r >= n 40 // For Min: r >=n, n=1 41 class UnsortedSegmentOpInfo : public OperatorInfo { 42 public: UnsortedSegmentOpInfo(const std::string & name,const Shapes & inputs_shape,const Shapes & outputs_shape,const PrimitiveAttrs & attrs,OperatorCostPtr cost)43 UnsortedSegmentOpInfo(const std::string &name, const Shapes &inputs_shape, const Shapes &outputs_shape, 44 const PrimitiveAttrs &attrs, OperatorCostPtr cost) 45 : OperatorInfo(name, inputs_shape, outputs_shape, attrs, cost) {} 46 ~UnsortedSegmentOpInfo() override = default; 47 Status Init(const StrategyPtr &strategy) override; 48 Status InitForCostModel(const StrategyPtr &strategy) override; 49 50 std::vector<StrategyPtr> GenerateOpStrategies(int64_t stage_id) override; 51 Status SetCostUnderStrategy(const StrategyPtr &strategy) override; 52 std::shared_ptr<Strategys> GenerateBatchStrategies() override; 53 54 protected: 55 Status CheckStrategy(const StrategyPtr &strategy) override; 56 Status InferForwardCommunication() override; 57 Status InferMirrorOps() override; 58 Status InferDevMatrixShape() override; 59 Status InferTensorMap() override; 60 Status GetAttrs() override; 61 62 private: 63 Status ComputeReplaceGraph(const CNodePtr &cnode); 64 }; 65 66 class UnsortedSegmentSumInfo : public UnsortedSegmentOpInfo { 67 public: UnsortedSegmentSumInfo(const std::string & name,const Shapes & inputs_shape,const Shapes & outputs_shape,const PrimitiveAttrs & attrs)68 UnsortedSegmentSumInfo(const std::string &name, const Shapes &inputs_shape, const Shapes &outputs_shape, 69 const PrimitiveAttrs &attrs) 70 : UnsortedSegmentOpInfo(name, inputs_shape, outputs_shape, attrs, std::make_shared<UnsortedSegmentSumCost>()) {} 71 ~UnsortedSegmentSumInfo() override = default; 72 }; 73 74 class UnsortedSegmentMinInfo : public UnsortedSegmentOpInfo { 75 public: UnsortedSegmentMinInfo(const std::string & name,const Shapes & inputs_shape,const Shapes & outputs_shape,const PrimitiveAttrs & attrs)76 UnsortedSegmentMinInfo(const std::string &name, const Shapes &inputs_shape, const Shapes &outputs_shape, 77 const PrimitiveAttrs &attrs) 78 : UnsortedSegmentOpInfo(name, inputs_shape, outputs_shape, attrs, std::make_shared<UnsortedSegmentMinCost>()) {} 79 ~UnsortedSegmentMinInfo() override = default; 80 81 ReplaceGraphPtr replace_graph(const CNodePtr &cnode) override; InferForwardCommunication()82 Status InferForwardCommunication() override { return SUCCESS; } 83 84 protected: 85 Status ComputeReplaceGraph(const CNodePtr &cnode); 86 }; 87 class UnsortedSegmentMaxInfo : public UnsortedSegmentOpInfo { 88 public: UnsortedSegmentMaxInfo(const std::string & name,const Shapes & inputs_shape,const Shapes & outputs_shape,const PrimitiveAttrs & attrs)89 UnsortedSegmentMaxInfo(const std::string &name, const Shapes &inputs_shape, const Shapes &outputs_shape, 90 const PrimitiveAttrs &attrs) 91 : UnsortedSegmentOpInfo(name, inputs_shape, outputs_shape, attrs, std::make_shared<UnsortedSegmentMaxCost>()) {} 92 ~UnsortedSegmentMaxInfo() override = default; 93 94 ReplaceGraphPtr replace_graph(const CNodePtr &cnode) override; InferForwardCommunication()95 Status InferForwardCommunication() override { return SUCCESS; } 96 97 protected: 98 Status ComputeReplaceGraph(const CNodePtr &cnode); 99 }; 100 101 } // namespace parallel 102 } // namespace mindspore 103 #endif // MINDSPORE_CCSRC_FRONTEND_PARALLEL_OPS_INFO_UNSORTEDSEGMENTOP_INFO_H_ 104