diff --git a/mindspore/_extends/parse/parser.py b/mindspore/_extends/parse/parser.py index 85d17b27f5f..2a9eab0475b 100644 --- a/mindspore/_extends/parse/parser.py +++ b/mindspore/_extends/parse/parser.py @@ -165,7 +165,7 @@ def resolve_symbol(namespace, symbol): resolve_ = convert_object_map.get(resolve_) logger.debug("convert resolve = %r", resolve_) if resolve_ == NO_IMPLEMENT: - raise NotImplementedError("not implemented for ", str(symbol)) + raise NotImplementedError(f"Not support for `{symbol}`") except Exception as e: if isinstance(e, NotImplementedError): raise e @@ -249,7 +249,7 @@ def get_obj_type(obj): else: # here for ndarray, just print its shape (in case of the array to large and print many data in screen) is_ndarray = type(obj).__name__ == 'ndarray' and hasattr(obj, 'shape') - raise TypeError(f'Invalid object with type `{type(obj)}` and {"shape" if is_ndarray else "value"} ' + raise TypeError(f'Not support for this object with type `{type(obj)}` and {"shape" if is_ndarray else "value"} ' f'`{obj.shape if is_ndarray else obj}`.') return obj_type diff --git a/mindspore/_extends/parse/resources.py b/mindspore/_extends/parse/resources.py index 61d407829e0..db396ec57c4 100644 --- a/mindspore/_extends/parse/resources.py +++ b/mindspore/_extends/parse/resources.py @@ -120,13 +120,13 @@ convert_object_map = { T.iter: M.ms_iter, T.next: M.ms_next, T.hasnext: M.hasnext, - - T.MakeTuple: F.make_tuple, + T.MakeTuple: F.make_tuple, T.make_dict: F.make_dict, T.make_list: F.make_list, T.make_slice: F.make_slice, T.range: F.make_range, T.while_cond: M.while_cond, + # lib function math.floor: NO_IMPLEMENT, math.trunc: NO_IMPLEMENT, @@ -137,6 +137,6 @@ convert_object_map = { math.tan: NO_IMPLEMENT, # user defined - RowTensor: F.make_row_tensor, + RowTensor: F.make_row_tensor, SparseTensor: F.make_sparse_tensor, } diff --git a/mindspore/ccsrc/debug/trace.cc b/mindspore/ccsrc/debug/trace.cc index 15888ee2779..f450dec2ba5 100644 --- a/mindspore/ccsrc/debug/trace.cc +++ b/mindspore/ccsrc/debug/trace.cc @@ -506,7 +506,7 @@ void GetEvalStackInfo(std::ostringstream &oss) { } OutputAnalyzedGraphWithType(file_name); - oss << "\nThe function call stack (See file '" << file_name << "' for details):\n"; + oss << "\nThe function call stack (See file '" << file_name << "' for more details):\n"; int index = 0; std::string last_location_info = ""; diff --git a/mindspore/ccsrc/frontend/optimizer/irpass/symbol_resolver.h b/mindspore/ccsrc/frontend/optimizer/irpass/symbol_resolver.h index cc5972fa948..68545b213b3 100644 --- a/mindspore/ccsrc/frontend/optimizer/irpass/symbol_resolver.h +++ b/mindspore/ccsrc/frontend/optimizer/irpass/symbol_resolver.h @@ -130,7 +130,7 @@ class ResolverResolveAndGetAttr : public OptimizerCaller { resolver_optimizers_ = {std::make_shared(), std::make_shared(), std::make_shared()}; } - ~ResolverResolveAndGetAttr() = default; + virtual ~ResolverResolveAndGetAttr() = default; AnfNodePtr operator()(const OptimizerPtr &optimizer, const AnfNodePtr &node) override { AnfNodePtr new_node; diff --git a/mindspore/ccsrc/pipeline/jit/parse/parse.cc b/mindspore/ccsrc/pipeline/jit/parse/parse.cc index ca82d33fde8..93204e8da2b 100644 --- a/mindspore/ccsrc/pipeline/jit/parse/parse.cc +++ b/mindspore/ccsrc/pipeline/jit/parse/parse.cc @@ -756,13 +756,14 @@ AnfNodePtr Parser::ParseAttribute(const FunctionBlockPtr &block, const py::objec // Process comparison expression : a == b. a > b etc. AnfNodePtr Parser::ParseCompare(const FunctionBlockPtr &block, const py::object &node) { MS_LOG(DEBUG) << "Process ast Compare"; + TraceGuard guard(GetLocation(node)); // For python comparison ,there may be if x>y>5 , // Which there is two ops , but we only support one now py::list ops = python_adapter::GetPyObjAttr(node, "ops"); if (ops.size() > MAX_COMPARISON_OPS_SUPPORTED) { - MS_LOG(ERROR) << "MindSpore does not support comparison with operators more than one now, ops size =" << ops.size(); - return nullptr; + MS_EXCEPTION(NotSupportError) + << "MindSpore does not support comparison with operators more than one now, ops size =" << ops.size(); } py::object left = python_adapter::GetPyObjAttr(node, "left"); @@ -1500,14 +1501,17 @@ void Parser::HandleAssignClassMember(const FunctionBlockPtr &block, const py::ob // Now only support the self.xxx = yyy, where self.xxx must be a defined Parameter type if (!py::hasattr(ast()->obj(), common::SafeCStr(attr_name))) { - MS_EXCEPTION(TypeError) << "'" << var_name << "' should be a Parameter, but not defined."; + MS_EXCEPTION(TypeError) << "'" << var_name << "' should be defined in the class '__init__' function. \n\n" + << trace::GetDebugInfo(target_node->debug_info()); } auto obj = ast()->obj().attr(common::SafeCStr(attr_name)); auto obj_type = obj.attr("__class__").attr("__name__"); if (!py::hasattr(obj, "__parameter__")) { - MS_EXCEPTION(TypeError) << "'" << var_name << "' should be a Parameter, but got '" + MS_EXCEPTION(TypeError) << "'" << var_name + << "' should be defined with a Parameter type in the class '__init__' function, but got '" << py::str(obj).cast() << "' with type '" - << py::str(obj_type).cast() << "."; + << py::str(obj_type).cast() << ".\n\n" + << trace::GetDebugInfo(target_node->debug_info()); } MS_EXCEPTION_IF_NULL(block); @@ -1530,14 +1534,17 @@ void Parser::HandleAssignSubscript(const FunctionBlockPtr &block, const py::obje auto attr_name = value_obj.attr("attr").cast(); var_name = "self." + attr_name; if (!py::hasattr(ast()->obj(), common::SafeCStr(attr_name))) { - MS_EXCEPTION(TypeError) << "'" << var_name << "' was not defined in the class '__init__' function."; + MS_EXCEPTION(TypeError) << "'" << var_name << "' was not defined in the class '__init__' function.\n\n" + << trace::GetDebugInfo(value_node->debug_info()); } auto obj = ast()->obj().attr(common::SafeCStr(attr_name)); auto obj_type = obj.attr("__class__").attr("__name__"); if (!py::hasattr(obj, "__parameter__")) { - MS_EXCEPTION(TypeError) << "'" << var_name << "' should be a Parameter, but got '" + MS_EXCEPTION(TypeError) << "'" << var_name + << "' should be defined with a Parameter in the class '__init__' function, but got '" << py::str(obj).cast() << "' with type '" - << py::str(obj_type).cast() << "'."; + << py::str(obj_type).cast() << "'.\n\n" + << trace::GetDebugInfo(value_node->debug_info()); } block->WriteVariable(var_name, setitem_app); return; @@ -1548,7 +1555,8 @@ void Parser::HandleAssignSubscript(const FunctionBlockPtr &block, const py::obje return; } if (!py::hasattr(value_obj, "id")) { - MS_EXCEPTION(TypeError) << "Attribute id not found in " << py::str(value_obj).cast(); + MS_EXCEPTION(TypeError) << "Attribute id not found in " << py::str(value_obj).cast() << "\n\n" + << trace::GetDebugInfo(value_node->debug_info()); } var_name = value_obj.attr("id").cast(); block->WriteVariable(var_name, setitem_app); @@ -1567,11 +1575,11 @@ void Parser::WriteAssignVars(const FunctionBlockPtr &block, const py::object &ta } else if (ast_->IsClassMember(targ)) { HandleAssignClassMember(block, targ, value_node); } else if (ast_type == AST_SUB_TYPE_ATTRIBUTE) { - MS_LOG(EXCEPTION) << "The subnet attributes cannot be changed in the network" - << " NodeInfo: " << trace::GetDebugInfo(value_node->debug_info()); + MS_LOG(EXCEPTION) << "The subnet attributes cannot be changed in the network. \n\n" + << trace::GetDebugInfo(value_node->debug_info()); } else { - MS_LOG(EXCEPTION) << "Not supported assign type: " << ast_type - << " NodeInfo: " << trace::GetDebugInfo(value_node->debug_info()); + MS_LOG(EXCEPTION) << "Not support this assign type: " << ast_type << "\n\n" + << trace::GetDebugInfo(value_node->debug_info()); } } @@ -1855,6 +1863,7 @@ FuncGraphPtr MakeTopGraph(const py::object &cell, const ValuePtr &cell_ptr) { MS_LOG(EXCEPTION) << "Current graph cast failed from " << cell_ptr->ToString(); } + TraceGuard guard(current_graph->debug_info()->location()); auto func_graph = std::make_shared(); func_graph->debug_info()->set_name(current_graph->debug_info()->name() + "_wrapper"); diff --git a/mindspore/ccsrc/pipeline/jit/static_analysis/evaluator.cc b/mindspore/ccsrc/pipeline/jit/static_analysis/evaluator.cc index f06e1d3a656..6ebc9659d9c 100644 --- a/mindspore/ccsrc/pipeline/jit/static_analysis/evaluator.cc +++ b/mindspore/ccsrc/pipeline/jit/static_analysis/evaluator.cc @@ -207,9 +207,9 @@ EvalResultPtr BaseFuncGraphEvaluator::Eval(AnalysisEnginePtr engine, const Abstr MS_EXCEPTION_IF_NULL(fg); std::size_t nargs = fg->parameters().size(); if (args_abs_list.size() != nargs) { - MS_EXCEPTION(TypeError) << "Function " << fg->ToString() << ", The number of parameters of this function is " + MS_EXCEPTION(TypeError) << "For function " << fg->ToString() << ", the number of parameters of this function is " << fg->parameters().size() << ", but the number of provided arguments is " - << args_abs_list.size() << ". NodeInfo: " << trace::GetDebugInfo(fg->debug_info()); + << args_abs_list.size() << "."; } MS_EXCEPTION_IF_NULL(parent_context_); context_ = parent_context_->NewFuncGraphContext(fg, args_abs_list); diff --git a/mindspore/ccsrc/pipeline/jit/static_analysis/prim.cc b/mindspore/ccsrc/pipeline/jit/static_analysis/prim.cc index a1da2579666..9467f757e0d 100644 --- a/mindspore/ccsrc/pipeline/jit/static_analysis/prim.cc +++ b/mindspore/ccsrc/pipeline/jit/static_analysis/prim.cc @@ -916,7 +916,8 @@ EvalResultPtr GetEvaluatedValueForBuiltinTypeAttrOrMethod(const AnalysisEnginePt if (require.empty()) { require = pipeline::Resource::GetAttrPtr(data_type->type_id(), item_name); if (require.empty()) { - MS_LOG(EXCEPTION) << "The object of type: " << data_type->ToString() << " has no method or attr: " << item_name; + MS_LOG(EXCEPTION) << "Mindspore don't support this usage '" << item_name << "' for the object with type <" + << data_type->ToString() << ">."; } require_type = REQUIRE_TYPE::ATTR; } @@ -1153,8 +1154,8 @@ class CreateInstanceEvaluator : public TransitionPrimEvaluator { // Create class instance. auto obj = parse::data_converter::CreatePythonObject(class_type, params); if (py::isinstance(obj)) { - MS_LOG(EXCEPTION) << "Create python object" << py::str(class_type) - << " failed, only support create Cell or Primitive object."; + MS_LOG(EXCEPTION) << "Create python object `" << py::str(class_type) + << "` failed, only support create Cell or Primitive object."; } // Process the object. diff --git a/mindspore/ccsrc/pipeline/jit/static_analysis/static_analysis.cc b/mindspore/ccsrc/pipeline/jit/static_analysis/static_analysis.cc index 9af15b19fef..fa83c5ee519 100644 --- a/mindspore/ccsrc/pipeline/jit/static_analysis/static_analysis.cc +++ b/mindspore/ccsrc/pipeline/jit/static_analysis/static_analysis.cc @@ -283,8 +283,7 @@ EvalResultPtr AnalysisEngine::EvalCNode(const CNodePtr &cnode, const AnfNodeConf } AbstractFunctionPtr func = dyn_cast(maybe_func); if (func == nullptr) { - MS_LOG(EXCEPTION) << "Not AbstractFunction: " << maybe_func->ToString() - << ", NodeInfo: " << trace::GetDebugInfo(cnode->debug_info()); + MS_LOG(EXCEPTION) << "Not AbstractFunction: " << maybe_func->ToString() << "."; } ConfigPtrList args_conf_list; diff --git a/mindspore/core/abstract/abstract_value.cc b/mindspore/core/abstract/abstract_value.cc index 8c35434f2d9..9bbaa675668 100644 --- a/mindspore/core/abstract/abstract_value.cc +++ b/mindspore/core/abstract/abstract_value.cc @@ -85,7 +85,7 @@ std::string AbstractBase::ToString() const { MS_EXCEPTION_IF_NULL(type_); MS_EXCEPTION_IF_NULL(shape_); buffer << type_name() << "(" - << "Type: " << type_->ToString() << " Value: " << value << " Shape: " << shape_->ToString() << ")"; + << "Type: " << type_->ToString() << ", Value: " << value << ", Shape: " << shape_->ToString() << ")"; return buffer.str(); } diff --git a/mindspore/core/abstract/prim_statement.cc b/mindspore/core/abstract/prim_statement.cc index 46778b6b410..81821a4b884 100644 --- a/mindspore/core/abstract/prim_statement.cc +++ b/mindspore/core/abstract/prim_statement.cc @@ -63,7 +63,7 @@ AbstractBasePtr InferImplSwitch(const AnalysisEnginePtr &, const PrimitivePtr &p } } - MS_LOG(EXCEPTION) << "Invalid condition value for switch " << cond->ToString(); + MS_LOG(EXCEPTION) << "Not support this condition value: " << cond->GetValueTrack()->ToString(); } AbstractBasePtr InferImplSwitchLayer(const AnalysisEnginePtr &, const PrimitivePtr &primitive, @@ -130,8 +130,8 @@ AbstractBasePtr InferImplIs_(const AnalysisEnginePtr &, const PrimitivePtr &prim CheckArgsSize(op_name, args_spec_list, 2); ValuePtr t = args_spec_list[1]->BuildValue(); if (!SupportedIsTargetValue(t)) { - MS_LOG(EXCEPTION) << "Not supported type:" << t->ToString() - << " for statement is, supported list is:None, False, True "; + MS_LOG(EXCEPTION) << "This comparator '" << t->ToString() + << "' is not supported. For statement 'is', only support compare with 'None', 'False' or 'True'"; } ValuePtr x = args_spec_list[0]->BuildValue(); @@ -146,8 +146,9 @@ AbstractBasePtr InferImplIsNot(const AnalysisEnginePtr &, const PrimitivePtr &pr CheckArgsSize(op_name, args_spec_list, 2); ValuePtr t = args_spec_list[1]->BuildValue(); if (!SupportedIsTargetValue(t)) { - MS_LOG(EXCEPTION) << "Not supported type:" << t->ToString() - << " for statement is not, supported list is:None, False, True "; + MS_LOG(EXCEPTION) + << "This comparator '" << t->ToString() + << "' is not supported. For statement 'is not' , only support compare with 'None', 'False' or 'True'"; } ValuePtr x = args_spec_list[0]->BuildValue(); diff --git a/mindspore/nn/layer/container.py b/mindspore/nn/layer/container.py index f084cdc9e25..eb4821fa23b 100644 --- a/mindspore/nn/layer/container.py +++ b/mindspore/nn/layer/container.py @@ -32,7 +32,7 @@ def _valid_index(cell_num, index): def _valid_cell(cell): if issubclass(cell.__class__, Cell): return True - raise TypeError('Cell {} is not subclass of Cell'.format(cell)) + raise TypeError('`{}` is not a subclass of Cell. Please check your code'.format(cell)) def _get_prefix_and_index(cells): diff --git a/tests/ut/python/dtype/test_dictionary.py b/tests/ut/python/dtype/test_dictionary.py index 052372dd399..f467f8ffce2 100644 --- a/tests/ut/python/dtype/test_dictionary.py +++ b/tests/ut/python/dtype/test_dictionary.py @@ -90,9 +90,8 @@ def test_dict_set_or_get_item(): return ret net = DictNet() - with pytest.raises(TypeError) as ex: + with pytest.raises(TypeError): net() - assert "'self.dict_' should be a Parameter" in str(ex.value) def test_dict_set_or_get_item_2(): @@ -138,9 +137,8 @@ def test_dict_set_or_get_item_3(): return self.dict_["x"] net = DictNet() - with pytest.raises(TypeError) as ex: + with pytest.raises(TypeError): net() - assert "'self.dict_' should be a Parameter" in str(ex.value) def test_dict_set_item(): diff --git a/tests/ut/python/dtype/test_list.py b/tests/ut/python/dtype/test_list.py index 3ec19a9b1f7..f0b7db15395 100644 --- a/tests/ut/python/dtype/test_list.py +++ b/tests/ut/python/dtype/test_list.py @@ -142,9 +142,8 @@ def test_class_member_list_append(): y = Tensor(np.zeros([3, 4, 5], np.int32)) z = [[1, 2], 3] net = Net(z) - with pytest.raises(TypeError) as ex: + with pytest.raises(TypeError): net(x, y) - assert "'self.z' should be a Parameter, but got '[[1, 2], 3]' with type 'list'." in str(ex.value) def test_class_member_not_defined(): @@ -180,9 +179,8 @@ def test_change_list_element(): y = Tensor(np.zeros([3, 4, 5], np.int32)) z = [[1, 2], 3] net = Net(z) - with pytest.raises(TypeError) as ex: + with pytest.raises(TypeError): net(x, y) - assert "'self.z' should be a Parameter, but got '[[1, 2], 3]' with type 'list'." in str(ex.value) class ListOperate(nn.Cell): diff --git a/tests/ut/python/pipeline/parse/test_dtype_and_shape_as_attr.py b/tests/ut/python/pipeline/parse/test_dtype_and_shape_as_attr.py index f46242d7f64..3756c8da283 100644 --- a/tests/ut/python/pipeline/parse/test_dtype_and_shape_as_attr.py +++ b/tests/ut/python/pipeline/parse/test_dtype_and_shape_as_attr.py @@ -71,9 +71,8 @@ def test_type_not_have_the_attr(): net = Net() x = Tensor(np.ones([1, 2, 3], np.int32)) - with pytest.raises(RuntimeError) as ex: + with pytest.raises(RuntimeError): net(x) - assert "The object of type: Tensor[Int32] has no method or attr: shapes" in str(ex.value) def test_type_not_have_the_method(): @@ -87,6 +86,5 @@ def test_type_not_have_the_method(): net = Net() x = Tensor(np.ones([1, 2, 3], np.int32)) - with pytest.raises(RuntimeError) as ex: + with pytest.raises(RuntimeError): net(x) - assert "The object of type: Tensor[Int32] has no method or attr: dtypes" in str(ex.value) diff --git a/tests/ut/python/pipeline/parse/test_use_undefined_name_or_unsupported_builtin_function.py b/tests/ut/python/pipeline/parse/test_use_undefined_name_or_unsupported_builtin_function.py index 632cd305eca..01b989b3916 100644 --- a/tests/ut/python/pipeline/parse/test_use_undefined_name_or_unsupported_builtin_function.py +++ b/tests/ut/python/pipeline/parse/test_use_undefined_name_or_unsupported_builtin_function.py @@ -394,4 +394,4 @@ def test_use_defined_class_obj_in_for(): net = Net() with pytest.raises(TypeError) as err: net(Tensor([1, 2, 3], mstype.float32)) - assert "Invalid object with type" in str(err.value) + assert "Not support for this object with type" in str(err.value)