From 96de085a1f5bae7e1d2a72630d4714c012ed6adb Mon Sep 17 00:00:00 2001 From: shen_jingxing Date: Wed, 2 Jun 2021 11:33:21 +0800 Subject: [PATCH] Log&Reciprocal operator --- .../core/abstract/primitive_infer_map.cc | 5 ++- mindspore/core/base/core_ops.h | 7 ++- mindspore/core/ops/log.cc | 38 +++++++++++++--- mindspore/core/ops/log.h | 6 +-- mindspore/core/ops/reciprocal.cc | 44 ++++++++++++++----- mindspore/core/ops/reciprocal.h | 6 +-- 6 files changed, 80 insertions(+), 26 deletions(-) diff --git a/mindspore/core/abstract/primitive_infer_map.cc b/mindspore/core/abstract/primitive_infer_map.cc index ba196344c98..68ac052567d 100644 --- a/mindspore/core/abstract/primitive_infer_map.cc +++ b/mindspore/core/abstract/primitive_infer_map.cc @@ -17,11 +17,12 @@ */ #include "abstract/primitive_infer_map.h" - #include #include #include #include "ops/exp.h" +#include "ops/log.h" +#include "ops/reciprocal.h" #include "ops/real_div.h" #include "ops/add.h" #include "ops/equal.h" @@ -181,6 +182,8 @@ PrimitiveEvalImplMap &GetPrimitiveToBackendEvalImplMap() { {prim::kPrimTile, {ops::TileInfer, nullptr, true}}, {prim::kPrimEqual, {ops::EqualInfer, nullptr, true}}, {prim::kPrimNotEqual, {ops::NotEqualInfer, nullptr, true}}, + {prim::kPrimLog, {ops::LogInfer, nullptr, true}}, + {prim::kPrimReciprocal, {ops::ReciprocalInfer, nullptr, true}}, {prim::kPrimReduceSum, {InferImplReduceFunc, nullptr, true}}, {prim::kPrimReduceMean, {InferImplReduceFunc, nullptr, true}}, {prim::kPrimReduceAll, {InferImplReduceFunc, nullptr, true}}, diff --git a/mindspore/core/base/core_ops.h b/mindspore/core/base/core_ops.h index 8cde463d073..3701b8bf443 100644 --- a/mindspore/core/base/core_ops.h +++ b/mindspore/core/base/core_ops.h @@ -51,6 +51,9 @@ constexpr auto kNotEqual = "NotEqual"; constexpr auto kSub = "Sub"; constexpr auto kMul = "Mul"; constexpr auto kRealDiv = "RealDiv"; +constexpr auto kReciprocal = "Reciprocal"; +constexpr auto kLog = "Log"; + constexpr auto kAdd = "Add"; constexpr auto kTile = "Tile"; constexpr auto kBiasAddGrad = "BiasAddGrad"; @@ -429,13 +432,13 @@ inline const PrimitivePtr kPrimRealDiv = std::make_shared(kRealDiv); inline const PrimitivePtr kPrimFloorDiv = std::make_shared("FloorDiv"); inline const PrimitivePtr kPrimSqrt = std::make_shared("Sqrt"); inline const PrimitivePtr kPrimSqrtGrad = std::make_shared("SqrtGrad"); -inline const PrimitivePtr kPrimReciprocal = std::make_shared("Reciprocal"); +inline const PrimitivePtr kPrimReciprocal = std::make_shared(kReciprocal); inline const PrimitivePtr kPrimExpandDims = std::make_shared("ExpandDims"); inline const PrimitivePtr kPrimAbs = std::make_shared("Abs"); inline const PrimitivePtr kPrimRint = std::make_shared("Rint"); inline const PrimitivePtr kPrimRound = std::make_shared("Round"); inline const PrimitivePtr kPrimExp = std::make_shared(kExp); -inline const PrimitivePtr kPrimLog = std::make_shared("Log"); +inline const PrimitivePtr kPrimLog = std::make_shared(kLog); inline const PrimitivePtr kPrimRsqrt = std::make_shared("Rsqrt"); inline const PrimitivePtr kPrimRsqrtGrad = std::make_shared("RsqrtGrad"); inline const PrimitivePtr kPrimSplitV = std::make_shared("SplitV"); diff --git a/mindspore/core/ops/log.cc b/mindspore/core/ops/log.cc index 8916394f2e9..b5203673e89 100644 --- a/mindspore/core/ops/log.cc +++ b/mindspore/core/ops/log.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. @@ -14,8 +14,12 @@ * limitations under the License. */ -#include #include "ops/log.h" +#include +#include +#include +#include +#include #include "ops/op_utils.h" #include "utils/check_convert_utils.h" #include "abstract/primitive_infer_map.h" @@ -24,14 +28,34 @@ namespace mindspore { namespace ops { namespace { abstract::ShapePtr InferShape(const PrimitivePtr &primitive, const std::vector &input_args) { - auto x_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape]; - return std::make_shared(x_shape); + MS_EXCEPTION_IF_NULL(primitive); + auto prim_name = primitive->name(); + CheckAndConvertUtils::CheckInteger("input numbers", input_args.size(), kEqual, 1, prim_name); + for (const auto &item : input_args) { + MS_EXCEPTION_IF_NULL(item); + } + auto shape_map = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape()); + auto in_shape = shape_map[kShape]; + auto min_shape = shape_map[kMinShape]; + auto max_shape = shape_map[kMaxShape]; + if (min_shape.size() != 0 && max_shape.size() != 0) { + return std::make_shared(in_shape, min_shape, max_shape); + } + return std::make_shared(in_shape); } TypePtr InferType(const PrimitivePtr &prim, const std::vector &input_args) { - const std::set valid_types = {kTensorType}; - return CheckAndConvertUtils::CheckTensorTypeValid("infer type", input_args[0]->BuildType(), valid_types, - prim->name()); + MS_EXCEPTION_IF_NULL(prim); + auto op_name = prim->name(); + CheckAndConvertUtils::CheckInteger("input number", input_args.size(), kEqual, 1, op_name); + for (const auto &item : input_args) { + MS_EXCEPTION_IF_NULL(item); + } + std::map types; + types.emplace("x", input_args[0]->BuildType()); + std::set valid_params_types = {kTensorType}; + CheckAndConvertUtils::CheckSubClass("x_type", input_args[0]->BuildType(), valid_params_types, op_name); + return CheckAndConvertUtils::CheckTensorTypeSame(types, common_valid_types, prim->name()); } } // namespace diff --git a/mindspore/core/ops/log.h b/mindspore/core/ops/log.h index c847dec3b5c..43c1ea74154 100644 --- a/mindspore/core/ops/log.h +++ b/mindspore/core/ops/log.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. @@ -25,10 +25,10 @@ namespace mindspore { namespace ops { -constexpr auto kNameLog = "Log"; +constexpr auto kNameLog = prim::kLog; class Log : public PrimitiveC { public: - Log() : PrimitiveC(kNameLog) { InitIOName({"x"}, {"y"}); } + Log() : PrimitiveC(prim::kPrimLog->name()) { InitIOName({"x"}, {"y"}); } ~Log() = default; MS_DECLARE_PARENT(Log, PrimitiveC); }; diff --git a/mindspore/core/ops/reciprocal.cc b/mindspore/core/ops/reciprocal.cc index 7d3bdd996f0..08388173569 100644 --- a/mindspore/core/ops/reciprocal.cc +++ b/mindspore/core/ops/reciprocal.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. @@ -14,31 +14,55 @@ * limitations under the License. */ +#include "ops/reciprocal.h" #include #include #include #include #include -#include "ops/reciprocal.h" #include "ops/op_utils.h" #include "utils/check_convert_utils.h" #include "abstract/primitive_infer_map.h" namespace mindspore { namespace ops { -AbstractBasePtr ReciprocalInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive, - const std::vector &input_args) { +namespace { +abstract::ShapePtr InferShape(const PrimitivePtr &primitive, const std::vector &input_args) { MS_EXCEPTION_IF_NULL(primitive); auto prim_name = primitive->name(); + CheckAndConvertUtils::CheckInteger("input numbers", input_args.size(), kEqual, 1, prim_name); for (const auto &item : input_args) { MS_EXCEPTION_IF_NULL(item); } - // infer shape - auto in_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->GetShapeTrack())[kShape]; - // infer type - std::set valid_x_type = {kTensorType}; - auto x_type = CheckAndConvertUtils::CheckTypeValid("x_type", input_args[0]->BuildType(), valid_x_type, prim_name); - return std::make_shared(x_type, in_shape); + auto shape_map = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape()); + auto in_shape = shape_map[kShape]; + auto min_shape = shape_map[kMinShape]; + auto max_shape = shape_map[kMaxShape]; + if (min_shape.size() != 0 && max_shape.size() != 0) { + return std::make_shared(in_shape, min_shape, max_shape); + } + return std::make_shared(in_shape); +} + +TypePtr InferType(const PrimitivePtr &prim, const std::vector &input_args) { + MS_EXCEPTION_IF_NULL(prim); + auto op_name = prim->name(); + CheckAndConvertUtils::CheckInteger("input number", input_args.size(), kEqual, 1, op_name); + for (const auto &item : input_args) { + MS_EXCEPTION_IF_NULL(item); + } + std::map types; + types.emplace("x", input_args[0]->BuildType()); + std::set valid_params_types = {kTensorType}; + CheckAndConvertUtils::CheckSubClass("x_type", input_args[0]->BuildType(), valid_params_types, op_name); + return CheckAndConvertUtils::CheckTensorTypeSame(types, common_valid_types, prim->name()); +} +} // namespace + +AbstractBasePtr ReciprocalInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive, + const std::vector &input_args) { + return std::make_shared(InferType(primitive, input_args), + InferShape(primitive, input_args)->shape()); } REGISTER_PRIMITIVE_C(kNameReciprocal, Reciprocal); } // namespace ops diff --git a/mindspore/core/ops/reciprocal.h b/mindspore/core/ops/reciprocal.h index a75b5fdb597..85cd39fd57a 100644 --- a/mindspore/core/ops/reciprocal.h +++ b/mindspore/core/ops/reciprocal.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. @@ -24,10 +24,10 @@ namespace mindspore { namespace ops { -constexpr auto kNameReciprocal = "Reciprocal"; +constexpr auto kNameReciprocal = prim::kReciprocal; class Reciprocal : public PrimitiveC { public: - Reciprocal() : PrimitiveC(kNameReciprocal) { InitIOName({"x"}, {"y"}); } + Reciprocal() : PrimitiveC(prim::kPrimReciprocal->name()) { InitIOName({"x"}, {"y"}); } ~Reciprocal() = default; MS_DECLARE_PARENT(Reciprocal, PrimitiveC); void Init() {}