1 /** 2 * Copyright 2019-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_REDUCE_SUM_INFO_H_ 18 #define MINDSPORE_CCSRC_FRONTEND_PARALLEL_OPS_INFO_REDUCE_SUM_INFO_H_ 19 20 #include <memory> 21 #include <string> 22 #include <unordered_map> 23 #include <vector> 24 25 #include "ir/tensor.h" 26 #include "ir/value.h" 27 #include "frontend/parallel/auto_parallel/operator_costmodel.h" 28 #include "frontend/parallel/ops_info/activation_info.h" 29 #include "frontend/parallel/strategy.h" 30 31 namespace mindspore { 32 namespace parallel { 33 class ReduceMethod : public OperatorInfo { 34 public: ReduceMethod(const std::string & name,const Shapes & inputs_shape,const Shapes & outputs_shape,const PrimitiveAttrs & attrs,OperatorCostPtr cost)35 ReduceMethod(const std::string &name, const Shapes &inputs_shape, const Shapes &outputs_shape, 36 const PrimitiveAttrs &attrs, OperatorCostPtr cost) 37 : OperatorInfo(name, inputs_shape, outputs_shape, attrs, cost) {} 38 ~ReduceMethod() override = default; 39 40 Status Init(const StrategyPtr &strategy) override; 41 Status InitForCostModel(const StrategyPtr &strategy) override; 42 43 std::vector<StrategyPtr> GenerateOpStrategies(int64_t stage_id) override; 44 Status SetCostUnderStrategy(const StrategyPtr &strategy) override; 45 46 protected: 47 std::string reduce_method_; 48 bool keepdims_ = false; 49 bool cross_batch_ = false; 50 Status CheckStrategy(const StrategyPtr &strategy) override; 51 Status GetAttrs() override; 52 Dimensions InferOutputStrategy(); 53 Status InferTensorMap() override; 54 Status InferTensorInfo() override; 55 Status InferMirrorOps() override; 56 virtual std::vector<int64_t> reduce_dim(); 57 Status InferForwardCommunication() override; 58 Status InferDevMatrixShape() override; 59 }; 60 61 class ReduceMaxInfo : public ReduceMethod { 62 public: ReduceMaxInfo(const std::string & name,const Shapes & inputs_shape,const Shapes & outputs_shape,const PrimitiveAttrs & attrs)63 ReduceMaxInfo(const std::string &name, const Shapes &inputs_shape, const Shapes &outputs_shape, 64 const PrimitiveAttrs &attrs) 65 : ReduceMethod(name, inputs_shape, outputs_shape, attrs, std::make_shared<ReduceMaxCost>()) { 66 reduce_method_ = REDUCE_OP_MAX; 67 } 68 69 ~ReduceMaxInfo() override = default; 70 }; 71 72 class ArgMaxWithValueInfo : public ReduceMethod { 73 public: ArgMaxWithValueInfo(const std::string & name,const Shapes & inputs_shape,const Shapes & outputs_shape,const PrimitiveAttrs & attrs)74 ArgMaxWithValueInfo(const std::string &name, const Shapes &inputs_shape, const Shapes &outputs_shape, 75 const PrimitiveAttrs &attrs) 76 : ReduceMethod(name, inputs_shape, outputs_shape, attrs, std::make_shared<ArgMaxWithValueCost>()) { 77 reduce_method_ = REDUCE_OP_MAX; 78 } 79 80 ~ArgMaxWithValueInfo() override = default; 81 82 std::vector<StrategyPtr> GenerateOpStrategies(int64_t stage_id) override; 83 84 protected: 85 std::vector<int64_t> reduce_dim() override; 86 Status CheckStrategy(const StrategyPtr &strategy) override; 87 Status InferMirrorOps() override; 88 Status InferTensorMap() override; 89 Status InferTensorInfo() override; 90 Status InferAsLossDivisor() override; 91 }; 92 93 class ArgMinWithValueInfo : public ArgMaxWithValueInfo { 94 public: ArgMinWithValueInfo(const std::string & name,const Shapes & inputs_shape,const Shapes & outputs_shape,const PrimitiveAttrs & attrs)95 ArgMinWithValueInfo(const std::string &name, const Shapes &inputs_shape, const Shapes &outputs_shape, 96 const PrimitiveAttrs &attrs) 97 : ArgMaxWithValueInfo(name, inputs_shape, outputs_shape, attrs) { 98 reduce_method_ = REDUCE_OP_MIN; 99 } 100 101 ~ArgMinWithValueInfo() override = default; 102 }; 103 104 class ReduceMeanInfo : public ReduceMethod { 105 public: ReduceMeanInfo(const std::string & name,const Shapes & inputs_shape,const Shapes & outputs_shape,const PrimitiveAttrs & attrs)106 ReduceMeanInfo(const std::string &name, const Shapes &inputs_shape, const Shapes &outputs_shape, 107 const PrimitiveAttrs &attrs) 108 : ReduceMethod(name, inputs_shape, outputs_shape, attrs, std::make_shared<ReduceMeanCost>()) {} 109 110 ~ReduceMeanInfo() override = default; 111 112 protected: 113 Status InferForwardCommunication() override; 114 }; 115 116 class ReduceSumInfo : public ReduceMethod { 117 public: ReduceSumInfo(const std::string & name,const Shapes & inputs_shape,const Shapes & outputs_shape,const PrimitiveAttrs & attrs)118 ReduceSumInfo(const std::string &name, const Shapes &inputs_shape, const Shapes &outputs_shape, 119 const PrimitiveAttrs &attrs) 120 : ReduceMethod(name, inputs_shape, outputs_shape, attrs, std::make_shared<ReduceSumCost>()) { 121 reduce_method_ = REDUCE_OP_SUM; 122 } 123 124 ~ReduceSumInfo() override = default; 125 }; 126 127 class ReduceAnyInfo : public ReduceMethod { 128 public: ReduceAnyInfo(const std::string & name,const Shapes & inputs_shape,const Shapes & outputs_shape,const PrimitiveAttrs & attrs)129 ReduceAnyInfo(const std::string &name, const Shapes &inputs_shape, const Shapes &outputs_shape, 130 const PrimitiveAttrs &attrs) 131 : ReduceMethod(name, inputs_shape, outputs_shape, attrs, std::make_shared<ReduceSumCost>()) { 132 reduce_method_ = REDUCE_OP_ANY; 133 } 134 ~ReduceAnyInfo() override = default; 135 136 protected: 137 Status CheckStrategy(const StrategyPtr &strategy) override; 138 }; 139 140 class ReduceMinInfo : public ReduceMethod { 141 public: ReduceMinInfo(const std::string & name,const Shapes & inputs_shape,const Shapes & outputs_shape,const PrimitiveAttrs & attrs)142 ReduceMinInfo(const std::string &name, const Shapes &inputs_shape, const Shapes &outputs_shape, 143 const PrimitiveAttrs &attrs) 144 : ReduceMethod(name, inputs_shape, outputs_shape, attrs, std::make_shared<ReduceMinCost>()) { 145 reduce_method_ = REDUCE_OP_MIN; 146 } 147 148 ~ReduceMinInfo() override = default; 149 }; 150 } // namespace parallel 151 } // namespace mindspore 152 #endif // MINDSPORE_CCSRC_FRONTEND_PARALLEL_OPS_INFO_REDUCE_SUM_INFO_H_ 153