From cfd7b4baf8df83da2baa4549ef6afc579e0c0be3 Mon Sep 17 00:00:00 2001 From: shen_jingxing Date: Mon, 10 May 2021 10:13:30 +0800 Subject: [PATCH] relu --- mindspore/core/abstract/infer_functions.h | 2 - mindspore/core/abstract/prim_nn.cc | 7 ---- .../core/abstract/primitive_infer_map.cc | 2 - mindspore/core/base/core_ops.h | 6 ++- mindspore/core/ops/relu.cc | 37 +++++++++++++++++-- mindspore/core/ops/relu.h | 11 ++++-- mindspore/core/ops/relu6.cc | 5 ++- mindspore/core/ops/relu6.h | 2 +- 8 files changed, 50 insertions(+), 22 deletions(-) diff --git a/mindspore/core/abstract/infer_functions.h b/mindspore/core/abstract/infer_functions.h index 709a372944..63bb768c10 100644 --- a/mindspore/core/abstract/infer_functions.h +++ b/mindspore/core/abstract/infer_functions.h @@ -57,8 +57,6 @@ AbstractBasePtr InferImplBiasAdd(const AnalysisEnginePtr &, const PrimitivePtr & const AbstractBasePtrList &args_spec_list); AbstractBasePtr InferImplBiasAddGrad(const AnalysisEnginePtr &, const PrimitivePtr &primitive, const AbstractBasePtrList &args_spec_list); -AbstractBasePtr InferImplRelu(const AnalysisEnginePtr &, const PrimitivePtr &primitive, - const AbstractBasePtrList &args_spec_list); AbstractBasePtr InferImplHSigmoid(const AnalysisEnginePtr &, const PrimitivePtr &primitive, const AbstractBasePtrList &args_spec_list); AbstractBasePtr InferImplHSigmoidGrad(const AnalysisEnginePtr &, const PrimitivePtr &primitive, diff --git a/mindspore/core/abstract/prim_nn.cc b/mindspore/core/abstract/prim_nn.cc index b90d075b0c..c649835aa8 100644 --- a/mindspore/core/abstract/prim_nn.cc +++ b/mindspore/core/abstract/prim_nn.cc @@ -409,13 +409,6 @@ AbstractBasePtr InferImplBiasAddGrad(const AnalysisEnginePtr &, const PrimitiveP return ret; } -AbstractBasePtr InferImplRelu(const AnalysisEnginePtr &, const PrimitivePtr &primitive, - const AbstractBasePtrList &args_spec_list) { - // Inputs: a tensor. - CheckArgsSize(primitive->name(), args_spec_list, 1); - return args_spec_list[0]->Broaden(); -} - AbstractBasePtr InferImplHSigmoid(const AnalysisEnginePtr &, const PrimitivePtr &primitive, const AbstractBasePtrList &args_spec_list) { // Inputs: a tensor. diff --git a/mindspore/core/abstract/primitive_infer_map.cc b/mindspore/core/abstract/primitive_infer_map.cc index 93b7cdcbb6..6311a6cb44 100644 --- a/mindspore/core/abstract/primitive_infer_map.cc +++ b/mindspore/core/abstract/primitive_infer_map.cc @@ -121,8 +121,6 @@ PrimitiveEvalImplMap &GetPrimitiveToEvalImplMap() { {prim::kPrimReluGrad, {InferImplReluGrad, nullptr, true}}, {prim::kPrimConv2D, {InferImplConv2D, nullptr, true}}, {prim::kPrimBiasAdd, {InferImplBiasAdd, nullptr, true}}, - {prim::kPrimRelu, {InferImplRelu, nullptr, true}}, - {prim::kPrimRelu6, {InferImplRelu, nullptr, true}}, {prim::kPrimZerosLike, {InferImplZerosLike, nullptr, true}}, {prim::kPrimBpropCut, {InferImplBpropCut, nullptr, true}}, {prim::kPrimLayerNorm, {InferImplLayerNorm, nullptr, true}}, diff --git a/mindspore/core/base/core_ops.h b/mindspore/core/base/core_ops.h index 3f796b03ad..189f7050c4 100644 --- a/mindspore/core/base/core_ops.h +++ b/mindspore/core/base/core_ops.h @@ -51,6 +51,8 @@ constexpr auto kStack = "Stack"; constexpr auto kUnstack = "Unstack"; constexpr auto kTupleGetItem = "TupleGetItem"; constexpr auto kGeLU = "GeLU"; +constexpr auto kReLU = "ReLU"; +constexpr auto kReLU6 = "ReLU6"; constexpr auto kGeLUGrad = "GeLUGrad"; constexpr auto kFastGeLU = "FastGeLU"; constexpr auto kFastGeLUGrad = "FastGeLUGrad"; @@ -284,9 +286,9 @@ inline const PrimitivePtr kPrimGeLU = std::make_shared(kGeLU); inline const PrimitivePtr kPrimGeLUGrad = std::make_shared(kGeLUGrad); inline const PrimitivePtr kPrimFastGeLU = std::make_shared(kFastGeLU); inline const PrimitivePtr kPrimFastGeLUGrad = std::make_shared(kFastGeLUGrad); -inline const PrimitivePtr kPrimRelu = std::make_shared("ReLU"); +inline const PrimitivePtr kPrimRelu = std::make_shared(kReLU); inline const PrimitivePtr kPrimElu = std::make_shared("Elu"); -inline const PrimitivePtr kPrimRelu6 = std::make_shared("ReLU6"); +inline const PrimitivePtr kPrimRelu6 = std::make_shared(kReLU6); inline const PrimitivePtr kPrimReluV2 = std::make_shared("ReLUV2"); inline const PrimitivePtr kPrimPRelu = std::make_shared("PReLU"); inline const PrimitivePtr kPrimSoftplus = std::make_shared("Softplus"); diff --git a/mindspore/core/ops/relu.cc b/mindspore/core/ops/relu.cc index d81f85ee4a..5e2c931d1d 100644 --- a/mindspore/core/ops/relu.cc +++ b/mindspore/core/ops/relu.cc @@ -1,5 +1,5 @@ /** - * Copyright 2020 Huawei Technologies Co., Ltd + * Copyright 2020-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,13 +13,44 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #include "ops/relu.h" +#include +#include +#include +#include +#include + +#include "ops/op_utils.h" #include "utils/check_convert_utils.h" #include "abstract/primitive_infer_map.h" namespace mindspore { namespace ops { -REGISTER_PRIMITIVE_C(kNameReLU, ReLU); +abstract::ShapePtr InferShape(const PrimitivePtr &primitive, const std::vector &input_args) { + MS_EXCEPTION_IF_NULL(primitive); + auto x = input_args[0]->BuildShape(); + MS_EXCEPTION_IF_NULL(x); + auto shape_element = x->cast(); + MS_EXCEPTION_IF_NULL(shape_element); + return shape_element; +} +TypePtr InferType(const PrimitivePtr &prim, const std::vector &input_args) { + MS_EXCEPTION_IF_NULL(prim); + auto prim_name = prim->name(); + CheckAndConvertUtils::CheckInteger("ReLU infer", input_args.size(), kEqual, 1, prim_name); + MS_EXCEPTION_IF_NULL(input_args[0]); + auto x_type_map = input_args[0]->BuildType(); + MS_EXCEPTION_IF_NULL(x_type_map); + auto x_type = x_type_map->cast(); + std::set valid_x_type = {kTensorType}; + return CheckAndConvertUtils::CheckTensorTypeValid("input_x", x_type, valid_x_type, prim_name); +} +AbstractBasePtr ReLUInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive, + const std::vector &input_args) { + return std::make_shared(InferType(primitive, input_args), + InferShape(primitive, input_args)); +} +REGISTER_PRIMITIVE_EVAL_IMPL(ReLU, prim::kPrimRelu, ReLUInfer, nullptr, true); + } // namespace ops } // namespace mindspore diff --git a/mindspore/core/ops/relu.h b/mindspore/core/ops/relu.h index 50236de3fb..3c21bb610f 100644 --- a/mindspore/core/ops/relu.h +++ b/mindspore/core/ops/relu.h @@ -1,5 +1,5 @@ /** - * Copyright 2020 Huawei Technologies Co., Ltd + * Copyright 2020-2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,16 +13,20 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #ifndef MINDSPORE_CORE_OPS_RELU_H_ #define MINDSPORE_CORE_OPS_RELU_H_ +#include +#include +#include +#include #include "ops/primitive_c.h" +#include "ops/op_utils.h" #include "abstract/abstract_value.h" #include "utils/check_convert_utils.h" namespace mindspore { namespace ops { -constexpr auto kNameReLU = "ReLU"; +constexpr auto kNameReLU = prim::kReLU; class ReLU : public PrimitiveC { public: ReLU() : PrimitiveC(kNameReLU) { InitIOName({"x"}, {"output"}); } @@ -30,6 +34,7 @@ class ReLU : public PrimitiveC { MS_DECLARE_PARENT(ReLU, PrimitiveC); void Init() {} }; +using PrimReLUPtr = std::shared_ptr; } // namespace ops } // namespace mindspore diff --git a/mindspore/core/ops/relu6.cc b/mindspore/core/ops/relu6.cc index e74ac04882..cd4b7c657f 100644 --- a/mindspore/core/ops/relu6.cc +++ b/mindspore/core/ops/relu6.cc @@ -29,6 +29,7 @@ namespace { abstract::ShapePtr InferShape(const PrimitivePtr &primitive, const std::vector &input_args) { MS_EXCEPTION_IF_NULL(primitive); auto x = input_args[0]->GetShapeTrack(); + MS_EXCEPTION_IF_NULL(x); auto shape_element = x->cast(); MS_EXCEPTION_IF_NULL(shape_element); return shape_element; @@ -47,8 +48,8 @@ TypePtr InferType(const PrimitivePtr &prim, const std::vector & AbstractBasePtr ReLU6Infer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive, const std::vector &input_args) { return std::make_shared(InferType(primitive, input_args), - InferShape(primitive, input_args)->shape()); + InferShape(primitive, input_args)); } -REGISTER_PRIMITIVE_C(kNameReLU6, ReLU6); +REGISTER_PRIMITIVE_EVAL_IMPL(ReLU6, prim::kPrimRelu6, ReLU6Infer, nullptr, true); } // namespace ops } // namespace mindspore diff --git a/mindspore/core/ops/relu6.h b/mindspore/core/ops/relu6.h index 57003c2dcd..de76b6f686 100644 --- a/mindspore/core/ops/relu6.h +++ b/mindspore/core/ops/relu6.h @@ -25,7 +25,7 @@ namespace mindspore { namespace ops { -constexpr auto kNameReLU6 = "ReLU6"; +constexpr auto kNameReLU6 = prim::kReLU6; class ReLU6 : public PrimitiveC { public: ReLU6() : PrimitiveC(kNameReLU6) { InitIOName({"x"}, {"output"}); }