diff --git a/mindspore/core/abstract/infer_functions.h b/mindspore/core/abstract/infer_functions.h index 63bb768c10a..70d4ebf0d04 100644 --- a/mindspore/core/abstract/infer_functions.h +++ b/mindspore/core/abstract/infer_functions.h @@ -61,8 +61,6 @@ AbstractBasePtr InferImplHSigmoid(const AnalysisEnginePtr &, const PrimitivePtr const AbstractBasePtrList &args_spec_list); AbstractBasePtr InferImplHSigmoidGrad(const AnalysisEnginePtr &, const PrimitivePtr &primitive, const AbstractBasePtrList &args_spec_list); -AbstractBasePtr InferImplZerosLike(const AnalysisEnginePtr &, const PrimitivePtr &primitive, - const AbstractBasePtrList &args_spec_list); AbstractBasePtr InferImplBpropCut(const AnalysisEnginePtr &, const PrimitivePtr &primitive, const AbstractBasePtrList &args_spec_list); AbstractBasePtr InferImplLayerNorm(const AnalysisEnginePtr &, const PrimitivePtr &primitive, diff --git a/mindspore/core/abstract/prim_arrays.cc b/mindspore/core/abstract/prim_arrays.cc index 80939b4fa5b..617648cd4f6 100644 --- a/mindspore/core/abstract/prim_arrays.cc +++ b/mindspore/core/abstract/prim_arrays.cc @@ -738,25 +738,6 @@ AbstractBasePtr InferImplDynamicShape(const AnalysisEnginePtr &, const Primitive return tensor->ToAbstract(); } -AbstractBasePtr InferImplZerosLike(const AnalysisEnginePtr &, const PrimitivePtr &primitive, - const AbstractBasePtrList &args_spec_list) { - const std::string op_name = primitive->name(); - CheckArgsSize(op_name, args_spec_list, 1); - AbstractTensorPtr input_x = CheckArg(op_name, args_spec_list, 0); - ShapeVector x_shape = input_x->shape()->shape(); - ShapeVector x_shape_min = input_x->shape()->min_shape(); - if (x_shape_min.empty()) { - x_shape_min = x_shape; - } - ShapeVector x_shape_max = input_x->shape()->max_shape(); - if (x_shape_max.empty()) { - x_shape_max = x_shape; - } - - ShapePtr output_shape = std::make_shared(x_shape, x_shape_min, x_shape_max); - return std::make_shared(input_x->element(), output_shape); -} - AbstractBasePtr InferImplTranspose(const AnalysisEnginePtr &, const PrimitivePtr &primitive, const AbstractBasePtrList &args_spec_list) { const std::string &op_name = primitive->name(); diff --git a/mindspore/core/abstract/primitive_infer_map.cc b/mindspore/core/abstract/primitive_infer_map.cc index d0e8261eea0..de1d6bdba4a 100644 --- a/mindspore/core/abstract/primitive_infer_map.cc +++ b/mindspore/core/abstract/primitive_infer_map.cc @@ -122,7 +122,6 @@ PrimitiveEvalImplMap &GetPrimitiveToEvalImplMap() { {prim::kPrimReluGrad, {InferImplReluGrad, nullptr, true}}, {prim::kPrimConv2D, {InferImplConv2D, nullptr, true}}, {prim::kPrimBiasAdd, {InferImplBiasAdd, nullptr, true}}, - {prim::kPrimZerosLike, {InferImplZerosLike, nullptr, true}}, {prim::kPrimBpropCut, {InferImplBpropCut, nullptr, true}}, {prim::kPrimLayerNorm, {InferImplLayerNorm, nullptr, true}}, {prim::kPrimLayerNormGrad, {InferImplLayerNormGrad, nullptr, true}}, diff --git a/mindspore/core/base/core_ops.h b/mindspore/core/base/core_ops.h index 6a0a1eee72f..e9c7ac4f53c 100644 --- a/mindspore/core/base/core_ops.h +++ b/mindspore/core/base/core_ops.h @@ -59,6 +59,8 @@ constexpr auto kReLU6 = "ReLU6"; constexpr auto kGeLUGrad = "GeLUGrad"; constexpr auto kFastGeLU = "FastGeLU"; constexpr auto kFastGeLUGrad = "FastGeLUGrad"; +constexpr auto kZerosLike = "ZerosLike"; +constexpr auto kOnesLike = "OnesLike"; // NN constexpr auto kCTCLoss = "CTCLoss"; @@ -301,8 +303,8 @@ inline const PrimitivePtr kPrimReluV2 = std::make_shared("ReLUV2"); inline const PrimitivePtr kPrimPRelu = std::make_shared("PReLU"); inline const PrimitivePtr kPrimSoftplus = std::make_shared("Softplus"); inline const PrimitivePtr kPrimZeros = std::make_shared("Zeros"); -inline const PrimitivePtr kPrimZerosLike = std::make_shared("ZerosLike"); -inline const PrimitivePtr kPrimOnesLike = std::make_shared("OnesLike"); +inline const PrimitivePtr kPrimZerosLike = std::make_shared(kZerosLike); +inline const PrimitivePtr kPrimOnesLike = std::make_shared(kOnesLike); inline const PrimitivePtr kPrimBpropCut = std::make_shared("bprop_cut"); inline const PrimitivePtr kPrimFakeQuantPerLayer = std::make_shared("FakeQuantPerLayer"); inline const PrimitivePtr kPrimFakeQuantPerChannel = std::make_shared("FakeQuantPerChannel"); diff --git a/mindspore/core/ir/dtype/number.cc b/mindspore/core/ir/dtype/number.cc index e08f50b0ae4..1c5a185023a 100644 --- a/mindspore/core/ir/dtype/number.cc +++ b/mindspore/core/ir/dtype/number.cc @@ -46,22 +46,4 @@ Float::Float(const int nbits) : Number(FloatBitsToTypeId(nbits), nbits, false) { MS_LOG(EXCEPTION) << "Wrong number of bits."; } } - -const TypePtr kBool = std::make_shared(); -const TypePtr kInt8 = std::make_shared(8); -const TypePtr kInt16 = std::make_shared(16); -const TypePtr kInt32 = std::make_shared(32); -const TypePtr kInt64 = std::make_shared(64); -const TypePtr kUInt8 = std::make_shared(8); -const TypePtr kUInt16 = std::make_shared(16); -const TypePtr kUInt32 = std::make_shared(32); -const TypePtr kUInt64 = std::make_shared(64); -const TypePtr kFloat16 = std::make_shared(16); -const TypePtr kFloat32 = std::make_shared(32); -const TypePtr kFloat64 = std::make_shared(64); -const TypePtr kInt = std::make_shared(); -const TypePtr kUInt = std::make_shared(); -const TypePtr kFloat = std::make_shared(); -const TypePtr kNumber = std::make_shared(); -const TypePtr kComplex64 = std::make_shared(); } // namespace mindspore diff --git a/mindspore/core/ir/dtype/number.h b/mindspore/core/ir/dtype/number.h index 0d84fbf46fc..72331827ece 100644 --- a/mindspore/core/ir/dtype/number.h +++ b/mindspore/core/ir/dtype/number.h @@ -166,23 +166,23 @@ class Complex64 : public Number { } }; -extern const TypePtr kBool; -extern const TypePtr kInt8; -extern const TypePtr kInt16; -extern const TypePtr kInt32; -extern const TypePtr kInt64; -extern const TypePtr kUInt8; -extern const TypePtr kUInt16; -extern const TypePtr kUInt32; -extern const TypePtr kUInt64; -extern const TypePtr kFloat16; -extern const TypePtr kFloat32; -extern const TypePtr kFloat64; -extern const TypePtr kInt; -extern const TypePtr kUInt; -extern const TypePtr kFloat; -extern const TypePtr kNumber; -extern const TypePtr kComplex64; +inline const TypePtr kBool = std::make_shared(); +inline const TypePtr kInt8 = std::make_shared(8); +inline const TypePtr kInt16 = std::make_shared(16); +inline const TypePtr kInt32 = std::make_shared(32); +inline const TypePtr kInt64 = std::make_shared(64); +inline const TypePtr kUInt8 = std::make_shared(8); +inline const TypePtr kUInt16 = std::make_shared(16); +inline const TypePtr kUInt32 = std::make_shared(32); +inline const TypePtr kUInt64 = std::make_shared(64); +inline const TypePtr kFloat16 = std::make_shared(16); +inline const TypePtr kFloat32 = std::make_shared(32); +inline const TypePtr kFloat64 = std::make_shared(64); +inline const TypePtr kInt = std::make_shared(); +inline const TypePtr kUInt = std::make_shared(); +inline const TypePtr kFloat = std::make_shared(); +inline const TypePtr kNumber = std::make_shared(); +inline const TypePtr kComplex64 = std::make_shared(); } // namespace mindspore #endif // MINDSPORE_CORE_IR_DTYPE_NUMBER_H_ diff --git a/mindspore/core/ops/ones_like.cc b/mindspore/core/ops/ones_like.cc index 7f19ef7f9e0..c7af079390b 100644 --- a/mindspore/core/ops/ones_like.cc +++ b/mindspore/core/ops/ones_like.cc @@ -13,12 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "ops/ones_like.h" -#include #include #include #include -#include "ops/ones_like.h" + #include "ops/op_utils.h" #include "utils/check_convert_utils.h" #include "utils/tensor_construct_utils.h" @@ -28,21 +28,23 @@ namespace mindspore { namespace ops { namespace { abstract::ShapePtr InferShape(const PrimitivePtr &primitive, const std::vector &input_args) { - MS_EXCEPTION_IF_NULL(primitive); - auto input_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape]; - return std::make_shared(input_shape); + auto op_name = primitive->name(); + CheckAndConvertUtils::CheckInteger("infer_shape", input_args.size(), kGreaterEqual, 1, op_name); + return CheckAndConvertUtils::GetInputShapePtr(input_args, 0, op_name); } TypePtr InferType(const PrimitivePtr &primitive, const std::vector &input_args) { + auto op_name = primitive->name(); auto infer_type = input_args[0]->BuildType(); auto valid_type = common_valid_types; valid_type.insert(kBool); - return CheckAndConvertUtils::CheckTensorTypeValid("infer_type", infer_type, valid_type, "OnesLike"); + return CheckAndConvertUtils::CheckTensorTypeValid("infer_type", infer_type, valid_type, op_name); } - } // namespace + AbstractBasePtr OnesLikeInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive, const std::vector &input_args) { + MS_EXCEPTION_IF_NULL(primitive); return std::make_shared(InferType(primitive, input_args), InferShape(primitive, input_args)); } diff --git a/mindspore/core/ops/zeros_like.cc b/mindspore/core/ops/zeros_like.cc index 58784283040..b4a6848ac1b 100644 --- a/mindspore/core/ops/zeros_like.cc +++ b/mindspore/core/ops/zeros_like.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,47 +13,41 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "ops/zeros_like.h" #include -#include -#include #include #include -#include "ops/zeros_like.h" #include "ops/op_utils.h" #include "utils/check_convert_utils.h" +#include "utils/tensor_construct_utils.h" #include "abstract/primitive_infer_map.h" namespace mindspore { namespace ops { 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 number", input_args.size(), kEqual, 1, prim_name); - for (const auto &item : input_args) { - MS_EXCEPTION_IF_NULL(item); - } - auto in_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape]; - return std::make_shared(in_shape); + auto op_name = primitive->name(); + CheckAndConvertUtils::CheckInteger("infer_shape", input_args.size(), kGreaterEqual, 1, op_name); + return CheckAndConvertUtils::GetInputShapePtr(input_args, 0, op_name); } -TypePtr InferType(const PrimitivePtr &prim, const std::vector &input_args) { - std::set valid_types(common_valid_types); - valid_types.emplace(kBool); - if (std::any_of(input_args.begin(), input_args.end(), [](const AbstractBasePtr &a) { return a == nullptr; })) { - MS_LOG(EXCEPTION) << "nullptr"; - } - return CheckAndConvertUtils::CheckTensorTypeValid("x", input_args[0]->BuildType(), valid_types, prim->name()); +TypePtr InferType(const PrimitivePtr &primitive, const std::vector &input_args) { + auto op_name = primitive->name(); + auto infer_type = input_args[0]->BuildType(); + auto valid_type = common_valid_types; + valid_type.insert(kBool); + return CheckAndConvertUtils::CheckTensorTypeValid("infer_type", infer_type, valid_type, op_name); } + } // namespace - AbstractBasePtr ZerosLikeInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive, const std::vector &input_args) { + MS_EXCEPTION_IF_NULL(primitive); return std::make_shared(InferType(primitive, input_args), - InferShape(primitive, input_args)->shape()); + InferShape(primitive, input_args)); } -REGISTER_PRIMITIVE_C(kNameZerosLike, ZerosLike); +REGISTER_PRIMITIVE_EVAL_IMPL(ZerosLike, prim::kPrimZerosLike, ZerosLikeInfer, nullptr, true); } // namespace ops } // namespace mindspore diff --git a/mindspore/core/ops/zeros_like.h b/mindspore/core/ops/zeros_like.h index dd45d45f370..7dde20d6876 100644 --- a/mindspore/core/ops/zeros_like.h +++ b/mindspore/core/ops/zeros_like.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. @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef MINDSPORE_CORE_OPS_ZEROSLIKE_H_ -#define MINDSPORE_CORE_OPS_ZEROSLIKE_H_ +#ifndef MINDSPORE_CORE_OPS_ZEROS_LIKE_H_ +#define MINDSPORE_CORE_OPS_ZEROS_LIKE_H_ #include #include @@ -25,18 +25,17 @@ namespace mindspore { namespace ops { -constexpr auto kNameZerosLike = "ZerosLike"; class ZerosLike : public PrimitiveC { public: - ZerosLike() : PrimitiveC(kNameZerosLike) { InitIOName({"x"}, {"y"}); } + ZerosLike() : PrimitiveC(prim::kPrimZerosLike->name()) { InitIOName({"x"}, {"y"}); } ~ZerosLike() = default; MS_DECLARE_PARENT(ZerosLike, PrimitiveC); void Init() {} }; AbstractBasePtr ZerosLikeInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive, const std::vector &input_args); -using PrimZerosLike = std::shared_ptr; +using PrimZerosLikePtr = std::shared_ptr; } // namespace ops } // namespace mindspore -#endif // MINDSPORE_CORE_OPS_ZEROSLIKE_H_ +#endif // MINDSPORE_CORE_OPS_ZEROS_LIKE_H_ diff --git a/mindspore/core/utils/check_convert_utils.cc b/mindspore/core/utils/check_convert_utils.cc index 2535de3d7f9..9e7fa87ce7a 100644 --- a/mindspore/core/utils/check_convert_utils.cc +++ b/mindspore/core/utils/check_convert_utils.cc @@ -398,6 +398,25 @@ ShapeMap CheckAndConvertUtils::ConvertShapePtrToShapeMap(const BaseShapePtr &sha return shape_map; } +abstract::ShapePtr CheckAndConvertUtils::GetInputShapePtr(const std::vector &input_args, int64_t index, + const std::string &prim_name) { + int64_t input_size = input_args.size(); + if (input_size < index + 1) { + MS_EXCEPTION(ValueError) << prim_name << " input args index out of bound, size " << input_args.size() << ", index " + << index; + } + auto input = input_args[index]; + MS_EXCEPTION_IF_NULL(input); + auto base_shape = input->BuildShape(); + MS_EXCEPTION_IF_NULL(base_shape); + if (!base_shape->isa()) { + MS_EXCEPTION(ValueError) << prim_name << " can not get shape for input " << index; + } + auto shape = base_shape->cast(); + MS_EXCEPTION_IF_NULL(shape); + return shape; +} + void CheckAndConvertUtils::Check(const string &arg_name, int64_t arg_value, CompareEnum compare_type, const string &value_name, int64_t value, const string &prim_name, ExceptionType exception_type) { diff --git a/mindspore/core/utils/check_convert_utils.h b/mindspore/core/utils/check_convert_utils.h index 9472460e029..f5e92538071 100644 --- a/mindspore/core/utils/check_convert_utils.h +++ b/mindspore/core/utils/check_convert_utils.h @@ -237,7 +237,8 @@ class CheckAndConvertUtils { } static ShapeMap ConvertShapePtrToShapeMap(const BaseShapePtr &shape); - + static abstract::ShapePtr GetInputShapePtr(const std::vector &input_args, int64_t index, + const std::string &prim_name); static void Check(const std::string &arg_name, int64_t arg_value, CompareEnum compare_type, const std::string &value_name, int64_t value, const std::string &prim_name = "", ExceptionType exception_type = ValueError); diff --git a/mindspore/ops/_op_impl/tbe/__init__.py b/mindspore/ops/_op_impl/tbe/__init__.py index 84458a49030..e44de504696 100644 --- a/mindspore/ops/_op_impl/tbe/__init__.py +++ b/mindspore/ops/_op_impl/tbe/__init__.py @@ -124,6 +124,7 @@ from .xdivy import _xdivy_tbe from .xlogy import _xlogy_tbe from .floor_div import _floor_div_tbe from .zeros_like import _zeros_like_tbe +from .zeros_like_ds import _zeros_like_ds_tbe from .neg import _neg_tbe from .npu_clear_float_status import _npu_clear_float_status_tbe from .npu_get_float_status import _npu_get_float_status_tbe @@ -223,6 +224,7 @@ from .avg_pool import _avg_pool_tbe from .avg_pool_grad import _avg_pool_grad_tbe from .avg_pool_grad_vm import _avg_pool_grad_vm_tbe from .ones_like import _ones_like_tbe +from .ones_like_ds import _ones_like_ds_tbe from .batch_to_space import _batch_to_space_tbe from .space_to_batch import _space_to_batch_tbe from .depth_to_space import _depth_to_space_tbe diff --git a/mindspore/ops/_op_impl/tbe/ones_like_ds.py b/mindspore/ops/_op_impl/tbe/ones_like_ds.py new file mode 100644 index 00000000000..22a0454672c --- /dev/null +++ b/mindspore/ops/_op_impl/tbe/ones_like_ds.py @@ -0,0 +1,41 @@ +# Copyright 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ + +"""OnesLike op""" +from mindspore.ops.op_info_register import op_info_register, TBERegOp, DataType + +ones_like_op_info = TBERegOp("OnesLike") \ + .fusion_type("OPAQUE") \ + .async_flag(False) \ + .binfile_name("ones_like.so") \ + .compute_cost(10) \ + .kernel_name("ones_like") \ + .partial_flag(True) \ + .dynamic_shape(True) \ + .input(0, "x", False, "required", "all") \ + .output(0, "y", False, "required", "all") \ + .op_pattern("formatAgnostic") \ + .dtype_format(DataType.U8_None, DataType.U8_None) \ + .dtype_format(DataType.I8_None, DataType.I8_None) \ + .dtype_format(DataType.I32_None, DataType.I32_None) \ + .dtype_format(DataType.F16_None, DataType.F16_None) \ + .dtype_format(DataType.F32_None, DataType.F32_None) \ + .get_op_info() + + +@op_info_register(ones_like_op_info) +def _ones_like_ds_tbe(): + """OnesLike TBE register""" + return diff --git a/mindspore/ops/_op_impl/tbe/zeros_like_ds.py b/mindspore/ops/_op_impl/tbe/zeros_like_ds.py new file mode 100644 index 00000000000..ae97a63bec6 --- /dev/null +++ b/mindspore/ops/_op_impl/tbe/zeros_like_ds.py @@ -0,0 +1,42 @@ +# Copyright 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ + +"""ZerosLike op""" +from mindspore.ops.op_info_register import op_info_register, TBERegOp, DataType + +zeros_like_op_info = TBERegOp("ZerosLike") \ + .fusion_type("ELEMWISE") \ + .async_flag(False) \ + .binfile_name("zeros_like.so") \ + .compute_cost(10) \ + .kernel_name("zeros_like") \ + .partial_flag(True) \ + .input(0, "x", False, "required", "all") \ + .output(0, "y", False, "required", "all") \ + .op_pattern("formatAgnostic") \ + .dynamic_shape(True) \ + .dtype_format(DataType.BOOL_None, DataType.BOOL_None) \ + .dtype_format(DataType.I8_None, DataType.I8_None) \ + .dtype_format(DataType.U8_None, DataType.U8_None) \ + .dtype_format(DataType.I32_None, DataType.I32_None) \ + .dtype_format(DataType.F16_None, DataType.F16_None) \ + .dtype_format(DataType.F32_None, DataType.F32_None) \ + .get_op_info() + + +@op_info_register(zeros_like_op_info) +def _zeros_like_ds_tbe(): + """ZerosLike TBE register""" + return diff --git a/mindspore/ops/operations/array_ops.py b/mindspore/ops/operations/array_ops.py index 13849b937d7..fe0742ebb14 100644 --- a/mindspore/ops/operations/array_ops.py +++ b/mindspore/ops/operations/array_ops.py @@ -1379,7 +1379,7 @@ class OnesLike(Primitive): """Initialize OnesLike""" -class ZerosLike(PrimitiveWithCheck): +class ZerosLike(Primitive): """ Creates a new tensor. All elements value are 0. @@ -1411,9 +1411,6 @@ class ZerosLike(PrimitiveWithCheck): """Initialize ZerosLike""" self.init_prim_io_names(inputs=['x'], outputs=['y']) - def check_dtype(self, x_dtype): - validator.check_tensor_dtype_valid('x', x_dtype, mstype.number_type + (mstype.bool_,), self.name) - class TupleToArray(PrimitiveWithInfer): """