!16345 [DFX]Optimize exception log

From: @irmo
Reviewed-by: 
Signed-off-by:
This commit is contained in:
mindspore-ci-bot 2021-05-31 09:22:48 +08:00 committed by Gitee
commit 8c1e7ce231
15 changed files with 51 additions and 47 deletions

View File

@ -164,7 +164,7 @@ def resolve_symbol(namespace, symbol):
resolve_ = convert_object_map.get(resolve_) resolve_ = convert_object_map.get(resolve_)
logger.debug("convert resolve = %r", resolve_) logger.debug("convert resolve = %r", resolve_)
if resolve_ == NO_IMPLEMENT: if resolve_ == NO_IMPLEMENT:
raise NotImplementedError("not implemented for ", str(symbol)) raise NotImplementedError(f"Not support for `{symbol}`")
except Exception as e: except Exception as e:
if isinstance(e, NotImplementedError): if isinstance(e, NotImplementedError):
raise e raise e
@ -248,7 +248,7 @@ def get_obj_type(obj):
else: else:
# here for ndarray, just print its shape (in case of the array to large and print many data in screen) # 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') 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}`.') f'`{obj.shape if is_ndarray else obj}`.')
return obj_type return obj_type

View File

@ -120,13 +120,13 @@ convert_object_map = {
T.iter: M.ms_iter, T.iter: M.ms_iter,
T.next: M.ms_next, T.next: M.ms_next,
T.hasnext: M.hasnext, T.hasnext: M.hasnext,
T.MakeTuple: F.make_tuple,
T.MakeTuple: F.make_tuple,
T.make_dict: F.make_dict, T.make_dict: F.make_dict,
T.make_list: F.make_list, T.make_list: F.make_list,
T.make_slice: F.make_slice, T.make_slice: F.make_slice,
T.range: F.make_range, T.range: F.make_range,
T.while_cond: M.while_cond, T.while_cond: M.while_cond,
# lib function # lib function
math.floor: NO_IMPLEMENT, math.floor: NO_IMPLEMENT,
math.trunc: NO_IMPLEMENT, math.trunc: NO_IMPLEMENT,
@ -137,6 +137,6 @@ convert_object_map = {
math.tan: NO_IMPLEMENT, math.tan: NO_IMPLEMENT,
# user defined # user defined
RowTensor: F.make_row_tensor, RowTensor: F.make_row_tensor,
SparseTensor: F.make_sparse_tensor, SparseTensor: F.make_sparse_tensor,
} }

View File

@ -506,7 +506,7 @@ void GetEvalStackInfo(std::ostringstream &oss) {
} }
OutputAnalyzedGraphWithType(file_name); 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; int index = 0;
std::string last_location_info = ""; std::string last_location_info = "";

View File

@ -130,7 +130,7 @@ class ResolverResolveAndGetAttr : public OptimizerCaller {
resolver_optimizers_ = {std::make_shared<ResolverGetAttrResolve>(), std::make_shared<ResolverResolve>(), resolver_optimizers_ = {std::make_shared<ResolverGetAttrResolve>(), std::make_shared<ResolverResolve>(),
std::make_shared<ResolverGetAttr>()}; std::make_shared<ResolverGetAttr>()};
} }
~ResolverResolveAndGetAttr() = default; virtual ~ResolverResolveAndGetAttr() = default;
AnfNodePtr operator()(const OptimizerPtr &optimizer, const AnfNodePtr &node) override { AnfNodePtr operator()(const OptimizerPtr &optimizer, const AnfNodePtr &node) override {
AnfNodePtr new_node; AnfNodePtr new_node;

View File

@ -756,13 +756,14 @@ AnfNodePtr Parser::ParseAttribute(const FunctionBlockPtr &block, const py::objec
// Process comparison expression : a == b. a > b etc. // Process comparison expression : a == b. a > b etc.
AnfNodePtr Parser::ParseCompare(const FunctionBlockPtr &block, const py::object &node) { AnfNodePtr Parser::ParseCompare(const FunctionBlockPtr &block, const py::object &node) {
MS_LOG(DEBUG) << "Process ast Compare"; MS_LOG(DEBUG) << "Process ast Compare";
TraceGuard guard(GetLocation(node));
// For python comparison ,there may be if x>y>5 , // For python comparison ,there may be if x>y>5 ,
// Which there is two ops , but we only support one now // Which there is two ops , but we only support one now
py::list ops = python_adapter::GetPyObjAttr(node, "ops"); py::list ops = python_adapter::GetPyObjAttr(node, "ops");
if (ops.size() > MAX_COMPARISON_OPS_SUPPORTED) { 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(); MS_EXCEPTION(NotSupportError)
return nullptr; << "MindSpore does not support comparison with operators more than one now, ops size =" << ops.size();
} }
py::object left = python_adapter::GetPyObjAttr(node, "left"); 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 // 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))) { 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 = ast()->obj().attr(common::SafeCStr(attr_name));
auto obj_type = obj.attr("__class__").attr("__name__"); auto obj_type = obj.attr("__class__").attr("__name__");
if (!py::hasattr(obj, "__parameter__")) { 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<std::string>() << "' with type '" << py::str(obj).cast<std::string>() << "' with type '"
<< py::str(obj_type).cast<std::string>() << "."; << py::str(obj_type).cast<std::string>() << ".\n\n"
<< trace::GetDebugInfo(target_node->debug_info());
} }
MS_EXCEPTION_IF_NULL(block); 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<std::string>(); auto attr_name = value_obj.attr("attr").cast<std::string>();
var_name = "self." + attr_name; var_name = "self." + attr_name;
if (!py::hasattr(ast()->obj(), common::SafeCStr(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 = ast()->obj().attr(common::SafeCStr(attr_name));
auto obj_type = obj.attr("__class__").attr("__name__"); auto obj_type = obj.attr("__class__").attr("__name__");
if (!py::hasattr(obj, "__parameter__")) { 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<std::string>() << "' with type '" << py::str(obj).cast<std::string>() << "' with type '"
<< py::str(obj_type).cast<std::string>() << "'."; << py::str(obj_type).cast<std::string>() << "'.\n\n"
<< trace::GetDebugInfo(value_node->debug_info());
} }
block->WriteVariable(var_name, setitem_app); block->WriteVariable(var_name, setitem_app);
return; return;
@ -1548,7 +1555,8 @@ void Parser::HandleAssignSubscript(const FunctionBlockPtr &block, const py::obje
return; return;
} }
if (!py::hasattr(value_obj, "id")) { if (!py::hasattr(value_obj, "id")) {
MS_EXCEPTION(TypeError) << "Attribute id not found in " << py::str(value_obj).cast<std::string>(); MS_EXCEPTION(TypeError) << "Attribute id not found in " << py::str(value_obj).cast<std::string>() << "\n\n"
<< trace::GetDebugInfo(value_node->debug_info());
} }
var_name = value_obj.attr("id").cast<std::string>(); var_name = value_obj.attr("id").cast<std::string>();
block->WriteVariable(var_name, setitem_app); 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)) { } else if (ast_->IsClassMember(targ)) {
HandleAssignClassMember(block, targ, value_node); HandleAssignClassMember(block, targ, value_node);
} else if (ast_type == AST_SUB_TYPE_ATTRIBUTE) { } else if (ast_type == AST_SUB_TYPE_ATTRIBUTE) {
MS_LOG(EXCEPTION) << "The subnet attributes cannot be changed in the network" MS_LOG(EXCEPTION) << "The subnet attributes cannot be changed in the network. \n\n"
<< " NodeInfo: " << trace::GetDebugInfo(value_node->debug_info()); << trace::GetDebugInfo(value_node->debug_info());
} else { } else {
MS_LOG(EXCEPTION) << "Not supported assign type: " << ast_type MS_LOG(EXCEPTION) << "Not support this assign type: " << ast_type << "\n\n"
<< " NodeInfo: " << trace::GetDebugInfo(value_node->debug_info()); << 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(); MS_LOG(EXCEPTION) << "Current graph cast failed from " << cell_ptr->ToString();
} }
TraceGuard guard(current_graph->debug_info()->location());
auto func_graph = std::make_shared<FuncGraph>(); auto func_graph = std::make_shared<FuncGraph>();
func_graph->debug_info()->set_name(current_graph->debug_info()->name() + "_wrapper"); func_graph->debug_info()->set_name(current_graph->debug_info()->name() + "_wrapper");

View File

@ -207,9 +207,9 @@ EvalResultPtr BaseFuncGraphEvaluator::Eval(AnalysisEnginePtr engine, const Abstr
MS_EXCEPTION_IF_NULL(fg); MS_EXCEPTION_IF_NULL(fg);
std::size_t nargs = fg->parameters().size(); std::size_t nargs = fg->parameters().size();
if (args_abs_list.size() != nargs) { 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 " << 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_); MS_EXCEPTION_IF_NULL(parent_context_);
context_ = parent_context_->NewFuncGraphContext(fg, args_abs_list); context_ = parent_context_->NewFuncGraphContext(fg, args_abs_list);

View File

@ -916,7 +916,8 @@ EvalResultPtr GetEvaluatedValueForBuiltinTypeAttrOrMethod(const AnalysisEnginePt
if (require.empty()) { if (require.empty()) {
require = pipeline::Resource::GetAttrPtr(data_type->type_id(), item_name); require = pipeline::Resource::GetAttrPtr(data_type->type_id(), item_name);
if (require.empty()) { 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; require_type = REQUIRE_TYPE::ATTR;
} }
@ -1153,8 +1154,8 @@ class CreateInstanceEvaluator : public TransitionPrimEvaluator {
// Create class instance. // Create class instance.
auto obj = parse::data_converter::CreatePythonObject(class_type, params); auto obj = parse::data_converter::CreatePythonObject(class_type, params);
if (py::isinstance<py::none>(obj)) { if (py::isinstance<py::none>(obj)) {
MS_LOG(EXCEPTION) << "Create python object" << py::str(class_type) MS_LOG(EXCEPTION) << "Create python object `" << py::str(class_type)
<< " failed, only support create Cell or Primitive object."; << "` failed, only support create Cell or Primitive object.";
} }
// Process the object. // Process the object.

View File

@ -283,8 +283,7 @@ EvalResultPtr AnalysisEngine::EvalCNode(const CNodePtr &cnode, const AnfNodeConf
} }
AbstractFunctionPtr func = dyn_cast<AbstractFunction>(maybe_func); AbstractFunctionPtr func = dyn_cast<AbstractFunction>(maybe_func);
if (func == nullptr) { if (func == nullptr) {
MS_LOG(EXCEPTION) << "Not AbstractFunction: " << maybe_func->ToString() MS_LOG(EXCEPTION) << "Not AbstractFunction: " << maybe_func->ToString() << ".";
<< ", NodeInfo: " << trace::GetDebugInfo(cnode->debug_info());
} }
ConfigPtrList args_conf_list; ConfigPtrList args_conf_list;

View File

@ -85,7 +85,7 @@ std::string AbstractBase::ToString() const {
MS_EXCEPTION_IF_NULL(type_); MS_EXCEPTION_IF_NULL(type_);
MS_EXCEPTION_IF_NULL(shape_); MS_EXCEPTION_IF_NULL(shape_);
buffer << type_name() << "(" buffer << type_name() << "("
<< "Type: " << type_->ToString() << " Value: " << value << " Shape: " << shape_->ToString() << ")"; << "Type: " << type_->ToString() << ", Value: " << value << ", Shape: " << shape_->ToString() << ")";
return buffer.str(); return buffer.str();
} }

View File

@ -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, 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); CheckArgsSize(op_name, args_spec_list, 2);
ValuePtr t = args_spec_list[1]->BuildValue(); ValuePtr t = args_spec_list[1]->BuildValue();
if (!SupportedIsTargetValue(t)) { if (!SupportedIsTargetValue(t)) {
MS_LOG(EXCEPTION) << "Not supported type:" << t->ToString() MS_LOG(EXCEPTION) << "This comparator '" << t->ToString()
<< " for statement is, supported list is:None, False, True "; << "' is not supported. For statement 'is', only support compare with 'None', 'False' or 'True'";
} }
ValuePtr x = args_spec_list[0]->BuildValue(); 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); CheckArgsSize(op_name, args_spec_list, 2);
ValuePtr t = args_spec_list[1]->BuildValue(); ValuePtr t = args_spec_list[1]->BuildValue();
if (!SupportedIsTargetValue(t)) { if (!SupportedIsTargetValue(t)) {
MS_LOG(EXCEPTION) << "Not supported type:" << t->ToString() MS_LOG(EXCEPTION)
<< " for statement is not, supported list is:None, False, True "; << "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(); ValuePtr x = args_spec_list[0]->BuildValue();

View File

@ -32,7 +32,7 @@ def _valid_index(cell_num, index):
def _valid_cell(cell): def _valid_cell(cell):
if issubclass(cell.__class__, Cell): if issubclass(cell.__class__, Cell):
return True 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): def _get_prefix_and_index(cells):

View File

@ -90,9 +90,8 @@ def test_dict_set_or_get_item():
return ret return ret
net = DictNet() net = DictNet()
with pytest.raises(TypeError) as ex: with pytest.raises(TypeError):
net() net()
assert "'self.dict_' should be a Parameter" in str(ex.value)
def test_dict_set_or_get_item_2(): def test_dict_set_or_get_item_2():
@ -138,9 +137,8 @@ def test_dict_set_or_get_item_3():
return self.dict_["x"] return self.dict_["x"]
net = DictNet() net = DictNet()
with pytest.raises(TypeError) as ex: with pytest.raises(TypeError):
net() net()
assert "'self.dict_' should be a Parameter" in str(ex.value)
def test_dict_set_item(): def test_dict_set_item():

View File

@ -142,9 +142,8 @@ def test_class_member_list_append():
y = Tensor(np.zeros([3, 4, 5], np.int32)) y = Tensor(np.zeros([3, 4, 5], np.int32))
z = [[1, 2], 3] z = [[1, 2], 3]
net = Net(z) net = Net(z)
with pytest.raises(TypeError) as ex: with pytest.raises(TypeError):
net(x, y) 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(): def test_class_member_not_defined():
@ -180,9 +179,8 @@ def test_change_list_element():
y = Tensor(np.zeros([3, 4, 5], np.int32)) y = Tensor(np.zeros([3, 4, 5], np.int32))
z = [[1, 2], 3] z = [[1, 2], 3]
net = Net(z) net = Net(z)
with pytest.raises(TypeError) as ex: with pytest.raises(TypeError):
net(x, y) 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): class ListOperate(nn.Cell):

View File

@ -71,9 +71,8 @@ def test_type_not_have_the_attr():
net = Net() net = Net()
x = Tensor(np.ones([1, 2, 3], np.int32)) x = Tensor(np.ones([1, 2, 3], np.int32))
with pytest.raises(RuntimeError) as ex: with pytest.raises(RuntimeError):
net(x) 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(): def test_type_not_have_the_method():
@ -87,6 +86,5 @@ def test_type_not_have_the_method():
net = Net() net = Net()
x = Tensor(np.ones([1, 2, 3], np.int32)) x = Tensor(np.ones([1, 2, 3], np.int32))
with pytest.raises(RuntimeError) as ex: with pytest.raises(RuntimeError):
net(x) net(x)
assert "The object of type: Tensor[Int32] has no method or attr: dtypes" in str(ex.value)

View File

@ -394,4 +394,4 @@ def test_use_defined_class_obj_in_for():
net = Net() net = Net()
with pytest.raises(TypeError) as err: with pytest.raises(TypeError) as err:
net(Tensor([1, 2, 3], mstype.float32)) 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)