forked from huawei/mindspore2022
commit
5c72a3c2bc
|
|
@ -18,34 +18,6 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
namespace {
|
||||
abstract::ShapePtr ArgMaxInferShape(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
auto axis = GetValue<int64_t>(primitive->GetAttr(kAxis));
|
||||
auto x_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
auto x_rank = SizeToLong(x_shape.size());
|
||||
CheckAndConvertUtils::CheckInRange<int64_t>("argmax axis", axis, kIncludeLeft, {-x_rank, x_rank}, prim_name);
|
||||
axis = axis < 0 ? axis + x_rank : axis;
|
||||
std::vector<int64_t> out_shape;
|
||||
for (size_t i = 0; i < x_shape.size(); ++i) {
|
||||
if (SizeToLong(i) != axis) {
|
||||
(void)out_shape.emplace_back(x_shape[i]);
|
||||
}
|
||||
}
|
||||
return std::make_shared<abstract::Shape>(out_shape);
|
||||
}
|
||||
|
||||
TypePtr ArgMaxInferType(const PrimitivePtr &prim, const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(prim);
|
||||
(void)CheckAndConvertUtils::CheckInteger("input number", SizeToLong(input_args.size()), kEqual, 1, prim->name());
|
||||
for (const auto &item : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(item);
|
||||
}
|
||||
return kInt32;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void ArgMax::Init(const int64_t axis, const TypeId output_type) {
|
||||
set_axis(axis);
|
||||
set_output_type(output_type);
|
||||
|
|
@ -60,11 +32,6 @@ TypeId ArgMax::get_output_type() const {
|
|||
return type_ptr->type_id();
|
||||
}
|
||||
|
||||
AbstractBasePtr ArgMaxInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
return std::make_shared<abstract::AbstractTensor>(ArgMaxInferType(primitive, input_args),
|
||||
ArgMaxInferShape(primitive, input_args)->shape());
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameArgMax, ArgMax);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -34,32 +34,6 @@ TypeId ArgMin::get_output_type() const {
|
|||
return type_ptr->type_id();
|
||||
}
|
||||
|
||||
AbstractBasePtr ArgMinInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
(void)CheckAndConvertUtils::CheckInteger("arg_min_infer", SizeToLong(input_args.size()), kEqual, 1, prim_name);
|
||||
|
||||
// Infer shape
|
||||
auto axis = GetValue<int64_t>(primitive->GetAttr(kAxis));
|
||||
MS_EXCEPTION_IF_NULL(input_args[0]);
|
||||
auto x_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
auto x_rank = SizeToLong(x_shape.size());
|
||||
CheckAndConvertUtils::CheckInRange<int64_t>("axis", axis, kIncludeLeft, {-x_rank, x_rank}, prim_name);
|
||||
if (axis < 0) {
|
||||
axis += x_rank;
|
||||
}
|
||||
std::vector<int64_t> out_shape;
|
||||
for (int64_t i = 0; i < x_rank; i++) {
|
||||
if (i != axis) {
|
||||
out_shape.push_back(x_shape[LongToSize(i)]);
|
||||
}
|
||||
}
|
||||
|
||||
// Infer type
|
||||
auto x_dtype = input_args[0]->BuildType()->cast<TensorTypePtr>()->element();
|
||||
return std::make_shared<abstract::AbstractTensor>(x_dtype, std::make_shared<abstract::Shape>(out_shape));
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameArgMin, ArgMin);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -34,51 +34,6 @@ int64_t Assert::get_summarize() const {
|
|||
return GetValue<int64_t>(value_ptr);
|
||||
}
|
||||
|
||||
AbstractBasePtr AssertInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto op_name = primitive->name();
|
||||
for (const auto &item : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(item);
|
||||
}
|
||||
TypePtr condition;
|
||||
if (!(input_args[0]->BuildType()->type_id() == kObjectTypeTensorType)) {
|
||||
auto condition_values = GetValue<std::vector<bool>>(input_args[0]->BuildValue());
|
||||
(void)CheckAndConvertUtils::CheckInteger("condition's rank", SizeToLong(condition_values.size()), kLessEqual, 1,
|
||||
op_name);
|
||||
if (condition_values.size() == 1) {
|
||||
if (!condition_values[0]) {
|
||||
MS_EXCEPTION(ValueError) << "For '" << op_name
|
||||
<< "', condition value must be `true` when only one value contained, but got "
|
||||
<< !condition_values[0];
|
||||
}
|
||||
}
|
||||
condition = TypeIdToType(kNumberTypeBool);
|
||||
} else {
|
||||
auto condition_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
(void)CheckAndConvertUtils::CheckInteger("condition's rank", condition_shape[0], kLessEqual, 1, op_name);
|
||||
if (condition_shape[0] == 1) {
|
||||
auto condition_value = reinterpret_cast<bool *>(input_args[0]->BuildValue()->cast<tensor::TensorPtr>()->data_c());
|
||||
MS_EXCEPTION_IF_NULL(condition_value);
|
||||
if (!*condition_value) {
|
||||
MS_EXCEPTION(ValueError) << "For '" << op_name
|
||||
<< "', condition value must be `true` when only one value contained, but got "
|
||||
<< !*condition_value;
|
||||
}
|
||||
}
|
||||
condition = input_args[0]->BuildType();
|
||||
}
|
||||
std::vector<int64_t> output_shape = {1};
|
||||
std::set<TypePtr> local_bool = {kBool};
|
||||
std::map<std::string, TypePtr> args = {{"condition", condition}};
|
||||
(void)CheckAndConvertUtils::CheckScalarOrTensorTypesSame(args, local_bool, op_name);
|
||||
auto inputs_type = input_args[1]->BuildType()->cast<TuplePtr>()->elements();
|
||||
for (auto dtype : inputs_type) {
|
||||
std::set<TypePtr> template_types = {kTensorType};
|
||||
(void)CheckAndConvertUtils::CheckSubClass("input", dtype, template_types, op_name);
|
||||
}
|
||||
return std::make_shared<abstract::AbstractTensor>(kInt32, output_shape);
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameAssert, Assert);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -78,63 +78,6 @@ void AvgPool::Init(const std::vector<int64_t> &kernel_size, const std::vector<in
|
|||
this->set_round_mode(round_mode);
|
||||
}
|
||||
|
||||
namespace {
|
||||
abstract::ShapePtr AvgPoolInferShape(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
|
||||
auto op_name = primitive->name();
|
||||
auto in_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->GetShapeTrack())[kShape];
|
||||
auto format = Format(GetValue<int64_t>(primitive->GetAttr(kFormat)));
|
||||
const int64_t x_size = 4;
|
||||
const int64_t attr_size = 4;
|
||||
(void)CheckAndConvertUtils::CheckInteger("x_rank", SizeToLong(in_shape.size()), kEqual, x_size, op_name);
|
||||
if (format == NHWC) {
|
||||
in_shape = {in_shape[0], in_shape[3], in_shape[1], in_shape[2]};
|
||||
}
|
||||
auto kernel_size = GetValue<std::vector<int64_t>>(primitive->GetAttr(kKernelSize));
|
||||
auto pad_mode = PadMode(GetValue<int64_t>(primitive->GetAttr(kPadMode)));
|
||||
auto batch = in_shape[0];
|
||||
auto channel = in_shape[1];
|
||||
auto in_h = in_shape[2];
|
||||
auto in_w = in_shape[3];
|
||||
auto strides = GetValue<std::vector<int64_t>>(primitive->GetAttr(kStrides));
|
||||
(void)CheckAndConvertUtils::CheckInteger("kernel size", SizeToLong(kernel_size.size()), kEqual, attr_size, op_name);
|
||||
(void)CheckAndConvertUtils::CheckInteger("strides size", SizeToLong(strides.size()), kEqual, attr_size, op_name);
|
||||
if (std::any_of(strides.begin(), strides.end(), [](int64_t stride) { return stride <= 0; })) {
|
||||
MS_LOG(EXCEPTION) << "For '" << op_name << "', strides must be positive, but it's " << strides << ".";
|
||||
}
|
||||
if (std::any_of(kernel_size.begin(), kernel_size.end(), [](int64_t size) { return size <= 0; })) {
|
||||
MS_LOG(EXCEPTION) << "For '" << op_name << "', Kernel size must be positive, but it's " << kernel_size << ".";
|
||||
}
|
||||
auto kernel_h = kernel_size[2];
|
||||
auto kernel_w = kernel_size[3];
|
||||
auto stride_h = strides[2];
|
||||
auto stride_w = strides[3];
|
||||
int64_t out_h = abstract::Shape::SHP_ANY;
|
||||
int64_t out_w = abstract::Shape::SHP_ANY;
|
||||
if (pad_mode == VALID) {
|
||||
out_h = static_cast<int64_t>(std::ceil((in_h - (kernel_h - 1)) / static_cast<float>(stride_h)));
|
||||
out_w = static_cast<int64_t>(std::ceil((in_w - (kernel_w - 1)) / static_cast<float>(stride_w)));
|
||||
} else if (pad_mode == SAME) {
|
||||
out_h = static_cast<int64_t>(std::ceil(in_h / static_cast<float>(stride_h)));
|
||||
out_w = static_cast<int64_t>(std::ceil(in_w / static_cast<float>(stride_w)));
|
||||
}
|
||||
std::vector<int64_t> out_shape = {batch, channel, out_h, out_w};
|
||||
if (format == NHWC) {
|
||||
out_shape = {batch, out_h, out_w, channel};
|
||||
}
|
||||
return std::make_shared<abstract::Shape>(out_shape);
|
||||
}
|
||||
|
||||
TypePtr AvgPoolInferType(const std::vector<AbstractBasePtr> &input_args) { return input_args[0]->BuildType(); }
|
||||
} // namespace
|
||||
|
||||
AbstractBasePtr AvgPoolInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
const int64_t input_num = 1;
|
||||
CheckAndConvertUtils::CheckInputArgs(input_args, kEqual, input_num, primitive->name());
|
||||
return std::make_shared<abstract::AbstractTensor>(AvgPoolInferType(input_args),
|
||||
AvgPoolInferShape(primitive, input_args)->shape());
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameAvgPool, AvgPool);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -68,71 +68,6 @@ Format BatchNorm::get_format() const {
|
|||
return Format(GetValue<int64_t>(value_ptr));
|
||||
}
|
||||
|
||||
AbstractBasePtr BatchNormInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
// Infer shape
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
const int64_t input_num = 5;
|
||||
(void)CheckAndConvertUtils::CheckInteger("input number", SizeToLong(input_args.size()), kEqual, input_num, prim_name);
|
||||
|
||||
auto input_x = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
auto format = Format(GetValue<int64_t>(primitive->GetAttr(kFormat)));
|
||||
if (format == NHWC) {
|
||||
input_x = {input_x[0], input_x[3], input_x[1], input_x[2]};
|
||||
}
|
||||
auto scale = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[kInputIndex1]->BuildShape())[kShape];
|
||||
auto bias = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[kInputIndex2]->BuildShape())[kShape];
|
||||
auto mean = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[kInputIndex3]->BuildShape())[kShape];
|
||||
auto variance = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[kInputIndex4]->BuildShape())[kShape];
|
||||
|
||||
std::vector<int64_t> input_shape_norm;
|
||||
if (format == NCHW) {
|
||||
input_shape_norm = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->GetShapeTrack())[kShape];
|
||||
} else {
|
||||
input_shape_norm.push_back(input_x[0]);
|
||||
input_shape_norm.push_back(input_x[3]);
|
||||
input_shape_norm.push_back(input_x[1]);
|
||||
input_shape_norm.push_back(input_x[2]);
|
||||
}
|
||||
(void)CheckAndConvertUtils::CheckInteger("scale rank", SizeToLong(scale.size()), kEqual, 1, prim_name);
|
||||
CheckAndConvertUtils::Check("scale shape", scale, kEqual, bias, prim_name, TypeError);
|
||||
CheckAndConvertUtils::Check("scale shape[0]", scale[0], kEqual, input_shape_norm[1], prim_name, TypeError);
|
||||
|
||||
if (!GetValue<bool>(primitive->GetAttr(kIsTraining))) {
|
||||
(void)CheckAndConvertUtils::CheckInteger("mean rank", SizeToLong(mean.size()), kEqual, 1, prim_name);
|
||||
CheckAndConvertUtils::Check("mean shape", mean, kEqual, variance, prim_name, TypeError);
|
||||
CheckAndConvertUtils::Check("mean shape", mean, kEqual, scale, prim_name, TypeError);
|
||||
}
|
||||
|
||||
// Infer type
|
||||
auto scale_type = input_args[kInputIndex1]->BuildType()->cast<TensorTypePtr>()->element();
|
||||
auto bias_type = input_args[kInputIndex2]->BuildType()->cast<TensorTypePtr>()->element();
|
||||
|
||||
const std::set<TypePtr> valid_types = {kFloat16, kFloat32};
|
||||
auto input_x_type =
|
||||
CheckAndConvertUtils::CheckTensorTypeValid("x", input_args[kInputIndex0]->BuildType(), valid_types, prim_name);
|
||||
std::map<std::string, TypePtr> args;
|
||||
(void)args.emplace("scale", input_args[kInputIndex1]->BuildType());
|
||||
(void)args.emplace("bias", input_args[kInputIndex2]->BuildType());
|
||||
(void)CheckAndConvertUtils::CheckTensorTypeSame(args, valid_types, prim_name);
|
||||
std::map<std::string, TypePtr> args_moving;
|
||||
(void)args_moving.emplace("scale", input_args[kInputIndex2]->BuildType());
|
||||
(void)args_moving.emplace("bias", input_args[kInputIndex3]->BuildType());
|
||||
(void)CheckAndConvertUtils::CheckTensorTypeSame(args_moving, valid_types, prim_name);
|
||||
|
||||
auto output0 = std::make_shared<abstract::AbstractTensor>(input_x_type, input_x);
|
||||
auto output1 = std::make_shared<abstract::AbstractTensor>(scale_type, scale);
|
||||
auto output2 = std::make_shared<abstract::AbstractTensor>(bias_type, scale);
|
||||
auto output3 = std::make_shared<abstract::AbstractTensor>(input_x_type, scale);
|
||||
if (format == NHWC) {
|
||||
output2 = std::make_shared<abstract::AbstractTensor>(scale_type, scale);
|
||||
output3 = std::make_shared<abstract::AbstractTensor>(bias_type, scale);
|
||||
output1 = std::make_shared<abstract::AbstractTensor>(input_x_type, scale);
|
||||
}
|
||||
AbstractBasePtrList output = {output0, output1, output2, output3, output3};
|
||||
return std::make_shared<abstract::AbstractTuple>(output);
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameBatchNorm, BatchNorm);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -44,43 +44,6 @@ std::vector<std::vector<int64_t>> BatchToSpace::get_crops() const {
|
|||
return GetValue<std::vector<std::vector<int64_t>>>(value_ptr);
|
||||
}
|
||||
|
||||
AbstractBasePtr BatchToSpaceInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
(void)CheckAndConvertUtils::CheckInteger("input number", SizeToLong(input_args.size()), kEqual, 1, prim_name);
|
||||
for (const auto &item : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(item);
|
||||
}
|
||||
(void)CheckAndConvertUtils::CheckTensorTypeValid("input_x", input_args[0]->BuildType(), common_valid_types,
|
||||
prim_name);
|
||||
|
||||
auto x_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
auto block_size = GetValue<std::vector<int64_t>>(primitive->GetAttr(kBlockSize));
|
||||
auto crops = GetValue<std::vector<std::vector<int64_t>>>(primitive->GetAttr(kCrops));
|
||||
auto out_shape = x_shape;
|
||||
const int64_t attr_size = 4;
|
||||
const int64_t x_rank = 4;
|
||||
(void)CheckAndConvertUtils::CheckInteger("x rank", SizeToLong(x_shape.size()), kEqual, x_rank, prim_name);
|
||||
(void)CheckAndConvertUtils::CheckInteger("block_size size", SizeToLong(block_size.size()), kEqual, attr_size,
|
||||
prim_name);
|
||||
(void)CheckAndConvertUtils::CheckInteger("crops size", SizeToLong(crops.size()), kEqual, attr_size, prim_name);
|
||||
(void)CheckAndConvertUtils::CheckInteger("crops[0] size", SizeToLong(crops[0].size()), kEqual, attr_size, prim_name);
|
||||
(void)CheckAndConvertUtils::CheckInteger("crops[1] size", SizeToLong(crops[1].size()), kEqual, attr_size, prim_name);
|
||||
for (size_t i = 0; i < 2; ++i) {
|
||||
auto x_block_prod = out_shape[i + 2] * block_size[i];
|
||||
auto crops_sum = crops[i][0] + crops[i][1];
|
||||
CheckAndConvertUtils::Check("x block shape prod", x_block_prod, kGreaterThan, attr_size, prim_name);
|
||||
out_shape[i + 2] = x_block_prod - crops_sum;
|
||||
}
|
||||
(void)CheckAndConvertUtils::CheckInteger("x_shape[0] % (block_size[0]*block_size[1])",
|
||||
out_shape[0] % (block_size[0] * block_size[1]), kEqual, 0, prim_name);
|
||||
out_shape[0] /= block_size[0] * block_size[1];
|
||||
|
||||
auto ret = input_args[0]->Broaden();
|
||||
ret->set_shape(std::make_shared<abstract::Shape>(out_shape));
|
||||
return ret;
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameBatchToSpace, BatchToSpace);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -42,26 +42,7 @@ std::string Broadcast::get_group() const {
|
|||
auto value_ptr = this->GetAttr(kGroup);
|
||||
return GetValue<std::string>(value_ptr);
|
||||
}
|
||||
AbstractBasePtr BroadcastInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
for (const auto &item : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(item);
|
||||
}
|
||||
// infer shape
|
||||
auto in_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
// infer type
|
||||
auto x_type = input_args[0]->BuildType()->cast<TensorTypePtr>()->element();
|
||||
std::vector<TypePtr> output_types;
|
||||
const std::set<TypePtr> valid_types = {kInt8, kInt32, kFloat16, kFloat32};
|
||||
for (size_t i = 0; i < input_args.size(); i++) {
|
||||
auto out_type = input_args[i]->BuildType()->cast<TensorTypePtr>()->element();
|
||||
output_types.push_back(out_type);
|
||||
(void)CheckAndConvertUtils::CheckTensorTypeValid("index_type", out_type, valid_types, prim_name);
|
||||
}
|
||||
return std::make_shared<abstract::AbstractTensor>(x_type, in_shape);
|
||||
}
|
||||
|
||||
REGISTER_PRIMITIVE_C(kNameBroadcast, Broadcast);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -29,52 +29,6 @@ int64_t Concat::get_axis() const {
|
|||
|
||||
void Concat::set_axis(const int64_t axis) { (void)this->AddAttr(kAxis, MakeValue(axis)); }
|
||||
|
||||
AbstractBasePtr ConcatInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
(void)CheckAndConvertUtils::CheckInteger("input number", SizeToLong(input_args.size()), kEqual, 1, prim_name);
|
||||
for (const auto &item : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(item);
|
||||
}
|
||||
auto input_tuple = input_args[0]->cast<abstract::AbstractTuplePtr>();
|
||||
MS_EXCEPTION_IF_NULL(input_tuple);
|
||||
auto elements = input_tuple->elements();
|
||||
const int64_t kOneNum = 1;
|
||||
(void)CheckAndConvertUtils::CheckInteger("concat element num", SizeToLong(elements.size()), kGreaterEqual, kOneNum,
|
||||
prim_name);
|
||||
auto element0 = elements[0]->cast<abstract::AbstractTensorPtr>();
|
||||
MS_EXCEPTION_IF_NULL(element0);
|
||||
auto element0_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(element0->BuildShape())[kShape];
|
||||
auto element0_rank = element0_shape.size();
|
||||
auto axis_temp = GetValue<int64_t>(primitive->GetAttr(kAxis));
|
||||
CheckAndConvertUtils::CheckInRange<int64_t>("Concat axis", axis_temp, kIncludeBoth,
|
||||
{-SizeToLong(element0_rank) - kOneNum, SizeToLong(element0_rank)},
|
||||
prim_name);
|
||||
auto axis = axis_temp < 0 ? LongToSize(axis_temp) + element0_rank : LongToSize(axis_temp);
|
||||
|
||||
std::map<std::string, TypePtr> types;
|
||||
(void)types.emplace("element0", element0->BuildType());
|
||||
int64_t all_shp = element0_shape[axis];
|
||||
for (size_t i = 1; i < elements.size(); ++i) {
|
||||
std::string elementi = "element" + std::to_string(i);
|
||||
auto elementi_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(elements[i]->BuildShape())[kShape];
|
||||
(void)CheckAndConvertUtils::CheckInteger(elementi + " shape rank", SizeToLong(elementi_shape.size()), kEqual,
|
||||
SizeToLong(element0_shape.size()), prim_name);
|
||||
for (size_t j = 0; j < element0_rank; ++j) {
|
||||
if (j != axis && elementi_shape[j] != element0_shape[j]) {
|
||||
MS_LOG(EXCEPTION) << "For '" << prim_name << "', element " << i
|
||||
<< " shape in input should concat with first element, but it can not.";
|
||||
}
|
||||
}
|
||||
all_shp = all_shp == -1 || elementi_shape[axis] == -1 ? -1 : all_shp + elementi_shape[axis];
|
||||
(void)types.emplace(elementi, elements[i]->BuildType());
|
||||
}
|
||||
auto infer_type = CheckAndConvertUtils::CheckTensorTypeSame(types, all_types, prim_name);
|
||||
auto ret_shape = element0_shape;
|
||||
ret_shape[axis] = all_shp;
|
||||
return std::make_shared<abstract::AbstractTensor>(infer_type, std::make_shared<abstract::Shape>(ret_shape));
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameConcat, Concat);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -21,23 +21,6 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
namespace {
|
||||
abstract::ShapePtr ConstantOfShapeInferShape(const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
(void)CheckAndConvertUtils::CheckInteger("input args size", SizeToLong(input_args.size()), kEqual, 1,
|
||||
"ConstantOfShape");
|
||||
auto input_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
return std::make_shared<abstract::Shape>(input_shape);
|
||||
}
|
||||
|
||||
TypePtr ConstantOfShapeInferType(const PrimitivePtr &primitive) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto data_type = TypeId(GetValue<int64_t>(primitive->GetAttr(kDataType)));
|
||||
return TypeIdToType(data_type);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void ConstantOfShape::Init(int64_t data_type, const std::vector<float> &value) {
|
||||
this->set_data_type(data_type);
|
||||
this->set_value(value);
|
||||
|
|
@ -56,11 +39,7 @@ std::vector<float> ConstantOfShape::get_value() const {
|
|||
auto value_ptr = this->GetAttr(kValue);
|
||||
return GetValue<std::vector<float>>(value_ptr);
|
||||
}
|
||||
AbstractBasePtr ConstantOfShapeInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
return std::make_shared<abstract::AbstractTensor>(ConstantOfShapeInferType(primitive),
|
||||
ConstantOfShapeInferShape(primitive, input_args)->shape());
|
||||
}
|
||||
|
||||
REGISTER_PRIMITIVE_C(kNameConstantOfShape, ConstantOfShape);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -40,21 +40,7 @@ std::vector<int64_t> Crop::get_offsets() const {
|
|||
auto value_ptr = this->GetAttr(kOffsets);
|
||||
return GetValue<std::vector<int64_t>>(value_ptr);
|
||||
}
|
||||
AbstractBasePtr CropInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
const int64_t input_num = 2;
|
||||
(void)CheckAndConvertUtils::CheckInteger("input number", SizeToLong(input_args.size()), kEqual, input_num, prim_name);
|
||||
for (const auto &item : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(item);
|
||||
}
|
||||
// infer shape
|
||||
auto out_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[1]->BuildShape())[kShape];
|
||||
// infer type
|
||||
auto x_type = input_args[0]->BuildType()->cast<TensorTypePtr>()->element();
|
||||
return std::make_shared<abstract::AbstractTensor>(x_type, out_shape);
|
||||
}
|
||||
|
||||
REGISTER_PRIMITIVE_C(kNameCrop, Crop);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -40,21 +40,7 @@ bool CumSum::get_reverse() const {
|
|||
auto value_ptr = this->GetAttr(kReverse);
|
||||
return GetValue<bool>(value_ptr);
|
||||
}
|
||||
AbstractBasePtr CumSumInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
const int64_t input_num = 2;
|
||||
(void)CheckAndConvertUtils::CheckInteger("input number", SizeToLong(input_args.size()), kEqual, input_num, prim_name);
|
||||
for (const auto &item : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(item);
|
||||
}
|
||||
// infer shape
|
||||
auto out_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
// infer type
|
||||
auto x_type = input_args[0]->BuildType()->cast<TensorTypePtr>()->element();
|
||||
return std::make_shared<abstract::AbstractTensor>(x_type, out_shape);
|
||||
}
|
||||
|
||||
REGISTER_PRIMITIVE_C(kNameCumSum, CumSum);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -21,29 +21,6 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
AbstractBasePtr CustomExtractFeaturesInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
MS_EXCEPTION_IF_NULL(input_args[0]);
|
||||
// Infer type
|
||||
auto output0_type = kInt32;
|
||||
auto output1_type = kFloat32;
|
||||
|
||||
// Infer shape
|
||||
std::vector<int64_t> out_shape;
|
||||
auto input_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
auto string_num = input_shape[0];
|
||||
if (string_num == 0) {
|
||||
out_shape.push_back(1);
|
||||
} else {
|
||||
out_shape.push_back(string_num);
|
||||
}
|
||||
|
||||
auto output0 = std::make_shared<abstract::AbstractTensor>(output0_type, out_shape);
|
||||
auto output1 = std::make_shared<abstract::AbstractTensor>(output1_type, out_shape);
|
||||
AbstractBasePtrList output = {output0, output1};
|
||||
return std::make_shared<abstract::AbstractTuple>(output);
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameCustomExtractFeatures, CustomExtractFeatures);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -20,41 +20,6 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
namespace {
|
||||
abstract::ShapePtr CustomNormalizeInferShape(const std::vector<AbstractBasePtr> &input_args) {
|
||||
auto base_value = input_args[0]->BuildValue();
|
||||
MS_EXCEPTION_IF_NULL(base_value);
|
||||
auto tensor_value = base_value->cast<tensor::TensorPtr>();
|
||||
MS_EXCEPTION_IF_NULL(tensor_value);
|
||||
MS_EXCEPTION_IF_NULL(tensor_value->data_c());
|
||||
std::vector<int64_t> infer_shape;
|
||||
auto string_num = reinterpret_cast<int64_t *>(tensor_value->data_c());
|
||||
if (*string_num == 0) {
|
||||
infer_shape.push_back(1);
|
||||
} else {
|
||||
infer_shape.push_back(*string_num);
|
||||
}
|
||||
return std::make_shared<abstract::Shape>(infer_shape);
|
||||
}
|
||||
|
||||
TypePtr CustomNormalizeInferType(const std::vector<AbstractBasePtr> &input_args) {
|
||||
auto infer_type = input_args[0]->BuildType();
|
||||
auto tensor_type = infer_type->cast<TensorTypePtr>();
|
||||
MS_EXCEPTION_IF_NULL(tensor_type);
|
||||
auto data_type = tensor_type->element();
|
||||
MS_EXCEPTION_IF_NULL(data_type);
|
||||
return data_type;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
AbstractBasePtr CustomNormalizeInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
const int64_t input_num = 1;
|
||||
CheckAndConvertUtils::CheckInputArgs(input_args, kGreaterEqual, input_num, primitive->name());
|
||||
return std::make_shared<abstract::AbstractTensor>(CustomNormalizeInferType(input_args),
|
||||
CustomNormalizeInferShape(input_args));
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameCustomNormalize, CustomNormalize);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -42,20 +42,6 @@ float CustomPredict::get_weight_threshold() const {
|
|||
return GetValue<float>(value_ptr);
|
||||
}
|
||||
|
||||
AbstractBasePtr CustomPredictInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
for (const auto &input : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(input);
|
||||
}
|
||||
std::vector<int64_t> shape;
|
||||
shape.push_back(GetValue<int64_t>(primitive->GetAttr(kOutputNum)));
|
||||
|
||||
auto output0 = std::make_shared<abstract::AbstractTensor>(kInt32, shape);
|
||||
auto output1 = std::make_shared<abstract::AbstractTensor>(kFloat32, shape);
|
||||
AbstractBasePtrList output = {output0, output1};
|
||||
return std::make_shared<abstract::AbstractTuple>(output);
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameCustomPredict, CustomPredict);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -113,53 +113,7 @@ void DetectionPostProcess::set_format(const Format &format) {
|
|||
(void)this->AddAttr(kFormat, MakeValue(f));
|
||||
}
|
||||
Format DetectionPostProcess::get_format() const { return Format(GetValue<int64_t>(GetAttr(kFormat))); }
|
||||
AbstractBasePtr DetectionPostProcessInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
const int64_t input_num = 3;
|
||||
(void)CheckAndConvertUtils::CheckInteger("input number", SizeToLong(input_args.size()), kEqual, input_num, prim_name);
|
||||
MS_EXCEPTION_IF_NULL(input_args[kInputIndex0]);
|
||||
MS_EXCEPTION_IF_NULL(input_args[kInputIndex1]);
|
||||
MS_EXCEPTION_IF_NULL(input_args[kInputIndex2]);
|
||||
auto boxes = input_args[kInputIndex0];
|
||||
auto scores = input_args[kInputIndex1];
|
||||
auto anchors = input_args[kInputIndex2];
|
||||
auto boxes_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(boxes->BuildShape())[kShape];
|
||||
auto scores_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(scores->BuildShape())[kShape];
|
||||
auto anchors_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(anchors->BuildShape())[kShape];
|
||||
auto format = Format(GetValue<int64_t>(primitive->GetAttr(kFormat)));
|
||||
if (format == NHWC) {
|
||||
boxes_shape = {boxes_shape[0], boxes_shape[3], boxes_shape[1], boxes_shape[2]};
|
||||
scores_shape = {scores_shape[0], scores_shape[3], scores_shape[1], scores_shape[2]};
|
||||
anchors_shape = {anchors_shape[0], anchors_shape[3], anchors_shape[1], anchors_shape[2]};
|
||||
}
|
||||
auto num_classes = GetValue<int64_t>(primitive->GetAttr(kNumClasses));
|
||||
CheckAndConvertUtils::CheckInRange("scores_shape[2]", scores_shape[2], kIncludeBoth, {num_classes, num_classes + 1},
|
||||
prim_name);
|
||||
CheckAndConvertUtils::Check("boxes_shape[1]", boxes_shape[1], kEqual, scores_shape[1], prim_name, ValueError);
|
||||
CheckAndConvertUtils::Check("boxes_shape[1]", boxes_shape[1], kEqual, anchors_shape[0], prim_name, ValueError);
|
||||
|
||||
// Infer shape
|
||||
auto max_detections = GetValue<int64_t>(primitive->GetAttr(kMaxDetections));
|
||||
auto max_classes_per_detection = GetValue<int64_t>(primitive->GetAttr(kMaxClassesPerDetection));
|
||||
auto num_detected_boxes = max_detections * max_classes_per_detection;
|
||||
std::vector<int64_t> output_boxes_shape = {1, num_detected_boxes, 4};
|
||||
std::vector<int64_t> output_class_shape = {1, num_detected_boxes};
|
||||
std::vector<int64_t> output_num_shape = {1};
|
||||
|
||||
// Infer type
|
||||
auto output_type = kFloat32;
|
||||
|
||||
auto output0 = std::make_shared<abstract::AbstractTensor>(output_type, output_boxes_shape);
|
||||
auto output1 = std::make_shared<abstract::AbstractTensor>(output_type, output_class_shape);
|
||||
auto output2 = std::make_shared<abstract::AbstractTensor>(output_type, output_num_shape);
|
||||
AbstractBasePtrList output = {output0, output1, output1, output2};
|
||||
if (format == NHWC) {
|
||||
output = {output0, output1, output2, output1};
|
||||
}
|
||||
return std::make_shared<abstract::AbstractTuple>(output);
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameDetectionPostProcess, DetectionPostProcess);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -25,29 +25,6 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
namespace {
|
||||
abstract::ShapePtr DivInferShape(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
return BroadCastInferShape(prim_name, input_args);
|
||||
}
|
||||
|
||||
TypePtr DivInferType(const PrimitivePtr &prim, const std::vector<AbstractBasePtr> &input_args) {
|
||||
for (const auto &item : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(item);
|
||||
}
|
||||
std::map<std::string, TypePtr> types;
|
||||
(void)types.emplace("x", input_args[0]->BuildType());
|
||||
(void)types.emplace("y", input_args[1]->BuildType());
|
||||
return CheckAndConvertUtils::CheckTensorTypeSame(types, common_valid_types, prim->name());
|
||||
}
|
||||
} // namespace
|
||||
|
||||
AbstractBasePtr DivInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
return std::make_shared<abstract::AbstractTensor>(DivInferType(primitive, input_args),
|
||||
DivInferShape(primitive, input_args)->shape());
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameDiv, Div);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -36,26 +36,6 @@ float Dropout::get_keep_prob() const {
|
|||
return GetValue<float>(value_ptr);
|
||||
}
|
||||
|
||||
AbstractBasePtr DropoutInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
(void)CheckAndConvertUtils::CheckInteger("dropout_infer", SizeToLong(input_args.size()), kEqual, 1, prim_name);
|
||||
|
||||
// Infer shape
|
||||
auto x_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
(void)CheckAndConvertUtils::CheckInteger("x_shape", SizeToLong(x_shape.size()), kGreaterEqual, 1, prim_name);
|
||||
std::vector<int64_t> out_shape;
|
||||
(void)out_shape.insert(out_shape.end(), x_shape.begin(), x_shape.end());
|
||||
(void)out_shape.insert(out_shape.end(), x_shape.begin(), x_shape.end());
|
||||
auto infer_shape = std::make_shared<abstract::Shape>(out_shape);
|
||||
|
||||
// Infer type
|
||||
const std::set<TypePtr> valid_types = {kFloat16, kFloat32};
|
||||
auto infer_type =
|
||||
CheckAndConvertUtils::CheckTensorTypeValid("x_dtype", input_args[0]->BuildType(), valid_types, prim_name);
|
||||
return std::make_shared<abstract::AbstractTensor>(infer_type, infer_shape->shape());
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameDropout, Dropout);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -29,23 +29,7 @@ void DynamicQuant::Init(const bool symmetric, const int64_t dst_type) {
|
|||
this->set_symmetric(symmetric);
|
||||
this->set_dst_type(dst_type);
|
||||
}
|
||||
AbstractBasePtr DynamicQuantInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
const int64_t input_num = 1;
|
||||
const size_t x_index = 0;
|
||||
CheckAndConvertUtils::CheckInputArgs(input_args, kGreaterEqual, input_num, primitive->name());
|
||||
auto input_type = CheckAndConvertUtils::GetTensorInputType(primitive->name(), input_args, x_index);
|
||||
auto dst_type = TypeIdToType(TypeId(GetValue<int64_t>(primitive->GetAttr(kDstType))));
|
||||
MS_EXCEPTION_IF_NULL(dst_type);
|
||||
if (input_type->type_id() != kNumberTypeFloat16 && input_type->type_id() != kNumberTypeFloat32) {
|
||||
MS_EXCEPTION(TypeError) << "For '" << primitive->name()
|
||||
<< "', Input type should be kNumberTypeFloat16 or kNumberTypeFloat32"
|
||||
<< ", but " << input_type->ToString();
|
||||
}
|
||||
auto input_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
return std::make_shared<abstract::AbstractTensor>(dst_type, input_shape);
|
||||
}
|
||||
|
||||
REGISTER_PRIMITIVE_C(kNameDynamicQuant, DynamicQuant);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -33,50 +33,6 @@ bool EmbeddingLookup::get_setattr_flag() const {
|
|||
return GetValue<bool>(value_ptr);
|
||||
}
|
||||
|
||||
AbstractBasePtr EmbeddingLookupInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
const int64_t input_num = 3;
|
||||
(void)CheckAndConvertUtils::CheckInteger("input number", SizeToLong(input_args.size()), kEqual, input_num, prim_name);
|
||||
for (const auto &item : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(item);
|
||||
}
|
||||
auto params = input_args[0]->cast<abstract::AbstractTensorPtr>();
|
||||
MS_EXCEPTION_IF_NULL(params);
|
||||
auto indices = input_args[1]->cast<abstract::AbstractTensorPtr>();
|
||||
MS_EXCEPTION_IF_NULL(indices);
|
||||
const std::set<TypePtr> int_valid_types = {kInt8, kInt16, kInt32, kInt64};
|
||||
(void)CheckAndConvertUtils::CheckTensorTypeValid("indices type", indices->BuildType(), int_valid_types, prim_name);
|
||||
(void)CheckAndConvertUtils::CheckTensorTypeValid("offset", input_args[kInputIndex2]->BuildType(), int_valid_types,
|
||||
prim_name);
|
||||
MS_EXCEPTION_IF_NULL(params->shape());
|
||||
auto params_shp = params->shape()->shape();
|
||||
MS_EXCEPTION_IF_NULL(indices->shape());
|
||||
auto indices_shp = indices->shape()->shape();
|
||||
ShapeVector shape;
|
||||
(void)shape.insert(shape.end(), indices_shp.begin(), indices_shp.end());
|
||||
(void)shape.insert(shape.end(), params_shp.begin() + 1, params_shp.end());
|
||||
auto indices_max_shape = indices->shape()->max_shape();
|
||||
ShapeVector max_shape;
|
||||
if (!indices_max_shape.empty()) {
|
||||
(void)max_shape.insert(max_shape.end(), indices_max_shape.begin(), indices_max_shape.end());
|
||||
(void)max_shape.insert(max_shape.end(), params_shp.begin() + 1, params_shp.end());
|
||||
} else {
|
||||
max_shape = shape;
|
||||
}
|
||||
auto indices_min_shape = indices->shape()->min_shape();
|
||||
ShapeVector min_shape;
|
||||
if (!indices_min_shape.empty()) {
|
||||
(void)min_shape.insert(min_shape.end(), indices_min_shape.begin(), indices_min_shape.end());
|
||||
(void)min_shape.insert(min_shape.end(), params_shp.begin() + 1, params_shp.end());
|
||||
} else {
|
||||
min_shape = shape;
|
||||
}
|
||||
|
||||
return std::make_shared<abstract::AbstractTensor>(params->element(),
|
||||
std::make_shared<abstract::Shape>(shape, min_shape, max_shape));
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameEmbeddingLookup, EmbeddingLookup);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -27,31 +27,6 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
AbstractBasePtr ExpandDimsInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
const int64_t input_num = 2;
|
||||
(void)CheckAndConvertUtils::CheckInteger("input number", SizeToLong(input_args.size()), kEqual, input_num, prim_name);
|
||||
for (const auto &item : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(item);
|
||||
}
|
||||
// Infer shape
|
||||
auto x_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
auto dim_val = GetValue<int64_t>(input_args[1]->BuildValue());
|
||||
auto rank = x_shape.size();
|
||||
(void)CheckAndConvertUtils::CheckInRange<int64_t>("axis", dim_val, kIncludeBoth, {-rank - 1, rank}, prim_name);
|
||||
if (dim_val < 0) {
|
||||
dim_val += SizeToLong(x_shape.size()) + 1;
|
||||
}
|
||||
auto out_shape = x_shape;
|
||||
(void)out_shape.insert(out_shape.begin() + dim_val, 1, 1);
|
||||
|
||||
// Infer type
|
||||
const size_t x_index = 0;
|
||||
auto x_type = CheckAndConvertUtils::GetTensorInputType(prim_name, input_args, x_index);
|
||||
return std::make_shared<abstract::AbstractTensor>(x_type, out_shape);
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameExpandDims, ExpandDims);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -25,41 +25,6 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
namespace {
|
||||
abstract::ShapePtr FakeQuantWithMinMaxVarsInferShape(const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
auto in_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[kInputIndex0]->BuildShape())[kShape];
|
||||
auto min_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[kInputIndex1]->BuildShape())[kShape];
|
||||
auto max_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[kInputIndex2]->BuildShape())[kShape];
|
||||
(void)CheckAndConvertUtils::CheckInteger("x_rank", SizeToLong(in_shape.size()), kGreaterEqual, 1, prim_name);
|
||||
CheckAndConvertUtils::Check("min_shape", min_shape, kEqual, max_shape, prim_name);
|
||||
(void)CheckAndConvertUtils::CheckInteger("min_shape", SizeToLong(min_shape.size()), kEqual, 1, prim_name);
|
||||
int64_t shape_val = 1;
|
||||
for (size_t i = 0; i < in_shape.size(); i++) {
|
||||
shape_val = shape_val * in_shape[i];
|
||||
if (min_shape[0] > 1 && min_shape[0] != shape_val) {
|
||||
MS_EXCEPTION(ValueError) << "For '" << prim_name
|
||||
<< "', the shape of \'min\' cannot broadcast to the shape of \'x\'";
|
||||
}
|
||||
}
|
||||
return std::make_shared<abstract::Shape>(in_shape);
|
||||
}
|
||||
|
||||
TypePtr FakeQuantWithMinMaxVarsInferType(const PrimitivePtr &prim, const std::vector<AbstractBasePtr> &input_args) {
|
||||
const std::set<TypePtr> valid_types = {kFloat16, kFloat32};
|
||||
if (std::any_of(input_args.begin(), input_args.end(), [](const AbstractBasePtr arg) { return arg == nullptr; })) {
|
||||
MS_LOG(EXCEPTION) << "For '" << prim->name()
|
||||
<< "', the input args userd for infer shape and type, can not be a nullptr.";
|
||||
}
|
||||
std::map<std::string, TypePtr> types;
|
||||
(void)types.emplace("x", input_args[kInputIndex0]->BuildType());
|
||||
(void)types.emplace("min", input_args[kInputIndex1]->BuildType());
|
||||
(void)types.emplace("max", input_args[kInputIndex2]->BuildType());
|
||||
return CheckAndConvertUtils::CheckTensorTypeSame(types, valid_types, prim->name());
|
||||
}
|
||||
} // namespace
|
||||
void FakeQuantWithMinMaxVars::Init(const bool narrow_range, const int64_t num_bits) {
|
||||
this->set_narrow_range(narrow_range);
|
||||
this->set_num_bits(num_bits);
|
||||
|
|
@ -82,11 +47,7 @@ int64_t FakeQuantWithMinMaxVars::get_num_bits() const {
|
|||
auto value_ptr = this->GetAttr(kNumBits);
|
||||
return GetValue<int64_t>(value_ptr);
|
||||
}
|
||||
AbstractBasePtr FakeQuantWithMinMaxVarsInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
return std::make_shared<abstract::AbstractTensor>(FakeQuantWithMinMaxVarsInferType(primitive, input_args),
|
||||
FakeQuantWithMinMaxVarsInferShape(primitive, input_args)->shape());
|
||||
}
|
||||
|
||||
REGISTER_PRIMITIVE_C(kNameFakeQuantWithMinMaxVars, FakeQuantWithMinMaxVars);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -39,33 +39,6 @@ bool FakeQuantWithMinMaxVarsPerChannel::get_narrow_range() const {
|
|||
return GetValue<bool>(value_ptr);
|
||||
}
|
||||
|
||||
AbstractBasePtr FakeQuantWithMinMaxVarsPerChannelInfer(const abstract::AnalysisEnginePtr &,
|
||||
const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto op_name = primitive->name();
|
||||
auto x_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[kInputIndex0]->BuildShape())[kShape];
|
||||
auto min_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[kInputIndex1]->BuildShape())[kShape];
|
||||
auto max_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[kInputIndex2]->BuildShape())[kShape];
|
||||
(void)CheckAndConvertUtils::CheckInteger("x rank", (int64_t)x_shape.size(), kGreaterThan, 1, op_name);
|
||||
CheckAndConvertUtils::Check("min shape", min_shape, kEqual, max_shape, op_name);
|
||||
(void)CheckAndConvertUtils::CheckInteger("min shape", (int64_t)min_shape.size(), kEqual, 1, op_name);
|
||||
CheckAndConvertUtils::Check("min shape", min_shape[0], kEqual, x_shape[x_shape.size() - 1], op_name);
|
||||
|
||||
auto x_type = input_args[kInputIndex0]->BuildType();
|
||||
auto min_type = input_args[kInputIndex1]->BuildType();
|
||||
auto max_type = input_args[kInputIndex2]->BuildType();
|
||||
std::vector<std::string> type_name = {"x", "min", "max"};
|
||||
std::vector<TypePtr> type = {x_type, min_type, max_type};
|
||||
for (size_t i = 0; i < 3; i++) {
|
||||
(void)CheckAndConvertUtils::CheckTensorTypeValid(type_name[i], type[i], {kFloat16, kFloat32}, op_name);
|
||||
}
|
||||
auto tensor_type = x_type->cast<TensorTypePtr>();
|
||||
MS_EXCEPTION_IF_NULL(tensor_type);
|
||||
auto data_type = tensor_type->element();
|
||||
MS_EXCEPTION_IF_NULL(data_type);
|
||||
return std::make_shared<abstract::AbstractTensor>(data_type, x_shape);
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameFakeQuantWithMinMaxVarsPerChannel, FakeQuantWithMinMaxVarsPerChannel);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -21,23 +21,6 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
namespace {
|
||||
abstract::ShapePtr FftImagInferShape(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
|
||||
auto name = primitive->name();
|
||||
MS_LOG(DEBUG) << "Infer shape for " << name;
|
||||
auto in_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
in_shape.pop_back();
|
||||
return std::make_shared<abstract::Shape>(in_shape);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
AbstractBasePtr FftImagInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
const int64_t input_num = 1;
|
||||
CheckAndConvertUtils::CheckInputArgs(input_args, kEqual, input_num, primitive->name());
|
||||
return std::make_shared<abstract::AbstractTensor>(kFloat32, FftImagInferShape(primitive, input_args));
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameFftImag, FftImag);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -24,19 +24,6 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
AbstractBasePtr FftRealInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
(void)CheckAndConvertUtils::CheckInteger("input number", SizeToLong(input_args.size()), kEqual, 1, prim_name);
|
||||
for (const auto &item : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(item);
|
||||
}
|
||||
auto out_dtype = kFloat32;
|
||||
auto out_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
out_shape.pop_back();
|
||||
return std::make_shared<abstract::AbstractTensor>(out_dtype, std::make_shared<abstract::Shape>(out_shape));
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameFftReal, FftReal);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -22,46 +22,6 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
AbstractBasePtr FillInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
const int64_t input_num = 3;
|
||||
(void)CheckAndConvertUtils::CheckInteger("input number", SizeToLong(input_args.size()), kEqual, input_num, prim_name);
|
||||
for (const auto &item : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(item);
|
||||
}
|
||||
auto input_dtype = input_args[kInputIndex0]->cast<abstract::AbstractTypePtr>();
|
||||
MS_EXCEPTION_IF_NULL(input_dtype);
|
||||
auto dtype_value = input_dtype->BuildValue();
|
||||
MS_EXCEPTION_IF_NULL(dtype_value);
|
||||
auto dtype = dtype_value->cast<TypePtr>();
|
||||
MS_EXCEPTION_IF_NULL(dtype);
|
||||
auto valid_types = common_valid_types;
|
||||
valid_types.insert(kBool);
|
||||
(void)CheckAndConvertUtils::CheckTypeValid("output datatype", dtype, valid_types, prim_name);
|
||||
auto out_shape = GetValue<std::vector<int64_t>>(input_args[kInputIndex1]->BuildValue());
|
||||
auto x_type = input_args[kInputIndex2]->BuildType();
|
||||
auto x_type_id = x_type->type_id();
|
||||
auto x_value = input_args[kInputIndex2]->BuildValue();
|
||||
auto abs = std::make_shared<abstract::AbstractTensor>(dtype, std::make_shared<abstract::Shape>(out_shape));
|
||||
tensor::TensorPtr tensor = std::make_shared<tensor::Tensor>(x_type_id, out_shape);
|
||||
MS_EXCEPTION_IF_NULL(tensor);
|
||||
auto mem_size = IntToSize(tensor->ElementsNum());
|
||||
if (x_type_id == kNumberTypeInt) {
|
||||
auto int_value = GetValue<int>(x_value);
|
||||
SetTensorData(tensor->data_c(), int_value, mem_size);
|
||||
} else if (x_type_id == kNumberTypeFloat || x_type_id == kNumberTypeFloat32) {
|
||||
auto float_value = GetValue<float>(x_value);
|
||||
SetTensorData(tensor->data_c(), float_value, mem_size);
|
||||
} else {
|
||||
MS_LOG(ERROR) << "For '" << prim_name
|
||||
<< "', value's supported constant type is ['int', 'float', 'float64'], but got "
|
||||
<< input_args[kInputIndex2]->ToString() << ".";
|
||||
}
|
||||
abs->set_value(tensor);
|
||||
return abs;
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameFill, Fill);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -35,30 +35,6 @@ ActivationType AddFusion::get_activation_type() const {
|
|||
}
|
||||
void AddFusion::Init(const ActivationType activation_type) { this->set_activation_type(activation_type); }
|
||||
|
||||
namespace {
|
||||
abstract::ShapePtr AddFusionInferShape(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto op_name = primitive->name();
|
||||
return BroadCastInferShape(op_name, input_args);
|
||||
}
|
||||
|
||||
TypePtr AddFusionInferType(const PrimitivePtr &prim, const std::vector<AbstractBasePtr> &input_args) {
|
||||
if (std::any_of(input_args.begin(), input_args.end(), [](const AbstractBasePtr &a) { return a == nullptr; })) {
|
||||
MS_LOG(EXCEPTION) << "For '" << prim->name()
|
||||
<< ", the input args userd for infer shape and type, can not be a nullptr.";
|
||||
}
|
||||
std::map<std::string, TypePtr> types;
|
||||
(void)types.emplace("x", input_args[kInputIndex0]->BuildType());
|
||||
(void)types.emplace("y", input_args[kInputIndex1]->BuildType());
|
||||
return CheckAndConvertUtils::CheckTensorTypeSame(types, common_valid_types, prim->name());
|
||||
}
|
||||
} // namespace
|
||||
|
||||
AbstractBasePtr AddFusionInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
return std::make_shared<abstract::AbstractTensor>(AddFusionInferType(primitive, input_args),
|
||||
AddFusionInferShape(primitive, input_args));
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameAddFusion, AddFusion);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -50,66 +50,6 @@ ActivationType AvgPoolFusion::get_activation_type() const {
|
|||
return ActivationType(GetValue<int64_t>(value_ptr));
|
||||
}
|
||||
|
||||
namespace {
|
||||
abstract::ShapePtr AvgPoolFusionInferShape(const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
for (auto item : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(item);
|
||||
}
|
||||
auto op_name = primitive->name();
|
||||
auto in_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->GetShapeTrack())[kShape];
|
||||
auto format = Format(GetValue<int64_t>(primitive->GetAttr(kFormat)));
|
||||
if (format == NHWC) {
|
||||
in_shape = {in_shape[0], in_shape[3], in_shape[1], in_shape[2]};
|
||||
}
|
||||
const int64_t x_rank = 4;
|
||||
(void)CheckAndConvertUtils::CheckInteger("x_rank", SizeToLong(in_shape.size()), kEqual, x_rank, op_name);
|
||||
auto kernel_size = GetValue<std::vector<int64_t>>(primitive->GetAttr(kKernelSize));
|
||||
auto pad_mode = PadMode(GetValue<int64_t>(primitive->GetAttr(kPadMode)));
|
||||
auto batch = in_shape[0];
|
||||
auto channel = in_shape[1];
|
||||
auto in_h = in_shape[2];
|
||||
auto in_w = in_shape[3];
|
||||
|
||||
auto strides = GetValue<std::vector<int64_t>>(primitive->GetAttr(kStrides));
|
||||
(void)CheckAndConvertUtils::CheckPositiveVector(kStride, strides, op_name);
|
||||
auto kernel_h = kernel_size[2];
|
||||
auto kernel_w = kernel_size[3];
|
||||
auto stride_h = strides[2];
|
||||
auto stride_w = strides[3];
|
||||
int64_t out_h = abstract::Shape::SHP_ANY;
|
||||
int64_t out_w = abstract::Shape::SHP_ANY;
|
||||
if (pad_mode == VALID) {
|
||||
out_h = static_cast<int64_t>(ceil((in_h - (kernel_h - 1)) / static_cast<float>(stride_h)));
|
||||
out_w = static_cast<int64_t>(ceil((in_w - (kernel_w - 1)) / static_cast<float>(stride_w)));
|
||||
} else if (pad_mode == SAME) {
|
||||
out_h = static_cast<int64_t>(ceil(in_h / static_cast<float>(stride_h)));
|
||||
out_w = static_cast<int64_t>(ceil(in_w / static_cast<float>(stride_w)));
|
||||
}
|
||||
std::vector<int64_t> out_shape = {batch, channel, out_h, out_w};
|
||||
if (format == NHWC) {
|
||||
out_shape = {batch, out_h, out_w, channel};
|
||||
}
|
||||
if (std::any_of(out_shape.begin(), out_shape.end(), [](int64_t a) { return a <= 0; })) {
|
||||
MS_LOG(EXCEPTION) << "For '" << op_name << "', Kernel size should be positive, but got not valid.";
|
||||
}
|
||||
return std::make_shared<abstract::Shape>(out_shape);
|
||||
}
|
||||
|
||||
TypePtr AvgPoolFusionInferType(const std::vector<AbstractBasePtr> &input_args) {
|
||||
for (auto item : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(item);
|
||||
}
|
||||
return input_args[0]->BuildType();
|
||||
}
|
||||
} // namespace
|
||||
|
||||
AbstractBasePtr AvgPoolFusionInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
return std::make_shared<abstract::AbstractTensor>(AvgPoolFusionInferType(input_args),
|
||||
AvgPoolFusionInferShape(primitive, input_args));
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameAvgPoolFusion, AvgPoolFusion);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -58,65 +58,7 @@ void FullConnection::Init(const bool has_bias, const int64_t axis, const bool us
|
|||
this->set_use_axis(use_axis);
|
||||
this->set_activation_type(activation_type);
|
||||
}
|
||||
AbstractBasePtr FullConnectionInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
MS_EXCEPTION_IF_NULL(input_args[kInputIndex0]);
|
||||
MS_EXCEPTION_IF_NULL(input_args[kInputIndex1]);
|
||||
auto input0 = input_args[0];
|
||||
auto input1 = input_args[1];
|
||||
auto input0_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input0->BuildShape())[kShape];
|
||||
auto input1_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input1->BuildShape())[kShape];
|
||||
auto prim_axis = GetValue<int64_t>(primitive->GetAttr(kAxis));
|
||||
auto has_bias = GetValue<bool>(primitive->GetAttr(kHasBias));
|
||||
const int64_t input_num_bias = 3;
|
||||
const int64_t input_num = 2;
|
||||
if (has_bias) {
|
||||
(void)CheckAndConvertUtils::CheckInteger("input_args.size()", SizeToLong(input_args.size()), kEqual, input_num_bias,
|
||||
prim_name);
|
||||
} else {
|
||||
(void)CheckAndConvertUtils::CheckInteger("input_args.size()", SizeToLong(input_args.size()), kEqual, input_num,
|
||||
prim_name);
|
||||
}
|
||||
auto use_axis = GetValue<bool>(primitive->GetAttr(kUseAxis));
|
||||
if (use_axis && (prim_axis < 1 || prim_axis > (int64_t)input0_shape.size())) {
|
||||
MS_EXCEPTION(ValueError) << "Full Connection axis is invalid";
|
||||
}
|
||||
int64_t new_k = 1;
|
||||
if (use_axis) {
|
||||
for (size_t t = LongToSize(prim_axis); t < input0_shape.size(); t++) {
|
||||
new_k *= input0_shape[t];
|
||||
}
|
||||
if (new_k != input1_shape[1]) {
|
||||
MS_EXCEPTION(ValueError) << "Input1 size is invalid";
|
||||
}
|
||||
} else {
|
||||
new_k = input1_shape[1];
|
||||
}
|
||||
if (has_bias) {
|
||||
auto input2_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[kInputIndex2]->BuildShape())[kShape];
|
||||
if (input2_shape[0] != input1_shape[0]) {
|
||||
MS_EXCEPTION(ValueError) << "Bias size is invalid";
|
||||
}
|
||||
}
|
||||
std::vector<int64_t> out_shape = {(int64_t)input0_shape.size()};
|
||||
if (use_axis) {
|
||||
out_shape.resize(LongToSize(prim_axis) + 1);
|
||||
out_shape[LongToSize(prim_axis)] = input1_shape[0];
|
||||
} else {
|
||||
int64_t total = 1;
|
||||
for (size_t i = 0; i < input0_shape.size(); i++) {
|
||||
total *= input0_shape[i];
|
||||
}
|
||||
out_shape.resize(2);
|
||||
auto batch_size = total / new_k;
|
||||
out_shape[0] = batch_size;
|
||||
out_shape[1] = input1_shape[0];
|
||||
}
|
||||
auto input0_type = input_args[0]->BuildType()->cast<TensorTypePtr>()->element();
|
||||
return std::make_shared<abstract::AbstractTensor>(input0_type, out_shape);
|
||||
}
|
||||
|
||||
REGISTER_PRIMITIVE_C(kNameFullConnection, FullConnection);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -50,62 +50,6 @@ ActivationType MaxPoolFusion::get_activation_type() const {
|
|||
return ActivationType(GetValue<int64_t>(value_ptr));
|
||||
}
|
||||
|
||||
namespace {
|
||||
abstract::ShapePtr MaxPoolFusionInferShape(const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
MS_EXCEPTION_IF_NULL(input_args[0]);
|
||||
auto op_name = primitive->name();
|
||||
auto in_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->GetShapeTrack())[kShape];
|
||||
auto format = Format(GetValue<int64_t>(primitive->GetAttr(kFormat)));
|
||||
if (format == NHWC) {
|
||||
in_shape = {in_shape[0], in_shape[3], in_shape[1], in_shape[2]};
|
||||
}
|
||||
const int64_t in_shape_size = 4;
|
||||
(void)CheckAndConvertUtils::CheckInteger("x_rank", SizeToLong(in_shape.size()), kEqual, in_shape_size, op_name);
|
||||
auto kernel_size = GetValue<std::vector<int64_t>>(primitive->GetAttr(kKernelSize));
|
||||
auto pad_mode = PadMode(GetValue<int64_t>(primitive->GetAttr(kPadMode)));
|
||||
auto batch = in_shape[0];
|
||||
auto channel = in_shape[1];
|
||||
auto in_h = in_shape[2];
|
||||
auto in_w = in_shape[3];
|
||||
|
||||
auto strides = GetValue<std::vector<int64_t>>(primitive->GetAttr(kStrides));
|
||||
auto kernel_h = kernel_size[2];
|
||||
auto kernel_w = kernel_size[3];
|
||||
auto stride_h = strides[2];
|
||||
auto stride_w = strides[3];
|
||||
int64_t out_h = abstract::Shape::SHP_ANY;
|
||||
int64_t out_w = abstract::Shape::SHP_ANY;
|
||||
if (pad_mode == VALID) {
|
||||
out_h = static_cast<int64_t>(ceil((in_h - (kernel_h - 1)) / static_cast<float>(stride_h)));
|
||||
out_w = static_cast<int64_t>(ceil((in_w - (kernel_w - 1)) / static_cast<float>(stride_w)));
|
||||
} else if (pad_mode == SAME) {
|
||||
out_h = static_cast<int64_t>(ceil(in_h / static_cast<float>(stride_h)));
|
||||
out_w = static_cast<int64_t>(ceil(in_w / static_cast<float>(stride_w)));
|
||||
}
|
||||
std::vector<int64_t> out_shape = {batch, channel, out_h, out_w};
|
||||
if (format == NHWC) {
|
||||
out_shape = {batch, out_h, out_w, channel};
|
||||
}
|
||||
if (std::any_of(out_shape.begin(), out_shape.end(), [](int64_t a) { return a <= 0; })) {
|
||||
MS_LOG(EXCEPTION) << "For '" << op_name << "', Kernel size must be positive, but got invalid.";
|
||||
}
|
||||
return std::make_shared<abstract::Shape>(out_shape);
|
||||
}
|
||||
|
||||
TypePtr MaxPoolFusionInferType(const std::vector<AbstractBasePtr> &input_args) { return input_args[0]->BuildType(); }
|
||||
} // namespace
|
||||
|
||||
AbstractBasePtr MaxPoolFusionInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
for (auto item : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(item);
|
||||
}
|
||||
return std::make_shared<abstract::AbstractTensor>(MaxPoolFusionInferType(input_args),
|
||||
MaxPoolFusionInferShape(primitive, input_args));
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameMaxPoolFusion, MaxPoolFusion);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -34,34 +34,6 @@ void PowFusion::set_shift(const float &shift) { (void)this->AddAttr(kShift, Make
|
|||
float PowFusion::get_scale() const { return GetValue<float>(GetAttr(kScale)); }
|
||||
float PowFusion::get_shift() const { return GetValue<float>(GetAttr(kShift)); }
|
||||
|
||||
namespace {
|
||||
abstract::ShapePtr PowFusionInferShape(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto op_name = primitive->name();
|
||||
return BroadCastInferShape(op_name, input_args);
|
||||
}
|
||||
|
||||
TypePtr PowFusionInferType(const PrimitivePtr &prim, const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(prim);
|
||||
for (const auto &item : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(item);
|
||||
}
|
||||
std::map<std::string, TypePtr> types;
|
||||
(void)types.emplace("x", input_args[0]->BuildType());
|
||||
(void)types.emplace("y", input_args[1]->BuildType());
|
||||
return CheckAndConvertUtils::CheckTensorTypeSame(types, common_valid_types, prim->name());
|
||||
}
|
||||
} // namespace
|
||||
|
||||
AbstractBasePtr PowFusionInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
const int64_t input_num = 2;
|
||||
(void)CheckAndConvertUtils::CheckInteger("PowFusion infer", SizeToLong(input_args.size()), kGreaterEqual, input_num,
|
||||
primitive->name());
|
||||
return std::make_shared<abstract::AbstractTensor>(PowFusionInferType(primitive, input_args),
|
||||
PowFusionInferShape(primitive, input_args));
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNamePowFusion, PowFusion);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -30,40 +30,6 @@ std::vector<int64_t> SliceFusion::get_axes() const {
|
|||
return GetValue<std::vector<int64_t>>(value_ptr);
|
||||
}
|
||||
|
||||
AbstractBasePtr SliceFusionInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto op_name = primitive->name();
|
||||
auto x_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
auto x_shape_len = x_shape.size();
|
||||
auto begin_v = input_args[kInputIndex1]->BuildValue();
|
||||
auto size_v = input_args[kInputIndex2]->BuildValue();
|
||||
auto x_type = input_args[kInputIndex0]->BuildType();
|
||||
MS_EXCEPTION_IF_NULL(x_type);
|
||||
MS_EXCEPTION_IF_NULL(begin_v);
|
||||
MS_EXCEPTION_IF_NULL(size_v);
|
||||
auto tensor_type = x_type->cast<TensorTypePtr>();
|
||||
MS_EXCEPTION_IF_NULL(tensor_type);
|
||||
auto data_type = tensor_type->element();
|
||||
MS_EXCEPTION_IF_NULL(data_type);
|
||||
if (begin_v == kAnyValue || size_v == kAnyValue) {
|
||||
return std::make_shared<abstract::AbstractTensor>(data_type, std::vector<int64_t>{});
|
||||
}
|
||||
auto begin = GetValue<std::vector<int64_t>>(begin_v);
|
||||
auto size = GetValue<std::vector<int64_t>>(size_v);
|
||||
CheckAndConvertUtils::Check("len of begin", (int64_t)begin.size(), kEqual, SizeToLong(x_shape_len));
|
||||
CheckAndConvertUtils::Check("len of size", (int64_t)size.size(), kEqual, SizeToLong(x_shape_len));
|
||||
|
||||
for (size_t i = 0; i < x_shape_len; i++) {
|
||||
(void)CheckAndConvertUtils::CheckInteger("input size[" + std::to_string(i) + "]", size[i], kGreaterThan, 0, "");
|
||||
if (x_shape[i] < (begin[i] + size[i])) {
|
||||
auto y = begin[i] + size[i];
|
||||
MS_EXCEPTION(ValueError) << "For " + op_name + "slice shape can't bigger than origin shape " +
|
||||
std::to_string(x_shape[i]) + "," + std::to_string(y);
|
||||
}
|
||||
}
|
||||
return std::make_shared<abstract::AbstractTensor>(data_type, size);
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameSliceFusion, SliceFusion);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -19,16 +19,6 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
AbstractBasePtr AvgPoolGradInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
MS_EXCEPTION_IF_NULL(input_args[0]->BuildValue());
|
||||
auto origin_input_shape = GetValue<std::vector<int64_t>>(input_args[0]->BuildValue());
|
||||
auto tensor_type = input_args[1]->BuildType()->cast<TensorTypePtr>();
|
||||
MS_EXCEPTION_IF_NULL(tensor_type);
|
||||
auto element = tensor_type->element();
|
||||
return std::make_shared<abstract::AbstractTensor>(element, origin_input_shape);
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameAvgPoolGrad, AvgPoolGrad);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -22,35 +22,6 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
namespace {
|
||||
abstract::ShapePtr BinaryCrossEntroyGradInferShape(const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
auto x_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[kInputIndex0]->BuildShape())[kShape];
|
||||
auto y_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[kInputIndex1]->BuildShape())[kShape];
|
||||
auto weight_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[kInputIndex2]->BuildShape())[kShape];
|
||||
CheckAndConvertUtils::Check("x shape", x_shape, kEqual, y_shape, prim_name);
|
||||
if (weight_shape.size() < 1) {
|
||||
CheckAndConvertUtils::Check("y shape", y_shape, kEqual, weight_shape, prim_name);
|
||||
}
|
||||
return std::make_shared<abstract::Shape>(x_shape);
|
||||
}
|
||||
|
||||
TypePtr BinaryCrossEntroyGradInferType(const PrimitivePtr &prim, const std::vector<AbstractBasePtr> &input_args) {
|
||||
const std::set<TypePtr> valid_types = {kFloat16, kFloat32};
|
||||
std::map<std::string, TypePtr> types;
|
||||
(void)types.emplace("x_shape", input_args[kInputIndex0]->BuildType());
|
||||
(void)types.emplace("y_shape", input_args[kInputIndex1]->BuildType());
|
||||
auto infer_type = CheckAndConvertUtils::CheckTensorTypeSame(types, valid_types, prim->name());
|
||||
if (input_args[kInputIndex3]->BuildType() != nullptr) {
|
||||
(void)types.emplace("x_shape", input_args[kInputIndex0]->BuildType());
|
||||
(void)types.emplace("weight_shape", input_args[kInputIndex2]->BuildType());
|
||||
infer_type = CheckAndConvertUtils::CheckTensorTypeSame(types, valid_types, prim->name());
|
||||
}
|
||||
return infer_type;
|
||||
}
|
||||
} // namespace
|
||||
void BinaryCrossEntropyGrad::Init(const Reduction &reduction) { set_reduction(reduction); }
|
||||
|
||||
void BinaryCrossEntropyGrad::set_reduction(const Reduction &reduction) {
|
||||
|
|
@ -62,18 +33,6 @@ Reduction BinaryCrossEntropyGrad::get_reduction() const {
|
|||
return Reduction(GetValue<int64_t>(value_ptr));
|
||||
}
|
||||
|
||||
AbstractBasePtr BinaryCrossEntropyGradInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
for (const auto &item : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(item);
|
||||
}
|
||||
const int64_t input_num = 4;
|
||||
(void)CheckAndConvertUtils::CheckInteger("BinaryCrossEntropyGrad infer", SizeToLong(input_args.size()), kGreaterEqual,
|
||||
input_num, primitive->name());
|
||||
return std::make_shared<abstract::AbstractTensor>(BinaryCrossEntroyGradInferType(primitive, input_args),
|
||||
BinaryCrossEntroyGradInferShape(primitive, input_args));
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameBinaryCrossEntropyGrad, BinaryCrossEntropyGrad);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -30,30 +30,6 @@ std::string EinsumGrad::get_equation() const {
|
|||
auto value_ptr = this->GetAttr(kEquation);
|
||||
return GetValue<std::string>(value_ptr);
|
||||
}
|
||||
AbstractBasePtr EinsumGradInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
for (auto item : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(item);
|
||||
}
|
||||
auto elements = input_args[0]->isa<abstract::AbstractTuple>()
|
||||
? input_args[0]->cast<abstract::AbstractTuplePtr>()->elements()
|
||||
: input_args[0]->cast<abstract::AbstractListPtr>()->elements();
|
||||
AbstractBasePtrList rets;
|
||||
std::vector<std::vector<size_t>> input_shapes;
|
||||
std::vector<size_t> cur_shape;
|
||||
for (size_t idx = 0; idx < elements.size(); ++idx) {
|
||||
auto dx = elements[idx]->Broaden();
|
||||
rets.emplace_back(dx);
|
||||
auto shape = elements[idx]->BuildShape();
|
||||
auto &shape_int = shape->cast<abstract::ShapePtr>()->shape();
|
||||
std::transform(shape_int.begin(), shape_int.end(), std::back_inserter(cur_shape), SizeToLong);
|
||||
input_shapes.emplace_back(cur_shape);
|
||||
cur_shape.clear();
|
||||
}
|
||||
(void)primitive->AddAttr("input_shape_vec", MakeValue<std::vector<std::vector<size_t>>>(input_shapes));
|
||||
return std::make_shared<abstract::AbstractTuple>(rets);
|
||||
}
|
||||
// REGISTER_PRIMITIVE_EVAL_IMPL(EinsumGrad, prim::kPrimEinsumGrad, EinsumGradInfer, nullptr, true);
|
||||
REGISTER_PRIMITIVE_C(kNameEinsumGrad, EinsumGrad);
|
||||
} // namespace ops
|
||||
|
|
|
|||
|
|
@ -18,24 +18,6 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
AbstractBasePtr FlattenGradInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
const int64_t input_num = 2;
|
||||
(void)CheckAndConvertUtils::CheckInteger("input number", SizeToLong(input_args.size()), kEqual, input_num, prim_name);
|
||||
for (const auto &item : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(item);
|
||||
}
|
||||
auto input_x = input_args[0]->cast<abstract::AbstractTensorPtr>();
|
||||
MS_EXCEPTION_IF_NULL(input_x);
|
||||
auto input_shape = input_args[1]->cast<abstract::AbstractTuplePtr>();
|
||||
MS_EXCEPTION_IF_NULL(input_shape);
|
||||
auto out_shape = GetValue<std::vector<int64_t>>(input_shape->BuildValue());
|
||||
auto ret = input_x->Broaden();
|
||||
ret->set_shape(std::make_shared<abstract::Shape>(out_shape));
|
||||
return ret;
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameFlattenGrad, FlattenGrad);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -158,28 +158,6 @@ bool GroupConv2DGradInput::get_has_bias() const {
|
|||
MS_EXCEPTION_IF_NULL(value_ptr);
|
||||
return GetValue<bool>(value_ptr);
|
||||
}
|
||||
AbstractBasePtr GroupConv2DGradInputInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
const int64_t input_num = 2;
|
||||
(void)CheckAndConvertUtils::CheckInteger("group_conv_2D_infer", SizeToLong(input_args.size()), kGreaterEqual,
|
||||
input_num, prim_name);
|
||||
MS_EXCEPTION_IF_NULL(input_args[0]);
|
||||
|
||||
// Infer shape
|
||||
auto shape_ptr = primitive->GetAttr(kInputShape);
|
||||
MS_EXCEPTION_IF_NULL(shape_ptr);
|
||||
auto shape = GetValue<std::vector<int64_t>>(shape_ptr);
|
||||
|
||||
// Infer type
|
||||
auto type_ptr = input_args[0]->BuildType();
|
||||
MS_EXCEPTION_IF_NULL(type_ptr);
|
||||
auto type_tensor_ptr = type_ptr->cast<TensorTypePtr>();
|
||||
MS_EXCEPTION_IF_NULL(type_tensor_ptr);
|
||||
auto type = type_tensor_ptr->element();
|
||||
return std::make_shared<abstract::AbstractTensor>(type, shape);
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameGroupConv2DGradInput, GroupConv2DGradInput);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -20,17 +20,6 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
namespace {
|
||||
AbstractBasePtr LstmGradInfer(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
|
||||
// infer shape
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
for (const auto &input : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(input);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void LSTMGrad::set_input_size(const int64_t input_size) {
|
||||
(void)CheckAndConvertUtils::CheckInteger(kInput_size, input_size, kGreaterThan, 0, this->name());
|
||||
(void)AddAttr(kInput_size, MakeValue(input_size));
|
||||
|
|
@ -95,10 +84,6 @@ void LSTMGrad::Init(const int64_t input_size, const int64_t hidden_size, const i
|
|||
this->set_zoneout_hidden(zoneout_hidden);
|
||||
}
|
||||
|
||||
AbstractBasePtr LstmGradInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
return std::make_shared<abstract::AbstractTensor>(LstmGradInfer(primitive, input_args));
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameLSTMGrad, LSTMGrad);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -20,17 +20,6 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
namespace {
|
||||
AbstractBasePtr LstmGradDataInfer(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
|
||||
// infer shape
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
for (const auto &input : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(input);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void LSTMGradData::set_input_size(const int64_t input_size) {
|
||||
(void)CheckAndConvertUtils::CheckInteger(kInput_size, input_size, kGreaterThan, 0, this->name());
|
||||
(void)AddAttr(kInput_size, MakeValue(input_size));
|
||||
|
|
@ -99,10 +88,6 @@ void LSTMGradData::Init(const int64_t input_size, const int64_t hidden_size, con
|
|||
this->set_zoneout_hidden(zoneout_hidden);
|
||||
}
|
||||
|
||||
AbstractBasePtr LstmGradDataInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
return std::make_shared<abstract::AbstractTensor>(LstmGradDataInfer(primitive, input_args));
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameLSTMGradData, LSTMGradData);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -20,17 +20,6 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
namespace {
|
||||
AbstractBasePtr LstmGradWeightInfer(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
|
||||
// infer shape
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
for (const auto &input : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(input);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void LSTMGradWeight::set_input_size(const int64_t input_size) {
|
||||
(void)CheckAndConvertUtils::CheckInteger(kInput_size, input_size, kGreaterThan, 0, this->name());
|
||||
(void)AddAttr(kInput_size, MakeValue(input_size));
|
||||
|
|
@ -99,10 +88,6 @@ void LSTMGradWeight::Init(const int64_t input_size, const int64_t hidden_size, c
|
|||
this->set_zoneout_hidden(zoneout_hidden);
|
||||
}
|
||||
|
||||
AbstractBasePtr LstmGradWeightInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
return std::make_shared<abstract::AbstractTensor>(LstmGradWeightInfer(primitive, input_args));
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameLSTMGradWeight, LSTMGradWeight);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -19,15 +19,6 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
AbstractBasePtr MaxPoolGradInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(input_args[0]->BuildValue());
|
||||
auto x1_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
auto tensor_type = input_args[0]->BuildType()->cast<TensorTypePtr>();
|
||||
MS_EXCEPTION_IF_NULL(tensor_type);
|
||||
auto element = tensor_type->element();
|
||||
return std::make_shared<abstract::AbstractTensor>(element, x1_shape);
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameMaxPoolGrad, MaxPoolGrad);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -22,28 +22,6 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
AbstractBasePtr HashtableLookupInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
const int64_t input_num = 3;
|
||||
auto op_name = primitive->name();
|
||||
CheckAndConvertUtils::CheckInputArgs(input_args, kGreaterEqual, input_num, op_name);
|
||||
std::vector<int64_t> hits_shape;
|
||||
auto input = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
(void)CheckAndConvertUtils::CheckInteger("logits size", SizeToLong(input.size()), kGreaterEqual, 1, op_name);
|
||||
hits_shape.push_back(input[0]);
|
||||
|
||||
auto value_type = input_args[kInputIndex2]->BuildType();
|
||||
MS_EXCEPTION_IF_NULL(value_type);
|
||||
auto tensor_type = value_type->cast<TensorTypePtr>();
|
||||
MS_EXCEPTION_IF_NULL(tensor_type);
|
||||
auto data_type = tensor_type->element();
|
||||
std::vector<int64_t> value_shape;
|
||||
auto output = std::make_shared<abstract::AbstractTensor>(data_type, value_shape);
|
||||
auto hits = std::make_shared<abstract::AbstractTensor>(kInt8, hits_shape);
|
||||
AbstractBasePtrList output1 = {output, hits};
|
||||
return std::make_shared<abstract::AbstractTuple>(output1);
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameHashtableLookup, HashtableLookup);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -36,24 +36,6 @@ float L2Normalize::get_epsilon() const {
|
|||
return GetValue<float>(value_ptr);
|
||||
}
|
||||
|
||||
AbstractBasePtr L2NormalizeInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
(void)CheckAndConvertUtils::CheckInteger("input number", int64_t(input_args.size()), kEqual, 1, prim_name);
|
||||
for (const auto &item : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(item);
|
||||
}
|
||||
const std::set<TypePtr> valid_types = {kFloat16, kFloat32};
|
||||
(void)CheckAndConvertUtils::CheckTensorTypeValid("input_x", input_args[0]->BuildType(), valid_types, prim_name);
|
||||
auto x_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
auto x_rank = SizeToLong(x_shape.size());
|
||||
auto axiss = GetValue<std::vector<int64_t>>(primitive->GetAttr(kAxis));
|
||||
for (auto &axis : axiss) {
|
||||
CheckAndConvertUtils::CheckInRange<int64_t>("axis", axis, kIncludeLeft, {-x_rank, x_rank}, prim_name);
|
||||
}
|
||||
return input_args[0]->Broaden();
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameL2Normalize, L2Normalize);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -18,26 +18,6 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
namespace {
|
||||
abstract::ShapePtr LeakyReluInferShape(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto x = input_args[0]->BuildShape();
|
||||
auto shape_element = x->cast<abstract::ShapePtr>();
|
||||
MS_EXCEPTION_IF_NULL(shape_element);
|
||||
return shape_element;
|
||||
}
|
||||
|
||||
TypePtr LeakyReluInferType(const PrimitivePtr &prim, const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(prim);
|
||||
(void)CheckAndConvertUtils::CheckInteger("input number", SizeToLong(input_args.size()), kEqual, 1, prim->name());
|
||||
for (const auto &item : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(item);
|
||||
}
|
||||
std::map<std::string, TypePtr> types;
|
||||
(void)types.emplace("x", input_args[0]->BuildType());
|
||||
return CheckAndConvertUtils::CheckTensorTypeSame(types, common_valid_types, prim->name());
|
||||
}
|
||||
} // namespace
|
||||
void LeakyRelu::Init(const float negative_slope) { this->set_negative_slope(negative_slope); }
|
||||
|
||||
void LeakyRelu::set_negative_slope(const float negative_slope) {
|
||||
|
|
@ -45,11 +25,6 @@ void LeakyRelu::set_negative_slope(const float negative_slope) {
|
|||
}
|
||||
float LeakyRelu::get_negative_slope() const { return GetValue<float>(GetAttr(kNegativeSlope)); }
|
||||
|
||||
AbstractBasePtr LeakyReluInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
return std::make_shared<abstract::AbstractTensor>(LeakyReluInferType(primitive, input_args),
|
||||
LeakyReluInferShape(primitive, input_args));
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameLeakyRelu, LeakyRelu);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -74,35 +74,6 @@ void LRN::Init(const int64_t depth_radius, const float bias, const float alpha,
|
|||
this->set_norm_region(norm_region);
|
||||
}
|
||||
|
||||
namespace {
|
||||
abstract::ShapePtr LRNInferShape(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
const int64_t x_size = 4;
|
||||
auto in_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
(void)CheckAndConvertUtils::CheckInteger("x rank", SizeToLong(in_shape.size()), kEqual, x_size, prim_name);
|
||||
|
||||
return std::make_shared<abstract::Shape>(in_shape);
|
||||
}
|
||||
|
||||
TypePtr LRNInferType(const PrimitivePtr &prim, const std::vector<AbstractBasePtr> &input_args) {
|
||||
const std::set<TypePtr> valid_types = {kFloat16, kFloat32};
|
||||
if (std::any_of(input_args.begin(), input_args.end(), [](const AbstractBasePtr arg) { return arg == nullptr; })) {
|
||||
MS_LOG(EXCEPTION) << "For '" << prim->name()
|
||||
<< "', the input args userd for infer shape and type, can not be a nullptr.";
|
||||
}
|
||||
std::map<std::string, TypePtr> types;
|
||||
MS_EXCEPTION_IF_NULL(input_args[0]);
|
||||
(void)types.emplace("x", input_args[0]->BuildType());
|
||||
return CheckAndConvertUtils::CheckTensorTypeSame(types, valid_types, prim->name());
|
||||
}
|
||||
} // namespace
|
||||
|
||||
AbstractBasePtr LrnInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
return std::make_shared<abstract::AbstractTensor>(LRNInferType(primitive, input_args),
|
||||
LRNInferShape(primitive, input_args)->shape());
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameLRN, LRN);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -28,41 +28,6 @@ void LshProjection::set_type(const LshProjectionType &type) {
|
|||
|
||||
LshProjectionType LshProjection::get_type() const { return LshProjectionType(GetValue<int64_t>(GetAttr(kType))); }
|
||||
|
||||
AbstractBasePtr LshProjectionInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto op_name = primitive->name();
|
||||
const int64_t input_num = 2;
|
||||
const int64_t input0_size = 2;
|
||||
const int64_t input0_last_dim = 32;
|
||||
CheckAndConvertUtils::CheckInputArgs(input_args, kGreaterEqual, input_num, op_name);
|
||||
auto input0 = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[kInputIndex0]->BuildShape())[kShape];
|
||||
auto input1 = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[kInputIndex1]->BuildShape())[kShape];
|
||||
(void)CheckAndConvertUtils::CheckInteger("input0 rank", SizeToLong(input0.size()), kEqual, input0_size, op_name);
|
||||
(void)CheckAndConvertUtils::CheckInteger("input0_shape_dimen_1", input0[1], kLessEqual, input0_last_dim, op_name);
|
||||
(void)CheckAndConvertUtils::CheckInteger("input1 rank", SizeToLong(input1.size()), kGreaterEqual, 1, op_name);
|
||||
|
||||
if (input_args.size() == 3) {
|
||||
auto input2 = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[kInputIndex2]->BuildShape())[kShape];
|
||||
(void)CheckAndConvertUtils::CheckInteger("input2 rank", SizeToLong(input2.size()), kEqual, 1, op_name);
|
||||
(void)CheckAndConvertUtils::CheckInteger("input2_shape_dimen_0", input2[0], kEqual, input1[0], op_name);
|
||||
}
|
||||
|
||||
std::vector<int64_t> out_shape;
|
||||
auto attr_value = GetValue<int64_t>(primitive->GetAttr(kType));
|
||||
switch ((int64_t)LshProjectionType(attr_value)) {
|
||||
case (int64_t)LshProjectionType::SPARSE:
|
||||
out_shape.push_back(input0[0]);
|
||||
break;
|
||||
case (int64_t)LshProjectionType::DENSE:
|
||||
out_shape.push_back(input0[0] * input0[1]);
|
||||
break;
|
||||
default:
|
||||
MS_LOG(ERROR) << "For '" << op_name << "', type: " << (int64_t)LshProjectionType(attr_value)
|
||||
<< " is unsupported.";
|
||||
}
|
||||
return std::make_shared<abstract::AbstractTensor>(kInt32, out_shape);
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameLshProjection, LshProjection);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -21,85 +21,6 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
namespace {
|
||||
abstract::ShapePtr MatMulInferShape(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
auto x_shape_map = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape());
|
||||
auto y_shape_map = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[1]->BuildShape());
|
||||
auto x_shp = x_shape_map[kShape];
|
||||
auto y_shp = y_shape_map[kShape];
|
||||
constexpr size_t dim_limit = 2;
|
||||
if (x_shp.size() != dim_limit || y_shp.size() != dim_limit) {
|
||||
MS_EXCEPTION(ValueError) << "For MatMul, input x, y should have the same dimension size and should be greater"
|
||||
<< "or equal to 3, while x size = " << x_shp.size() << ", y size = " << y_shp.size();
|
||||
}
|
||||
constexpr size_t offset = 2;
|
||||
std::vector<int> x_last(x_shp.end() - offset, x_shp.end());
|
||||
std::vector<int> y_last(y_shp.end() - offset, y_shp.end());
|
||||
ValuePtr transpose_a_ptr = primitive->GetAttr("transpose_a");
|
||||
ValuePtr transpose_b_ptr = primitive->GetAttr("transpose_b");
|
||||
bool transpose_a = GetValue<bool>(transpose_a_ptr);
|
||||
bool transpose_b = GetValue<bool>(transpose_b_ptr);
|
||||
int64_t x_col = x_last[static_cast<size_t>(!transpose_a)];
|
||||
int64_t y_row = y_last[static_cast<size_t>(transpose_b)];
|
||||
if (std::find(x_shp.begin(), x_shp.end(), -1) == x_shp.end() &&
|
||||
std::find(y_shp.begin(), y_shp.end(), -1) == y_shp.end()) {
|
||||
if (x_col != y_row) {
|
||||
MS_EXCEPTION(ValueError) << "For " << prim_name << " evaluator shapes of inputs can not do this operator, "
|
||||
<< "got " << x_col << " and " << y_row << " , with x1 shape " << x_shp
|
||||
<< "(transpose_a=" << transpose_a << "})"
|
||||
<< ", x2 shape " << y_shp << "(transpose_b=" << transpose_b << "})";
|
||||
}
|
||||
}
|
||||
(void)primitive->AddAttr("transpose_x1", transpose_a_ptr);
|
||||
(void)primitive->AddAttr("transpose_x2", transpose_b_ptr);
|
||||
|
||||
ShapeVector x_min_shape = x_shape_map[kMinShape];
|
||||
ShapeVector x_max_shape = x_shape_map[kMaxShape];
|
||||
ShapeVector y_min_shape = y_shape_map[kMinShape];
|
||||
ShapeVector y_max_shape = y_shape_map[kMaxShape];
|
||||
CheckAndConvertUtils::CheckMinMaxShape(x_shp, &x_min_shape, &x_max_shape);
|
||||
CheckAndConvertUtils::CheckMinMaxShape(y_shp, &y_min_shape, &y_max_shape);
|
||||
// Additional check for dynamic shape
|
||||
// Last infer will be real shape values
|
||||
bool x_not_dyn =
|
||||
std::all_of(x_shp.begin(), x_shp.end(), [](int64_t value) { return value != abstract::Shape::SHP_ANY; });
|
||||
bool y_not_dyn =
|
||||
std::all_of(y_shp.begin(), y_shp.end(), [](int64_t value) { return value != abstract::Shape::SHP_ANY; });
|
||||
if (x_not_dyn && y_not_dyn) {
|
||||
auto x_c = x_shp[(transpose_a ? 0 : 1)];
|
||||
auto y_r = y_shp[(transpose_b ? 1 : 0)];
|
||||
if (x_c != y_r) {
|
||||
MS_LOG(EXCEPTION) << "MatMul shape error, got x_col: " << x_c << ", y_row: " << y_r
|
||||
<< ". In MatMul x_col and y_row should be equal.";
|
||||
}
|
||||
}
|
||||
ShapeVector ret_shape;
|
||||
ShapeVector ret_min_shape;
|
||||
ShapeVector ret_max_shape;
|
||||
auto make_shape = [&transpose_a, &transpose_b](ShapeVector &output, const ShapeVector xshp,
|
||||
const ShapeVector yshp) -> void {
|
||||
output.push_back(xshp[(transpose_a ? 1 : 0)]);
|
||||
output.push_back(yshp[(transpose_b ? 0 : 1)]);
|
||||
return;
|
||||
};
|
||||
make_shape(ret_shape, x_shp, y_shp);
|
||||
make_shape(ret_min_shape, x_min_shape, y_min_shape);
|
||||
make_shape(ret_max_shape, x_max_shape, y_max_shape);
|
||||
return std::make_shared<abstract::Shape>(ret_shape, ret_min_shape, ret_max_shape);
|
||||
}
|
||||
|
||||
TypePtr MatMulInferType(const PrimitivePtr &prim, const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(prim);
|
||||
const std::set<TypePtr> valid_types = {kInt8, kInt16, kInt32, kInt64, kFloat16, kFloat32, kFloat64};
|
||||
std::map<std::string, TypePtr> types;
|
||||
(void)types.emplace("x", input_args[0]->BuildType());
|
||||
(void)types.emplace("w", input_args[1]->BuildType());
|
||||
return CheckAndConvertUtils::CheckTensorTypeSame(types, valid_types, prim->name());
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void MatMul::Init(bool transpose_a, bool transpose_b) {
|
||||
set_transpose_a(transpose_a);
|
||||
set_transpose_b(transpose_b);
|
||||
|
|
@ -119,14 +40,6 @@ bool MatMul::get_transpose_b() const {
|
|||
return GetValue<bool>(value_ptr);
|
||||
}
|
||||
|
||||
AbstractBasePtr MatMulInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
const int64_t input_num = 2;
|
||||
(void)CheckAndConvertUtils::CheckInteger("MatMul infer", SizeToLong(input_args.size()), kGreaterEqual, input_num,
|
||||
primitive->name());
|
||||
return abstract::MakeAbstract(MatMulInferShape(primitive, input_args), MatMulInferType(primitive, input_args));
|
||||
}
|
||||
// Add
|
||||
REGISTER_PRIMITIVE_C(kNameMatMul, MatMul);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -78,74 +78,6 @@ void MaxPool::Init(const std::vector<int64_t> &kernel_size, const std::vector<in
|
|||
this->set_round_mode(round_mode);
|
||||
}
|
||||
|
||||
namespace {
|
||||
abstract::ShapePtr MaxPoolInferShape(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto op_name = primitive->name();
|
||||
MS_EXCEPTION_IF_NULL(input_args[0]);
|
||||
auto in_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->GetShapeTrack())[kShape];
|
||||
auto format = Format(GetValue<int64_t>(primitive->GetAttr(kFormat)));
|
||||
if (format == NHWC) {
|
||||
in_shape = {in_shape[0], in_shape[3], in_shape[1], in_shape[2]};
|
||||
}
|
||||
const int64_t x_rank = 4;
|
||||
(void)CheckAndConvertUtils::CheckInteger("x_rank", SizeToLong(in_shape.size()), kEqual, x_rank, op_name);
|
||||
|
||||
auto kernel_size = GetValue<std::vector<int64_t>>(primitive->GetAttr(kKernelSize));
|
||||
auto pad_mode_value = (primitive->GetAttr(kPadMode));
|
||||
auto pad_mode = PadMode(GetValue<int64_t>(pad_mode_value));
|
||||
auto batch = in_shape[0];
|
||||
auto channel = in_shape[1];
|
||||
auto in_h = in_shape[2];
|
||||
auto in_w = in_shape[3];
|
||||
auto strides = GetValue<std::vector<int64_t>>(primitive->GetAttr(kStrides));
|
||||
auto kernel_h = kernel_size[2];
|
||||
auto kernel_w = kernel_size[3];
|
||||
auto stride_h = strides[2];
|
||||
auto stride_w = strides[3];
|
||||
int64_t out_h = abstract::Shape::SHP_ANY;
|
||||
int64_t out_w = abstract::Shape::SHP_ANY;
|
||||
if (pad_mode == VALID) {
|
||||
out_h = static_cast<int64_t>(ceil((in_h - (kernel_h - 1)) + static_cast<float>(stride_h) - 1) /
|
||||
static_cast<float>(stride_h));
|
||||
out_w = static_cast<int64_t>(ceil((in_w - (kernel_w - 1)) + static_cast<float>(stride_w) - 1) /
|
||||
static_cast<float>(stride_w));
|
||||
} else if (pad_mode == SAME) {
|
||||
out_h = static_cast<int64_t>(ceil(in_h / static_cast<float>(stride_h)));
|
||||
out_w = static_cast<int64_t>(ceil(in_w / static_cast<float>(stride_w)));
|
||||
}
|
||||
std::vector<int64_t> out_shape = {batch, channel, out_h, out_w};
|
||||
if (format == NHWC) {
|
||||
out_shape = {batch, out_h, out_w, channel};
|
||||
}
|
||||
if (std::any_of(out_shape.begin(), out_shape.end(), [](int64_t a) { return a <= 0; })) {
|
||||
MS_LOG(EXCEPTION) << "For '" << op_name << "', Kernel size must be positive, but it's " << kernel_size << ".";
|
||||
}
|
||||
return std::make_shared<abstract::Shape>(out_shape);
|
||||
}
|
||||
|
||||
TypePtr MaxPoolInferType(const PrimitivePtr &prim, const std::vector<AbstractBasePtr> &input_args) {
|
||||
if (std::any_of(input_args.begin(), input_args.end(), [](const AbstractBasePtr arg) { return arg == nullptr; })) {
|
||||
MS_LOG(EXCEPTION) << "For '" << prim->name()
|
||||
<< "', the input args userd for infer shape and type, can not be a nullptr.";
|
||||
}
|
||||
auto name = prim->name();
|
||||
MS_LOG(DEBUG) << "Infer data type for : " << name;
|
||||
auto input_type = input_args[0]->BuildType();
|
||||
MS_EXCEPTION_IF_NULL(input_type);
|
||||
auto input_tensor_type = input_type->cast<TensorTypePtr>();
|
||||
if (input_tensor_type == nullptr) {
|
||||
MS_LOG_EXCEPTION << "For '" << name << "', the input must be a tensor but got " << input_type->ToString();
|
||||
}
|
||||
return input_tensor_type->element();
|
||||
}
|
||||
} // namespace
|
||||
|
||||
AbstractBasePtr MaxPoolInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
return std::make_shared<abstract::AbstractTensor>(MaxPoolInferType(primitive, input_args),
|
||||
MaxPoolInferShape(primitive, input_args)->shape());
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameMaxPool, MaxPool);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -21,32 +21,6 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
namespace {
|
||||
abstract::ShapePtr MfccInferShape(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
const int64_t input0_size = 3;
|
||||
const int64_t input1_size = 1;
|
||||
auto first_input_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
auto second_input_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[1]->BuildShape())[kShape];
|
||||
(void)CheckAndConvertUtils::CheckInteger("input 0 rank", SizeToLong(first_input_shape.size()), kEqual, input0_size,
|
||||
prim_name);
|
||||
(void)CheckAndConvertUtils::CheckInteger("input 1 rank", SizeToLong(second_input_shape.size()), kEqual, input1_size,
|
||||
prim_name);
|
||||
std::vector<int64_t> out_shape = {first_input_shape[0], first_input_shape[1],
|
||||
GetValue<int64_t>(primitive->GetAttr(kDctCoeffNum))};
|
||||
return std::make_shared<abstract::Shape>(out_shape);
|
||||
}
|
||||
|
||||
TypePtr MfccInferType(const std::vector<AbstractBasePtr> &input_args) {
|
||||
for (const auto &item : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(item);
|
||||
}
|
||||
auto infer_type = input_args[0]->BuildType()->cast<TensorTypePtr>()->element();
|
||||
return infer_type;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void Mfcc::Init(const float freq_upper_limit, const float freq_lower_limit, const int64_t filter_bank_channel_num,
|
||||
const int64_t dct_coeff_num) {
|
||||
this->set_freq_upper_limit(freq_upper_limit);
|
||||
|
|
@ -88,11 +62,6 @@ void Mfcc::set_dct_coeff_num(const int64_t dct_coeff_num) {
|
|||
|
||||
int64_t Mfcc::get_dct_coeff_num() const { return GetValue<int64_t>(GetAttr(kDctCoeffNum)); }
|
||||
|
||||
AbstractBasePtr MfccInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
return std::make_shared<abstract::AbstractTensor>(MfccInferType(input_args),
|
||||
MfccInferShape(primitive, input_args)->shape());
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameMfcc, Mfcc);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -26,34 +26,6 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
namespace {
|
||||
abstract::ShapePtr MinimumInferShape(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto op_name = primitive->name();
|
||||
return BroadCastInferShape(op_name, input_args);
|
||||
}
|
||||
|
||||
TypePtr MinimumInferType(const PrimitivePtr &prim, const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(prim);
|
||||
auto op_name = prim->name();
|
||||
const int64_t input_num = 2;
|
||||
(void)CheckAndConvertUtils::CheckInteger("input number", SizeToLong(input_args.size()), kEqual, input_num, op_name);
|
||||
if (std::any_of(input_args.begin(), input_args.end(), [](const AbstractBasePtr &a) { return a == nullptr; })) {
|
||||
MS_LOG(EXCEPTION) << "For '" << prim->name()
|
||||
<< ", the input args userd for infer shape and type, can not be a nullptr.";
|
||||
}
|
||||
std::map<std::string, TypePtr> types;
|
||||
(void)types.emplace("x", input_args[0]->BuildType());
|
||||
(void)types.emplace("y", input_args[1]->BuildType());
|
||||
return CheckAndConvertUtils::CheckTensorTypeSame(types, common_valid_types, op_name);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
AbstractBasePtr MinimumInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
return std::make_shared<abstract::AbstractTensor>(MinimumInferType(primitive, input_args),
|
||||
MinimumInferShape(primitive, input_args));
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameMinimum, Minimum);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -29,11 +29,6 @@ int64_t NonMaxSuppression::get_center_point_box() const {
|
|||
}
|
||||
void NonMaxSuppression::Init(const int64_t center_point_box) { this->set_center_point_box(center_point_box); }
|
||||
|
||||
AbstractBasePtr NonMaxSuppressionInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_LOG(INFO) << "NonMaxSuppression infer shape in runtime.";
|
||||
return std::make_shared<abstract::AbstractTensor>(kInt32, std::vector<int64_t>{});
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameNonMaxSuppression, NonMaxSuppression);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -18,55 +18,12 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
namespace {
|
||||
std::vector<int64_t> _get_pack_shape(std::vector<BaseShapePtr> x_shapes, std::vector<TypePtr> x_types, int64_t axis,
|
||||
const std::string &name) {
|
||||
(void)CheckAndConvertUtils::CheckInteger("len of input_x", (int64_t)x_shapes.size(), kGreaterEqual, 1, name);
|
||||
(void)CheckAndConvertUtils::CheckSubClass("input_x[0]", x_types[0], {TypeIdToType(kObjectTypeTensorType)}, name);
|
||||
auto output_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(x_shapes[0])[kShape];
|
||||
int64_t rank_base = SizeToLong(output_shape.size());
|
||||
int64_t N = SizeToLong(x_shapes.size());
|
||||
if (axis < 0) {
|
||||
axis = axis + rank_base + 1;
|
||||
}
|
||||
for (int64_t i = 1; i < N; i++) {
|
||||
auto type = x_types[LongToSize(i)]->cast<TensorTypePtr>()->element();
|
||||
MS_EXCEPTION_IF_NULL(type);
|
||||
auto type0 = x_types[0]->cast<TensorTypePtr>()->element();
|
||||
MS_EXCEPTION_IF_NULL(type0);
|
||||
CheckAndConvertUtils::Check("x_type[" + std::to_string(i) + "]", type->type_id(), kEqual, type0->type_id(), name);
|
||||
auto shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(x_shapes[LongToSize(i)])[kShape];
|
||||
if (shape != output_shape) {
|
||||
MS_EXCEPTION(ValueError) << "For '" + name + "' element " + std::to_string(i) +
|
||||
"shape in input can't pack with first element.";
|
||||
}
|
||||
}
|
||||
(void)output_shape.insert(output_shape.begin() + axis, N);
|
||||
return output_shape;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void Pack::set_axis(const int64_t &axis) { (void)AddAttr(kAxis, MakeValue(axis)); }
|
||||
|
||||
int64_t Pack::get_axis() const { return GetValue<int64_t>(GetAttr(kAxis)); }
|
||||
|
||||
void Pack::Init(const int64_t &axis) { this->set_axis(axis); }
|
||||
|
||||
AbstractBasePtr PackInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
|
||||
MS_EXCEPTION_IF_NULL(input_args[0]);
|
||||
auto x_shapes = input_args[0]->BuildShape()->cast<abstract::TupleShapePtr>()->shape();
|
||||
auto x_types = input_args[0]->BuildType()->cast<TuplePtr>()->elements();
|
||||
auto all_shape = _get_pack_shape(x_shapes, x_types, GetValue<int64_t>(primitive->GetAttr(kAxis)), prim_name);
|
||||
auto tensor_type = x_types[0]->cast<TensorTypePtr>();
|
||||
MS_EXCEPTION_IF_NULL(tensor_type);
|
||||
auto data_type = tensor_type->element();
|
||||
MS_EXCEPTION_IF_NULL(data_type);
|
||||
return std::make_shared<abstract::AbstractTensor>(data_type, all_shape);
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNamePack, Pack);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -20,40 +20,6 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
namespace {
|
||||
abstract::ShapePtr PadInferShape(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
auto paddings_attr = GetValue<std::vector<std::vector<int64_t>>>(primitive->GetAttr(kPaddings));
|
||||
auto x_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
(void)CheckAndConvertUtils::CheckInteger("paddings_size", SizeToLong(paddings_attr.size()), kEqual,
|
||||
int64_t(2 * x_shape.size()), prim_name);
|
||||
int64_t size = SizeToLong(paddings_attr.size());
|
||||
for (int64_t i = 0; i < size; i++) {
|
||||
for (int64_t j = 0; j < 2; j++) {
|
||||
if (paddings_attr[LongToSize(i)][LongToSize(j)] < 0) {
|
||||
MS_LOG_ERROR << "For '" << prim_name << "', All elements of paddings must be >= 0, but got "
|
||||
<< paddings_attr[LongToSize(i)][LongToSize(j)];
|
||||
}
|
||||
}
|
||||
}
|
||||
std::vector<int64_t> out_shape;
|
||||
for (int64_t i = 0; i < int64_t(paddings_attr.size() / 2); i++) {
|
||||
(void)out_shape.emplace_back(x_shape[LongToSize(i)] + paddings_attr[LongToSize(i)][0] +
|
||||
paddings_attr[LongToSize(i)][1]);
|
||||
}
|
||||
return std::make_shared<abstract::Shape>(out_shape);
|
||||
}
|
||||
|
||||
TypePtr PadInferType(const PrimitivePtr &prim, const std::vector<AbstractBasePtr> &input_args) {
|
||||
for (const auto &item : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(item);
|
||||
}
|
||||
const std::set<TypePtr> valid_types = {kTensorType};
|
||||
return CheckAndConvertUtils::CheckSubClass("infer type", input_args[0]->BuildType(), valid_types, prim->name());
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void Pad::Init(const std::vector<std::vector<int64_t>> &paddings) { this->set_paddings(paddings); }
|
||||
void Pad::set_paddings(const std::vector<std::vector<int64_t>> &paddings) {
|
||||
(void)this->AddAttr(kPaddings, MakeValue(paddings));
|
||||
|
|
@ -61,11 +27,7 @@ void Pad::set_paddings(const std::vector<std::vector<int64_t>> &paddings) {
|
|||
std::vector<std::vector<int64_t>> Pad::get_paddings() const {
|
||||
return GetValue<std::vector<std::vector<int64_t>>>(GetAttr(kPaddings));
|
||||
}
|
||||
AbstractBasePtr PadInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
return std::make_shared<abstract::AbstractTensor>(PadInferType(primitive, input_args),
|
||||
PadInferShape(primitive, input_args)->shape());
|
||||
}
|
||||
|
||||
REGISTER_PRIMITIVE_C(kNamePad, Pad);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -19,42 +19,6 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
namespace {
|
||||
abstract::ShapePtr PReLUInferShape(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
|
||||
auto prim_name = primitive->name();
|
||||
auto x = input_args[0]->BuildShape();
|
||||
auto w = input_args[1]->BuildShape();
|
||||
auto x_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(x)[kShape];
|
||||
auto w_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(w)[kShape];
|
||||
const int64_t x_rank = 2;
|
||||
const int64_t w_rank = 1;
|
||||
(void)CheckAndConvertUtils::CheckInteger("x rank", SizeToLong(x_shape.size()), kGreaterEqual, x_rank, prim_name);
|
||||
(void)CheckAndConvertUtils::CheckInteger("weight rank", SizeToLong(w_shape.size()), kEqual, w_rank, prim_name);
|
||||
if (w_shape[0] != x_shape[1] && w_shape[0] != 1) {
|
||||
MS_LOG(EXCEPTION) << "For " << prim_name << ", channel of input_x and weight must be matched, "
|
||||
<< "while channel of input_x is " << x_shape[1] << ", weight_shape[0] is " << w_shape[0];
|
||||
}
|
||||
MS_EXCEPTION_IF_NULL(x);
|
||||
auto shape_element = x->cast<abstract::ShapePtr>();
|
||||
MS_EXCEPTION_IF_NULL(shape_element);
|
||||
return shape_element;
|
||||
}
|
||||
|
||||
TypePtr PReLUInferType(const PrimitivePtr &prim, const std::vector<AbstractBasePtr> &input_args) {
|
||||
const std::set<TypePtr> valid_types = {kFloat16, kFloat32};
|
||||
std::map<string, TypePtr> check_map = {{"input_x", input_args[0]->BuildType()},
|
||||
{"weight", input_args[1]->BuildType()}};
|
||||
return CheckAndConvertUtils::CheckTensorTypeSame(check_map, valid_types, prim->name());
|
||||
}
|
||||
} // namespace
|
||||
AbstractBasePtr PReLUInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
const int64_t input_num = 2;
|
||||
CheckAndConvertUtils::CheckInputArgs(input_args, kEqual, input_num, primitive->name());
|
||||
return std::make_shared<abstract::AbstractTensor>(PReLUInferType(primitive, input_args),
|
||||
PReLUInferShape(primitive, input_args));
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNamePReLU, PReLU);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -119,30 +119,6 @@ void PriorBox::Init(const std::vector<int64_t> &min_sizes, const std::vector<int
|
|||
this->set_offset(offset);
|
||||
}
|
||||
|
||||
AbstractBasePtr PriorBoxInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
MS_EXCEPTION_IF_NULL(input_args[0]);
|
||||
std::vector<float> different_aspect_ratios{1.0f};
|
||||
auto aspect_ratios = GetValue<std::vector<float>>(primitive->GetAttr(kAspectRatios));
|
||||
for (size_t i = 0; i < aspect_ratios.size(); i++) {
|
||||
float ratio = aspect_ratios[i];
|
||||
bool exist = std::any_of(different_aspect_ratios.begin(), different_aspect_ratios.end(),
|
||||
[&](float v) { return abs(ratio - v) < 1e-6; });
|
||||
if (!exist) {
|
||||
(void)different_aspect_ratios.emplace_back(ratio);
|
||||
if (GetValue<bool>(primitive->GetAttr(kFlip))) {
|
||||
(void)different_aspect_ratios.emplace_back(1.0f / ratio);
|
||||
}
|
||||
}
|
||||
}
|
||||
auto min_sizes = GetValue<std::vector<int64_t>>(primitive->GetAttr(kMinSizes));
|
||||
int64_t num_priors_box = SizeToLong(min_sizes.size() * different_aspect_ratios.size() + min_sizes.size());
|
||||
auto input = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
int64_t h = input[0] * input[1] * num_priors_box * 4;
|
||||
std::vector<int64_t> output_shape{1, h, 1, 2};
|
||||
return std::make_shared<abstract::AbstractTensor>(kFloat32, output_shape);
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNamePriorBox, PriorBox);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -29,23 +29,7 @@ void QuantDTypeCast::Init(const int64_t src_t, const int64_t dst_t) {
|
|||
this->set_src_t(src_t);
|
||||
this->set_dst_t(dst_t);
|
||||
}
|
||||
AbstractBasePtr QuantDTypeCastInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
const int64_t input_num = 1;
|
||||
const size_t x_index = 0;
|
||||
CheckAndConvertUtils::CheckInputArgs(input_args, kGreaterEqual, input_num, primitive->name());
|
||||
auto input_type = CheckAndConvertUtils::GetTensorInputType(primitive->name(), input_args, x_index);
|
||||
auto dst_type = TypeIdToType(TypeId(GetValue<int64_t>(primitive->GetAttr(kDstT))));
|
||||
MS_EXCEPTION_IF_NULL(dst_type);
|
||||
if (input_type != dst_type) {
|
||||
MS_EXCEPTION(TypeError) << "For '" << primitive->name() << "', Input type should be " << dst_type->ToString()
|
||||
<< ", but got " << input_type->ToString();
|
||||
input_type->ToString();
|
||||
}
|
||||
auto input_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
return std::make_shared<abstract::AbstractTensor>(dst_type, input_shape);
|
||||
}
|
||||
|
||||
REGISTER_PRIMITIVE_C(kNameQuantDTypeCast, QuantDTypeCast);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -57,51 +57,6 @@ void Range::Init(const int64_t d_type, const int64_t start, const int64_t limit,
|
|||
this->set_delta(delta);
|
||||
}
|
||||
|
||||
AbstractBasePtr RangeInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
int64_t shape_size = 0;
|
||||
const size_t max_input_num = 3;
|
||||
if (input_args.size() == max_input_num) {
|
||||
MS_EXCEPTION_IF_NULL(input_args[kInputIndex0]->BuildValue());
|
||||
MS_EXCEPTION_IF_NULL(input_args[kInputIndex1]->BuildValue());
|
||||
MS_EXCEPTION_IF_NULL(input_args[kInputIndex2]->BuildValue());
|
||||
auto start_tensor = input_args[kInputIndex0]->BuildValue()->cast<tensor::TensorPtr>();
|
||||
auto limit_tensor = input_args[kInputIndex1]->BuildValue()->cast<tensor::TensorPtr>();
|
||||
auto delta_tensor = input_args[kInputIndex2]->BuildValue()->cast<tensor::TensorPtr>();
|
||||
auto dtype = start_tensor->data_type();
|
||||
switch (dtype) {
|
||||
case kNumberTypeInt:
|
||||
case kNumberTypeInt32: {
|
||||
auto start = *reinterpret_cast<int *>(start_tensor->data_c());
|
||||
auto limit = *reinterpret_cast<int *>(limit_tensor->data_c());
|
||||
auto delta = *reinterpret_cast<int *>(delta_tensor->data_c());
|
||||
shape_size =
|
||||
std::max(static_cast<int64_t>(std::ceil(static_cast<float>(limit - start) / delta)), static_cast<int64_t>(0));
|
||||
} break;
|
||||
case kNumberTypeFloat32:
|
||||
case kNumberTypeFloat: {
|
||||
auto start = *reinterpret_cast<float *>(start_tensor->data_c());
|
||||
auto limit = *reinterpret_cast<float *>(limit_tensor->data_c());
|
||||
auto delta = *reinterpret_cast<float *>(delta_tensor->data_c());
|
||||
shape_size =
|
||||
std::max(static_cast<int64_t>(std::ceil(static_cast<float>(limit - start) / delta)), static_cast<int64_t>(0));
|
||||
} break;
|
||||
default: {
|
||||
MS_LOG(EXCEPTION) << "For '" << primitive->name()
|
||||
<< "', the supported dataType is ['int32', 'float32'], but got " << dtype;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
int64_t start = GetValue<int64_t>(primitive->GetAttr(kStart));
|
||||
int64_t limit = GetValue<int64_t>(primitive->GetAttr(kLimit));
|
||||
int64_t delta = GetValue<int64_t>(primitive->GetAttr(kDelta));
|
||||
shape_size =
|
||||
std::max(static_cast<int64_t>(std::ceil(LongToDouble(limit - start) / delta)), static_cast<int64_t>(0));
|
||||
}
|
||||
return std::make_shared<abstract::AbstractTensor>(
|
||||
kInt32, std::make_shared<abstract::Shape>(std::vector<int64_t>{shape_size}));
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameRange, Range);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -18,21 +18,6 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
namespace {
|
||||
TypePtr RankInferType(const PrimitivePtr &prim, const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(prim);
|
||||
auto op_name = prim->name();
|
||||
MS_EXCEPTION_IF_NULL(input_args[0]);
|
||||
auto infer_dtype = input_args[0]->BuildType();
|
||||
(void)CheckAndConvertUtils::CheckTensorTypeValid("x", infer_dtype, {kTensorType}, op_name);
|
||||
return kTypeNone;
|
||||
}
|
||||
} // namespace
|
||||
AbstractBasePtr RankInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
std::vector<int64_t> infer_shape;
|
||||
return std::make_shared<abstract::AbstractTensor>(RankInferType(primitive, input_args), infer_shape);
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameRank, Rank);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -25,79 +25,12 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
namespace {
|
||||
void reduce_one_axis(const int64_t one_axis, const int64_t dim, std::set<int64_t> axis_reduce) {
|
||||
CheckAndConvertUtils::CheckInRange("axis", one_axis, kIncludeLeft, {-dim, dim}, "Reduce");
|
||||
if (one_axis < 0) {
|
||||
axis_reduce.insert(one_axis);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<int64_t> infer_shape_reduce(std::vector<int64_t> input_x_shape, const ValuePtr axis_value,
|
||||
const bool keep_dims) {
|
||||
int64_t dim = SizeToLong(input_x_shape.size());
|
||||
std::set<int64_t> axis_reduce;
|
||||
if (axis_value == nullptr) {
|
||||
std::vector<int64_t> vec;
|
||||
if (keep_dims) {
|
||||
return std::vector<int64_t>(dim, 1);
|
||||
}
|
||||
return vec;
|
||||
}
|
||||
auto axis_value_elem = GetValue<std::vector<int64_t>>(axis_value);
|
||||
if (axis_value_elem.size() == 1) {
|
||||
reduce_one_axis(axis_value_elem[0], dim, axis_reduce);
|
||||
} else {
|
||||
size_t size = axis_value_elem.size();
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
reduce_one_axis(axis_value_elem[i], dim, axis_reduce);
|
||||
}
|
||||
}
|
||||
std::vector<int64_t> out_shape;
|
||||
for (int64_t i = 0; i < dim; i++) {
|
||||
if (axis_reduce.find(i) != axis_reduce.end()) {
|
||||
if (keep_dims) {
|
||||
(void)out_shape.emplace_back(1);
|
||||
}
|
||||
} else {
|
||||
(void)out_shape.emplace_back(input_x_shape[LongToSize(i)]);
|
||||
}
|
||||
}
|
||||
return out_shape;
|
||||
}
|
||||
|
||||
abstract::ShapePtr ReduceInferShape(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
|
||||
auto axis_value = input_args[1]->BuildValue();
|
||||
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto input_x_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
|
||||
auto keep_dims = GetValue<bool>(primitive->GetAttr(kKeepDims));
|
||||
auto out_shape = infer_shape_reduce(input_x_shape, axis_value, keep_dims);
|
||||
|
||||
return std::make_shared<abstract::Shape>(out_shape);
|
||||
}
|
||||
|
||||
TypePtr ReduceInferType(const PrimitivePtr &prim, const std::vector<AbstractBasePtr> &input_args) {
|
||||
for (const auto &item : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(item);
|
||||
}
|
||||
return CheckAndConvertUtils::CheckTensorTypeValid("input_x", input_args[0]->BuildType(), common_valid_types,
|
||||
prim->name());
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void Reduce::set_keep_dims(const bool keep_dims) { (void)this->AddAttr(kKeepDims, MakeValue(keep_dims)); }
|
||||
|
||||
bool Reduce::get_keep_dims() const { return GetValue<bool>(GetAttr(kKeepDims)); }
|
||||
|
||||
void Reduce::Init(const bool keep_dims) { this->set_keep_dims(keep_dims); }
|
||||
|
||||
AbstractBasePtr ReduceInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
return std::make_shared<abstract::AbstractTensor>(ReduceInferType(primitive, input_args),
|
||||
ReduceInferShape(primitive, input_args)->shape());
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameReduce, Reduce);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -27,80 +27,6 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
AbstractBasePtr ReshapeInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
const int64_t input_num = 2;
|
||||
(void)CheckAndConvertUtils::CheckInteger("input number", SizeToLong(input_args.size()), kEqual, input_num, prim_name);
|
||||
for (const auto &item : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(item);
|
||||
}
|
||||
auto x = input_args[0]->cast<abstract::AbstractTensorPtr>();
|
||||
MS_EXCEPTION_IF_NULL(x);
|
||||
auto shape = input_args[1]->cast<abstract::AbstractTuplePtr>();
|
||||
MS_EXCEPTION_IF_NULL(shape);
|
||||
auto shape_v = GetValue<std::vector<int64_t>>(shape->BuildValue());
|
||||
int64_t neg_index = -1;
|
||||
int64_t dim_prod = 1;
|
||||
for (size_t i = 0; i < shape_v.size(); ++i) {
|
||||
if (shape_v[i] == -1) {
|
||||
if (neg_index != -1) {
|
||||
MS_LOG(EXCEPTION) << "For '" << prim_name << "', The Reshape's shape input can only has one -1 at most.";
|
||||
}
|
||||
neg_index = SizeToLong(i);
|
||||
} else {
|
||||
dim_prod *= shape_v[i];
|
||||
}
|
||||
}
|
||||
MS_EXCEPTION_IF_NULL(x->shape());
|
||||
auto x_shape = x->shape()->shape();
|
||||
int64_t arr_prod =
|
||||
std::accumulate(x_shape.begin(), x_shape.end(), static_cast<int64_t>(1), std::multiplies<int64_t>());
|
||||
if (arr_prod <= 0) {
|
||||
ShapeVector x_max_shape = x->shape()->max_shape();
|
||||
ShapeVector x_min_shape = x->shape()->min_shape();
|
||||
if (x_max_shape.empty()) {
|
||||
x_max_shape = x_shape;
|
||||
}
|
||||
if (x_min_shape.empty()) {
|
||||
x_min_shape = x_shape;
|
||||
}
|
||||
int64_t max_arr_prod =
|
||||
std::accumulate(x_max_shape.begin(), x_max_shape.end(), static_cast<int64_t>(1), std::multiplies<int64_t>());
|
||||
int64_t min_arr_prod =
|
||||
std::accumulate(x_min_shape.begin(), x_min_shape.end(), static_cast<int64_t>(1), std::multiplies<int64_t>());
|
||||
ShapeVector max_shape = shape_v;
|
||||
ShapeVector min_shape = shape_v;
|
||||
if (neg_index != -1) {
|
||||
max_shape[LongToSize(neg_index)] = max_arr_prod / dim_prod;
|
||||
min_shape[LongToSize(neg_index)] = min_arr_prod / dim_prod;
|
||||
} else {
|
||||
MS_LOG(EXCEPTION) << "For dynamic shape, Reshape's shape input must have neg index";
|
||||
}
|
||||
return std::make_shared<abstract::AbstractTensor>(x->element(),
|
||||
std::make_shared<abstract::Shape>(shape_v, min_shape, max_shape));
|
||||
} else {
|
||||
if (dim_prod <= 0 || arr_prod % dim_prod != 0) {
|
||||
MS_LOG(EXCEPTION)
|
||||
<< "For '" << prim_name
|
||||
<< "', The product of input_x's shape should > 0, and can be divided by product of input_shape, "
|
||||
"but product of input_x's shape is "
|
||||
<< arr_prod << ", product of input_shape is" << dim_prod;
|
||||
}
|
||||
if (neg_index != -1) {
|
||||
shape_v[LongToSize(neg_index)] = arr_prod / dim_prod;
|
||||
dim_prod *= shape_v[LongToSize(neg_index)];
|
||||
}
|
||||
if (arr_prod != dim_prod) {
|
||||
MS_LOG(EXCEPTION) << "For '" << prim_name
|
||||
<< "',The product of input_x's shape should be equal to product of input_shape, "
|
||||
"but product of input_x's shape is "
|
||||
<< arr_prod << ", product of input_shape is" << dim_prod;
|
||||
}
|
||||
return std::make_shared<abstract::AbstractTensor>(x->element(), std::make_shared<abstract::Shape>(shape_v));
|
||||
}
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameReshape, Reshape);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -42,25 +42,7 @@ void ResizeBilinear::Init(const std::vector<int64_t> &size, const bool align_cor
|
|||
this->set_size(size);
|
||||
this->set_align_corners(align_corners);
|
||||
}
|
||||
AbstractBasePtr ResizeBilinearInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
(void)CheckAndConvertUtils::CheckInteger("infer", SizeToLong(input_args.size()), kEqual, 1, prim_name);
|
||||
|
||||
// Infer shape
|
||||
auto input_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
const int64_t shape_size = 4;
|
||||
(void)CheckAndConvertUtils::CheckInteger("input rank", SizeToLong(input_shape.size()), kEqual, shape_size, prim_name);
|
||||
std::vector<int64_t> out_shape = {input_shape[0], input_shape[1]};
|
||||
auto size = GetValue<std::vector<int64_t>>(primitive->GetAttr(kSize));
|
||||
(void)out_shape.insert(out_shape.end(), size.begin(), size.end());
|
||||
|
||||
// Infer type
|
||||
const std::set<TypePtr> valid_types = {kFloat16, kFloat32};
|
||||
(void)CheckAndConvertUtils::CheckTensorTypeValid("input_type", input_args[0]->BuildType(), valid_types, prim_name);
|
||||
return std::make_shared<abstract::AbstractTensor>(input_args[0]->BuildType(), out_shape);
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameResizeBilinear, ResizeBilinear);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -35,38 +35,7 @@ int64_t ReverseSequence::get_batch_dim() const {
|
|||
auto value_ptr = this->GetAttr(kBatchDim);
|
||||
return GetValue<int64_t>(value_ptr);
|
||||
}
|
||||
AbstractBasePtr ReverseSequenceInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
const int64_t input_num = 2;
|
||||
(void)CheckAndConvertUtils::CheckInteger("input numbers", SizeToLong(input_args.size()), kEqual, input_num,
|
||||
prim_name);
|
||||
for (const auto &item : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(item);
|
||||
}
|
||||
// infer shape
|
||||
auto input_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
auto seq_lengths = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[1]->BuildShape())[kShape];
|
||||
auto seq_dim = GetValue<int64_t>(primitive->GetAttr(kSeqDim));
|
||||
auto batch_dim = GetValue<int64_t>(primitive->GetAttr(kBatchDim));
|
||||
(void)CheckAndConvertUtils::CheckInteger("seq_dim", seq_dim, kLessEqual, SizeToLong(input_shape.size()), prim_name);
|
||||
(void)CheckAndConvertUtils::CheckInteger("batch_dim", batch_dim, kLessEqual, SizeToLong(input_shape.size()),
|
||||
prim_name);
|
||||
(void)CheckAndConvertUtils::CheckInteger("batch_dim", batch_dim, kNotEqual, seq_dim, prim_name);
|
||||
(void)CheckAndConvertUtils::CheckInteger("seq_lengths rank", SizeToLong(seq_lengths.size()), kEqual, 1, prim_name);
|
||||
(void)CheckAndConvertUtils::CheckInteger("seq_lengths vector size", seq_lengths[0], kEqual,
|
||||
input_shape[LongToSize(batch_dim)], prim_name);
|
||||
// infer type
|
||||
std::set<TypePtr> valid_x_types(common_valid_types);
|
||||
(void)valid_x_types.emplace(kBool);
|
||||
const std::set<TypePtr> valid_seq_types = {kInt32, kInt64};
|
||||
auto x_type = input_args[0]->BuildType()->cast<TensorTypePtr>()->element();
|
||||
auto seq_type = input_args[1]->BuildType()->cast<TensorTypePtr>()->element();
|
||||
auto infered_type = CheckAndConvertUtils::CheckTensorTypeValid("x_type", x_type, valid_x_types, prim_name);
|
||||
(void)CheckAndConvertUtils::CheckTensorTypeValid("seq_type", seq_type, valid_seq_types, prim_name);
|
||||
return std::make_shared<abstract::AbstractTensor>(infered_type, input_shape);
|
||||
}
|
||||
|
||||
REGISTER_PRIMITIVE_C(kNameReverseSequence, ReverseSequence);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -21,24 +21,6 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
namespace {
|
||||
abstract::ShapePtr ReverseV2InferShape(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto x_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
return std::make_shared<abstract::Shape>(x_shape);
|
||||
}
|
||||
|
||||
TypePtr ReverseV2InferType(const PrimitivePtr &prim, const std::vector<AbstractBasePtr> &input_args) {
|
||||
for (const auto &item : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(item);
|
||||
}
|
||||
const std::set<TypePtr> valid_types = {kInt8, kInt16, kInt32, kInt64, kUInt8, kUInt16,
|
||||
kUInt32, kUInt64, kFloat16, kFloat32, kFloat64, kBool};
|
||||
auto infer_type = input_args[0]->BuildType();
|
||||
return CheckAndConvertUtils::CheckTensorTypeValid("x type", infer_type, valid_types, prim->name());
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void ReverseV2::Init(const std::vector<int64_t> &axis) { this->set_axis(axis); }
|
||||
void ReverseV2::set_axis(const std::vector<int64_t> &axis) { (void)this->AddAttr(kAxis, MakeValue(axis)); }
|
||||
std::vector<int64_t> ReverseV2::get_axis() const {
|
||||
|
|
@ -46,11 +28,6 @@ std::vector<int64_t> ReverseV2::get_axis() const {
|
|||
return GetValue<std::vector<int64_t>>(value_ptr);
|
||||
}
|
||||
|
||||
AbstractBasePtr ReverseV2Infer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
return std::make_shared<abstract::AbstractTensor>(ReverseV2InferType(primitive, input_args),
|
||||
ReverseV2InferShape(primitive, input_args)->shape());
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameReverseV2, ReverseV2);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -21,36 +21,12 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
namespace {
|
||||
abstract::ShapePtr RfftInferShape(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto first_input_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
auto out_shape = first_input_shape;
|
||||
out_shape[out_shape.size() - 1] = GetValue<int64_t>(primitive->GetAttr(kFftLength)) / 2 + 1;
|
||||
out_shape.push_back(2);
|
||||
return std::make_shared<abstract::Shape>(out_shape);
|
||||
}
|
||||
|
||||
TypePtr RfftInferType(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
for (const auto &item : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(item);
|
||||
}
|
||||
return kComplex64;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void Rfft::Init(const int64_t fft_length) { this->set_fft_length(fft_length); }
|
||||
|
||||
void Rfft::set_fft_length(const int64_t fft_length) { (void)this->AddAttr(kFftLength, MakeValue(fft_length)); }
|
||||
|
||||
int64_t Rfft::get_fft_length() const { return GetValue<int64_t>(GetAttr(kFftLength)); }
|
||||
|
||||
AbstractBasePtr RfftInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
return std::make_shared<abstract::AbstractTensor>(RfftInferType(primitive, input_args),
|
||||
RfftInferShape(primitive, input_args)->shape());
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameRfft, Rfft);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -48,31 +48,7 @@ void ROIPooling::Init(const int64_t pooled_h, const int64_t pooled_w, const floa
|
|||
this->set_pooled_w(pooled_w);
|
||||
this->set_scale(scale);
|
||||
}
|
||||
AbstractBasePtr ROIPoolingInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
const int64_t input_num = 2;
|
||||
(void)CheckAndConvertUtils::CheckInteger("infer", SizeToLong(input_args.size()), kEqual, input_num, prim_name);
|
||||
MS_EXCEPTION_IF_NULL(input_args[0]);
|
||||
MS_EXCEPTION_IF_NULL(input_args[1]);
|
||||
|
||||
// Infer type
|
||||
auto output_data_type = input_args[0]->BuildType()->cast<TensorTypePtr>()->element();
|
||||
|
||||
// Infer shape
|
||||
auto new_h = GetValue<int64_t>(primitive->GetAttr(kPooledH));
|
||||
auto new_w = GetValue<int64_t>(primitive->GetAttr(kPooledW));
|
||||
auto input_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
auto roi_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[1]->BuildShape())[kShape];
|
||||
std::vector<int64_t> output_shape;
|
||||
output_shape.push_back(roi_shape[0]);
|
||||
output_shape.push_back(new_h);
|
||||
output_shape.push_back(new_w);
|
||||
output_shape.push_back(input_shape[1]);
|
||||
|
||||
return std::make_shared<abstract::AbstractTensor>(output_data_type, std::make_shared<abstract::Shape>(output_shape));
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameROIPooling, ROIPooling);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -22,39 +22,6 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
namespace {
|
||||
abstract::ShapePtr ScatterNdInferShape(const std::vector<AbstractBasePtr> &input_args) {
|
||||
auto shape_value = input_args[kInputIndex2]->BuildValue();
|
||||
auto shape_value_element = GetValue<std::vector<int64_t>>(shape_value);
|
||||
for (const auto &shape : shape_value_element) {
|
||||
(void)CheckAndConvertUtils::CheckInteger("shape value", shape, kGreaterThan, 0, "ScatterNd");
|
||||
}
|
||||
auto indices_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[kInputIndex0]->BuildShape())[kShape];
|
||||
auto update_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[kInputIndex1]->BuildShape())[kShape];
|
||||
(void)CheckAndConvertUtils::CheckInteger("indices_shape[0] and update_shape[0]", indices_shape[0], kEqual,
|
||||
update_shape[0], "ScatterNd");
|
||||
return std::make_shared<abstract::Shape>(shape_value_element);
|
||||
}
|
||||
|
||||
TypePtr ScatterNdInferType(const PrimitivePtr &prim, const std::vector<AbstractBasePtr> &input_args) {
|
||||
for (const auto &item : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(item);
|
||||
}
|
||||
const std::set<TypePtr> indices_valid_types = {kInt32, kInt64};
|
||||
const std::set<TypePtr> update_valid_types = {kTensorType};
|
||||
auto indices_type = input_args[0]->BuildType();
|
||||
auto update_type = input_args[1]->BuildType();
|
||||
(void)CheckAndConvertUtils::CheckTypeValid("update type", update_type, update_valid_types, prim->name());
|
||||
(void)CheckAndConvertUtils::CheckTensorTypeValid("indices type", indices_type, indices_valid_types, prim->name());
|
||||
return input_args[1]->BuildType();
|
||||
}
|
||||
} // namespace
|
||||
|
||||
AbstractBasePtr ScatterNdInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
return std::make_shared<abstract::AbstractTensor>(ScatterNdInferType(primitive, input_args),
|
||||
ScatterNdInferShape(input_args)->shape());
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameScatterNd, ScatterNd);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -20,27 +20,6 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
namespace {
|
||||
abstract::ShapePtr SkipGramInferShape(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
if (input_args.size() != 1) {
|
||||
MS_LOG(ERROR) << "For " << primitive->name() << ", it should have one input, but got " << input_args.size()
|
||||
<< " input.";
|
||||
}
|
||||
auto infer_value = input_args[0]->BuildValue();
|
||||
if (infer_value == nullptr) {
|
||||
MS_LOG(INFO) << " Do '" << primitive->name() << "' operator infer shape in runtime.";
|
||||
}
|
||||
auto in_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
return std::make_shared<abstract::Shape>(in_shape);
|
||||
}
|
||||
|
||||
TypePtr SkipGramInferType(const std::vector<AbstractBasePtr> &input_args) {
|
||||
auto infer_type = input_args[0]->BuildType();
|
||||
return infer_type;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void SkipGram::set_include_all_grams(const bool include_all_grams) {
|
||||
(void)AddAttr(kIncludeALLGrams, MakeValue(include_all_grams));
|
||||
}
|
||||
|
|
@ -64,11 +43,6 @@ void SkipGram::Init(const bool include_all_grams, const int64_t max_skip_size, c
|
|||
this->set_ngram_size(ngram_size);
|
||||
}
|
||||
|
||||
AbstractBasePtr SkipGramInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
return std::make_shared<abstract::AbstractTensor>(SkipGramInferType(input_args),
|
||||
SkipGramInferShape(primitive, input_args)->shape());
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameSkipGram, SkipGram);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -25,39 +25,6 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
namespace {
|
||||
abstract::ShapePtr SpaceToBatchInferShape(const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
auto input_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
const int64_t x_rank = 4;
|
||||
(void)CheckAndConvertUtils::CheckInteger("x rank", SizeToLong(input_shape.size()), kEqual, x_rank, prim_name);
|
||||
std::vector<int64_t> output_shape(input_shape.size());
|
||||
auto block_shape_vector = GetValue<std::vector<int64_t>>(primitive->GetAttr(kBlockSize));
|
||||
auto paddings = GetValue<std::vector<std::vector<int64_t>>>(primitive->GetAttr(kPaddings));
|
||||
const size_t kDimsOffset = 2;
|
||||
for (size_t i = 0; i < kDimsOffset; i++) {
|
||||
auto padded = output_shape[i + kDimsOffset] + paddings[i][0] + paddings[i][1];
|
||||
const int64_t input_num = 0;
|
||||
(void)CheckAndConvertUtils::CheckInteger("padded shape", SizeToLong(padded % block_shape_vector.size()), kEqual,
|
||||
input_num, prim_name);
|
||||
output_shape[i + kDimsOffset] = padded / SizeToLong(block_shape_vector.size());
|
||||
}
|
||||
output_shape[0] *= SizeToLong(block_shape_vector.size() * block_shape_vector.size());
|
||||
return std::make_shared<abstract::Shape>(output_shape);
|
||||
}
|
||||
|
||||
TypePtr SpaceToBatchInferType(const PrimitivePtr &prim, const std::vector<AbstractBasePtr> &input_args) {
|
||||
if (std::any_of(input_args.begin(), input_args.end(), [](const AbstractBasePtr arg) { return arg == nullptr; })) {
|
||||
MS_LOG(EXCEPTION) << "For '" << prim->name()
|
||||
<< ", the input args userd for infer shape and type, can not be a nullptr.";
|
||||
}
|
||||
std::map<std::string, TypePtr> types;
|
||||
(void)types.emplace("x", input_args[0]->BuildType());
|
||||
return CheckAndConvertUtils::CheckTensorTypeSame(types, common_valid_types, prim->name());
|
||||
}
|
||||
} // namespace
|
||||
void SpaceToBatch::set_paddings(const std::vector<std::vector<int64_t>> &paddings) {
|
||||
(void)this->AddAttr(kPaddings, MakeValue(paddings));
|
||||
int64_t h = SizeToLong(paddings.size());
|
||||
|
|
@ -87,11 +54,7 @@ void SpaceToBatch::Init(const std::vector<int64_t> block_size, const std::vector
|
|||
this->set_paddings(paddings);
|
||||
this->set_block_size(block_size);
|
||||
}
|
||||
AbstractBasePtr SpaceToBatchInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
return std::make_shared<abstract::AbstractTensor>(SpaceToBatchInferType(primitive, input_args),
|
||||
SpaceToBatchInferShape(primitive, input_args));
|
||||
}
|
||||
|
||||
REGISTER_PRIMITIVE_C(kNameSpaceToBatch, SpaceToBatch);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -25,42 +25,6 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
namespace {
|
||||
abstract::ShapePtr SpaceToBatchNDInferShape(const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
auto x_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
const int64_t shape_size = 4;
|
||||
(void)CheckAndConvertUtils::CheckInteger("input_x rank", SizeToLong(x_shape.size()), kEqual, shape_size, prim_name);
|
||||
auto out_shape = x_shape;
|
||||
int64_t block_shape_prod = 1;
|
||||
const size_t offset = 2;
|
||||
auto block_shape = GetValue<std::vector<int64_t>>(primitive->GetAttr(kBlockShape));
|
||||
auto padding = GetValue<std::vector<std::vector<int64_t>>>(primitive->GetAttr(kPaddings));
|
||||
size_t size = block_shape.size();
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
int64_t padded = out_shape[i + offset] + padding[i][0] + padding[i][1];
|
||||
if (padded % block_shape[i] != 0) {
|
||||
MS_EXCEPTION(ValueError) << prim_name << " padded[" << i << "]" << padded << "should be divisible by block_shape["
|
||||
<< i << "]" << block_shape[i];
|
||||
}
|
||||
out_shape[i + offset] = int64_t(floor(padded / static_cast<float>(block_shape[i])));
|
||||
block_shape_prod = block_shape_prod * block_shape[i];
|
||||
}
|
||||
out_shape[0] = out_shape[0] * block_shape_prod;
|
||||
return std::make_shared<abstract::Shape>(out_shape);
|
||||
}
|
||||
|
||||
TypePtr SpaceToBatchNDInferType(const PrimitivePtr &prim, const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(prim);
|
||||
for (const auto &item : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(item);
|
||||
}
|
||||
return input_args[0]->BuildType()->cast<TensorTypePtr>()->element();
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void SpaceToBatchND::set_paddings(std::vector<std::vector<int64_t>> paddings) {
|
||||
const int64_t pad_size = 2;
|
||||
(void)CheckAndConvertUtils::CheckInteger(kPaddings, SizeToLong(paddings.size()), kEqual, pad_size, this->name());
|
||||
|
|
@ -99,11 +63,6 @@ void SpaceToBatchND::Init(const std::vector<int64_t> block_shape, const std::vec
|
|||
this->set_block_shape(block_shape);
|
||||
}
|
||||
|
||||
AbstractBasePtr SpaceToBatchNDInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
return std::make_shared<abstract::AbstractTensor>(SpaceToBatchNDInferType(primitive, input_args),
|
||||
SpaceToBatchNDInferShape(primitive, input_args)->shape());
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameSpaceToBatchND, SpaceToBatchND);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -33,29 +33,6 @@ void SparseSoftmaxCrossEntropyWithLogits::set_is_grad(const bool is_grad) {
|
|||
|
||||
bool SparseSoftmaxCrossEntropyWithLogits::get_is_grad() const { return GetValue<bool>(GetAttr(kIsGrad)); }
|
||||
|
||||
AbstractBasePtr SparseSoftmaxCrossEntropyWithLogitsInfer(const abstract::AnalysisEnginePtr &,
|
||||
const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
const int64_t input_num = 2;
|
||||
(void)CheckAndConvertUtils::CheckInteger("input numbers", SizeToLong(input_args.size()), kEqual, input_num,
|
||||
prim_name);
|
||||
for (const auto &item : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(item);
|
||||
}
|
||||
// infer shape
|
||||
auto input_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
std::vector<int64_t> output_shape;
|
||||
if (GetValue<bool>(primitive->GetAttr(kIsGrad)) != 0) {
|
||||
output_shape = input_shape;
|
||||
} else {
|
||||
output_shape.push_back(1);
|
||||
}
|
||||
// infer type
|
||||
auto output_type = input_args[0]->BuildType()->cast<TensorTypePtr>()->element();
|
||||
return std::make_shared<abstract::AbstractTensor>(output_type, output_shape);
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameSparseSoftmaxCrossEntropyWithLogits, SparseSoftmaxCrossEntropyWithLogits);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -24,21 +24,6 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
AbstractBasePtr SparseToDenseInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
const int64_t input_num = 3;
|
||||
(void)CheckAndConvertUtils::CheckInteger("input number", SizeToLong(input_args.size()), kEqual, input_num, prim_name);
|
||||
for (const auto &item : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(item);
|
||||
}
|
||||
// infer shape
|
||||
auto dense_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[kInputIndex3]->BuildShape())[kShape];
|
||||
// infer type
|
||||
auto values_type = input_args[1]->BuildType()->cast<TensorTypePtr>()->element();
|
||||
return std::make_shared<abstract::AbstractTensor>(values_type, dense_shape);
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameSparseToDense, SparseToDense);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -18,63 +18,12 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
namespace {
|
||||
abstract::AbstractBasePtr StackInfer(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
|
||||
if (input_args.size() <= 1) {
|
||||
MS_LOG(ERROR) << "For '" << primitive->name()
|
||||
<< "', input args size should be greater than 1, but got invalid input args size:"
|
||||
<< input_args.size();
|
||||
}
|
||||
for (const auto &item : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(item);
|
||||
}
|
||||
auto input_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
for (size_t i = 1; i < input_args.size(); ++i) {
|
||||
auto input_shape_tmp = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[i]->BuildShape())[kShape];
|
||||
if (input_shape_tmp.size() != input_shape.size()) {
|
||||
MS_LOG(ERROR) << "For '" << primitive->name()
|
||||
<< "', all input shape size should be the same, but got input[0] size = " << input_shape.size()
|
||||
<< ", input[" << i << "] size = " << input_shape_tmp.size();
|
||||
}
|
||||
for (size_t j = 0; j < input_shape.size(); ++j) {
|
||||
if (input_shape_tmp.at(j) != input_shape.at(j)) {
|
||||
MS_LOG(ERROR) << "For '" << primitive->name()
|
||||
<< "', all input shape should be the same, but got input[0] shape = " << input_shape.at(j)
|
||||
<< ", input[" << i << "] shape = " << input_shape_tmp.at(j);
|
||||
}
|
||||
}
|
||||
}
|
||||
std::vector<int64_t> infer_shape = input_shape;
|
||||
(void)infer_shape.insert(infer_shape.begin() + GetValue<int64_t>(primitive->GetAttr(kAxis)), input_args.size());
|
||||
|
||||
auto infer_type0 = input_args[0]->BuildType()->cast<TensorTypePtr>()->element();
|
||||
for (size_t i = 1; i < input_args.size(); i++) {
|
||||
if (input_args[i]->BuildType()->cast<TensorTypePtr>()->element() == infer_type0) {
|
||||
MS_LOG(ERROR) << "For '" << primitive->name()
|
||||
<< "', all input should have the same data type, but input[0] data type = " << infer_type0
|
||||
<< ", input[" << i
|
||||
<< "] data type = " << input_args[i]->BuildType()->cast<TensorTypePtr>()->element();
|
||||
}
|
||||
}
|
||||
auto infer_type = input_args[0]->BuildType()->cast<TensorTypePtr>()->element();
|
||||
auto output0 = std::make_shared<abstract::AbstractTensor>(infer_type, infer_shape);
|
||||
AbstractBasePtrList output1 = {output0};
|
||||
return std::make_shared<abstract::AbstractTuple>(output1);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void Stack::set_axis(const int64_t axis) { (void)AddAttr(kAxis, MakeValue(axis)); }
|
||||
|
||||
int64_t Stack::get_axis() const { return GetValue<int64_t>(GetAttr(kAxis)); }
|
||||
|
||||
void Stack::Init(const int64_t axis) { this->set_axis(axis); }
|
||||
|
||||
AbstractBasePtr StackInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
return std::make_shared<abstract::AbstractTensor>(StackInfer(primitive, input_args));
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameStack, Stack);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -20,31 +20,6 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
namespace {
|
||||
abstract::ShapePtr TensorListFromTensorInferShape(const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto op_name = primitive->name();
|
||||
const int64_t input_num = 2;
|
||||
CheckAndConvertUtils::CheckInputArgs(input_args, kGreaterEqual, input_num, op_name);
|
||||
|
||||
auto input0_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
if (input0_shape.size() < 1) {
|
||||
MS_LOG(ERROR) << "For '" << op_name << "', input[0] shape size must be greater than 0, but got "
|
||||
<< input0_shape.size() << ".";
|
||||
}
|
||||
int64_t dim0 = input0_shape[0];
|
||||
if (dim0 < 0) {
|
||||
MS_LOG(ERROR) << "For '" << op_name << "', input[0] dim must be greater than or equal to 0, but got " << dim0
|
||||
<< ".";
|
||||
}
|
||||
std::vector<int64_t> infer_shape = {1, dim0};
|
||||
return std::make_shared<abstract::Shape>(infer_shape);
|
||||
}
|
||||
|
||||
TypePtr TensorListFromTensorInferType() { return kTensorType; }
|
||||
} // namespace
|
||||
|
||||
void TensorListFromTensor::Init(const int64_t element_dtype, const int64_t shape_type) {
|
||||
this->set_element_dtype(element_dtype);
|
||||
this->set_shape_type(shape_type);
|
||||
|
|
@ -68,11 +43,6 @@ void TensorListFromTensor::set_shape_type(const int64_t shape_type) {
|
|||
(void)this->AddAttr(kShapeType, MakeValue(shape_type));
|
||||
}
|
||||
|
||||
AbstractBasePtr TensorListFromTensorInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
return std::make_shared<abstract::AbstractTensor>(TensorListFromTensorInferType(),
|
||||
TensorListFromTensorInferShape(primitive, input_args)->shape());
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameTensorListFromTensor, TensorListFromTensor);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -46,24 +46,6 @@ int64_t TensorListStack::get_element_dtype() const {
|
|||
return GetValue<int64_t>(value_ptr);
|
||||
}
|
||||
|
||||
AbstractBasePtr TensorListStackInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
for (const auto &input : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(input);
|
||||
}
|
||||
auto input0_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
int64_t num = std::accumulate(input0_shape.begin(), input0_shape.end(), 1LL, std::multiplies<int64_t>());
|
||||
if (num == 0) {
|
||||
MS_LOG(ERROR) << "For '" << primitive->name() << "', Try to stack a empty tensorlist!";
|
||||
}
|
||||
if (input_args[1]->BuildShape() == nullptr) {
|
||||
MS_LOG(ERROR) << "For '" << primitive->name() << "', the seceond input data shape is nullptr";
|
||||
}
|
||||
auto input1_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[1]->BuildShape())[kShape];
|
||||
(void)input1_shape.insert(input1_shape.begin(), 1);
|
||||
return std::make_shared<abstract::AbstractTensor>(input_args[0]->BuildType(), input1_shape);
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameTensorListStack, TensorListStack);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -28,35 +28,7 @@ bool TopK::get_sorted() const {
|
|||
auto value_ptr = this->GetAttr(kSorted);
|
||||
return GetValue<bool>(value_ptr);
|
||||
}
|
||||
AbstractBasePtr TopKInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
const int64_t input_num = 2;
|
||||
(void)CheckAndConvertUtils::CheckInteger("top_k_infer", SizeToLong(input_args.size()), kEqual, input_num, prim_name);
|
||||
|
||||
// Infer dtype
|
||||
for (const auto &item : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(item);
|
||||
}
|
||||
auto output1_type = kInt32;
|
||||
const std::set<TypePtr> valid_types = {kFloat16, kFloat32};
|
||||
auto output0_type =
|
||||
CheckAndConvertUtils::CheckTensorTypeValid("input_x", input_args[0]->BuildType(), valid_types, prim_name);
|
||||
|
||||
// Infer shape
|
||||
auto x_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
auto k_v = GetValue<int>(input_args[1]->BuildValue());
|
||||
auto ndims = x_shape.size() - 1;
|
||||
std::pair<int64_t, int64_t> k_range(0, x_shape[ndims]);
|
||||
CheckAndConvertUtils::CheckInRange<int64_t>("top_k_infer", static_cast<int64_t>(k_v), kIncludeRight, k_range,
|
||||
prim_name);
|
||||
x_shape[ndims] = k_v;
|
||||
auto output0 = std::make_shared<abstract::AbstractTensor>(output0_type, x_shape);
|
||||
auto output1 = std::make_shared<abstract::AbstractTensor>(output1_type, x_shape);
|
||||
AbstractBasePtrList output = {output0, output1};
|
||||
return std::make_shared<abstract::AbstractTuple>(output);
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameTopK, TopK);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -22,35 +22,6 @@ void Unpack::Init(const int64_t axis) { this->set_axis(axis); }
|
|||
void Unpack::set_axis(const int64_t axis) { (void)AddAttr(kAxis, MakeValue(axis)); }
|
||||
int64_t Unpack::get_axis() const { return GetValue<int64_t>(GetAttr(kAxis)); }
|
||||
|
||||
AbstractBasePtr UnpackInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
MS_EXCEPTION_IF_NULL(input_args[0]);
|
||||
(void)CheckAndConvertUtils::CheckSubClass("x", input_args[0]->BuildType(), {TypeIdToType(kObjectTypeTensorType)},
|
||||
prim_name);
|
||||
auto x_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
int64_t dim = SizeToLong(x_shape.size());
|
||||
int64_t axis = GetValue<int64_t>(primitive->GetAttr(kAxis));
|
||||
if (axis < 0) {
|
||||
axis = axis + dim;
|
||||
}
|
||||
auto output_num = x_shape[LongToSize(axis)];
|
||||
(void)CheckAndConvertUtils::CheckInteger("output_num", output_num, kGreaterThan, 0, prim_name);
|
||||
auto output_valid_check = x_shape[LongToSize(axis)] - output_num;
|
||||
(void)CheckAndConvertUtils::CheckInteger("The dimension which to unpack divides output_num", output_valid_check,
|
||||
kEqual, 0, prim_name);
|
||||
std::vector<int64_t> infer_shape(x_shape.begin(), x_shape.begin() + axis);
|
||||
(void)infer_shape.insert(infer_shape.end(), x_shape.begin() + axis + 1, x_shape.end());
|
||||
AbstractBasePtrList output;
|
||||
auto tensor_type = input_args[0]->BuildType()->cast<TensorTypePtr>();
|
||||
MS_EXCEPTION_IF_NULL(tensor_type);
|
||||
auto element = tensor_type->element();
|
||||
for (int64_t i = 0; i != output_num; i++) {
|
||||
output.push_back(std::make_shared<abstract::AbstractTensor>(element, infer_shape));
|
||||
}
|
||||
return std::make_shared<abstract::AbstractTuple>(output);
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameUnpack, Unpack);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -25,45 +25,6 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
AbstractBasePtr UnsortedSegmentSumInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
|
||||
// Infer type
|
||||
for (const auto &item : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(item);
|
||||
}
|
||||
auto x_type = input_args[0]->BuildType()->cast<TensorTypePtr>()->element();
|
||||
// Infer shape
|
||||
auto x_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
(void)CheckAndConvertUtils::CheckInteger("x_shape", SizeToLong(x_shape.size()), kGreaterThan, 0, prim_name);
|
||||
auto shp = x_shape;
|
||||
auto segment_ids_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[1]->BuildShape())[kShape];
|
||||
(void)CheckAndConvertUtils::CheckInteger("segment_ids_shape", SizeToLong(segment_ids_shape.size()), kGreaterThan, 0,
|
||||
prim_name);
|
||||
CheckAndConvertUtils::Check("input_x", int64_t(x_shape.size()), kGreaterEqual, int64_t(segment_ids_shape.size()),
|
||||
prim_name);
|
||||
|
||||
if ((x_shape.end() != find(x_shape.begin(), x_shape.end(), -1)) &&
|
||||
(segment_ids_shape.end() != find(segment_ids_shape.begin(), segment_ids_shape.end(), -1))) {
|
||||
size_t size = segment_ids_shape.size();
|
||||
for (size_t i = 0; i < size; ++i) {
|
||||
CheckAndConvertUtils::Check("segment_ids_shp", segment_ids_shape[i], kEqual, x_shape[i], prim_name);
|
||||
}
|
||||
}
|
||||
|
||||
const std::set<TypePtr> valid_num_segments_types = {kInt32, kInt64};
|
||||
(void)CheckAndConvertUtils::CheckTensorTypeValid("num_segments", input_args[kInputIndex2]->BuildType(),
|
||||
valid_num_segments_types, prim_name);
|
||||
size_t size_segment_ids_shp = segment_ids_shape.size();
|
||||
size_t size_x_shape = x_shape.size();
|
||||
for (size_t i = size_segment_ids_shp; i < size_x_shape; ++i) {
|
||||
(void)shp.emplace_back(x_shape[i]);
|
||||
}
|
||||
|
||||
return std::make_shared<abstract::AbstractTensor>(x_type, shp);
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameUnsortedSegmentSum, UnsortedSegmentSum);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -26,46 +26,7 @@ void Unsqueeze::Init(const std::vector<int64_t> axis) { this->set_axis(axis); }
|
|||
void Unsqueeze::set_axis(const std::vector<int64_t> axis) { (void)this->AddAttr(kAxis, MakeValue(axis)); }
|
||||
|
||||
std::vector<int64_t> Unsqueeze::get_axis() const { return GetValue<std::vector<int64_t>>(GetAttr(kAxis)); }
|
||||
AbstractBasePtr UnsqueezeInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
(void)CheckAndConvertUtils::CheckInteger("unsqueeze_infer", SizeToLong(input_args.size()), kEqual, 1, prim_name);
|
||||
|
||||
MS_EXCEPTION_IF_NULL(input_args[0]);
|
||||
auto input = input_args[0];
|
||||
|
||||
// Infer type
|
||||
auto input_type = input->BuildType()->cast<TensorTypePtr>()->element();
|
||||
|
||||
// Infer shape
|
||||
auto dims = GetValue<std::vector<int64_t>>(primitive->GetAttr(kAxis));
|
||||
auto input_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input->BuildShape())[kShape];
|
||||
auto input_rank = input_shape.size();
|
||||
auto dim_rank = dims.size();
|
||||
std::vector<int64_t> out_shape;
|
||||
if (dim_rank == 0) {
|
||||
(void)std::copy_if(input_shape.begin(), input_shape.end(), out_shape.begin(),
|
||||
[](const auto item) { return item == 1; });
|
||||
} else {
|
||||
auto sz = input_rank + dim_rank;
|
||||
size_t in_itr = 0;
|
||||
size_t ax_itr = 0;
|
||||
for (size_t i = 0; i < sz; i++) {
|
||||
if (ax_itr < dim_rank && dims[ax_itr] == (int64_t)i) {
|
||||
(void)out_shape.emplace_back(1);
|
||||
ax_itr++;
|
||||
} else if (ax_itr < dim_rank && dims[ax_itr] + sz == LongToSize(i)) {
|
||||
(void)out_shape.emplace_back(1);
|
||||
ax_itr++;
|
||||
} else {
|
||||
(void)out_shape.emplace_back(input_shape[in_itr]);
|
||||
in_itr++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return std::make_shared<abstract::AbstractTensor>(input_type, out_shape);
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameUnsqueeze, Unsqueeze);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -21,33 +21,7 @@ namespace ops {
|
|||
void Unstack::Init(const int64_t axis) { this->set_axis(axis); }
|
||||
void Unstack::set_axis(const int64_t axis) { (void)AddAttr(kAxis, MakeValue(axis)); }
|
||||
int64_t Unstack::get_axis() const { return GetValue<int64_t>(GetAttr(kAxis)); }
|
||||
AbstractBasePtr UnstackInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
auto prim_name = primitive->name();
|
||||
MS_EXCEPTION_IF_NULL(input_args[0]);
|
||||
auto x_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
|
||||
int64_t dim = SizeToLong(x_shape.size());
|
||||
int64_t axis = GetValue<int64_t>(primitive->GetAttr(kAxis));
|
||||
if (axis < 0) {
|
||||
axis = axis + dim;
|
||||
}
|
||||
auto output_num = x_shape[LongToSize(axis)];
|
||||
(void)CheckAndConvertUtils::CheckInteger("output_num", output_num, kGreaterThan, 0, prim_name);
|
||||
auto output_valid_check = x_shape[LongToSize(axis)] - output_num;
|
||||
(void)CheckAndConvertUtils::CheckInteger("The dimension which to unstack divides output_num", output_valid_check,
|
||||
kEqual, 0, prim_name);
|
||||
std::vector<int64_t> infer_shape(x_shape.begin(), x_shape.begin() + axis);
|
||||
(void)infer_shape.insert(infer_shape.end(), x_shape.begin() + axis + 1, x_shape.end());
|
||||
AbstractBasePtrList output;
|
||||
auto tensor_type = input_args[0]->BuildType()->cast<TensorTypePtr>();
|
||||
MS_EXCEPTION_IF_NULL(tensor_type);
|
||||
auto element = tensor_type->element();
|
||||
for (int64_t i = 0; i != output_num; i++) {
|
||||
output.push_back(std::make_shared<abstract::AbstractTensor>(element, infer_shape));
|
||||
}
|
||||
return std::make_shared<abstract::AbstractTuple>(output);
|
||||
}
|
||||
|
||||
REGISTER_PRIMITIVE_C(kNameUnstack, Unstack);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
|
|
@ -22,49 +22,6 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace ops {
|
||||
AbstractBasePtr WhereInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
MS_EXCEPTION_IF_NULL(primitive);
|
||||
for (auto input : input_args) {
|
||||
MS_EXCEPTION_IF_NULL(input);
|
||||
}
|
||||
auto op_name = primitive->name();
|
||||
const int64_t input_num = 3;
|
||||
(void)CheckAndConvertUtils::CheckInteger("input numbers", SizeToLong(input_args.size()), kGreaterEqual, input_num,
|
||||
op_name);
|
||||
auto input0_type_ = input_args[kInputIndex0]->BuildType()->cast<TensorTypePtr>();
|
||||
MS_EXCEPTION_IF_NULL(input0_type_);
|
||||
auto input0_type = input0_type_->element();
|
||||
auto input0_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[kInputIndex0]->BuildShape())[kShape];
|
||||
auto num = input_args[kInputIndex0]->BuildValue()->cast<tensor::TensorPtr>()->ElementsNum();
|
||||
auto input1_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[kInputIndex1]->BuildShape())[kShape];
|
||||
auto num1 = input_args[kInputIndex1]->BuildValue()->cast<tensor::TensorPtr>()->ElementsNum();
|
||||
auto input2_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[kInputIndex2]->BuildShape())[kShape];
|
||||
auto num2 = input_args[kInputIndex2]->BuildValue()->cast<tensor::TensorPtr>()->ElementsNum();
|
||||
auto nummax = num > num1 ? num : (num1 > num2 ? num1 : num2);
|
||||
size_t axisout = 0;
|
||||
size_t temp = 0;
|
||||
for (size_t j = 0; j < input0_shape.size(); j++) {
|
||||
if (input0_shape[j] == input1_shape[j] && input0_shape[j] != input2_shape[j]) {
|
||||
axisout = j;
|
||||
break;
|
||||
}
|
||||
if (input0_shape[j] == input2_shape[j] && input0_shape[j] != input1_shape[j]) {
|
||||
axisout = j;
|
||||
break;
|
||||
}
|
||||
if (input1_shape[j] != input2_shape[j] && input0_shape[j] == input1_shape[j]) {
|
||||
axisout = j;
|
||||
break;
|
||||
}
|
||||
temp += 1;
|
||||
if (temp == input0_shape.size()) {
|
||||
return std::make_shared<abstract::AbstractTensor>(input0_type, input0_shape);
|
||||
}
|
||||
}
|
||||
input0_shape[axisout] = (int64_t)nummax;
|
||||
return std::make_shared<abstract::AbstractTensor>(input0_type, input0_shape);
|
||||
}
|
||||
REGISTER_PRIMITIVE_C(kNameWhere, Where);
|
||||
} // namespace ops
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
Loading…
Reference in New Issue