!16065 zeroslike and oneslike support dynamic shape

From: @wangnan39
Reviewed-by: 
Signed-off-by:
This commit is contained in:
mindspore-ci-bot 2021-05-19 14:32:54 +08:00 committed by Gitee
commit c9e091f355
15 changed files with 159 additions and 100 deletions

View File

@ -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,

View File

@ -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<AbstractTensor>(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<Shape>(x_shape, x_shape_min, x_shape_max);
return std::make_shared<AbstractTensor>(input_x->element(), output_shape);
}
AbstractBasePtr InferImplTranspose(const AnalysisEnginePtr &, const PrimitivePtr &primitive,
const AbstractBasePtrList &args_spec_list) {
const std::string &op_name = primitive->name();

View File

@ -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}},

View File

@ -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<Primitive>("ReLUV2");
inline const PrimitivePtr kPrimPRelu = std::make_shared<Primitive>("PReLU");
inline const PrimitivePtr kPrimSoftplus = std::make_shared<Primitive>("Softplus");
inline const PrimitivePtr kPrimZeros = std::make_shared<Primitive>("Zeros");
inline const PrimitivePtr kPrimZerosLike = std::make_shared<Primitive>("ZerosLike");
inline const PrimitivePtr kPrimOnesLike = std::make_shared<Primitive>("OnesLike");
inline const PrimitivePtr kPrimZerosLike = std::make_shared<Primitive>(kZerosLike);
inline const PrimitivePtr kPrimOnesLike = std::make_shared<Primitive>(kOnesLike);
inline const PrimitivePtr kPrimBpropCut = std::make_shared<Primitive>("bprop_cut");
inline const PrimitivePtr kPrimFakeQuantPerLayer = std::make_shared<Primitive>("FakeQuantPerLayer");
inline const PrimitivePtr kPrimFakeQuantPerChannel = std::make_shared<Primitive>("FakeQuantPerChannel");

View File

@ -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<Bool>();
const TypePtr kInt8 = std::make_shared<Int>(8);
const TypePtr kInt16 = std::make_shared<Int>(16);
const TypePtr kInt32 = std::make_shared<Int>(32);
const TypePtr kInt64 = std::make_shared<Int>(64);
const TypePtr kUInt8 = std::make_shared<UInt>(8);
const TypePtr kUInt16 = std::make_shared<UInt>(16);
const TypePtr kUInt32 = std::make_shared<UInt>(32);
const TypePtr kUInt64 = std::make_shared<UInt>(64);
const TypePtr kFloat16 = std::make_shared<Float>(16);
const TypePtr kFloat32 = std::make_shared<Float>(32);
const TypePtr kFloat64 = std::make_shared<Float>(64);
const TypePtr kInt = std::make_shared<Int>();
const TypePtr kUInt = std::make_shared<UInt>();
const TypePtr kFloat = std::make_shared<Float>();
const TypePtr kNumber = std::make_shared<Number>();
const TypePtr kComplex64 = std::make_shared<Complex64>();
} // namespace mindspore

View File

@ -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<Bool>();
inline const TypePtr kInt8 = std::make_shared<Int>(8);
inline const TypePtr kInt16 = std::make_shared<Int>(16);
inline const TypePtr kInt32 = std::make_shared<Int>(32);
inline const TypePtr kInt64 = std::make_shared<Int>(64);
inline const TypePtr kUInt8 = std::make_shared<UInt>(8);
inline const TypePtr kUInt16 = std::make_shared<UInt>(16);
inline const TypePtr kUInt32 = std::make_shared<UInt>(32);
inline const TypePtr kUInt64 = std::make_shared<UInt>(64);
inline const TypePtr kFloat16 = std::make_shared<Float>(16);
inline const TypePtr kFloat32 = std::make_shared<Float>(32);
inline const TypePtr kFloat64 = std::make_shared<Float>(64);
inline const TypePtr kInt = std::make_shared<Int>();
inline const TypePtr kUInt = std::make_shared<UInt>();
inline const TypePtr kFloat = std::make_shared<Float>();
inline const TypePtr kNumber = std::make_shared<Number>();
inline const TypePtr kComplex64 = std::make_shared<Complex64>();
} // namespace mindspore
#endif // MINDSPORE_CORE_IR_DTYPE_NUMBER_H_

View File

@ -13,12 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ops/ones_like.h"
#include <set>
#include <string>
#include <vector>
#include <memory>
#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<AbstractBasePtr> &input_args) {
MS_EXCEPTION_IF_NULL(primitive);
auto input_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
return std::make_shared<abstract::Shape>(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<AbstractBasePtr> &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<AbstractBasePtr> &input_args) {
MS_EXCEPTION_IF_NULL(primitive);
return std::make_shared<abstract::AbstractTensor>(InferType(primitive, input_args),
InferShape(primitive, input_args));
}

View File

@ -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 <vector>
#include <set>
#include <map>
#include <string>
#include <memory>
#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<AbstractBasePtr> &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<abstract::Shape>(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<AbstractBasePtr> &input_args) {
std::set<TypePtr> 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<AbstractBasePtr> &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<AbstractBasePtr> &input_args) {
MS_EXCEPTION_IF_NULL(primitive);
return std::make_shared<abstract::AbstractTensor>(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

View File

@ -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 <memory>
#include <vector>
@ -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<AbstractBasePtr> &input_args);
using PrimZerosLike = std::shared_ptr<ZerosLike>;
using PrimZerosLikePtr = std::shared_ptr<ZerosLike>;
} // namespace ops
} // namespace mindspore
#endif // MINDSPORE_CORE_OPS_ZEROSLIKE_H_
#endif // MINDSPORE_CORE_OPS_ZEROS_LIKE_H_

View File

@ -398,6 +398,25 @@ ShapeMap CheckAndConvertUtils::ConvertShapePtrToShapeMap(const BaseShapePtr &sha
return shape_map;
}
abstract::ShapePtr CheckAndConvertUtils::GetInputShapePtr(const std::vector<AbstractBasePtr> &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<abstract::Shape>()) {
MS_EXCEPTION(ValueError) << prim_name << " can not get shape for input " << index;
}
auto shape = base_shape->cast<abstract::ShapePtr>();
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) {

View File

@ -237,7 +237,8 @@ class CheckAndConvertUtils {
}
static ShapeMap ConvertShapePtrToShapeMap(const BaseShapePtr &shape);
static abstract::ShapePtr GetInputShapePtr(const std::vector<AbstractBasePtr> &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);

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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):
"""