• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020-2021 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 #include "ops/grad/max_pool_grad.h"
18 
19 #include <algorithm>
20 #include <map>
21 
22 #include "abstract/abstract_value.h"
23 #include "abstract/dshape.h"
24 #include "abstract/ops/op_infer.h"
25 #include "abstract/ops/primitive_infer_map.h"
26 #include "abstract/utils.h"
27 #include "base/base.h"
28 #include "ir/anf.h"
29 #include "ir/primitive.h"
30 #include "mindapi/src/helper.h"
31 #include "mindspore/core/ops/conv_pool_ops.h"
32 #include "ops/op_name.h"
33 #include "ops/primitive_c.h"
34 #include "utils/check_convert_utils.h"
35 #include "utils/log_adapter.h"
36 
37 namespace mindspore {
38 namespace ops {
39 namespace {
MaxPoolGradInferShape(const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)40 abstract::ShapePtr MaxPoolGradInferShape(const PrimitivePtr &primitive,
41                                          const std::vector<AbstractBasePtr> &input_args) {
42   MS_EXCEPTION_IF_NULL(primitive);
43   auto shape_map = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->GetShape());
44   auto shape = std::make_shared<abstract::Shape>(shape_map[kShape]);
45   return shape;
46 }
MaxPoolGradInferType(const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)47 TypePtr MaxPoolGradInferType(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
48   if (std::any_of(input_args.begin(), input_args.end(), [](const AbstractBasePtr &a) { return a == nullptr; })) {
49     MS_LOG(EXCEPTION) << "For '" << primitive->name()
50                       << ", the input args used for infer shape and type is necessary, but missing it.";
51   }
52 
53   return input_args[0]->GetType();
54 }
55 }  // namespace
56 MIND_API_OPERATOR_IMPL(MaxPoolGrad, PoolGrad);
MaxPoolGradInfer(const abstract::AnalysisEnginePtr &,const PrimitivePtr & primitive,const std::vector<abstract::AbstractBasePtr> & input_args)57 abstract::AbstractBasePtr MaxPoolGradInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
58                                            const std::vector<abstract::AbstractBasePtr> &input_args) {
59   MS_EXCEPTION_IF_NULL(primitive);
60   auto infer_type = MaxPoolGradInferType(primitive, input_args);
61   auto infer_shape = MaxPoolGradInferShape(primitive, input_args);
62   return abstract::MakeAbstract(infer_shape, infer_type);
63 }
64 
65 // AG means auto generated
66 class MIND_API AGMaxPoolGradInfer : public abstract::OpInferBase {
67  public:
InferShape(const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args) const68   BaseShapePtr InferShape(const PrimitivePtr &primitive,
69                           const std::vector<AbstractBasePtr> &input_args) const override {
70     return MaxPoolGradInferShape(primitive, input_args);
71   }
72 
InferType(const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args) const73   TypePtr InferType(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) const override {
74     return MaxPoolGradInferType(primitive, input_args);
75   }
InferShapeAndType(const abstract::AnalysisEnginePtr & engine,const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args) const76   AbstractBasePtr InferShapeAndType(const abstract::AnalysisEnginePtr &engine, const PrimitivePtr &primitive,
77                                     const std::vector<AbstractBasePtr> &input_args) const override {
78     return MaxPoolGradInfer(engine, primitive, input_args);
79   }
80 };
81 
82 REGISTER_PRIMITIVE_OP_INFER_IMPL(MaxPoolGrad, prim::kPrimMaxPoolGrad, AGMaxPoolGradInfer, false);
83 }  // namespace ops
84 }  // namespace mindspore
85