diff --git a/mindspore/ccsrc/debug/trace.cc b/mindspore/ccsrc/debug/trace.cc index f450dec2ba5..f32c9bbe603 100644 --- a/mindspore/ccsrc/debug/trace.cc +++ b/mindspore/ccsrc/debug/trace.cc @@ -122,7 +122,7 @@ class AnalyzedFuncGraphExporter : public AnfExporter { AnalyzedFuncGraphExporter() : AnfExporter(true, false) {} ~AnalyzedFuncGraphExporter() override = default; - void ExportFuncGraph(const std::string &filename, const std::vector &node_cfgs); + bool ExportFuncGraph(const std::string &filename, const std::vector &node_cfgs); void ExportOneFuncGraph(std::ofstream &ofs, const FuncGraphPtr &func_graph); void OutputCNodes(std::ofstream &ofs, const std::vector &nodes, const FuncGraphPtr &func_graph); @@ -156,9 +156,9 @@ std::unordered_map CalcTaggedFuncGraphs() { return tagged_func_graphs; } -void OutputAnalyzedGraphWithType(const string &file_path) { +bool OutputAnalyzedGraphWithType(const string &file_path) { AnalyzedFuncGraphExporter exporter; - exporter.ExportFuncGraph(file_path, GetCNodeDebugStack()); + return exporter.ExportFuncGraph(file_path, GetCNodeDebugStack()); } std::string AnalyzedFuncGraphExporter::GetNodeType(const AnfNodePtr &node) { @@ -420,11 +420,11 @@ void AnalyzedFuncGraphExporter::ExportOneFuncGraph(std::ofstream &ofs, const Fun ofs << "}\n"; } -void AnalyzedFuncGraphExporter::ExportFuncGraph(const std::string &filename, +bool AnalyzedFuncGraphExporter::ExportFuncGraph(const std::string &filename, const std::vector &node_cfgs) { if (node_cfgs.empty()) { MS_LOG(DEBUG) << "Node configs is empty"; - return; + return false; } context_map_.clear(); @@ -433,7 +433,7 @@ void AnalyzedFuncGraphExporter::ExportFuncGraph(const std::string &filename, std::ofstream ofs(filename); if (!ofs.is_open()) { MS_LOG(ERROR) << "Open file '" << filename << "' failed!"; - return; + return false; } param_index = 1; @@ -484,6 +484,7 @@ void AnalyzedFuncGraphExporter::ExportFuncGraph(const std::string &filename, ofs << "# num of total function graphs: " << context_map_.size() << "\n"; ofs.close(); + return true; } void GetEvalStackInfo(std::ostringstream &oss) { @@ -505,8 +506,12 @@ void GetEvalStackInfo(std::ostringstream &oss) { file_name = realpath.value(); } - OutputAnalyzedGraphWithType(file_name); - oss << "\nThe function call stack (See file '" << file_name << "' for more details):\n"; + auto ret = OutputAnalyzedGraphWithType(file_name); + oss << "\nThe function call stack"; + if (ret) { + oss << " (See file '" << file_name << "' for more details)"; + } + oss << ":\n"; int index = 0; std::string last_location_info = ""; diff --git a/mindspore/ccsrc/pipeline/jit/parse/function_block.cc b/mindspore/ccsrc/pipeline/jit/parse/function_block.cc index 157802bedb6..b92791707bd 100644 --- a/mindspore/ccsrc/pipeline/jit/parse/function_block.cc +++ b/mindspore/ccsrc/pipeline/jit/parse/function_block.cc @@ -101,7 +101,7 @@ AnfNodePtr FunctionBlock::ReadVariable(const std::string &var) { } return node; } - // Get var from predecessor block ,if can't get the make a resolve node to it + // Get var from predecessor block ,if can't get then make a resolve node to it if (matured_) { // If only one predecessor block, read the definition of var from it. if (prev_blocks_.size() == 1) { @@ -317,7 +317,7 @@ void FunctionBlock::Mature() { matured_ = true; } -// Force the conditIon node to bool using bool operation +// Force the condition node to bool using bool operation CNodePtr FunctionBlock::ForceToBoolNode(const AnfNodePtr &cond) { TraceGuard trace_guard(std::make_shared(cond->debug_info())); CNodePtr op_apply_node = func_graph()->NewCNodeInOrder({MakeResolveOperation(NAMED_PRIMITIVE_BOOL), cond}); diff --git a/mindspore/ccsrc/pipeline/jit/parse/parse.cc b/mindspore/ccsrc/pipeline/jit/parse/parse.cc index 93204e8da2b..de5738cc257 100644 --- a/mindspore/ccsrc/pipeline/jit/parse/parse.cc +++ b/mindspore/ccsrc/pipeline/jit/parse/parse.cc @@ -1863,9 +1863,9 @@ 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"); + func_graph->debug_info()->set_location(current_graph->debug_info()->location()); // Copy all parameters information for (auto ¶ : current_graph->parameters()) { @@ -1874,6 +1874,7 @@ FuncGraphPtr MakeTopGraph(const py::object &cell, const ValuePtr &cell_ptr) { auto name = orig_param->name(); param->set_name(name); param->debug_info()->set_name(name); + param->debug_info()->set_location(param->debug_info()->location()); } func_graph->set_has_vararg(current_graph->has_vararg()); func_graph->set_has_kwarg(current_graph->has_kwarg()); @@ -1892,6 +1893,7 @@ FuncGraphPtr MakeTopGraph(const py::object &cell, const ValuePtr &cell_ptr) { } auto unpacking = func_graph->has_vararg() || func_graph->has_kwarg(); + TraceGuard guard(current_graph->get_return()->debug_info()->location()); if (!unpacking) { std::vector inputs; inputs.emplace_back(NewValueNode(cell_ptr)); diff --git a/mindspore/ccsrc/pipeline/jit/parse/resolve.cc b/mindspore/ccsrc/pipeline/jit/parse/resolve.cc index 256de5255cd..207ca6317b2 100644 --- a/mindspore/ccsrc/pipeline/jit/parse/resolve.cc +++ b/mindspore/ccsrc/pipeline/jit/parse/resolve.cc @@ -296,7 +296,6 @@ AnfNodePtr ResolveSymbol(const FuncGraphManagerPtr &manager, const NameSpacePtr symbol_resolver.Resolve(); py::object obj = symbol_resolver.result(); AnfNodePtr resolved_node = ResolveObjectAndAddToManager(manager, obj, node); - TraceManager::ClearParseOrResolveDebugInfo(); return resolved_node; } diff --git a/mindspore/ccsrc/pipeline/jit/static_analysis/static_analysis.cc b/mindspore/ccsrc/pipeline/jit/static_analysis/static_analysis.cc index 13c8877ebe2..60e9e861965 100644 --- a/mindspore/ccsrc/pipeline/jit/static_analysis/static_analysis.cc +++ b/mindspore/ccsrc/pipeline/jit/static_analysis/static_analysis.cc @@ -267,8 +267,7 @@ AbstractBasePtr AnalysisEngine::GetCNodeOperatorAbstract(const CNodePtr &cnode, auto maybe_func_eval_result = func_conf->ObtainEvalResult(); AbstractBasePtr maybe_func = maybe_func_eval_result->abstract(); if (maybe_func == nullptr) { - MS_LOG(EXCEPTION) << "No abstract, func_conf: " << func_conf->ToString() - << " NodeInfo: " << trace::GetDebugInfo(cnode->debug_info()); + MS_LOG(EXCEPTION) << "No abstract, func_conf: " << func_conf->ToString(); } return maybe_func; } @@ -283,7 +282,8 @@ 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() << "."; + MS_LOG(ERROR) << "Can not cast to a AbstractFunction: " << maybe_func->ToString() << "."; + MS_EXCEPTION(ValueError) << "This may be not defined, and it can't be a operator. Please check code."; } ConfigPtrList args_conf_list; diff --git a/mindspore/core/utils/info.h b/mindspore/core/utils/info.h index ced6aab8765..bf47e61a576 100644 --- a/mindspore/core/utils/info.h +++ b/mindspore/core/utils/info.h @@ -93,7 +93,10 @@ class TraceGuard { TraceGuard(const DebugInfoPtr &debug_info, const TraceInfoPtr &trace_info) { TraceManager::DebugTrace(debug_info, trace_info); } - ~TraceGuard() { TraceManager::EndTrace(); } + ~TraceGuard() { + TraceManager::EndTrace(); + TraceManager::ClearParseOrResolveDebugInfo(); + } }; class TraceContext { diff --git a/mindspore/nn/cell.py b/mindspore/nn/cell.py index 83d1995e733..6e709a3aab1 100755 --- a/mindspore/nn/cell.py +++ b/mindspore/nn/cell.py @@ -344,11 +344,11 @@ class Cell(Cell_): default_args += 1 if len(inputs) < positional_args: - raise ValueError( + raise TypeError( f"The function construct need {positional_args} positional argument, but only provided {len(inputs)}.") if len(inputs) > positional_args + default_args: - raise ValueError( + raise TypeError( f"The function construct need {positional_args} positional argument and {default_args} default " f"argument, but provided {len(inputs)}") diff --git a/tests/ut/python/nn/test_l1_regularizer.py b/tests/ut/python/nn/test_l1_regularizer.py index a2d08db52dc..6d93b3cbb6c 100644 --- a/tests/ut/python/nn/test_l1_regularizer.py +++ b/tests/ut/python/nn/test_l1_regularizer.py @@ -87,7 +87,7 @@ def test_l1_regularizer07(): try: l1_regularizer = Net_l1_regularizer(scale) l1_regularizer() - except ValueError: + except TypeError: assert True 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 01b989b3916..cd60b4cd583 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 @@ -292,9 +292,9 @@ def test_call_none_in_if(): return ret net = Net() - with pytest.raises(RuntimeError) as err: + with pytest.raises(ValueError) as err: net(Tensor([1, 2, 3], mstype.float32)) - assert "Not AbstractFunction: AbstractNone(Value: None)" in str(err.value) + assert "not defined" in str(err.value) assert "tests/ut/python/pipeline/parse/test_use_undefined_name_or_unsupported_builtin_function.py(291)" in \ str(err.value) assert "ret = self.func(x)" in str(err.value)