diff --git a/mindspore/ccsrc/frontend/operator/cc_implementations.cc b/mindspore/ccsrc/frontend/operator/cc_implementations.cc index 42f6f2454c2..89b62ec2a3e 100644 --- a/mindspore/ccsrc/frontend/operator/cc_implementations.cc +++ b/mindspore/ccsrc/frontend/operator/cc_implementations.cc @@ -125,7 +125,7 @@ T InnerScalarMul(T x, T y) { template float InnerScalarDiv(T x, T y) { if (y == 0) { - MS_LOG(EXCEPTION) << "Divisor could not be zero"; + MS_LOG(EXCEPTION) << "The divisor could not be zero."; } if (std::is_integral::value && std::is_signed::value && IsSignedIntOverflow(x, y, OpType::DIV)) { MS_LOG(EXCEPTION) << "Overflow of the div of two signed number x: " << std::to_string(x) @@ -199,7 +199,7 @@ bool InnerScalarGe(T x, U y) { ValuePtr Scalar##op_t(const ValuePtrList &list) { \ do { \ if (list.size() < 2) { \ - MS_LOG(EXCEPTION) << "length of input list for Scalar" << #op_t << " is less than 2."; \ + MS_LOG(EXCEPTION) << "The length of input list for Scalar" << #op_t << " is less than 2."; \ } \ ValuePtr x = list[0]; \ ValuePtr y = list[1]; \ @@ -267,73 +267,75 @@ SCALAR_OP(Mod) SCALAR_OP(Pow) SCALAR_OP(Floordiv) -#define LOGIC_OP(op_t) \ - ValuePtr Scalar##op_t(const ValuePtrList &list) { \ - if (list.size() < 2) { \ - MS_LOG(EXCEPTION) << "length of input list for Scalar" << #op_t << " is less than 2."; \ - } \ - ValuePtr x = list[0]; \ - ValuePtr y = list[1]; \ - MS_EXCEPTION_IF_NULL(x); \ - MS_EXCEPTION_IF_NULL(y); \ - if (x->isa() && y->isa()) { \ - bool sum = InnerScalar##op_t(GetValue(x), GetValue(y)); \ - return MakeValue(sum); \ - } \ - if (x->isa() && y->isa()) { \ - bool sum = InnerScalar##op_t(GetValue(x), GetValue(y)); \ - return MakeValue(sum); \ - } \ - if (x->isa() && y->isa()) { \ - bool sum = InnerScalar##op_t(GetValue(x), GetValue(y)); \ - return MakeValue(sum); \ - } \ - if (x->isa() && y->isa()) { \ - bool sum = InnerScalar##op_t(GetValue(x), GetValue(y)); \ - return MakeValue(sum); \ - } \ - if (x->isa() && y->isa()) { \ - bool sum = InnerScalar##op_t(GetValue(x), GetValue(y)); \ - return MakeValue(sum); \ - } \ - if (x->isa() && y->isa()) { \ - bool sum = InnerScalar##op_t(GetValue(x), GetValue(y)); \ - return MakeValue(sum); \ - } \ - if (x->isa() && y->isa()) { \ - bool sum = InnerScalar##op_t(GetValue(x), GetValue(y)); \ - return MakeValue(sum); \ - } \ - if (x->isa() && y->isa()) { \ - bool sum = InnerScalar##op_t(GetValue(x), GetValue(y)); \ - return MakeValue(sum); \ - } \ - if (x->isa() && y->isa()) { \ - bool sum = InnerScalar##op_t(GetValue(x), GetValue(y)); \ - return MakeValue(sum); \ - } \ - if (x->isa() && y->isa()) { \ - bool sum = InnerScalar##op_t(GetValue(x), GetValue(y)); \ - return MakeValue(sum); \ - } \ - if (x->isa() && y->isa()) { \ - bool sum = InnerScalar##op_t(GetValue(x), GetValue(y)); \ - return MakeValue(sum); \ - } \ - if (x->isa() && y->isa()) { \ - bool sum = InnerScalar##op_t(GetValue(x), GetValue(y)); \ - return MakeValue(sum); \ - } \ - if (x->isa() && y->isa()) { \ - bool sum = InnerScalar##op_t(GetValue(x), GetValue(y)); \ - return MakeValue(sum); \ - } \ - if (x->isa() && y->isa()) { \ - bool sum = InnerScalar##op_t(GetValue(x), GetValue(y)); \ - return MakeValue(sum); \ - } \ - MS_LOG(EXCEPTION) << "Unsupported Value for Scalar" << #op_t << ", x: " << x->ToString() \ - << ", y: " << y->ToString() << "."; \ +#define LOGIC_OP(op_t) \ + ValuePtr Scalar##op_t(const ValuePtrList &list) { \ + constexpr size_t kListInputSize = 2; \ + if (list.size() < kListInputSize) { \ + MS_LOG(EXCEPTION) << "The length of input list for Scalar" << #op_t << " is less than 2."; \ + } \ + ValuePtr x = list[0]; \ + ValuePtr y = list[1]; \ + MS_EXCEPTION_IF_NULL(x); \ + MS_EXCEPTION_IF_NULL(y); \ + if (x->isa() && y->isa()) { \ + bool sum = InnerScalar##op_t(GetValue(x), GetValue(y)); \ + return MakeValue(sum); \ + } \ + if (x->isa() && y->isa()) { \ + bool sum = InnerScalar##op_t(GetValue(x), GetValue(y)); \ + return MakeValue(sum); \ + } \ + if (x->isa() && y->isa()) { \ + bool sum = InnerScalar##op_t(GetValue(x), GetValue(y)); \ + return MakeValue(sum); \ + } \ + if (x->isa() && y->isa()) { \ + bool sum = InnerScalar##op_t(GetValue(x), GetValue(y)); \ + return MakeValue(sum); \ + } \ + if (x->isa() && y->isa()) { \ + bool sum = InnerScalar##op_t(GetValue(x), GetValue(y)); \ + return MakeValue(sum); \ + } \ + if (x->isa() && y->isa()) { \ + bool sum = InnerScalar##op_t(GetValue(x), GetValue(y)); \ + return MakeValue(sum); \ + } \ + if (x->isa() && y->isa()) { \ + bool sum = InnerScalar##op_t(GetValue(x), GetValue(y)); \ + return MakeValue(sum); \ + } \ + if (x->isa() && y->isa()) { \ + bool sum = InnerScalar##op_t(GetValue(x), GetValue(y)); \ + return MakeValue(sum); \ + } \ + if (x->isa() && y->isa()) { \ + bool sum = InnerScalar##op_t(GetValue(x), GetValue(y)); \ + return MakeValue(sum); \ + } \ + if (x->isa() && y->isa()) { \ + bool sum = InnerScalar##op_t(GetValue(x), GetValue(y)); \ + return MakeValue(sum); \ + } \ + if (x->isa() && y->isa()) { \ + bool sum = InnerScalar##op_t(GetValue(x), GetValue(y)); \ + return MakeValue(sum); \ + } \ + if (x->isa() && y->isa()) { \ + bool sum = InnerScalar##op_t(GetValue(x), GetValue(y)); \ + return MakeValue(sum); \ + } \ + if (x->isa() && y->isa()) { \ + bool sum = InnerScalar##op_t(GetValue(x), GetValue(y)); \ + return MakeValue(sum); \ + } \ + if (x->isa() && y->isa()) { \ + bool sum = InnerScalar##op_t(GetValue(x), GetValue(y)); \ + return MakeValue(sum); \ + } \ + MS_LOG(EXCEPTION) << "Unsupported input type for Scalar" << #op_t << ", type of x:" << x->type_name() \ + << ", value of x:" << x->ToString() << ", type of y:" << y->type_name() \ + << ", value of y:" << y->ToString(); \ } LOGIC_OP(Eq) @@ -372,12 +374,12 @@ ValuePtr ScalarUSub(const ValuePtrList &list) { return MakeValue(sum); } - MS_LOG(EXCEPTION) << "Unsported Value for ScalarUSub, x: " << x->ToString() << "."; + MS_LOG(EXCEPTION) << "Unsupported Value for ScalarUSub, x: " << x->ToString() << "."; } ValuePtr ScalarLog(const ValuePtrList &list) { - if (list.empty()) { - MS_LOG(EXCEPTION) << "Input list of ScalarLog is empty."; + if (list.size() != 1) { + MS_LOG(EXCEPTION) << "Input number of ScalarLog should be 1, but got " << list.size(); } ValuePtr x = list[0]; MS_EXCEPTION_IF_NULL(x); @@ -391,12 +393,12 @@ ValuePtr ScalarLog(const ValuePtrList &list) { return MakeValue(v); } - MS_LOG(EXCEPTION) << "Unsported Value for ScalarLog, x: " << x->ToString(); + MS_LOG(EXCEPTION) << "Unsupported Value for ScalarLog, x: " << x->ToString(); } ValuePtr BoolNot(const ValuePtrList &list) { - if (list.empty()) { - MS_LOG(EXCEPTION) << "value list of BoolNot is empty"; + if (list.size() != 1) { + MS_LOG(EXCEPTION) << "Input number of BoolNot should be 1, but got " << list.size(); } ValuePtr x = list[0]; MS_EXCEPTION_IF_NULL(x); @@ -407,12 +409,13 @@ ValuePtr BoolNot(const ValuePtrList &list) { return MakeValue(res); } - MS_LOG(EXCEPTION) << "Unsported Value for BoolNot, x: " << x->ToString(); + MS_LOG(EXCEPTION) << "Unsupported Value for BoolNot, x: " << x->ToString(); } ValuePtr BoolAnd(const ValuePtrList &list) { - if (list.size() < 2) { - MS_LOG(EXCEPTION) << "Input number " << list.size() << " of BoolAnd is less then 2."; + constexpr size_t kListInputSize = 2; + if (list.size() != kListInputSize) { + MS_LOG(EXCEPTION) << "Input number of BoolAnd must be 2, but got " << list.size(); } ValuePtr x = list[0]; ValuePtr y = list[1]; @@ -426,12 +429,13 @@ ValuePtr BoolAnd(const ValuePtrList &list) { return MakeValue(res); } - MS_LOG(EXCEPTION) << "Unsported Value for BoolAnd, x: " << x->ToString() << "."; + MS_LOG(EXCEPTION) << "Unsupported Value for BoolAnd, x: " << x->ToString() << " y: " << y->ToString() << "."; } ValuePtr BoolOr(const ValuePtrList &list) { - if (list.size() < 2) { - MS_LOG(EXCEPTION) << "Input number " << list.size() << " of BoolOr is less then 2."; + constexpr size_t kListInputSize = 2; + if (list.size() != kListInputSize) { + MS_LOG(EXCEPTION) << "Input number of BoolOr must be 2, but got " << list.size(); } ValuePtr x = list[0]; ValuePtr y = list[1]; @@ -445,12 +449,13 @@ ValuePtr BoolOr(const ValuePtrList &list) { return MakeValue(res); } - MS_LOG(EXCEPTION) << "Unsported Value for BoolOr, x: " << x->ToString() << "."; + MS_LOG(EXCEPTION) << "Unsupported Value for BoolOr, x: " << x->ToString() << " y: " << y->ToString() << "."; } ValuePtr BoolEq(const ValuePtrList &list) { - if (list.size() < 2) { - MS_LOG(EXCEPTION) << "Input number " << list.size() << " of BoolEq is less than 2."; + constexpr size_t kListInputSize = 2; + if (list.size() != kListInputSize) { + MS_LOG(EXCEPTION) << "Input number of BoolEq must be 2, but got " << list.size(); } ValuePtr x = list[0]; ValuePtr y = list[1]; @@ -464,7 +469,7 @@ ValuePtr BoolEq(const ValuePtrList &list) { return MakeValue(res); } - MS_LOG(EXCEPTION) << "Unsported Value for BoolEq, x: " << x->ToString() << "."; + MS_LOG(EXCEPTION) << "Unsupported Value for BoolEq, x: " << x->ToString() << " y: " << y->ToString() << "."; } } // namespace prim } // namespace mindspore diff --git a/mindspore/ccsrc/frontend/operator/composite/composite.cc b/mindspore/ccsrc/frontend/operator/composite/composite.cc index 20e88c77a1f..22919eef69d 100644 --- a/mindspore/ccsrc/frontend/operator/composite/composite.cc +++ b/mindspore/ccsrc/frontend/operator/composite/composite.cc @@ -131,7 +131,7 @@ AnfNodePtr HyperMap::FullMake(const std::shared_ptr &type, const FuncGraph return false; }); if (is_not_same) { - MS_LOG(EXCEPTION) << "List in HyperMap should have same length"; + MS_LOG(EXCEPTION) << "List in HyperMap should have same length."; } // cannot use shared_from_base() also known as this, as it will make a reference cycle on @@ -189,7 +189,7 @@ AnfNodePtr HyperMap::FullMake(const std::shared_ptr &type, const FuncGrap return false; }); if (is_not_same) { - MS_LOG(EXCEPTION) << "Tuple in HyperMap should have same length"; + MS_LOG(EXCEPTION) << "Tuple in HyperMap should have same length."; } // cannot use shared_from_base() also known as this, as it will make a reference cycle on @@ -469,7 +469,7 @@ FuncGraphPtr Tail::GenerateFuncGraph(const AbstractBasePtrList &args_spec_list) return GenerateSequeueFuncGraph(a->cast()); } - MS_LOG(EXCEPTION) << "arg0 must be AbstractTuple or AbstractList, but: " << a->ToString(); + MS_LOG(EXCEPTION) << "'Tail' arg0 must be AbstractTuple or AbstractList, but: " << a->ToString(); } REGISTER_PYBIND_DEFINE( diff --git a/mindspore/ccsrc/frontend/operator/composite/do_signature.cc b/mindspore/ccsrc/frontend/operator/composite/do_signature.cc index adacc66ded1..fb834f155c3 100644 --- a/mindspore/ccsrc/frontend/operator/composite/do_signature.cc +++ b/mindspore/ccsrc/frontend/operator/composite/do_signature.cc @@ -237,7 +237,7 @@ void DoAutoCast(const std::string &func_name, const std::vector &sign if ((arg_value->isa()) && arg_type_id == it->second) { continue; } - MS_LOG(DEBUG) << "do cast for inputs " << i << " " << (*op_inputs)[i + 1]->ToString() << " " << arg_type_id + MS_LOG(DEBUG) << "Do cast for inputs " << i << " " << (*op_inputs)[i + 1]->ToString() << " " << arg_type_id << " to " << it->second; (*op_inputs)[i + 1] = DoCast((*op_inputs)[i + 1], it->second, graph); } @@ -339,7 +339,7 @@ void RaiseExceptionForConvertRefDtype(const std::string &func_name, const std::s const std::string &target_type) { MS_LOG(EXCEPTION) << "In op '" << func_name << "', \n" << "the type of writable argument is '" << ref_type << "', " - << "but the largest type in the same SignatureEumDtype is '" << target_type + << "but the largest type in the same SignatureEnumDType is '" << target_type << "'. The writable arg type is not equal to the largest type, " << "so can not cast automatically."; } diff --git a/mindspore/ccsrc/frontend/operator/composite/map.cc b/mindspore/ccsrc/frontend/operator/composite/map.cc index 5b91552aa23..37b086c8252 100644 --- a/mindspore/ccsrc/frontend/operator/composite/map.cc +++ b/mindspore/ccsrc/frontend/operator/composite/map.cc @@ -88,7 +88,7 @@ AnfNodePtr Map::FullMakeList(const std::shared_ptr &type, const FuncGraphP return false; }); if (is_not_same) { - MS_LOG(EXCEPTION) << "List in Map should have same length"; + MS_LOG(EXCEPTION) << "List in Map should have same length."; } constexpr size_t kPrimHoldLen = 1; @@ -147,7 +147,7 @@ AnfNodePtr Map::FullMakeTuple(const std::shared_ptr &type, const FuncGrap return false; }); if (is_not_same) { - MS_LOG(EXCEPTION) << "tuple in Map should have same length"; + MS_LOG(EXCEPTION) << "Tuple in Map should have same length."; } constexpr size_t kPrimHoldLen = 1; @@ -227,7 +227,7 @@ AnfNodePtr Map::FullMakeClass(const std::shared_ptr &type, const FuncGrap AnfNodePtr Map::Make(const FuncGraphPtr &func_graph, const AnfNodePtr &fn_arg, const ArgsPairList &arg_pairs) { if (arg_pairs.empty()) { - MS_EXCEPTION(TypeError) << "map() must have at least two arguments"; + MS_EXCEPTION(TypeError) << "The map operator must have at least two arguments."; } bool found = false; TypeId id = kObjectTypeEnd; diff --git a/mindspore/ccsrc/frontend/operator/composite/unpack_call.cc b/mindspore/ccsrc/frontend/operator/composite/unpack_call.cc index 5f13e0f15e5..7e2bc0cc099 100644 --- a/mindspore/ccsrc/frontend/operator/composite/unpack_call.cc +++ b/mindspore/ccsrc/frontend/operator/composite/unpack_call.cc @@ -85,7 +85,7 @@ FuncGraphPtr UnpackCall::GenerateFuncGraph(const AbstractBasePtrList &args_spec_ {NewValueNode(prim::kPrimMakeKeywordArg), NewValueNode(item.first), dict_get_item}); }); } else { - MS_LOG(EXCEPTION) << op_name << " require args should be tuple or dict, but got " + MS_LOG(EXCEPTION) << op_name << " require args should be tuple, list or dict, but got " << args_spec_list[index]->ToString(); } } diff --git a/mindspore/ccsrc/frontend/operator/composite/zip_operation.cc b/mindspore/ccsrc/frontend/operator/composite/zip_operation.cc index 4c765ecc87d..c1e5b92bc69 100644 --- a/mindspore/ccsrc/frontend/operator/composite/zip_operation.cc +++ b/mindspore/ccsrc/frontend/operator/composite/zip_operation.cc @@ -43,12 +43,12 @@ FuncGraphPtr ZipOperation::GenerateFuncGraph(const AbstractBasePtrList &args_spe MS_LOG(EXCEPTION) << "For 'zip', there is at least one input."; } - auto is_all_sequeue = + auto all_is_sequence = std::all_of(args_spec_list.begin(), args_spec_list.end(), [](const AbstractBasePtr &abs) -> bool { MS_EXCEPTION_IF_NULL(abs); return abs->isa(); }); - if (!is_all_sequeue) { + if (!all_is_sequence) { MS_LOG(EXCEPTION) << "For 'zip', all inputs must be sequence."; } @@ -62,7 +62,7 @@ FuncGraphPtr ZipOperation::GenerateFuncGraph(const AbstractBasePtrList &args_spe (void)ret_graph->add_parameter(); } - // generate tuple output of ziped arguments input + // generate tuple output of zipped arguments input std::vector make_tuple_nodes; make_tuple_nodes.push_back(NewValueNode(prim::kPrimMakeTuple)); for (size_t idx = 0; idx < (*min_abs)->cast()->size(); idx++) { diff --git a/mindspore/ccsrc/frontend/operator/ops_extends.cc b/mindspore/ccsrc/frontend/operator/ops_extends.cc index 0cab7fa7f5d..87fd1721090 100755 --- a/mindspore/ccsrc/frontend/operator/ops_extends.cc +++ b/mindspore/ccsrc/frontend/operator/ops_extends.cc @@ -26,7 +26,7 @@ ValuePtr GetPythonOps(const std::string &op_name, const std::string &module_name ValuePtr node = nullptr; bool succ = parse::ConvertData(obj, &node, use_signature); if (!succ) { - MS_LOG(EXCEPTION) << "get Python op " << op_name << " from " << module_name << " fail"; + MS_LOG(EXCEPTION) << "Get Python op " << op_name << " from " << module_name << " fail."; } return node; } diff --git a/mindspore/ccsrc/frontend/operator/ops_front_infer_function.cc b/mindspore/ccsrc/frontend/operator/ops_front_infer_function.cc index 09e8a5c53c6..821d5d43c2b 100644 --- a/mindspore/ccsrc/frontend/operator/ops_front_infer_function.cc +++ b/mindspore/ccsrc/frontend/operator/ops_front_infer_function.cc @@ -64,7 +64,7 @@ void CalcSlidePara(const AbstractBasePtrList &args_spec_list, SlideInfo *slide) MS_EXCEPTION_IF_NULL(args_spec_list[0]); auto arg_value = args_spec_list[0]->BuildValue(); if (!arg_value->isa()) { - MS_LOG(EXCEPTION) << "Only supported input an int64 number."; + MS_LOG(EXCEPTION) << "The type of inputs of make_range operator only support int64 number."; } arg1 = GetValue(arg_value); } @@ -73,7 +73,7 @@ void CalcSlidePara(const AbstractBasePtrList &args_spec_list, SlideInfo *slide) MS_EXCEPTION_IF_NULL(args_spec_list[1]); auto arg_value = args_spec_list[1]->BuildValue(); if (!arg_value->isa()) { - MS_LOG(EXCEPTION) << "Only supported input an int64 number."; + MS_LOG(EXCEPTION) << "The type of inputs of make_range operator only support int64 number."; } arg2 = GetValue(arg_value); } @@ -82,7 +82,7 @@ void CalcSlidePara(const AbstractBasePtrList &args_spec_list, SlideInfo *slide) MS_EXCEPTION_IF_NULL(args_spec_list[2]); auto arg_value = args_spec_list[2]->BuildValue(); if (!arg_value->isa()) { - MS_LOG(EXCEPTION) << "Only supported input an int64 number."; + MS_LOG(EXCEPTION) << "The type of inputs of make_range operator only support int64 number."; } slide->step = GetValue(arg_value); slide->start = arg1; @@ -183,11 +183,12 @@ AbstractBasePtr InferImplTypeof(const AnalysisEnginePtr &, const PrimitivePtr &, AbstractBasePtr InferImplHasType(const AnalysisEnginePtr &, const PrimitivePtr &primitive, const AbstractBasePtrList &args_spec_list) { + MS_EXCEPTION_IF_NULL(primitive); // Inputs: a pointer to an AbstractBase object and a pointer to a Type const std::string op_name = primitive->name(); CheckArgsSize(op_name, args_spec_list, 2); AbstractTypePtr abs_type = CheckArg(op_name, args_spec_list, 1); - + MS_EXCEPTION_IF_NULL(abs_type); auto mode_v = abs_type->GetValueTrack(); MS_EXCEPTION_IF_NULL(mode_v); if (!mode_v->isa()) { @@ -229,7 +230,7 @@ AbstractBasePtr DoInferReduceShape(const AbstractTuplePtr &x_shape, const ValueP int64_t e_value = CheckAxis(primitive->name(), elem, -SizeToLong(x_rank), SizeToLong(x_rank) - 1); (void)axis_set.insert(e_value); } - + MS_EXCEPTION_IF_NULL(x_shp_value->cast()); auto x_shp_data = x_shp_value->cast()->value(); if (x_shp_data.size() < x_rank) { MS_LOG(EXCEPTION) << "x_shape_data.size() " << x_shp_data.size() << " less than x_shape.size() " << x_rank; @@ -254,6 +255,7 @@ AbstractBasePtr InferImplBroadcastGradientArgs(const AnalysisEnginePtr &, const // this primitive get the index that need to reduce // input: x's shape and y's shape, inputs should be tuple // output: tuple of x and y 's reduce index, reduce index should be a tuple + MS_EXCEPTION_IF_NULL(primitive); const std::string op_name = primitive->name(); const size_t inputs_size = 2; CheckArgsSize(op_name, args_spec_list, inputs_size); @@ -289,6 +291,7 @@ AbstractBasePtr InferImplListMap(const AnalysisEnginePtr &engine, const Primitiv const AbstractBasePtrList &args_spec_list) { // Inputs: fn, list1, list2, ... MS_EXCEPTION_IF_NULL(engine); + MS_EXCEPTION_IF_NULL(primitive); if (args_spec_list.size() <= 1) { MS_LOG(EXCEPTION) << "List_map requires at least 1 list. while the input size is " << args_spec_list.size() << "."; } @@ -317,11 +320,13 @@ AbstractBasePtr InferImplListReduce(const AnalysisEnginePtr &engine, const Primi const AbstractBasePtrList &args_spec_list) { // Inputs: a fn, a list and an object of a subclass of a AbstractBase. MS_EXCEPTION_IF_NULL(engine); + MS_EXCEPTION_IF_NULL(primitive); const std::string op_name = primitive->name(); const size_t inputs_size = 3; CheckArgsSize(op_name, args_spec_list, inputs_size); AbstractFunctionPtr fn = CheckArg(op_name, args_spec_list, 0); AbstractListPtr lst = CheckArg(op_name, args_spec_list, 1); + MS_EXCEPTION_IF_NULL(lst); AbstractBasePtr dflt = args_spec_list[2]; AbstractBasePtr list_type = AbstractJoin(lst->elements()); @@ -337,10 +342,11 @@ AbstractBasePtr InferImplListReduce(const AnalysisEnginePtr &engine, const Primi AbstractBasePtr InferImplTupleReversed(const AnalysisEnginePtr &, const PrimitivePtr &primitive, const AbstractBasePtrList &args_spec_list) { // Inputs: a tuple + MS_EXCEPTION_IF_NULL(primitive); const std::string op_name = primitive->name(); CheckArgsSize(op_name, args_spec_list, 1); AbstractTuplePtr input = CheckArg(op_name, args_spec_list, 0); - + MS_EXCEPTION_IF_NULL(input); auto tuple_elements = input->elements(); AbstractBasePtrList elem_list; (void)std::transform(tuple_elements.rbegin(), tuple_elements.rend(), std::back_inserter(elem_list), @@ -351,10 +357,12 @@ AbstractBasePtr InferImplTupleReversed(const AnalysisEnginePtr &, const Primitiv AbstractBasePtr InferImplReduceShape(const AnalysisEnginePtr &, const PrimitivePtr &primitive, const AbstractBasePtrList &args_spec_list) { // Inputs: x_shape, axis + MS_EXCEPTION_IF_NULL(primitive); const std::string op_name = primitive->name(); constexpr size_t arg_size = 2; CheckArgsSize(op_name, args_spec_list, arg_size); AbstractTuplePtr shape_x = CheckArg(op_name, args_spec_list, 0); + MS_EXCEPTION_IF_NULL(shape_x); MS_EXCEPTION_IF_NULL(args_spec_list[1]); auto x_shp_value = shape_x->BuildValue(); @@ -391,44 +399,48 @@ AbstractBasePtr InferImplReduceShape(const AnalysisEnginePtr &, const PrimitiveP AbstractBasePtr InferImplTupleDiv(const AnalysisEnginePtr &, const PrimitivePtr &primitive, const AbstractBasePtrList &args_spec_list) { // Inputs: two tuples. + MS_EXCEPTION_IF_NULL(primitive); const std::string op_name = primitive->name(); constexpr size_t arg_size = 2; CheckArgsSize(op_name, args_spec_list, arg_size); AbstractTuplePtr shape_x = CheckArg(op_name, args_spec_list, 0); AbstractTuplePtr div_shp = CheckArg(op_name, args_spec_list, 1); - MS_LOG(INFO) << "DivShape input:" << shape_x->ToString() << ", div:" << div_shp->ToString(); + MS_EXCEPTION_IF_NULL(shape_x); + MS_EXCEPTION_IF_NULL(div_shp); + MS_LOG(INFO) << "The shape of dividend:" << shape_x->ToString() << ", the shape of divisor:" << div_shp->ToString(); auto div_shp_value = div_shp->BuildValue(); if (div_shp_value->isa()) { - MS_LOG(EXCEPTION) << "shape's data field can't be anythin: " << args_spec_list[0]->ToString(); + MS_LOG(EXCEPTION) << "The shape's data field can't be anything: " << args_spec_list[0]->ToString(); } - auto shpx_value = shape_x->BuildValue(); - if (shpx_value->isa()) { - MS_LOG(EXCEPTION) << "shape's data field can't be anythin: " << args_spec_list[1]->ToString(); + auto shape_x_value = shape_x->BuildValue(); + if (shape_x_value->isa()) { + MS_LOG(EXCEPTION) << "The shape's data field can't be anything: " << args_spec_list[1]->ToString(); } if (div_shp->size() != shape_x->size()) { - MS_LOG(EXCEPTION) << "tileshape elems shape must the same div_shp: " << div_shp->size() - << ", shapex: " << shape_x->size() << "."; + MS_LOG(EXCEPTION) << "The size of inputs of tuple_div operator must be same, but the size of divisor tuple is" + << div_shp->size() << ", the size of dividend tuple is " << shape_x->size() << "."; } - auto shpx_data = shpx_value->cast()->value(); - auto div_shp_data = div_shp_value->cast()->value(); + auto shape_x_data = shape_x_value->cast()->value(); + auto div_shape_data = div_shp_value->cast()->value(); AbstractBasePtrList values; - for (size_t i = 0; i < div_shp_data.size(); i++) { - if (div_shp_data[i]->cast() == nullptr) { + for (size_t i = 0; i < div_shape_data.size(); i++) { + if (div_shape_data[i]->cast() == nullptr) { MS_LOG(EXCEPTION) << "div_shp_shape data should be an int64 number, but it's " << args_spec_list[1]->ToString(); } - int64_t shapex_value = GetValue(shpx_data[i]); - int64_t div_value = GetValue(div_shp_data[i]); + int64_t shapex_value = GetValue(shape_x_data[i]); + int64_t div_value = GetValue(div_shape_data[i]); MS_LOG(DEBUG) << "div_shp_shape data shapex_value :" << shapex_value << " div_value: " << div_value; if (div_value == 0) { - MS_LOG(EXCEPTION) << "error: division value should not be 0!"; + MS_LOG(EXCEPTION) << "The divisor value should not be 0!"; } if ((shapex_value % div_value) != 0) { - MS_LOG(EXCEPTION) << "div_shp_shape data shapex must div int64_t:" << shapex_value << " div_value: " << div_value; + MS_LOG(EXCEPTION) << "The inputs of tuple_div is not divisible, the dividend is :" << shapex_value + << ", the divisor is: " << div_value << "."; } int64_t result = shapex_value / div_value; @@ -445,13 +457,13 @@ AbstractBasePtr InferImplTuple2Array(const AnalysisEnginePtr &, const PrimitiveP const std::string op_name = primitive->name(); CheckArgsSize(op_name, args_spec_list, 1); AbstractTuplePtr input = CheckArg(op_name, args_spec_list, 0); - + MS_EXCEPTION_IF_NULL(input); py::tuple data_tuple = ValueToPyData(input->BuildValue()); py::array data = py::array(data_tuple); auto tensor = tensor::TensorPy::MakeTensor(data); auto ret = tensor->ToAbstract(); ret->set_value(tensor); - MS_LOG(DEBUG) << "Tuple2arry result AbstractTensor: " << ret->ToString(); + MS_LOG(DEBUG) << "Tuple2array result AbstractTensor: " << ret->ToString(); return ret; } @@ -465,7 +477,7 @@ AbstractBasePtr InferImplShapeMul(const AnalysisEnginePtr &, const PrimitivePtr auto shpx_value = shape_x->BuildValue(); if (shpx_value->isa()) { - MS_LOG(EXCEPTION) << "shape's data field can't be anythin: " << shape_x->ToString(); + MS_LOG(EXCEPTION) << "The shape's data field can't be anything: " << shape_x->ToString(); } auto shpx_data = shpx_value->cast()->value(); @@ -477,7 +489,7 @@ AbstractBasePtr InferImplShapeMul(const AnalysisEnginePtr &, const PrimitivePtr } auto result_v = MakeValue(result); - MS_LOG(DEBUG) << "shape mul result:" << result_v->ToString(); + MS_LOG(DEBUG) << "The result of shape_mul:" << result_v->ToString(); return std::make_shared(result_v, result_v->type()); } @@ -523,8 +535,8 @@ AbstractBasePtr InferImplMakeSlice(const AnalysisEnginePtr &, const PrimitivePtr ValuePtr scalar_index = MakeValue(static_cast(scalar_value->cast()->value())); slice_args.push_back(scalar_index->ToAbstract()); } else { - MS_EXCEPTION(TypeError) << "MakeSlice eval " << index - << " the input scalar type should be int or bool, but got " << scalar_value->ToString(); + MS_EXCEPTION(TypeError) << "The " << index << "th input of scalar should be int or bool, but got " + << scalar_value->ToString(); } } else if (args_spec_list[index]->isa()) { auto arg = args_spec_list[index]->cast(); @@ -552,7 +564,7 @@ AbstractBasePtr InferImplMakeSlice(const AnalysisEnginePtr &, const PrimitivePtr slice_args.push_back(args_spec_list[index]); } } else { - MS_EXCEPTION(TypeError) << "MakeSlice eval " << index << " inputs should scalar, None or Tensor, but got" + MS_EXCEPTION(TypeError) << "The " << index << "th input of MakeSlice should be scalar, none or tensor, but got" << args_spec_list[index]->ToString(); } } @@ -563,19 +575,19 @@ AbstractBasePtr InferImplMakeSlice(const AnalysisEnginePtr &, const PrimitivePtr AbstractBasePtr InferImplMakeRange(const AnalysisEnginePtr &, const PrimitivePtr &, const AbstractBasePtrList &args_spec_list) { if (args_spec_list.empty()) { - MS_LOG(EXCEPTION) << "Cannot make range from empty input."; + MS_LOG(EXCEPTION) << "The inputs of make_range operator could not be empty."; } constexpr size_t max_args_size = 3; if (args_spec_list.size() > max_args_size) { - MS_LOG(EXCEPTION) << "Error args size of make range operational."; + MS_LOG(EXCEPTION) << "The size of inputs of make_range operator could not exceed 3."; } SlideInfo slide = {0, 1, 0}; CalcSlidePara(args_spec_list, &slide); if (slide.step == 0) { - MS_LOG(EXCEPTION) << "Error, step value is 0."; + MS_LOG(EXCEPTION) << "The step value of make_range operator could not be 0."; } AbstractBasePtrList args; diff --git a/tests/st/ops/ascend/test_dynamic_broadcast_gradient_args.py b/tests/st/ops/ascend/test_dynamic_broadcast_gradient_args.py index 0d15d0ce961..aa13beca7b9 100644 --- a/tests/st/ops/ascend/test_dynamic_broadcast_gradient_args.py +++ b/tests/st/ops/ascend/test_dynamic_broadcast_gradient_args.py @@ -25,7 +25,7 @@ context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") class Net(nn.Cell): def __init__(self): super(Net, self).__init__() - self.args = _inner_ops.BroadcastGradientArgs() + self.args = _inner_ops.DynamicBroadcastGradientArgs() def construct(self, s0, s1): return self.args(s0, s1) diff --git a/tests/syntax/simple_expression/test_hypermap.py b/tests/syntax/simple_expression/test_hypermap.py new file mode 100644 index 00000000000..e6ff8de212f --- /dev/null +++ b/tests/syntax/simple_expression/test_hypermap.py @@ -0,0 +1,189 @@ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ + +import pytest +import numpy as np + +from mindspore import Tensor +from mindspore.common.api import ms_function +from mindspore.ops import composite as C +from mindspore.ops import operations as P +from mindspore.nn import Cell + +add = P.Add() +hyper_map = C.HyperMap() + +@ms_function +def main_noleaf(x, y): + return hyper_map(add, x, y) + + +def test_hypermap_noleaf_tuple_list_mix(): + """ + Feature: Check the types of inputs of HyperMap. + Description: The types of inputs of HyperMap must be the same. + Expectation: The types of inputs of HyperMap must be the same. + """ + tensor1 = Tensor(np.array([[1.2, 2.1], [2.2, 3.2]]).astype('float32')) + tensor2 = Tensor(np.array([[1.2, 2.1], [2.2, 3.2]]).astype('float32')) + with pytest.raises(Exception, match="HyperMap cannot match up all input types of arguments."): + main_noleaf((tensor1, 1), [tensor2, 2]) + + +def test_hypermap_noleaf_tuple_length(): + """ + Feature: Check the length of arg of Tuple in HyperMap. + Description: The length of inputs of HyperMap must be the same. + Expectation: The length of inputs of HyperMap must be the same. + """ + tensor1 = Tensor(np.array([[1.2, 2.1], [2.2, 3.2]]).astype('float32')) + tensor2 = Tensor(np.array([[1.2, 2.1], [2.2, 3.2]]).astype('float32')) + with pytest.raises(Exception, match="Tuple in HyperMap should have same length."): + main_noleaf((tensor1, 1), (tensor2, 2, 2)) + + +def test_hypermap_noleaf_list_length(): + """ + Feature: Check the length of arg of List in HyperMap. + Description: Check the length of arg of List in HyperMap. + Expectation: Check the length of arg of List in HyperMap. + """ + tensor1 = Tensor(np.array([[1.2, 2.1], [2.2, 3.2]]).astype('float32')) + tensor2 = Tensor(np.array([[1.2, 2.1], [2.2, 3.2]]).astype('float32')) + with pytest.raises(Exception, match="List in HyperMap should have same length."): + main_noleaf([tensor1], [tensor2, tensor2]) + + +def test_hypermap_noleaf_list_tuple(): + """ + Feature: Check the types of inputs of HyperMap. + Description: The types of inputs of HyperMap must be the same. + Expectation: The types of inputs of HyperMap must be the same. + """ + tensor1 = Tensor(np.array([[1.2, 2.1], [2.2, 3.2]]).astype('float32')) + tensor2 = Tensor(np.array([[1.2, 2.1], [2.2, 3.2]]).astype('float32')) + with pytest.raises(Exception, match="HyperMap cannot match up all input types of arguments."): + main_noleaf([tensor1], (tensor2, tensor2)) + + +def test_tuple_slice_stop_index(): + """ + Feature: Check the type of stop index of slice. + Description: The type of stop index of slice must be scalar, None or Tensor. + Expectation: The type of stop index of slice must be scalar, None or Tensor. + """ + class TupleSliceNet(Cell): + def __init__(self): + super(TupleSliceNet, self).__init__() + self.addN = P.AddN() + self.index_0 = Tensor(3) + + def construct(self, tensor_tuple): + tensor_tuple_slice0 = tensor_tuple[:] + tensor_tuple_slice1 = tensor_tuple[self.index_0:"str"] # slice should be Scalar or None, rather than string + sum0 = self.addN(tensor_tuple_slice0) + sum1 = self.addN(tensor_tuple_slice1) + ret = sum0 + sum1 + return ret + + data = (Tensor(np.ones([2, 3, 4], np.int32)), + Tensor(np.zeros([2, 3, 4], np.int32)), + Tensor(np.ones([2, 3, 4], np.int32)), + Tensor(np.ones([2, 3, 4], np.int32)), + Tensor(np.zeros([2, 3, 4], np.int32)), + Tensor(np.ones([2, 3, 4], np.int32))) + + net = TupleSliceNet() + with pytest.raises(Exception, match="The 1th input of MakeSlice should be scalar, none or tensor, but got str"): + output = net(data) + print("output:", output) + + +def test_tuple_slice_start_index(): + """ + Feature: Check the type of start index of slice. + Description: The type of start index of slice must be scalar, None or Tensor. + Expectation: The type of start index of slice must be scalar, None or Tensor. + """ + class TupleSliceNet(Cell): + def __init__(self): + super(TupleSliceNet, self).__init__() + self.addN = P.AddN() + self.index_0 = Tensor(3) + self.index_1 = Tensor([5]) + self.index_3 = Tensor([True]) + + def construct(self, tensor_tuple): + tensor_tuple_slice0 = tensor_tuple[:] + tensor_tuple_slice1 = tensor_tuple["str":self.index_0] + tensor_tuple_slice2 = tensor_tuple[self.index_3:] + tensor_tuple_slice3 = tensor_tuple[2:self.index_1:] + sum0 = self.addN(tensor_tuple_slice0) + sum1 = self.addN(tensor_tuple_slice1) + sum2 = self.addN(tensor_tuple_slice2) + sum3 = self.addN(tensor_tuple_slice3) + ret = sum0 + sum1 + sum2 + sum3 + return ret + + data = (Tensor(np.ones([2, 3, 4], np.int32)), + Tensor(np.zeros([2, 3, 4], np.int32)), + Tensor(np.ones([2, 3, 4], np.int32)), + Tensor(np.ones([2, 3, 4], np.int32)), + Tensor(np.zeros([2, 3, 4], np.int32)), + Tensor(np.ones([2, 3, 4], np.int32))) + + net = TupleSliceNet() + with pytest.raises(Exception, match="The 0th input of MakeSlice should be scalar, none or tensor, but got str"): + output = net(data) + print("output:", output) + + +def test_tuple_slice_step(): + """ + Feature: Check the type of step of slice. + Description: The type of step of slice must not be 0. + Expectation: The type of step of slice must be scalar, None or Tensor. + """ + class TupleSliceNet(Cell): + def __init__(self): + super(TupleSliceNet, self).__init__() + self.addN = P.AddN() + self.index_0 = Tensor(3) + self.index_1 = Tensor([5]) + self.index_3 = Tensor([True]) + + def construct(self, tensor_tuple): + tensor_tuple_slice0 = tensor_tuple[:] + tensor_tuple_slice1 = tensor_tuple[:self.index_0] + tensor_tuple_slice2 = tensor_tuple[self.index_3:] + tensor_tuple_slice3 = tensor_tuple[2:self.index_1:0] + sum0 = self.addN(tensor_tuple_slice0) + sum1 = self.addN(tensor_tuple_slice1) + sum2 = self.addN(tensor_tuple_slice2) + sum3 = self.addN(tensor_tuple_slice3) + ret = sum0 + sum1 + sum2 + sum3 + return ret + + data = (Tensor(np.ones([2, 3, 4], np.int32)), + Tensor(np.zeros([2, 3, 4], np.int32)), + Tensor(np.ones([2, 3, 4], np.int32)), + Tensor(np.ones([2, 3, 4], np.int32)), + Tensor(np.zeros([2, 3, 4], np.int32)), + Tensor(np.ones([2, 3, 4], np.int32))) + + net = TupleSliceNet() + with pytest.raises(Exception, match="TupleSlice require the step value could not be 0, but got 0."): + output = net(data) + print("output:", output) diff --git a/tests/syntax/simple_expression/test_map.py b/tests/syntax/simple_expression/test_map.py new file mode 100644 index 00000000000..e9eb1f1fede --- /dev/null +++ b/tests/syntax/simple_expression/test_map.py @@ -0,0 +1,170 @@ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ + +import pytest +import numpy as np + +from mindspore import Tensor, nn, Parameter +from mindspore.nn import Cell +import mindspore as ms + + +def test_map_args_size(): + """ + Feature: Check the size of inputs of map. + Description: The size of inputs of map must be greater than 1. + Expectation: The size of inputs of map must be greater than 1. + """ + class MapNet(Cell): + def __init__(self): + super().__init__() + self.relu = nn.ReLU() + + def mul(self, x=2, y=4): + return x * y + + def construct(self, x): + if map(self.mul) == 8: + x = self.relu(x) + return x + input_np_x = np.random.randn(2, 3, 4, 5).astype(np.float32) + input_me_x = Tensor(input_np_x) + + net = MapNet() + with pytest.raises(Exception, match="The map operator must have at least two arguments."): + ret = net(input_me_x) + print("ret:", ret) + + +def test_map_args_type(): + """ + Feature: Check the type of inputs of Map(). + Description: The type of inputs of Map() must be list, tuple or class. + Expectation: The type of inputs of Map() must be list, tuple or class. + """ + class MapNet(Cell): + def __init__(self): + super().__init__() + self.relu = nn.ReLU() + + def mul(self, x=2, y=4): + return x * y + + def construct(self, x): + if map(self.mul, 3, 4) == 8: + x = self.relu(x) + return x + input_np_x = np.random.randn(2, 3, 4, 5).astype(np.float32) + input_me_x = Tensor(input_np_x) + + net = MapNet() + with pytest.raises(Exception, match="Map can only be applied to list, tuple and class"): + ret = net(input_me_x) + print("ret:", ret) + + +def test_map_args_full_make_list(): + """ + Feature: Check the types of all inputs in Map. + Description: The types of all inputs in Map must be same. + Expectation: The types of all inputs in Map must be same. + """ + class MapNet(Cell): + def mul(self, x=2, y=4): + return x * y + + def construct(self, x, y): + if map(self.mul, x, y) == [8]: + x = y + return x + + input_me_x = Tensor(np.random.randn(2, 3, 4, 5).astype(np.float32)) + input_me_y = Tensor(np.random.randn(2, 3, 4, 5).astype(np.float32)) + + net = MapNet() + with pytest.raises(Exception, match="Map cannot match up all input types of arguments."): + ret = net([input_me_x], (input_me_y)) + print("ret:", ret) + + +def test_map_args_full_make_list_same_length(): + """ + Feature: Check the length of list input Map. + Description: The list in Map should have same length. + Expectation: The list in Map should have same length. + """ + class MapNet(Cell): + def mul(self, x=2, y=4): + return x * y + + def construct(self, x, y): + if map(self.mul, x, y) == [8]: + x = y + return x + + input_me_x = Tensor(np.random.randn(2, 3, 4, 5).astype(np.float32)) + input_me_y = Tensor(np.random.randn(2, 3, 4, 5).astype(np.float32)) + + net = MapNet() + with pytest.raises(Exception, match="List in Map should have same length."): + ret = net([input_me_x], [input_me_y, input_me_y]) + print("ret:", ret) + + +def test_map_args_full_make_tuple_same_length(): + """ + Feature: Check the length of tuple input Map. + Description: The tuple in Map should have same length. + Expectation: The tuple in Map should have same length. + """ + class MapNet(Cell): + def mul(self, x=2, y=4): + return x * y + + def construct(self, x, y): + if map(self.mul, x, y) == [8]: + x = y + return x + + input_me_x = Tensor(np.random.randn(2, 3, 4, 5).astype(np.float32)) + input_me_y = Tensor(np.random.randn(2, 3, 4, 5).astype(np.float32)) + + net = MapNet() + with pytest.raises(Exception, match="Tuple in Map should have same length."): + ret = net((input_me_x, input_me_x), (input_me_y, input_me_y, input_me_y)) + print("ret:", ret) + + +def test_map_param_cast(): + """ + Feature: Check the ref type when insert auto cast. + Description: Check the ref type when insert auto cast. + Expectation: Check the ref type when insert auto cast. + """ + class MapNet(Cell): + def __init__(self): + super().__init__() + self.param = Parameter(Tensor(5, ms.float32), name="param_b") + + def construct(self, x): + self.param = x + return self.param + + input_me_x = Tensor(np.random.randn(2, 3, 4, 5).astype(np.float64)) + + net = MapNet() + with pytest.raises(Exception, match="In op 'S-Prim-Assign', the type of writable argument is 'float32'"): + ret = net(input_me_x) + print("ret:", ret) diff --git a/tests/syntax/simple_expression/test_operator.py b/tests/syntax/simple_expression/test_operator.py new file mode 100644 index 00000000000..4f26a1aa3fa --- /dev/null +++ b/tests/syntax/simple_expression/test_operator.py @@ -0,0 +1,240 @@ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ + +import sys +import pytest + +from mindspore import Tensor, context, Parameter +from mindspore.ops import operations as P +from mindspore.ops import functional as F +from mindspore.nn import Cell +import mindspore as ms + + +def test_inner_scalar_divisor(): + """ + Feature: Check whether the divisor of inner scalar is zero. + Description: The divisor of inner scalar must not be zero. + Expectation: The divisor of inner scalar must not be zero. + """ + class Net(Cell): + def __init__(self): + super().__init__() + self.param_a = Parameter(Tensor(5, ms.int32), name="param_a") + self.param_b = Parameter(Tensor(5, ms.int32), name="param_b") + + def construct(self, x): + return x + self.param_a + 5 / 0 + + context.set_context(device_target="GPU") + x = Tensor(2, dtype=ms.int32) + net = Net() + with pytest.raises(Exception, match="The divisor could not be zero."): + ret = net(x) + print("ret:", ret) + + +def test_inner_scalar_mod(): + """ + Feature: Check the input of inner scalar mod. + Description: The input of inner scalar mod must not be zero. + Expectation: The input of inner scalar mod must not be zero. + """ + class Net(Cell): + def __init__(self): + super().__init__() + self.param_a = Parameter(Tensor(5, ms.int32), name="param_a") + + def construct(self, x): + return x + self.param_a + 5 % 0 + + x = Tensor(2, dtype=ms.int32) + net = Net() + with pytest.raises(Exception, match="Could not mod to zero."): + ret = net(x) + print("ret:", ret) + + +def test_inner_scalar_mod_args_length(): + """ + Feature: Check the length of input of inner scalar mod. + Description: The length of input of inner scalar mod should not less than 2. + Expectation: The length of input of inner scalar mod should not less than 2. + """ + class Net(Cell): + def __init__(self): + super().__init__() + self.param_a = Parameter(Tensor(5, ms.int32), name="param_a") + self.mod = P.Mod() + + def construct(self, x): + return x + self.param_a + self.mod(5) + + x = Tensor(2, dtype=ms.int32) + net = Net() + with pytest.raises(Exception, match="Function S-Prim-Mod's input length is not equal to Signature length."): + ret = net(x) + print("ret:", ret) + + +def test_make_range_input_is_empty(): + """ + Feature: Check the length of inputs of make_range operator. + Description: The inputs of make_range operator could not be empty. + Expectation: The inputs of make_range operator could not be empty. + """ + class Net(Cell): + def construct(self, x, y): + for _ in F.make_range(): + x += y + return x + + x = Tensor(2, dtype=ms.int32) + y = Tensor(4, dtype=ms.int32) + net = Net() + with pytest.raises(Exception, match="The inputs of make_range operator could not be empty."): + ret = net(x, y) + print("ret:", ret) + + +def test_make_range_input_type(): + """ + Feature: Check the type of inputs of make_range operator. + Description: The type of inputs of make_range operator must be int64. + Expectation: The type of inputs of make_range operator must be int64. + """ + class Net(Cell): + def construct(self, x, y): + for _ in F.make_range(0, 0.02): + x += y + return x + + x = Tensor(2, dtype=ms.int32) + y = Tensor(4, dtype=ms.int32) + net = Net() + with pytest.raises(Exception, match="The type of inputs of make_range operator only support int64 number."): + ret = net(x, y) + print("ret:", ret) + + +def test_make_range_input_size(): + """ + Feature: Check the size of inputs of make_range operator. + Description: The size of inputs of make_range operator could not exceed 3. + Expectation: The size of inputs of make_range operator could not exceed 3. + """ + class Net(Cell): + def construct(self, x, y): + for _ in F.make_range(1, 2, 3, 4): + x += y + return x + + x = Tensor(2, dtype=ms.int32) + y = Tensor(4, dtype=ms.int32) + net = Net() + with pytest.raises(Exception, match="The size of inputs of make_range operator could not exceed 3."): + ret = net(x, y) + print("ret:", ret) + + +def test_make_range_overflow(): + """ + Feature: Check the size of inputs of make_range operator. + Description: The size of inputs of make_range operator could not exceed 3. + Expectation: The size of inputs of make_range operator could not exceed 3. + """ + class Net(Cell): + def construct(self, x, y): + max_index = sys.maxsize + for _ in F.make_range(max_index - 1, max_index, 3): + x += y + return x + + x = Tensor(2, dtype=ms.int32) + y = Tensor(4, dtype=ms.int32) + net = Net() + with pytest.raises(Exception, match="For make range, the required cycles number is greater than max cycles number"): + ret = net(x, y) + print("ret:", ret) + + +def test_typeof(): + """ + Feature: Check the size of inputs of typeof operator. + Description: The size of inputs of typeof operator must be 1. + Expectation: The size of inputs of typeof operator must be 1. + """ + class Net(Cell): + def construct(self, x): + return F.typeof(x, x) + + x = Tensor([2, 3, 4, 5], dtype=ms.int32) + net = Net() + with pytest.raises(Exception, match="Typeof evaluator requires 1 parameter, while the input size is 2."): + ret = net(x) + print("ret:", ret) + + +def test_tuple_div(): + """ + Feature: Check the size of inputs of tuple_div operator. + Description: The size of inputs of tuple_div operator must be same. + Expectation: The size of inputs of tuple_div operator must be same. + """ + class Net(Cell): + def construct(self, x, y): + return F.tuple_div(x, y) + + x = (8, 14, 20) + y = (2, 2) + net = Net() + with pytest.raises(Exception, match="The size of inputs of tuple_div operator must be same"): + ret = net(x, y) + print("ret:", ret) + + +def test_tuple_div_input_is_not_divisible(): + """ + Feature: Check whether the inputs of tuple_div is divisible. + Description: The inputs of tuple_div could be divisible. + Expectation: The inputs of tuple_div could be divisible. + """ + class Net(Cell): + def construct(self, x, y): + return F.tuple_div(x, y) + + x = (8, 14) + y = (2, 3) + net = Net() + with pytest.raises(Exception, match="The inputs of tuple_div is not divisible"): + ret = net(x, y) + print("ret:", ret) + + +def test_make_slice_scalar(): + """ + Feature: Check whether the scalar input of make_slice is int or bool. + Description: The scalar input of make_slice is int or bool. + Expectation: The scalar input of make_slice is int or bool. + """ + class Net(Cell): + def construct(self, data): + return data[F.make_slice(1.01, None, None)] + + x = Tensor((8, 10, 12), dtype=ms.int32) + net = Net() + with pytest.raises(Exception, match="The 0th input of scalar should be int or bool"): + ret = net(x) + print("ret:", ret) diff --git a/tests/syntax/simple_expression/test_zip_operation.py b/tests/syntax/simple_expression/test_zip_operation.py new file mode 100644 index 00000000000..feee2a02441 --- /dev/null +++ b/tests/syntax/simple_expression/test_zip_operation.py @@ -0,0 +1,85 @@ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ + +import pytest +import numpy as np + +from mindspore import Tensor, Parameter +from mindspore.ops import operations as P +from mindspore.nn import Cell +import mindspore as ms + + +def test_zip_operation_args_size(): + """ + Feature: Check the size of inputs of ZipOperation. + Description: The inputs of ZipOperation must not be empty. + Expectation: The size of inputs of ZipOperation must be greater than 0. + """ + class AssignInZipLoop(Cell): + def __init__(self): + super().__init__() + self.conv1 = ms.nn.Conv2d(3, 2, 1, weight_init="zero") + self.conv2 = ms.nn.Conv2d(3, 2, 1, weight_init="zero") + self.params1 = self.conv1.trainable_params() + self.params2 = self.conv2.trainable_params() + + def construct(self, x): + for p1, p2 in zip(): + P.Assign()(p2, p1 + x) + + out = 0 + for p1, p2 in zip(self.params1, self.params2): + out = p1 + p2 + + return out + + x = Tensor.from_numpy(np.ones([1], np.float32)) + net = AssignInZipLoop() + with pytest.raises(Exception, match="For 'zip', there is at least one input."): + out = net(x) + assert np.all(out.asnumpy() == 1) + + +def test_zip_operation_args_type(): + """ + Feature: Check the type of inputs of ZipOperation. + Description: Check whether all inputs in zip is sequeue. + Expectation: All inputs in zip must be sequeue. + """ + class AssignInZipLoop(Cell): + def __init__(self): + super().__init__() + self.conv1 = ms.nn.Conv2d(3, 2, 1, weight_init="zero") + self.conv2 = ms.nn.Conv2d(3, 2, 1, weight_init="zero") + self.params1 = self.conv1.trainable_params() + self.params2 = self.conv2.trainable_params() + self.param = Parameter(Tensor(5, ms.float32), name="param") + + def construct(self, x): + for p1, p2 in zip(self.params1, self.params2, self.param): + P.Assign()(p2, p1 + x) + + out = 0 + for p1, p2 in zip(self.params1, self.params2): + out = p1 + p2 + + return out + + x = Tensor.from_numpy(np.ones([1], np.float32)) + net = AssignInZipLoop() + with pytest.raises(Exception, match="For 'zip', all inputs must be sequence."): + out = net(x) + assert np.all(out.asnumpy() == 1) diff --git a/tests/ut/cpp/operator/cc_implementations_test.cc b/tests/ut/cpp/operator/cc_implementations_test.cc index a84ae3c533f..fc12bd8e64e 100644 --- a/tests/ut/cpp/operator/cc_implementations_test.cc +++ b/tests/ut/cpp/operator/cc_implementations_test.cc @@ -191,7 +191,7 @@ TEST_F(TestImplementations, ScalarDivTest) { ScalarDiv(list); FAIL(); } catch (std::runtime_error const &err) { - ASSERT_TRUE(std::string(err.what()).find("Divisor could not be zero") != std::string::npos); + ASSERT_TRUE(std::string(err.what()).find("The divisor could not be zero.") != std::string::npos); } list.clear();