diff --git a/mindspore/ccsrc/frontend/operator/composite/composite.cc b/mindspore/ccsrc/frontend/operator/composite/composite.cc index ca1f0521bee..3612a02d746 100644 --- a/mindspore/ccsrc/frontend/operator/composite/composite.cc +++ b/mindspore/ccsrc/frontend/operator/composite/composite.cc @@ -108,6 +108,23 @@ AnfNodePtr HyperMap::FullMake(const FuncGraphPtr &func_graph, const AnfNodePtr & return func_graph->NewCNodeInOrder(inputs); } +std::vector HyperMap::GetHyperMapInputIndex(size_t num) { + std::string error_index; + std::string next_index; + if (num == 1) { + // The first element in HyperMap is func_graph + error_index = "first"; + next_index = "second"; + } else if (num == 2) { + error_index = "second"; + next_index = "third"; + } else { + error_index = std::to_string(num) + "th"; + next_index = std::to_string(num + 1) + "th"; + } + return {error_index, next_index}; +} + AnfNodePtr HyperMap::FullMake(const std::shared_ptr &type, const FuncGraphPtr &func_graph, const AnfNodePtr &fn_arg, const ArgsPairList &arg_map) { MS_EXCEPTION_IF_NULL(func_graph); @@ -116,21 +133,24 @@ AnfNodePtr HyperMap::FullMake(const std::shared_ptr &type, const FuncGraph size_t size = type->elements().size(); size_t num = 0; std::ostringstream oss; - bool is_not_same = - std::any_of(arg_map.begin(), arg_map.end(), [&num, size, &oss](const std::pair &item) { - num++; - auto lhs = std::static_pointer_cast(item.second); - if (lhs == nullptr) { - MS_LOG(EXCEPTION) << "The " << (num - 1) << "th element in HyperMap has wrong type, expected a List, but got " - << item.second->ToString() << "."; - } - if (lhs->elements().size() != size) { - oss << "The length of " << (num - 1) << "th List in HyperMap is " << size << ", but the length of " << num - << "th List in HyperMap is " << lhs->elements().size() << ".\n"; - return true; - } - return false; - }); + bool is_not_same = false; + for (auto &item : arg_map) { + num++; + auto lhs = std::static_pointer_cast(item.second); + std::vector indexes = GetHyperMapInputIndex(num); + std::string error_index = indexes[0]; + std::string next_index = indexes[1]; + if (lhs == nullptr) { + MS_LOG(EXCEPTION) << "The " << error_index << " element in HyperMap has wrong type, expected a List, but got " + << item.second->ToString() << "."; + } + if (lhs->elements().size() != size) { + oss << "\nThe length of the " << error_index << " element in HyperMap is " << size << ", but the length of the " + << next_index << " element in HyperMap is " << lhs->elements().size() << ".\n"; + is_not_same = true; + break; + } + } if (is_not_same) { MS_LOG(EXCEPTION) << "The lists in HyperMap should have the same length. " << oss.str(); } @@ -175,21 +195,24 @@ AnfNodePtr HyperMap::FullMake(const std::shared_ptr &type, const FuncGrap size_t size = type->elements().size(); size_t num = 0; std::ostringstream oss; - bool is_not_same = - std::any_of(arg_map.begin(), arg_map.end(), [&num, size, &oss](const std::pair &item) { - num++; - auto lhs = std::static_pointer_cast(item.second); - if (lhs == nullptr) { - MS_LOG(EXCEPTION) << "The " << (num - 1) << "th element in HyperMap has wrong type, expected a Tuple, but got " - << item.second->ToString() << "."; - } - if (lhs->elements().size() != size) { - oss << "The length of " << (num - 1) << "th Tuple in HyperMap is " << size << ", but the length of " << num - << "th Tuple in HyperMap is " << lhs->elements().size() << ".\n"; - return true; - } - return false; - }); + bool is_not_same = false; + for (auto &item : arg_map) { + num++; + auto lhs = std::static_pointer_cast(item.second); + std::vector indexes = GetHyperMapInputIndex(num); + std::string error_index = indexes[0]; + std::string next_index = indexes[1]; + if (lhs == nullptr) { + MS_LOG(EXCEPTION) << "The " << error_index << " element in HyperMap has wrong type, expected a Tuple, but got " + << item.second->ToString() << "."; + } + if (lhs->elements().size() != size) { + oss << "\nThe length of the " << error_index << " element in HyperMap is " << size << ", but the length of the " + << next_index << " element in HyperMap is " << lhs->elements().size() << ".\n"; + is_not_same = true; + break; + } + } if (is_not_same) { MS_LOG(EXCEPTION) << "The length of tuples in HyperMap must be the same. " << oss.str(); } @@ -293,8 +316,18 @@ AnfNodePtr HyperMap::Make(const FuncGraphPtr &func_graph, const AnfNodePtr &fn_a oss << "There are " << arg_map.size() << " inputs of `" << name_ << "`, corresponding type info:\n" << trace::GetDebugInfo(func_graph->debug_info()) << "\n"; int64_t idx = 0; + std::string str_index = "first"; for (auto &item : arg_map) { - oss << "The type of " << (++idx + 1) << "th argument in HyperMap is " << item.second->ToString() << ".\n"; + // The first element in HyperMap is func_graph + if (idx == 0) { + str_index = "second"; + } else if (idx == 1) { + str_index = "third"; + } else { + str_index = std::to_string(idx + 2) + "th"; + } + ++idx; + oss << "The type of the " << str_index << " argument in HyperMap is " << item.second->ToString() << ".\n"; } MS_LOG(EXCEPTION) << "The types of arguments in HyperMap must be consistent, " << "but the types of arguments are inconsistent.\n" diff --git a/mindspore/ccsrc/frontend/operator/composite/composite.h b/mindspore/ccsrc/frontend/operator/composite/composite.h index 2005a8eb6a1..5d50fc61991 100644 --- a/mindspore/ccsrc/frontend/operator/composite/composite.h +++ b/mindspore/ccsrc/frontend/operator/composite/composite.h @@ -81,6 +81,7 @@ class HyperMap : public MetaFuncGraph { const ArgsPairList &arg_map); AnfNodePtr Make(const FuncGraphPtr &graph, const AnfNodePtr &fn_arg, const ArgsPairList &arg_map); ArgsPairList Harmonize(const FuncGraphPtr &graph, const ArgsPairList &args_spec_list); + std::vector GetHyperMapInputIndex(size_t num); MultitypeFuncGraphPtr fn_leaf_; bool reverse_; diff --git a/mindspore/ccsrc/frontend/operator/composite/do_signature.cc b/mindspore/ccsrc/frontend/operator/composite/do_signature.cc index 5bf2e39bc0e..f3046f6318e 100644 --- a/mindspore/ccsrc/frontend/operator/composite/do_signature.cc +++ b/mindspore/ccsrc/frontend/operator/composite/do_signature.cc @@ -347,8 +347,8 @@ FuncGraphPtr DoSignatureMetaFuncGraph::GenerateFuncGraph(const AbstractBasePtrLi void RaiseExceptionForConvertRefDtype(const std::string &func_name, const std::string &ref_type, const std::string &target_type) { - MS_LOG(EXCEPTION) << "Data type conversion of parameter is not supported, so data type " << ref_type - << " cannot be converted to data type " << target_type << " by inserting cast automatically.\n" + MS_LOG(EXCEPTION) << "Data type conversion of 'Parameter' is not supported, so data type " << ref_type + << " cannot be converted to data type " << target_type << " automatically.\n" << "For more details, please refer at " << "https://www.mindspore.cn/docs/note/zh-CN/master/operator_list_implicit.html."; } diff --git a/mindspore/ccsrc/frontend/operator/composite/map.cc b/mindspore/ccsrc/frontend/operator/composite/map.cc index f198a5aeaee..d2a1cac45d7 100644 --- a/mindspore/ccsrc/frontend/operator/composite/map.cc +++ b/mindspore/ccsrc/frontend/operator/composite/map.cc @@ -65,6 +65,23 @@ FuncGraphPtr Map::GenerateLeafFunc(const size_t &args_size) { return ptrGraph; } +std::vector Map::GetMapInputIndex(size_t num) { + std::string error_index; + std::string next_index; + if (num == 1) { + // The first element in Map is func_graph + error_index = "first"; + next_index = "second"; + } else if (num == 2) { + error_index = "second"; + next_index = "third"; + } else { + error_index = std::to_string(num) + "th"; + next_index = std::to_string(num + 1) + "th"; + } + return {error_index, next_index}; +} + AnfNodePtr Map::FullMakeList(const std::shared_ptr &type, const FuncGraphPtr &func_graph, const AnfNodePtr &fn_arg, const ArgsPairList &arg_pairs) { MS_EXCEPTION_IF_NULL(func_graph); @@ -73,21 +90,24 @@ AnfNodePtr Map::FullMakeList(const std::shared_ptr &type, const FuncGraphP std::size_t size = type->elements().size(); size_t num = 0; std::ostringstream oss; - bool is_not_same = - std::any_of(arg_pairs.begin(), arg_pairs.end(), [&num, size, &oss](const std::pair &item) { - num++; - auto lhs = std::dynamic_pointer_cast(item.second); - if (lhs == nullptr) { - MS_LOG(EXCEPTION) << "The " << (num - 1) << "th element in Map has wrong type, expected a List, but got " - << item.second->ToString() << "."; - } - if (lhs->elements().size() != size) { - oss << "The length of " << (num - 1) << "th List in Map is " << size << ", but the length of " << num - << "th List in Map is " << lhs->elements().size() << ".\n"; - return true; - } - return false; - }); + bool is_not_same = false; + for (auto &item : arg_pairs) { + num++; + auto lhs = std::dynamic_pointer_cast(item.second); + std::vector indexes = GetMapInputIndex(num); + std::string error_index = indexes[0]; + std::string next_index = indexes[1]; + if (lhs == nullptr) { + MS_LOG(EXCEPTION) << "The " << error_index << " element in Map has wrong type, expected a List, but got " + << item.second->ToString() << "."; + } + if (lhs->elements().size() != size) { + oss << "\nThe length of the " << error_index << " element in Map is " << size << ", but the length of the " + << next_index << " element in Map is " << lhs->elements().size() << ".\n"; + is_not_same = true; + break; + } + } if (is_not_same) { MS_LOG(EXCEPTION) << "The length of lists in Map must be the same. " << oss.str(); } @@ -133,21 +153,24 @@ AnfNodePtr Map::FullMakeTuple(const std::shared_ptr &type, const FuncGrap size_t size = type->elements().size(); size_t num = 0; std::ostringstream oss; - bool is_not_same = - std::any_of(arg_pairs.begin(), arg_pairs.end(), [&num, size, &oss](const std::pair &item) { - num++; - auto lhs = std::dynamic_pointer_cast(item.second); - if (lhs == nullptr) { - MS_LOG(EXCEPTION) << "The " << (num - 1) << "th element in Map has wrong type, expected a Tuple, but got " - << item.second->ToString() << "."; - } - if (lhs->elements().size() != size) { - oss << "The length of " << (num - 1) << "th Tuple in Map is " << size << ", but the length of " << num - << "th Tuple in Map is " << lhs->elements().size() << ".\n"; - return true; - } - return false; - }); + bool is_not_same = false; + for (auto &item : arg_pairs) { + num++; + auto lhs = std::dynamic_pointer_cast(item.second); + std::vector indexes = GetMapInputIndex(num); + std::string error_index = indexes[0]; + std::string next_index = indexes[1]; + if (lhs == nullptr) { + MS_LOG(EXCEPTION) << "The " << error_index << " element in Map has wrong type, expected a Tuple, but got " + << item.second->ToString() << "."; + } + if (lhs->elements().size() != size) { + oss << "\nThe length of the " << error_index << " element in Map is " << size << ", but the length of the " + << next_index << " element in Map is " << lhs->elements().size() << ".\n"; + is_not_same = true; + break; + } + } if (is_not_same) { MS_LOG(EXCEPTION) << "The length of tuples in Map must be the same. " << oss.str(); } @@ -259,8 +282,18 @@ AnfNodePtr Map::Make(const FuncGraphPtr &func_graph, const AnfNodePtr &fn_arg, c oss << "There are " << (arg_pairs.size() + 1) << " inputs of `" << name_ << "`, corresponding type info:\n" << trace::GetDebugInfo(func_graph->debug_info()) << ".\n"; int64_t idx = 0; + std::string str_index = "first"; for (auto &item : arg_pairs) { - oss << "The type of " << (++idx + 1) << "th argument in Map is: " << item.second->ToString() << ".\n"; + if (idx == 0) { + // The first element in HyperMap is func_graph + str_index = "second"; + } else if (idx == 1) { + str_index = "third"; + } else { + str_index = std::to_string(idx + 2) + "th"; + } + ++idx; + oss << "The type of the " << str_index << " argument in Map is: " << item.second->ToString() << ".\n"; } MS_LOG(EXCEPTION) << "The types of arguments in Map must be consistent, " << "but the types of arguments are inconsistent.\n" diff --git a/mindspore/ccsrc/frontend/operator/composite/map.h b/mindspore/ccsrc/frontend/operator/composite/map.h index 026a936b56c..411fc328d77 100644 --- a/mindspore/ccsrc/frontend/operator/composite/map.h +++ b/mindspore/ccsrc/frontend/operator/composite/map.h @@ -21,6 +21,7 @@ #include #include #include +#include #include "ir/dtype.h" #include "ir/meta_func_graph.h" @@ -77,6 +78,7 @@ class Map : public MetaFuncGraph { AnfNodePtr FullMakeClass(const std::shared_ptr &type, const FuncGraphPtr &func_graph, const AnfNodePtr &fn_arg, const ArgsPairList &arg_pairs); AnfNodePtr Make(const FuncGraphPtr &graph, const AnfNodePtr &fn_arg, const ArgsPairList &arg_pairs); + std::vector GetMapInputIndex(size_t num); void Init() { if (fn_leaf_ != nullptr) { name_ = "map[" + fn_leaf_->name() + "]"; diff --git a/mindspore/ccsrc/frontend/operator/composite/zip_operation.cc b/mindspore/ccsrc/frontend/operator/composite/zip_operation.cc index a6bcfa66c90..3f75a456679 100644 --- a/mindspore/ccsrc/frontend/operator/composite/zip_operation.cc +++ b/mindspore/ccsrc/frontend/operator/composite/zip_operation.cc @@ -43,18 +43,23 @@ FuncGraphPtr ZipOperation::GenerateFuncGraph(const AbstractBasePtrList &args_spe MS_LOG(EXCEPTION) << "The zip operator must have at least 1 argument, but the size of arguments is 0."; } - 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 (!all_is_sequence) { - std::ostringstream oss; - int64_t idx = 0; - for (auto &item : args_spec_list) { - oss << "the " << ++idx << " argument is: " << item->ToString() << "\n"; + for (size_t idx = 0; idx < args_spec_list.size(); idx++) { + auto abs = args_spec_list[idx]; + if (!abs->isa()) { + std::string error_index; + if (idx == 0) { + error_index = "first"; + } else if (idx == 1) { + error_index = "second"; + } else if (idx == 2) { + error_index = "third"; + } else { + error_index = std::to_string(idx) + "th"; + } + MS_LOG(EXCEPTION) << "For 'zip', the all inputs must be list or tuple, but the " << error_index + << " argument is not list or tuple.\nThe " << error_index + << " argument detail: " << args_spec_list[idx]->ToString() << "."; } - MS_LOG(EXCEPTION) << "The all inputs of zip operator must be sequence. But " << oss.str(); } auto min_abs = std::min_element( diff --git a/mindspore/ccsrc/frontend/operator/ops_front_infer_function.cc b/mindspore/ccsrc/frontend/operator/ops_front_infer_function.cc index 4d498626805..5d204a41b7e 100644 --- a/mindspore/ccsrc/frontend/operator/ops_front_infer_function.cc +++ b/mindspore/ccsrc/frontend/operator/ops_front_infer_function.cc @@ -57,6 +57,19 @@ AbstractBasePtr InferImplTupleOrListEqual(const std::string &op_name, const Abst return std::make_shared(*x_value == *y_value); } +void CheckSlideInput(const ValuePtr &arg_value) { + MS_EXCEPTION_IF_NULL(arg_value); + auto value_type = arg_value->type(); + std::string str_type; + if (value_type) { + str_type = value_type->ToString(); + } else { + str_type = "AnyValue"; + } + MS_LOG(EXCEPTION) << "The type of inputs in range operator only support int64 number. " + << "But get a " << str_type << " number."; +} + void CalcSlidePara(const AbstractBasePtrList &args_spec_list, SlideInfo *slide) { int64_t arg1 = 0; int64_t arg2 = 0; @@ -64,8 +77,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) << "The type of inputs in range operator only support int64 number. " - << "But get a " << arg_value->type() << " number."; + CheckSlideInput(arg_value); } arg1 = GetValue(arg_value); } @@ -74,8 +86,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) << "The type of inputs in range operator only support int64 number. " - << "But get a " << arg_value->type() << " number."; + CheckSlideInput(arg_value); } arg2 = GetValue(arg_value); } @@ -84,8 +95,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) << "The type of inputs in range operator only support int64 number. " - << "But get a " << arg_value->type() << " number."; + CheckSlideInput(arg_value); } slide->step = GetValue(arg_value); slide->start = arg1; @@ -437,8 +447,15 @@ AbstractBasePtr InferImplTupleDiv(const AnalysisEnginePtr &, const PrimitivePtr for (size_t i = 0; i < div_shape_data.size(); i++) { if (div_shape_data[i]->cast() == nullptr) { + auto value_type = div_shape_data[i]->type(); + std::string str_type; + if (value_type) { + str_type = value_type->ToString(); + } else { + str_type = "AnyValue"; + } MS_LOG(EXCEPTION) << "The data type of inputs of 'tuple_div' operator should be an int64 number, but got a " - << div_shape_data[i]->type() << " number " << div_shape_data[i]->ToString() << "."; + << str_type << " number " << div_shape_data[i]->ToString() << "."; } int64_t shapex_value = GetValue(shape_x_data[i]); int64_t div_value = GetValue(div_shape_data[i]); @@ -545,8 +562,13 @@ AbstractBasePtr InferImplMakeSlice(const AnalysisEnginePtr &, const PrimitivePtr slice_args.push_back(scalar_index->ToAbstract()); } else { auto type = scalar_value->type(); - MS_EXCEPTION(TypeError) << "Slice indices must be integers or bool. But got a " << type->ToString() - << " number."; + std::string str_type; + if (type) { + str_type = type->ToString(); + } else { + str_type = "AnyValue"; + } + MS_EXCEPTION(TypeError) << "Slice indices must be integers or bool. But got a " << str_type << " number."; } } else if (args_spec_list[index]->isa()) { auto arg = args_spec_list[index]->cast(); @@ -608,7 +630,7 @@ AbstractBasePtr InferImplMakeRange(const AnalysisEnginePtr &, const PrimitivePtr if (slide.start <= slide.stop) { if (slide.step <= 0) { MS_LOG(EXCEPTION) << "For 'range', while the argument 'start' " << slide.start - << " less than or equal to the argument 'stop' " << slide.stop << ", " + << " is less than or equal to the argument 'stop' " << slide.stop << ", " << "the argument 'step' must be more than 0, but the argument 'step' is " << slide.step << "."; } @@ -621,9 +643,9 @@ AbstractBasePtr InferImplMakeRange(const AnalysisEnginePtr &, const PrimitivePtr } } else { if (slide.step >= 0) { - MS_LOG(EXCEPTION) << "For 'range', while the argument 'start' " << slide.start << " more than the argument 'stop'" - << " " << slide.stop << ", the argument 'step' must be less than 0, but the argument 'step' is " - << slide.step << "."; + MS_LOG(EXCEPTION) << "For 'range', while the argument 'start' " << slide.start << " is more than the argument " + << "'stop' " << slide.stop << ", the argument 'step' must be less than 0, " + << "but the argument 'step' is " << slide.step << "."; } for (int64_t i = slide.start; i > slide.stop; i += slide.step) { diff --git a/tests/syntax/simple_expression/test_map.py b/tests/syntax/simple_expression/test_map.py index d11d153c90a..7fcdffcf9cf 100644 --- a/tests/syntax/simple_expression/test_map.py +++ b/tests/syntax/simple_expression/test_map.py @@ -165,6 +165,6 @@ def test_map_param_cast(): input_me_x = Tensor(np.random.randn(2, 3, 4, 5).astype(np.float64)) net = MapNet() - with pytest.raises(Exception, match="Data type conversion of parameter is not supported"): + with pytest.raises(Exception, match="Data type conversion of 'Parameter' is not supported"): ret = net(input_me_x) print("ret:", ret) diff --git a/tests/syntax/simple_expression/test_zip_operation.py b/tests/syntax/simple_expression/test_zip_operation.py index 2642ac8bc6b..da1a60b05a4 100644 --- a/tests/syntax/simple_expression/test_zip_operation.py +++ b/tests/syntax/simple_expression/test_zip_operation.py @@ -80,6 +80,6 @@ def test_zip_operation_args_type(): x = Tensor.from_numpy(np.ones([1], np.float32)) net = AssignInZipLoop() - with pytest.raises(Exception, match="The all inputs of zip operator must be sequence"): + with pytest.raises(Exception, match="For 'zip', the all inputs must be list or tuple."): out = net(x) assert np.all(out.asnumpy() == 1) diff --git a/tests/ut/python/pynative_mode/test_implicit_conversion.py b/tests/ut/python/pynative_mode/test_implicit_conversion.py index 46d634ba8a3..d92868415d9 100644 --- a/tests/ut/python/pynative_mode/test_implicit_conversion.py +++ b/tests/ut/python/pynative_mode/test_implicit_conversion.py @@ -271,4 +271,4 @@ def test_assign_check_in_sig(): y = Tensor(3, ms.uint8) with pytest.raises(RuntimeError) as e: net(x, y) - assert "Data type conversion of parameter is not supported" in e.value.args[0] + assert "Data type conversion of 'Parameter' is not supported" in e.value.args[0]